PageSourceSearch

https://muzobozrenie.ru/wp-content/themes/nayma/js/modular-lazy-load.js?ver=1.0

js muzobozrenie.ru collected 2026-10-02 10:19:09 UTC 5,017 bytes, 174 lines download raw bytes

1/**
2 * Ленивая подгрузка блоков quadro_mods: в HTML только первые 2 блока и контейнер.
3 * Когда пользователь доскроллил до второй секции, начинаем загружать оставшиеся по одной по очереди.
4 */
5(function() {
6	'use strict';
7
8	var LOG = false;
9
10	function log() {
11		if ( LOG && window.console && console.log ) {
12			Function.prototype.apply.call( console.log, console, [ '[modular-lazy]' ].concat( Array.prototype.slice.call( arguments ) ) );
13		}
14	}
15
16	if ( typeof ajax_object === 'undefined' ) {
17		return;
18	}
19
20	var container = document.querySelector( '.modular-lazy-container' );
21	if ( ! container ) {
22		return;
23	}
24
25	var modulesJson = container.getAttribute( 'data-modules' );
26	if ( ! modulesJson ) {
27		return;
28	}
29
30	var modules;
31	try {
32		modules = JSON.parse( modulesJson );
33	} catch ( e ) {
34		log( 'Ошибка разбора data-modules', e );
35		return;
36	}
37
38	if ( ! Array.isArray( modules ) || modules.length === 0 ) {
39		return;
40	}
41
42	log( 'Модулей к подгрузке:', modules.length );
43
44	var sentinel = container.querySelector( '.modular-lazy-sentinel' );
45	if ( ! sentinel ) {
46		return;
47	}
48
49	var isLoading = false;
50	var loadingStarted = false;
51
52	function startLoading() {
53		if ( loadingStarted ) {
54			return;
55		}
56		loadingStarted = true;
57		loadNext();
58	}
59
60	function loadNext() {
61		if ( isLoading || modules.length === 0 ) {
62			return;
63		}
64
65		var next = modules.shift();
66		var modId = next.id;
67		var modType = next.type;
68
69		isLoading = true;
70
71		var placeholder = document.createElement( 'div' );
72		placeholder.className = 'modular-lazy-placeholder modular-lazy-loading';
73		placeholder.innerHTML = '<div class="modular-lazy-preloader">' +
74			'<div class="modular-lazy-spinner"></div>' +
75			'<span class="modular-lazy-text">Загрузка…</span></div>';
76		container.insertBefore( placeholder, sentinel );
77
78		var loadUrl = ( typeof modular_lazy_load !== 'undefined' && modular_lazy_load.load_url )
79			? modular_lazy_load.load_url + '&mod_id=' + encodeURIComponent( modId ) + ( modular_lazy_load.nonce ? '&nonce=' + encodeURIComponent( modular_lazy_load.nonce ) : '' )
80			: null;
81
82		var url, opts = { credentials: 'same-origin' };
83		if ( loadUrl ) {
84			url = loadUrl;
85			opts.method = 'GET';
86		} else {
87			var formData = new FormData();
88			formData.append( 'action', 'quadro_load_modular_module' );
89			formData.append( 'mod_id', modId );
90			if ( typeof modular_lazy_load !== 'undefined' && modular_lazy_load.nonce ) {
91				formData.append( 'nonce', modular_lazy_load.nonce );
92			}
93			url = typeof ajax_object !== 'undefined' ? ajax_object.ajax_url : '';
94			opts.method = 'POST';
95			opts.body = formData;
96		}
97
98		fetch( url, opts )
99			.then( function( response ) {
100				if ( ! response.ok ) {
101					return response.text().then( function( text ) {
102						var err = new Error( 'HTTP ' + response.status );
103						err.body = text;
104						throw err;
105					});
106				}
107				return response.json();
108			})
109			.then( function( data ) {
110				if ( data.success && data.data && data.data.html ) {
111					var wrap = document.createElement( 'div' );
112					wrap.innerHTML = data.data.html.trim();
113					while ( wrap.firstChild ) {
114						container.insertBefore( wrap.firstChild, placeholder );
115					}
116					placeholder.remove();
117					log( 'Блок вставлен mod_id=', modId );
118				} else {
119					placeholder.classList.remove( 'modular-lazy-loading' );
120					placeholder.classList.add( 'modular-lazy-error' );
121					var errText = placeholder.querySelector( '.modular-lazy-text' );
122					if ( errText ) {
123						errText.textContent = 'Не удалось загрузить блок';
124					}
125				}
126			})
127			.catch( function( err ) {
128				log( 'Ошибка загрузки mod_id=', modId, err );
129				placeholder.classList.remove( 'modular-lazy-loading' );
130				placeholder.classList.add( 'modular-lazy-error' );
131				var errText = placeholder.querySelector( '.modular-lazy-text' );
132				if ( errText ) {
133					errText.textContent = 'Ошибка загрузки';
134				}
135			})
136			.finally( function() {
137				isLoading = false;
138				if ( modules.length === 0 ) {
139					sentinel.classList.add( 'modular-lazy-sentinel-done' );
140					return;
141				}
142				loadNext();
143			});
144	}
145
146	// Вторая секция — элемент прямо перед контейнером (последний из двух блоков, выведенных сразу).
147	var secondSection = container.previousElementSibling;
148
149	if ( secondSection ) {
150		var observer;
151		try {
152			observer = new IntersectionObserver(
153				function( entries ) {
154					entries.forEach( function( entry ) {
155						if ( ! entry.isIntersecting ) {
156							return;
157						}
158						if ( observer ) {
159							observer.disconnect();
160							observer = null;
161						}
162						startLoading();
163					});
164				},
165				{ rootMargin: '50px 0px', threshold: 0 }
166			);
167			observer.observe( secondSection );
168		} catch ( e ) {
169			startLoading();
170		}
171	} else {
172		startLoading();
173	}
174})();

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.