PluginProbe
Automatic YouTube Gallery – Embed Auto-Updating YouTube Video Galleries, Feeds, Playlists & Channels / 2.0.0
Automatic YouTube Gallery – Embed Auto-Updating YouTube Video Galleries, Feeds, Playlists & Channels v2.0.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 / admin / assets / js / admin.js

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

380 lines 9.8 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 * Display the media uploader.
6 *
7 * @since 1.0.0
8 */
9 function ayg_render_media_uploader( $elem ) {
10 var file_frame, attachment;
11
12 // If an instance of file_frame already exists, then we can open it rather than creating a new instance
13 if ( file_frame ) {
14 file_frame.open();
15 return;
16 };
17
18 // Use the wp.media library to define the settings of the media uploader
19 file_frame = wp.media.frames.file_frame = wp.media({
20 frame: 'post',
21 state: 'insert',
22 multiple: false
23 });
24
25 // Setup an event handler for what to do when a media has been selected
26 file_frame.on( 'insert', function() {
27 // Read the JSON data returned from the media uploader
28 attachment = file_frame.state().get( 'selection' ).first().toJSON();
29
30 // First, make sure that we have the URL of the media to display
31 if ( 0 > $.trim( attachment.url.length ) ) {
32 return;
33 };
34
35 // Set the data
36 $elem.prev( '.ayg-settings-url' )
37 .val( attachment.url );
38 });
39
40 // Now display the actual file_frame
41 file_frame.open();
42 };
43
44 /**
45 * Close the popup.
46 *
47 * @since 1.0.0
48 */
49 function ayg_modal_hide() {
50 $( '.ayg-modal' ).hide();
51 $( 'html' ).removeClass( 'ayg-no-scroll' );
52 }
53
54 /**
55 * Get Youtube playlist ID from Youtube URL.
56 *
57 * @since 1.0.0
58 * @param {string} url - URL from which to extract the ID.
59 * @return {string}
60 */
61 function ayg_get_youtube_playlist_id( url ) {
62 var id = /[&|\?]list=([a-zA-Z0-9_-]+)/gi.exec( url );
63 return ( id && id.length > 0 ) ? id[1] : url;
64 }
65
66 /**
67 * Get Youtube channel ID from Youtube URL.
68 *
69 * @since 1.0.0
70 * @param {string} url - URL from which to extract the ID.
71 * @return {object}
72 */
73 function ayg_get_youtube_channel_id( url ) {
74 var type = 'channel';
75 var id = url;
76
77 url = url.replace( /(>|<)/gi, '' ).split( /(\/channel\/|\/user\/)/ );
78
79 if ( url[2] !== undefined ) {
80 id = url[2].split( /[^0-9a-z_-]/i );
81 id = id[0];
82 }
83
84 if ( /\/user\//.test( url ) ) {
85 type = 'username';
86 }
87
88 return {
89 type: type,
90 id: id
91 };
92 }
93
94 /**
95 * Get Youtube video ID from Youtube URL.
96 *
97 * @since 1.0.0
98 * @param {string} url - URL from which to extract the ID.
99 * @return {string}
100 */
101 function ayg_get_youtube_video_id( url ) {
102 var id = url;
103
104 url = url.replace( /(>|<)/gi, '' ).split( /(vi\/|v=|\/v\/|youtu\.be\/|\/embed\/)/ );
105
106 if ( url[2] !== undefined ) {
107 id = url[2].split( /[^0-9a-z_\-]/i );
108 id = id[0];
109 }
110
111 return id;
112 }
113
114 /**
115 * Widget: Initiate color picker
116 *
117 * @since 1.0.0
118 */
119 function ayg_widget_color_picker( widget ) {
120 widget.find( '.ayg-color-picker' ).wpColorPicker( {
121 change: _.throttle( function() { // For Customizer
122 $( this ).trigger( 'change' );
123 }, 3000 )
124 });
125 }
126
127 function on_ayg_widget_update( event, widget ) {
128 ayg_widget_color_picker( widget );
129 }
130
131 /**
132 * Called when the page has loaded.
133 *
134 * @since 1.0.0
135 */
136 $(function() {
137 // Dashboard: Save API Key
138 $( '#ayg-button-save-api-key' ).on( 'click', function( e ) {
139 e.preventDefault();
140
141 $( this ).prop( 'disabled', true );
142 $( '.ayg-ajax-status', '#ayg-table-api-key' ).html( '<span class="spinner"></span>' );
143
144 var data = {
145 'action': 'ayg_save_api_key',
146 'api_key': $( '#ayg-api-key' ).val(),
147 'security': ayg_admin.ajax_nonce
148 };
149
150 if ( ! data.api_key ) {
151 $( this ).prop( 'disabled', false );
152 $( '.ayg-ajax-status', '#ayg-table-api-key' ).html( '<span class="ayg-text-error">' + ayg_admin.i18n.invalid_api_key + '</span>' );
153
154 return false;
155 }
156
157 $.post( ajaxurl, data, function( response ) {
158 window.location.reload(); // Reload document
159 });
160 });
161
162 // Dashboard: Generate shortcode
163 $( '#ayg-generate-shortcode' ).on( 'click', function( e ) {
164 e.preventDefault();
165
166 // Attributes
167 var props = {};
168
169 $( '.ayg-editor-control', '#ayg-shortcode-builder' ).each(function() {
170 var $elem = $( this ).find( '.ayg-editor-field' );
171 var type = $elem.attr( 'type' );
172 var key = $elem.attr( 'name' );
173 var value = $elem.val();
174 var def = $elem.data( 'default' );
175
176 // field type = checkbox
177 if ( 'checkbox' == type ) {
178 value = $elem.is( ':checked' ) ? 1 : 0;
179 }
180
181 // source = playlist
182 if ( 'playlist' == key ) {
183 value = ayg_get_youtube_playlist_id( value );
184 }
185
186 // source = channel
187 if ( 'channel' == key ) {
188 var result = ayg_get_youtube_channel_id( value );
189
190 key = result.type;
191 value = result.id;
192
193 if ( props.hasOwnProperty( 'type' ) && 'channel' == props.type ) {
194 props.type = key;
195 }
196 }
197
198 // source = video
199 if ( 'video' == key ) {
200 value = ayg_get_youtube_video_id( value );
201 }
202
203 // source = videos
204 if ( 'videos' == key ) {
205 var lines = value.split( '\n' ),
206 ids = [];
207
208 lines.map(function( url ) {
209 ids.push( ayg_get_youtube_video_id( url ) );
210 });
211
212 value = ids.join( ',' );
213 }
214
215 // Add only if the user input differ from the global configuration
216 if ( value != def ) {
217 props[ key ] = value;
218 }
219 });
220
221 var attrs = '';
222 for ( var key in props ) {
223 if ( props.hasOwnProperty( key ) ) {
224 attrs += ( ' ' + key + '="' + props[ key ] + '"' );
225 }
226 }
227
228 // Shortcode output
229 $( '#aiovg-shortcode').val( '[automatic_youtube_gallery' + attrs + ']' );
230
231 // Initialize the popup
232 $( 'html' ).addClass( 'ayg-no-scroll' );
233 $( '#ayg-shortcode-modal' ).show();
234 });
235
236 // Dashboard: Close the shortcode builder popup
237 $( '.ayg-modal-close' ).on( 'click', function( e ) {
238 e.preventDefault();
239 ayg_modal_hide();
240 });
241
242 $( '.ayg-modal-content' ).on( 'click', function( e ) {
243 if ( $( e.target ).hasClass( 'ayg-modal-content' ) ) {
244 ayg_modal_hide();
245 };
246 });
247
248 // Editor: Toggle between field sections
249 $( document ).on( 'click', '.ayg-editor-section-header', function( e ) {
250 var $elem = $( this ).parent();
251
252 if ( ! $elem.hasClass( 'ayg-active' ) ) {
253 $( this ).closest( '.ayg-editor' )
254 .find( '.ayg-editor-section.ayg-active' )
255 .toggleClass( 'ayg-active' )
256 .find( '.ayg-editor-controls' )
257 .slideToggle();
258 }
259
260 $elem.toggleClass( 'ayg-active' )
261 .find( '.ayg-editor-controls' )
262 .slideToggle();
263 });
264
265 // Editor: Show/Hide fields based on the selected source 'type'
266 $( document ).on( 'change', '.ayg-editor-field-type', function( e ) {
267 var type = $( this ).val();
268 var $elem = $( this ).closest( '.ayg-editor' );
269
270 $elem.removeClass(function( index, classes ) {
271 var matches = classes.match( /\ayg-editor-field-type-\S+/ig );
272 return ( matches ) ? matches.join(' ') : '';
273 });
274
275 $elem.addClass( 'ayg-editor-field-type-' + type );
276 });
277
278 // Editor: Show/Hide fields based on the selected theme
279 $( document ).on( 'change', '.ayg-editor-field-theme', function( e ) {
280 var theme = $( this ).val();
281 var $elem = $( this ).closest( '.ayg-editor' );
282
283 $elem.removeClass(function( index, classes ) {
284 var matches = classes.match( /\ayg-editor-field-theme-\S+/ig );
285 return ( matches ) ? matches.join(' ') : '';
286 });
287
288 $elem.addClass( 'ayg-editor-field-theme-' + theme );
289 });
290
291 // Settings: Initialize the color picker
292 $( '.ayg-color-picker', '#ayg-settings' ).wpColorPicker();
293
294 // Settings: Show/Hide fields based on the selected theme
295 $( 'tr.theme', '#ayg-settings' ).find( 'select' ).on( 'change', function() {
296 var theme = $( this ).val();
297 var $elem = $( '#ayg-settings' );
298
299 $elem.removeClass(function( index, classes ) {
300 var matches = classes.match( /\ayg-settings-theme-\S+/ig );
301 return ( matches ) ? matches.join(' ') : '';
302 });
303
304 $elem.addClass( 'ayg-settings-theme-' + theme );
305 });
306
307 // Settings: Browse button
308 $( '.ayg-settings-browse' ).on( 'click', function( e ) {
309 e.preventDefault();
310 ayg_render_media_uploader( $( this ) );
311 });
312
313 // Settings: Delete cache
314 $( '#ayg-button-delete-cache' ).on( 'click', function( e ) {
315 e.preventDefault();
316
317 $( this ).prop( 'disabled', true );
318 $( '.ayg-ajax-status', '#ayg-table-delete-cache' ).html( '<span class="spinner"></span>' );
319
320 var data = {
321 'action': 'ayg_delete_cache',
322 'security': ayg_admin.ajax_nonce
323 };
324
325 $.post( ajaxurl, data, function( response ) {
326 $( this ).prop( 'disabled', false );
327 $( '.ayg-ajax-status', '#ayg-table-delete-cache' ).html( '<span class="ayg-text-success">' + ayg_admin.i18n.cleared + '</span>' );
328 });
329 });
330
331 // Widget: Initiate color picker
332 $( '#widgets-right .widget:has(.ayg-color-picker)' ).each(function() {
333 ayg_widget_color_picker( $( this ) );
334 });
335
336 $( document ).on( 'widget-added widget-updated', on_ayg_widget_update );
337
338 // Gutenberg: Toggle fields or panels based on the selected source type
339 if ( 'undefined' !== typeof wp && 'undefined' !== typeof wp['hooks'] ) {
340 wp.hooks.addFilter( 'ayg_block_toggle_controls', 'automatic-youtube-gallery/block', function( value, control, attributes ) {
341 switch ( control ) {
342 case 'channel':
343 if ( 'livestream' == attributes.type ) {
344 value = true;
345 }
346 break;
347 case 'cache':
348 case 'player_title':
349 case 'player_description':
350 case 'loop':
351 if ( 'livestream' == attributes.type ) {
352 value = false;
353 }
354 break;
355 case 'autoadvance':
356 if ( 'video' == attributes.type || 'livestream' == attributes.type ) {
357 value = false;
358 }
359 break;
360 }
361
362 return value;
363 });
364
365 wp.hooks.addFilter( 'ayg_block_toggle_panels', 'automatic-youtube-gallery/block', function( value, panel, attributes ) {
366 switch ( panel ) {
367 case 'gallery':
368 if ( 'livestream' == attributes.type ) {
369 value = false;
370 }
371 break;
372 }
373
374 return value;
375 });
376 }
377 });
378
379 })( jQuery );
380