PageSourceSearch

https://www.tablotv.com/wp-content/plugins/ele-custom-skin/modules…/assets/js/ecs-slider.js?ver=4.1.4

js tablotv.com collected 2026-09-24 18:52:06 UTC 15,362 bytes, 426 lines download raw bytes

1( function () {
2	'use strict';
3
4	var CHEVRON_PREV = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 25 25"><path d="M15.5 5 8.5 12.5 15.5 20" stroke="currentColor" stroke-width="2" fill="none" stroke-linecap="round" stroke-linejoin="round"/></svg>';
5	var CHEVRON_NEXT = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 25 25"><path d="M9.5 5 16.5 12.5 9.5 20" stroke="currentColor" stroke-width="2" fill="none" stroke-linecap="round" stroke-linejoin="round"/></svg>';
6
7	// ── Live mode (PHP generated Swiper HTML) ─────────────────────────────────
8
9	function doInitLiveSlider( el ) {
10		el.setAttribute( 'data-ecs-swiper-init', '1' );
11
12		var settings = JSON.parse( el.getAttribute( 'data-ecs-slider-settings' ) || '{}' );
13
14		if ( settings.navigation ) {
15			settings.navigation = {
16				prevEl: el.querySelector( '.elementor-swiper-button-prev' ),
17				nextEl: el.querySelector( '.elementor-swiper-button-next' ),
18			};
19		}
20		if ( settings.pagination ) {
21			settings.pagination.el = el.querySelector( '.swiper-pagination' );
22		}
23
24		new Swiper( el, settings ); // eslint-disable-line no-undef
25	}
26
27	function initLiveSlider( el ) {
28		if ( el.hasAttribute( 'data-ecs-swiper-init' ) ) {
29			return;
30		}
31
32		// Defer init if hidden (e.g. inside display:none responsive-version on desktop).
33		// ResizeObserver fires when CSS shows the element — media query triggered by viewport change.
34		if ( el.offsetWidth === 0 && typeof ResizeObserver !== 'undefined' ) {
35			el.setAttribute( 'data-ecs-swiper-init', 'pending' );
36			var ro = new ResizeObserver( function ( entries ) {
37				if ( entries[ 0 ].contentRect.width > 0 ) {
38					ro.disconnect();
39					el.removeAttribute( 'data-ecs-swiper-init' );
40					doInitLiveSlider( el );
41				}
42			} );
43			ro.observe( el );
44			return;
45		}
46
47		doInitLiveSlider( el );
48	}
49
50	function initAllLiveSliders() {
51		document.querySelectorAll( '.ecs-swiper:not([data-ecs-swiper-init])' ).forEach( initLiveSlider );
52	}
53
54	// ── Editor mode (JS builds Swiper from existing DOM) ──────────────────────
55
56	/**
57	 * Read slider control values from the parent editor's Backbone model.
58	 * This is the same pattern used in ecs-editor-preview.js.
59	 */
60	function getEditorSettings( containerId ) {
61		try {
62			return window.parent.elementor.getContainer( containerId ).settings.toJSON();
63		} catch ( e ) {
64			return {};
65		}
66	}
67
68	function buildSwiperConfig( s ) {
69		var navigation = s.ecs_navigation || 'arrows';
70		var showArrows = navigation === 'arrows' || navigation === 'both';
71		var showDots   = navigation === 'dots'   || navigation === 'both';
72
73		var colsDesktop = parseInt( s.ecs_slider_columns, 10 ) || 1;
74		var colsTablet  = parseInt( s.ecs_slider_columns_tablet, 10 ) || colsDesktop;
75		var colsMobile  = parseInt( s.ecs_slider_columns_mobile, 10 ) || colsTablet;
76		var layoutTablet = s.ecs_layout_tablet || '';
77		var layoutMobile = s.ecs_layout_mobile || '';
78
79		var config = {
80			slidesPerView: colsDesktop,
81			loop:          s.ecs_loop === 'yes',
82			speed:         parseInt( s.ecs_speed, 10 ) || 500,
83			spaceBetween:  parseInt( s.ecs_space_between, 10 ) || 0,
84			breakpoints: {
85				0: {
86					slidesPerView: colsMobile,
87					enabled: layoutMobile !== 'flex' && layoutMobile !== 'grid',
88				},
89				768: {
90					slidesPerView: colsTablet,
91					enabled: layoutTablet !== 'flex' && layoutTablet !== 'grid',
92				},
93				1025: {
94					slidesPerView: colsDesktop,
95					enabled: true,
96				},
97			},
98		};
99
100		if ( s.ecs_autoplay === 'yes' ) {
101			config.autoplay = {
102				delay:                parseInt( s.ecs_autoplay_speed, 10 ) || 3000,
103				pauseOnMouseEnter:    s.ecs_pause_on_hover === 'yes',
104				disableOnInteraction: false,
105			};
106		}
107
108		return { config: config, showArrows: showArrows, showDots: showDots };
109	}
110
111	/**
112	 * Move real children back to their scope (e-con-inner or container) then remove swiper.
113	 * Using real elements (not clones) keeps Backbone model-binding intact and allows editing.
114	 */
115	function destroyEditorSlider( containerEl ) {
116		var existing = containerEl.querySelector( ':scope > .ecs-editor-swiper' );
117		if ( existing ) {
118			var inner = containerEl.querySelector( ':scope > .e-con-inner' );
119			var scope = inner || containerEl;
120			// Move real children back from slides to scope, preserving order
121			existing.querySelectorAll( ':scope > .swiper-wrapper > .swiper-slide:not(.swiper-slide-duplicate)' ).forEach( function ( slide ) {
122				var child = Array.from( slide.children ).find( function ( el ) {
123					return el.dataset && el.dataset.id;
124				} );
125				if ( child ) {
126					scope.appendChild( child );
127				}
128			} );
129			if ( existing.swiper ) {
130				existing.swiper.destroy( true, true );
131			}
132			existing.remove();
133		}
134		containerEl.classList.remove( 'ecs-editor-slider-active' );
135		containerEl.removeAttribute( 'data-ecs-slider-key' );
136		containerEl.removeAttribute( 'data-ecs-rebuild-key' );
137	}
138
139	/** Cache key based on child IDs only (structural changes). */
140	function sliderKey( children ) {
141		return children.map( function ( el ) { return el.getAttribute( 'data-id' ); } ).join( ',' );
142	}
143
144	/**
145	 * Key for settings that require HTML rebuild (navigation type).
146	 * Non-structural settings (columns, speed…) are updated in-place by
147	 * updateEditorSliderParams() to avoid disturbing the live DOM.
148	 */
149	function rebuildKey( built ) {
150		return ( built.showArrows ? 'a' : '' ) + ( built.showDots ? 'd' : '' );
151	}
152
153	/**
154	 * Build editor swiper by MOVING real children into slides (not cloning).
155	 * Real elements stay connected to their Backbone views so they remain editable.
156	 * Loop is disabled in editor to prevent Swiper from creating duplicate [data-id] nodes.
157	 */
158	function buildEditorSlider( containerEl ) {
159		var id       = containerEl.getAttribute( 'data-id' );
160		var settings = getEditorSettings( id );
161
162		var inner    = containerEl.querySelector( ':scope > .e-con-inner' );
163		var scope    = inner || containerEl;
164		var children = Array.from( scope.children ).filter( function ( el ) {
165			return el.dataset && el.dataset.id;
166		} );
167
168		if ( ! children.length ) {
169			return;
170		}
171
172		var built      = buildSwiperConfig( settings );
173		built.config.loop = false; // disable loop in editor — avoids duplicate [data-id] nodes
174
175		var swiperEl   = document.createElement( 'div' );
176		swiperEl.className = 'swiper ecs-swiper ecs-editor-swiper';
177
178		var wrapperEl  = document.createElement( 'div' );
179		wrapperEl.className = 'swiper-wrapper';
180
181		// Move real children into slides — preserves Backbone binding and allows editing
182		children.forEach( function ( child ) {
183			var slide = document.createElement( 'div' );
184			slide.className = 'swiper-slide';
185			slide.appendChild( child ); // MOVE, not clone
186			wrapperEl.appendChild( slide );
187		} );
188		swiperEl.appendChild( wrapperEl );
189
190		if ( built.showArrows ) {
191			swiperEl.insertAdjacentHTML( 'beforeend',
192				'<div class="elementor-swiper-button elementor-swiper-button-prev" role="button" tabindex="0">' + CHEVRON_PREV + '</div>' +
193				'<div class="elementor-swiper-button elementor-swiper-button-next" role="button" tabindex="0">' + CHEVRON_NEXT + '</div>'
194			);
195			built.config.navigation = {
196				prevEl: swiperEl.querySelector( '.elementor-swiper-button-prev' ),
197				nextEl: swiperEl.querySelector( '.elementor-swiper-button-next' ),
198			};
199		}
200
201		if ( built.showDots ) {
202			swiperEl.insertAdjacentHTML( 'beforeend', '<div class="swiper-pagination"></div>' );
203			built.config.pagination = {
204				el:        swiperEl.querySelector( '.swiper-pagination' ),
205				clickable: true,
206			};
207		}
208
209		// CSS class hides empty .e-con-inner (children were moved out into swiper slides)
210		containerEl.classList.add( 'ecs-editor-slider-active' );
211		containerEl.appendChild( swiperEl );
212		containerEl.setAttribute( 'data-ecs-slider-key', sliderKey( children ) );
213		containerEl.setAttribute( 'data-ecs-rebuild-key', rebuildKey( built ) );
214
215		if ( typeof Swiper !== 'undefined' ) { // eslint-disable-line no-undef
216			new Swiper( swiperEl, built.config ); // eslint-disable-line no-undef
217		}
218	}
219
220	function syncEditorSlider( containerEl ) {
221		// Backbone re-render may have removed .ecs-editor-swiper while leaving
222		// ecs-editor-slider-active class — reset so we fall through to rebuild.
223		if (
224			containerEl.classList.contains( 'ecs-editor-slider-active' ) &&
225			! containerEl.querySelector( ':scope > .ecs-editor-swiper' )
226		) {
227			destroyEditorSlider( containerEl );
228		}
229
230		var swiperEl = containerEl.querySelector( ':scope > .ecs-editor-swiper' );
231		var inner    = containerEl.querySelector( ':scope > .e-con-inner' );
232		var scope    = inner || containerEl;
233
234		var children;
235		if ( swiperEl ) {
236			// Slider active: real children live inside swiper slides
237			var slideChildren = Array.from(
238				swiperEl.querySelectorAll( ':scope > .swiper-wrapper > .swiper-slide:not(.swiper-slide-duplicate)' )
239			).map( function ( slide ) {
240				return Array.from( slide.children ).find( function ( el ) {
241					return el.dataset && el.dataset.id;
242				} );
243			} ).filter( Boolean );
244
245			// Plus any new direct scope children Backbone may have appended (new widget added)
246			var newScopeChildren = Array.from( scope.children ).filter( function ( el ) {
247				return el.dataset && el.dataset.id;
248			} );
249			children = slideChildren.concat( newScopeChildren );
250		} else {
251			children = Array.from( scope.children ).filter( function ( el ) {
252				return el.dataset && el.dataset.id;
253			} );
254		}
255
256		var cached = containerEl.getAttribute( 'data-ecs-slider-key' );
257		if ( cached !== null && cached === sliderKey( children ) ) {
258			return; // structure unchanged
259		}
260
261		destroyEditorSlider( containerEl ); // moves children back to scope
262		if ( children.length ) {
263			buildEditorSlider( containerEl );
264		}
265	}
266
267	function syncAllEditorSliders() {
268		// Cleanup: container changed type away from ecs-slider but swiper is still there
269		document.querySelectorAll( '.ecs-editor-slider-active:not(.e-ecs-slider)' ).forEach( destroyEditorSlider );
270		document.querySelectorAll( '.e-con.e-ecs-slider' ).forEach( syncEditorSlider );
271	}
272
273	/**
274	 * Update Swiper config in-place without destroy/clone.
275	 * Called on settings panel changes (columns, speed, color, etc.).
276	 * Falls back to full syncEditorSlider only when HTML structure must change
277	 * (navigation type or loop — these require different DOM elements).
278	 */
279	function updateEditorSliderParams( containerEl ) {
280		var swiperEl = containerEl.querySelector( ':scope > .ecs-editor-swiper' );
281		if ( ! swiperEl || ! swiperEl.swiper ) {
282			syncEditorSlider( containerEl ); // swiper missing — full rebuild
283			return;
284		}
285
286		var id       = containerEl.getAttribute( 'data-id' );
287		var settings = getEditorSettings( id );
288		var built    = buildSwiperConfig( settings );
289		var swiper   = swiperEl.swiper;
290
291		// Navigation type or loop change requires HTML rebuild (different DOM structure)
292		var currRebuildKey = rebuildKey( built );
293		var prevRebuildKey = containerEl.getAttribute( 'data-ecs-rebuild-key' );
294		if ( prevRebuildKey !== null && prevRebuildKey !== currRebuildKey ) {
295			syncEditorSlider( containerEl );
296			return;
297		}
298
299		// Safe in-place param update — no cloning, no race condition
300		swiper.params.slidesPerView = built.config.slidesPerView;
301		swiper.params.speed         = built.config.speed;
302		swiper.params.spaceBetween  = built.config.spaceBetween;
303		swiper.params.breakpoints   = built.config.breakpoints;
304
305		if ( built.config.autoplay ) {
306			swiper.params.autoplay = built.config.autoplay;
307			if ( swiper.autoplay && ! swiper.autoplay.running ) {
308				swiper.autoplay.start();
309			}
310		} else if ( swiper.autoplay && swiper.autoplay.running ) {
311			swiper.autoplay.stop();
312			swiper.params.autoplay = false;
313		}
314
315		swiper.update();
316	}
317
318	function updateAllEditorSliderParams() {
319		document.querySelectorAll( '.e-con.e-ecs-slider' ).forEach( updateEditorSliderParams );
320	}
321
322	// ── Entry point ───────────────────────────────────────────────────────────
323
324	var inEditMode = !! (
325		window.elementorFrontend &&
326		typeof elementorFrontend.isEditMode === 'function' &&
327		elementorFrontend.isEditMode()
328	);
329
330	if ( inEditMode ) {
331		var suppressObserver = false;
332
333		var observer = new MutationObserver( function ( mutations ) {
334			if ( suppressObserver ) { return; }
335
336			var relevant = false;
337			for ( var i = 0; i < mutations.length; i++ ) {
338				var m = mutations[ i ];
339				var t = m.target;
340				// Skip mutations caused by our own swiper structure
341				if ( t && t.classList && (
342					t.classList.contains( 'ecs-editor-swiper' ) ||
343					t.classList.contains( 'swiper-wrapper' ) ||
344					t.classList.contains( 'swiper-slide' )
345				) ) {
346					continue;
347				}
348				relevant = true;
349				break;
350			}
351			if ( relevant ) {
352				suppressObserver = true;
353				syncAllEditorSliders();
354				Promise.resolve().then( function () { suppressObserver = false; } );
355			}
356		} );
357
358		function setupEditor() {
359			syncAllEditorSliders();
360			observer.observe( document.body, {
361				childList:       true,
362				subtree:         true,
363				attributes:      true,
364				attributeFilter: [ 'class', 'data-id' ],
365			} );
366
367			// Safety rebuild at 800ms: catches cases where MutationObserver fires before
368			// Backbone has finished populating widget content. syncEditorSlider will
369			// also detect empty widgets on each MO-triggered call going forward.
370			setTimeout( syncAllEditorSliders, 800 );
371
372			// Settings panel changes (columns, colors, speed…) don't mutate the DOM so
373			// MutationObserver never fires. Update Swiper params in-place — no cloning,
374			// no race condition with Backbone re-renders.
375			try {
376				var settingsTimer = null;
377				window.parent.elementor.channels.editor.on( 'change', function () {
378					clearTimeout( settingsTimer );
379					settingsTimer = setTimeout( updateAllEditorSliderParams, 150 );
380				} );
381			} catch ( e ) { /* cross-origin or elementor unavailable */ }
382		}
383
384		// components:init already fired by the time a footer script loads.
385		// Run directly — Backbone views are ready by DOMContentLoaded.
386		if ( document.readyState === 'loading' ) {
387			document.addEventListener( 'DOMContentLoaded', function () {
388				setTimeout( setupEditor, 300 );
389			} );
390		} else {
391			setTimeout( setupEditor, 300 );
392		}
393
394	} else {
395		// Live frontend: PHP already generated Swiper HTML.
396		if ( document.readyState === 'loading' ) {
397			document.addEventListener( 'DOMContentLoaded', initAllLiveSliders );
398		} else {
399			initAllLiveSliders();
400		}
401
402		if ( typeof MutationObserver !== 'undefined' ) {
403			var liveObserver = new MutationObserver( function ( mutations ) {
404				var found = false;
405				for ( var i = 0; i < mutations.length; i++ ) {
406					var added = mutations[ i ].addedNodes;
407					for ( var j = 0; j < added.length; j++ ) {
408						var node = added[ j ];
409						if ( node.nodeType !== 1 ) { continue; }
410						if (
411							( node.classList && node.classList.contains( 'ecs-swiper' ) ) ||
412							( node.querySelector && node.querySelector( '.ecs-swiper:not([data-ecs-swiper-init])' ) )
413						) {
414							found = true;
415							break;
416						}
417					}
418					if ( found ) { break; }
419				}
420				if ( found ) { initAllLiveSliders(); }
421			} );
422			liveObserver.observe( document.body, { childList: true, subtree: true } );
423		}
424	}
425
426} )();

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.