PluginProbe
Automatic YouTube Gallery – Embed Auto-Updating YouTube Video Galleries, Feeds, Playlists & Channels / 2.2.0
Automatic YouTube Gallery – Embed Auto-Updating YouTube Video Galleries, Feeds, Playlists & Channels v2.2.0
2.9.1 2.9.0 trunk 1.0.0 1.1.0 1.2.0 1.3.0 1.4.0 1.5.0 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 2.0.0 2.1.0 2.2.0 2.3.2 2.3.3 2.3.5 2.3.6 2.3.8 2.3.9 2.4.3 All 37 releases
automatic-youtube-gallery / public / assets / js / public.js

public.js in Automatic YouTube Gallery – Embed Auto-Updating YouTube Video Galleries, Feeds, Playlists & Channels 2.2.0, at public/assets/js/public.js

546 lines 14.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 (function( $ ) {
2 'use strict';
3
4 /**
5 * Vars
6 */
7 window.ayg_is_ready = false;
8
9 /**
10 * Init Automatic YouTube Gallery. Called when YouTube API is ready.
11 *
12 * @since 2.0.0
13 */
14 function ayg_init() {
15 if ( true == window.ayg_is_ready ) {
16 return false;
17 }
18
19 window.ayg_is_ready = true;
20 $( document ).trigger( 'ayg.ready' );
21
22 // Classic Theme
23 $( '.ayg-theme-classic' ).each(function() {
24 init_classic_theme( $( this ) );
25 });
26
27 // Single Video
28 $( '.ayg-theme-single' ).each(function() {
29 init_single_video( $( this ) );
30 });
31
32 // Livestream
33 $( '.ayg-theme-livestream' ).each(function() {
34 init_livestream( $( this ) );
35 });
36
37 // Pagination
38 $( '.ayg-pagination' ).each(function() {
39 init_pagination( $( this ) );
40 });
41 }
42
43 /**
44 * Init Classic Theme.
45 *
46 * @since 2.0.0
47 */
48 function init_classic_theme( $root ) {
49 $root.addClass( 'ayg-theme-initialized' );
50
51 var params = $root.data( 'params' );
52 var uid = params.uid;
53 var $current_item = $( '.ayg-item', '#ayg-' + uid ).eq(0);
54 var $next_button = $( '.ayg-pagination-next-btn', '#ayg-' + uid );
55 var next_type = $next_button.data( 'type' );
56 var player_id = 'ayg-player-' + uid;
57
58 // Player
59 var player = ayg_init_player( player_id, {
60 custom: {
61 params: params,
62 image: $current_item.find( '.ayg-thumbnail-image' ).attr( 'src' )
63 },
64 events: {
65 'onStateChange': function( e ) {
66 if ( e.data == YT.PlayerState.ENDED ) {
67 if ( 1 == params.autoadvance ) {
68 player.stop();
69
70 if ( $current_item.is( ':last-child' ) ) {
71 if ( 'more' == next_type ) {
72 if ( 1 == params.loop ) {
73 $( '.ayg-item', '#ayg-' + uid ).eq(0).trigger( 'click' );
74 }
75 } else {
76 // Load Next Page
77 if ( $next_button.is( ':visible' ) ) {
78 $next_button.trigger( 'click' );
79
80 var __interval = setInterval(
81 function() {
82 if ( $( '.ayg-pagination.ayg-loading', '#ayg-' + uid ).length == 0 ) {
83 clearInterval( __interval );
84 $( '.ayg-item', '#ayg-' + uid ).eq(0).trigger( 'click' );
85 }
86 },
87 1000
88 );
89 }
90 }
91 } else {
92 $current_item.next( '.ayg-item' ).trigger( 'click' );
93 }
94 } else {
95 if ( 1 == params.loop ) {
96 player.play();
97 } else {
98 player.stop();
99 }
100 }
101 }
102 }
103 }
104 });
105
106 // Grid: On thumbnail clicked
107 $( '#ayg-' + uid ).on( 'click', '.ayg-item:not(.ayg-active)', function() {
108 $current_item = $( this );
109
110 $( '.ayg-active', '#ayg-' + uid ).removeClass( 'ayg-active' );
111 $current_item.addClass( 'ayg-active' );
112
113 // Change video
114 player.change({
115 id: $current_item.find( '.ayg-thumbnail' ).data( 'id' ),
116 image: $current_item.find( '.ayg-thumbnail-image' ).attr( 'src' )
117 });
118
119 if ( 1 == params.player_title ) {
120 var title = $current_item.find( '.ayg-thumbnail' ).data( 'title' );
121 $( '.ayg-player-title', '#ayg-' + uid ).html( title );
122 }
123
124 if ( 1 == params.player_description ) {
125 var description = $current_item.find( '.ayg-thumbnail-description' ).html();
126 $( '.ayg-player-description', '#ayg-' + uid ).html( description );
127 }
128
129 // Scroll to Top
130 $( 'html, body' ).animate({
131 scrollTop: $root.offset().top - ayg_public.top_offset
132 }, 500, function() {
133 // Change URL in Browser Address Bar
134 var url = $current_item.find( '.ayg-thumbnail' ).data( 'url' );
135 if ( '' != url ) {
136 window.history.replaceState( null, null, url );
137 }
138 });
139
140 // Load Next Page
141 if ( 1 == params.autoadvance && 'more' == next_type ) {
142 if ( $current_item.is( ':last-child' ) && $next_button.is( ':visible' ) ) {
143 $next_button.trigger( 'click' );
144 }
145 }
146 });
147 }
148
149 /**
150 * Init Single Video.
151 *
152 * @since 2.0.0
153 */
154 function init_single_video( $root ) {
155 $root.addClass( 'ayg-theme-initialized' );
156
157 var params = $root.data( 'params' );
158 var uid = params.uid;
159 var player_id = 'ayg-player-' + uid;
160
161 // Player
162 var player = ayg_init_player( player_id, {
163 custom: {
164 params: params,
165 image: $( '#' + player_id ).data( 'image' )
166 },
167 events: {
168 'onStateChange': function( e ) {
169 if ( e.data == YT.PlayerState.ENDED ) {
170 if ( 1 == params.loop ) {
171 player.play();
172 }
173 }
174 }
175 }
176 });
177 }
178
179 /**
180 * Init Livestream.
181 *
182 * @since 2.0.0
183 */
184 function init_livestream( $root ) {
185 $root.addClass( 'ayg-theme-initialized' );
186
187 var params = $root.data( 'params' );
188 var uid = params.uid;
189 var player_id = 'ayg-player-' + uid;
190
191 // Player
192 var player = ayg_init_player( player_id, {
193 custom: {
194 params: params,
195 image: 'none'
196 },
197 events: {
198 'onReady': function( e ) {
199 var url = e.target.getVideoUrl();
200
201 if ( url == 'https://www.youtube.com/watch?v=live_stream' ) {
202 $( '.ayg-player-wrapper', '#ayg-' + uid ).fadeOut( 'fast', function() {
203 $( '.ayg-fallback-message', '#ayg-' + uid ).fadeIn();
204 });
205 } else {
206 $( '#ayg-player-' + uid ).show();
207 }
208 },
209 'onStateChange': function( e ) {
210 if ( e.data == YT.PlayerState.ENDED ) {
211 player.stop();
212 }
213 }
214 }
215 });
216 }
217
218 /**
219 * Init Pagination.
220 *
221 * @since 2.0.0
222 */
223 function init_pagination( $pagination ) {
224 var params = $pagination.data( 'params' );
225 params.action = 'ayg_load_more_videos';
226 params.security = ayg_public.ajax_nonce;
227
228 var uid = params.uid;
229 var $gallery = $( '.ayg-gallery', '#ayg-' + uid );
230
231 // On button clicked
232 $( '.ayg-btn', '#ayg-' + uid ).on( 'click', function() {
233 $pagination.addClass( 'ayg-loading' );
234
235 var $this = $( this );
236 var type = $this.data( 'type' );
237 params.pageToken = ( 'previous' == type ) ? params.prev_page_token : params.next_page_token;
238
239 $.post( ayg_public.ajax_url, params, function( response ) {
240 if ( response.success ) {
241 var total_pages = parseInt( params.total_pages );
242
243 if ( response.data.next_page_token ) {
244 params.next_page_token = response.data.next_page_token;
245 } else {
246 params.next_page_token = '';
247 }
248
249 if ( response.data.prev_page_token ) {
250 params.prev_page_token = response.data.prev_page_token;
251 } else {
252 params.prev_page_token = '';
253 }
254
255 switch ( type ) {
256 case 'more':
257 params.paged = Math.min( parseInt( params.paged ) + 1, total_pages );
258
259 if ( params.paged == total_pages ) {
260 params.next_page_token = '';
261 $this.hide();
262 }
263
264 $gallery.append( response.data.html );
265 break;
266 case 'next':
267 params.paged = Math.min( parseInt( params.paged ) + 1, total_pages );
268
269 if ( params.paged == total_pages ) {
270 params.next_page_token = '';
271 $this.hide();
272 }
273
274 $( '.ayg-pagination-prev-btn', '#ayg-' + uid ).show();
275 $( '.ayg-pagination-current-page-number', '#ayg-' + uid ).html( params.paged );
276
277 $gallery.html( response.data.html );
278 break;
279 case 'previous':
280 params.paged = Math.max( parseInt( params.paged ) - 1, 1 );
281
282 if ( 1 == params.paged ) {
283 params.prev_page_token = '';
284 $this.hide();
285 }
286
287 $( '.ayg-pagination-next-btn', '#ayg-' + uid ).show();
288 $( '.ayg-pagination-current-page-number', '#ayg-' + uid ).html( params.paged );
289
290 $gallery.html( response.data.html );
291 break;
292 }
293
294 $pagination.removeClass( 'ayg-loading' );
295 } else {
296 $pagination.removeClass( 'ayg-loading' ).hide();
297 }
298 });
299 });
300 }
301
302 /**
303 * Init AYGPlayer.
304 *
305 * @since 2.0.0
306 */
307 var ayg_init_player = function ( player_id, args ) {
308 var player = null;
309 var $player_wrapper = $( '#' + player_id ).closest( 'div' );
310 var video_id = $( '#' + player_id ).data( 'id' );
311 var params = args.custom.params;
312
313 var init_player = function () {
314 var iframe_src = 'https://www.youtube.com/embed/' + video_id + '?enablejsapi=1';
315
316 if ( params.hasOwnProperty( 'is_live' ) ) {
317 iframe_src = 'https://www.youtube.com/embed/live_stream?channel=' + video_id + '&enablejsapi=1';
318 }
319
320 iframe_src += '&playsinline=1';
321 iframe_src += '&rel=0';
322
323 if ( params.hasOwnProperty( 'autoplay' ) ) {
324 iframe_src += ( '&autoplay=' + parseInt( params.autoplay ) );
325 }
326
327 if ( params.hasOwnProperty( 'controls' ) ) {
328 iframe_src += ( '&controls=' + parseInt( params.controls ) );
329 }
330
331 if ( params.hasOwnProperty( 'modestbranding' ) ) {
332 iframe_src += ( '&modestbranding=' + parseInt( params.modestbranding ) );
333 }
334
335 if ( params.hasOwnProperty( 'cc_load_policy' ) ) {
336 iframe_src += ( '&cc_load_policy=' + parseInt( params.cc_load_policy ) );
337 }
338
339 if ( params.hasOwnProperty( 'iv_load_policy' ) ) {
340 iframe_src += ( '&iv_load_policy=' + parseInt( params.iv_load_policy ) );
341 }
342
343 if ( params.hasOwnProperty( 'hl' ) ) {
344 iframe_src += ( '&hl=' + params.hl );
345 }
346
347 if ( params.hasOwnProperty( 'cc_lang_pref' ) ) {
348 iframe_src += ( '&cc_lang_pref=' + params.cc_lang_pref );
349 }
350
351 if ( $( '#' + player_id ).prop( 'tagName' ).toLowerCase() == 'iframe' ) {
352 $( '#' + player_id ).attr( 'src', iframe_src );
353 } else {
354 $( '#' + player_id ).replaceWith( '<iframe id="' + player_id + '" class="ayg-player-iframe" width="100%" height="100%" src="' + iframe_src + '" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>' );
355 }
356
357 player = new YT.Player( player_id, { events: args.events } );
358 }
359
360 if ( 1 == ayg_public.cookie_consent ) {
361 var html = '<div class="ayg-privacy-wrapper" style="background-image: url(' + args.custom.image + ');">';
362 html += '<div class="ayg-privacy-consent-block">';
363 html += '<div class="ayg-privacy-consent-message">' + ayg_public.consent_message + '</div>';
364 html += '<div class="ayg-privacy-consent-button">' + ayg_public.button_label + '</div>';
365 html += '</div>';
366 html += '</div>';
367
368 $player_wrapper.append( html );
369
370 $player_wrapper.find( '.ayg-privacy-consent-button' ).on( 'click', function() {
371 $( this ).html( '...' );
372
373 var data = {
374 'action': 'ayg_set_cookie',
375 'security': ayg_public.ajax_nonce
376 };
377
378 $.post(
379 ayg_public.ajax_url,
380 data,
381 function( response ) {
382 if ( response.success ) {
383 ayg_public.cookie_consent = 0;
384
385 params.autoplay = 1;
386 init_player();
387
388 $player_wrapper.find( '.ayg-privacy-wrapper' ).remove();
389 }
390 }
391 );
392 });
393 } else {
394 init_player();
395 }
396
397 return {
398 play: function() {
399 if ( player && player.playVideo ) {
400 player.playVideo();
401 }
402 },
403 change: function( obj ) {
404 if ( player && player.loadVideoById ) {
405 player.loadVideoById( obj.id );
406 } else {
407 video_id = obj.id;
408
409 if ( 1 == ayg_public.cookie_consent ) {
410 $player_wrapper.find( '.ayg-privacy-wrapper' ).css( 'background-image', "url(" + obj.image + ")" );
411 }
412 }
413 },
414 stop: function() {
415 if ( player && player.stopVideo ) {
416 player.stopVideo();
417 }
418 },
419 destroy: function() {
420 if ( player ) {
421 if ( player.stopVideo ) {
422 player.stopVideo();
423 }
424
425 if ( player.destroy ) {
426 player.destroy();
427 }
428 } else {
429 $( '#' + player_id ).remove();
430 }
431
432 if ( 1 == ayg_public.cookie_consent ) {
433 $player_wrapper.find( '.ayg-privacy-wrapper' ).remove();
434 }
435 }
436 };
437 }
438
439 window.ayg_init_player = ayg_init_player;
440
441 /**
442 * Called when the page has loaded.
443 *
444 * @since 1.0.0
445 */
446 $(function() {
447 // Init Automatic YouTube Gallery
448 if ( 'undefined' === typeof window['YT'] ) {
449 var tag = document.createElement( 'script' );
450 tag.src = "https://www.youtube.com/iframe_api";
451 var first_script_tag = document.getElementsByTagName( 'script' )[0];
452 first_script_tag.parentNode.insertBefore( tag, first_script_tag );
453 }
454
455 if ( 'undefined' == typeof window.onYouTubeIframeAPIReady ) {
456 window.onYouTubeIframeAPIReady = function() {
457 ayg_init();
458 };
459 } else if ( 'undefined' !== typeof window.YT ) {
460 ayg_init();
461 }
462
463 var interval = setInterval(
464 function() {
465 if ( 'undefined' !== typeof window.YT && window.YT.loaded ) {
466 clearInterval( interval );
467 ayg_init();
468 }
469 },
470 10
471 );
472
473 // Locate gallery element on single video pages
474 var gallery_id = ayg_public.gallery_id;
475 if ( gallery_id != '' && $( '#ayg-' + gallery_id ).length ) {
476 if ( history.scrollRestoration ) {
477 history.scrollRestoration = 'manual';
478 } else {
479 window.onbeforeunload = function () {
480 window.scrollTo( 0, 0 );
481 }
482 }
483
484 $( 'html, body' ).animate({
485 scrollTop: $( '#ayg-' + gallery_id ).offset().top - ayg_public.top_offset
486 }, 500);
487 }
488
489 // Toggle more/less content in the player description
490 $( document ).on( 'click', '.ayg-player-description-toggle-btn', function( event ) {
491 event.preventDefault();
492
493 var $this = $( this);
494 var $description = $this.closest( '.ayg-player-description' );
495 var $dots = $description.find( '.ayg-player-description-dots' );
496 var $more = $description.find( '.ayg-player-description-more' );
497
498 if ( $dots.is( ':visible' ) ) {
499 $this.html( ayg_public.i18n.show_less );
500 $dots.hide();
501 $more.fadeIn();
502 } else {
503 $more.fadeOut(function() {
504 $this.html( ayg_public.i18n.show_more );
505 $dots.show();
506 });
507 }
508 });
509
510 // Gutenberg: On block init
511 if ( 'undefined' !== typeof wp && 'undefined' !== typeof wp['hooks'] ) {
512 var ayg_block_interval;
513 var ayg_block_interval_retry_count;
514
515 wp.hooks.addFilter( 'ayg_block_init', 'automatic-youtube-gallery/block', function( attributes ) {
516 if ( 'livestream' == attributes.type ) {
517 if ( ayg_block_interval_retry_count > 0 ) {
518 clearInterval( ayg_block_interval );
519 }
520
521 ayg_block_interval_retry_count = 0;
522
523 ayg_block_interval = setInterval(
524 function() {
525 ayg_block_interval_retry_count++;
526 var $players = $( '.ayg-theme-livestream:not(.ayg-theme-initialized)' );
527
528 if ( $players.length > 0 || ayg_block_interval_retry_count >= 10 ) {
529 clearInterval( ayg_block_interval );
530 ayg_block_interval_retry_count = 0;
531
532 $players.each(function() {
533 init_livestream( $( this ) );
534 });
535 }
536 },
537 1000
538 );
539 }
540
541 return attributes;
542 });
543 }
544 });
545
546 })( jQuery );