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

366 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 // Common: Initialize the color picker
138 $( '.ayg-color-picker' ).wpColorPicker();
139
140 // Dashboard: Save API Key
141 $( '#ayg-button-save-api-key' ).on( 'click', function( e ) {
142 e.preventDefault();
143
144 $( this ).prop( 'disabled', true );
145 $( '.ayg-ajax-status', '#ayg-table-api-key' ).html( '<span class="spinner"></span>' );
146
147 var data = {
148 'action': 'ayg_save_api_key',
149 'api_key': $( '#ayg-api-key' ).val(),
150 'security': ayg_admin.ajax_nonce
151 };
152
153 if ( ! data.api_key ) {
154 $( this ).prop( 'disabled', false );
155 $( '.ayg-ajax-status', '#ayg-table-api-key' ).html( '<span class="ayg-text-error">' + ayg_admin.i18n.invalid_api_key + '</span>' );
156
157 return false;
158 }
159
160 $.post( ajaxurl, data, function( response ) {
161 window.location.reload(); // Reload document
162 });
163 });
164
165 // Dashboard: Generate shortcode
166 $( '#ayg-generate-shortcode' ).on( 'click', function( e ) {
167 e.preventDefault();
168
169 // Attributes
170 var props = {};
171
172 $( '.ayg-editor-control', '#ayg-shortcode-builder' ).each(function() {
173 var $elem = $( this ).find( '.ayg-editor-field' );
174 var type = $elem.attr( 'type' );
175 var key = $elem.attr( 'name' );
176 var value = $elem.val();
177 var def = $elem.data( 'default' );
178
179 // field type = checkbox
180 if ( 'checkbox' == type ) {
181 value = $elem.is( ':checked' ) ? 1 : 0;
182 }
183
184 // source = playlist
185 if ( 'playlist' == key ) {
186 value = ayg_get_youtube_playlist_id( value );
187 }
188
189 // source = channel
190 if ( 'channel' == key ) {
191 var result = ayg_get_youtube_channel_id( value );
192
193 key = result.type;
194 value = result.id;
195
196 if ( props.hasOwnProperty( 'type' ) && 'channel' == props.type ) {
197 props.type = key;
198 }
199 }
200
201 // source = video
202 if ( 'video' == key ) {
203 value = ayg_get_youtube_video_id( value );
204 }
205
206 // source = videos
207 if ( 'videos' == key ) {
208 var lines = value.split( '\n' ),
209 ids = [];
210
211 lines.map(function( url ) {
212 ids.push( ayg_get_youtube_video_id( url ) );
213 });
214
215 value = ids.join( ',' );
216 }
217
218 // Add only if the user input differ from the global configuration
219 if ( value != def ) {
220 props[ key ] = value;
221 }
222 });
223
224 var attrs = '';
225 for ( var key in props ) {
226 if ( props.hasOwnProperty( key ) ) {
227 attrs += ( ' ' + key + '="' + props[ key ] + '"' );
228 }
229 }
230
231 // Shortcode output
232 $( '#aiovg-shortcode').val( '[automatic_youtube_gallery' + attrs + ']' );
233
234 // Initialize the popup
235 $( 'html' ).addClass( 'ayg-no-scroll' );
236 $( '#ayg-shortcode-modal' ).show();
237 });
238
239 // Dashboard: Close the shortcode builder popup
240 $( '.ayg-modal-close' ).on( 'click', function( e ) {
241 e.preventDefault();
242 ayg_modal_hide();
243 });
244
245 $( '.ayg-modal-content' ).on( 'click', function( e ) {
246 if ( $( e.target ).hasClass( 'ayg-modal-content' ) ) {
247 ayg_modal_hide();
248 };
249 });
250
251 // Editor: Toggle between field sections
252 $( document ).on( 'click', '.ayg-editor-section-header', function( e ) {
253 var $elem = $( this ).parent();
254
255 if ( ! $elem.hasClass( 'ayg-active' ) ) {
256 $( this ).closest( '.ayg-editor' )
257 .find( '.ayg-editor-section.ayg-active' )
258 .toggleClass( 'ayg-active' )
259 .find( '.ayg-editor-controls' )
260 .slideToggle();
261 }
262
263 $elem.toggleClass( 'ayg-active' )
264 .find( '.ayg-editor-controls' )
265 .slideToggle();
266 });
267
268 // Editor: Show/Hide fields based on the selected source 'type'
269 $( document ).on( 'change', '.ayg-editor-field-type', function( e ) {
270 var type = $( this ).val();
271 var $elem = $( this ).closest( '.ayg-editor' );
272
273 $elem.removeClass(function( index, classes ) {
274 var matches = classes.match( /\ayg-editor-field-type-\S+/ig );
275 return ( matches ) ? matches.join(' ') : '';
276 });
277
278 $elem.addClass( 'ayg-editor-field-type-' + type );
279 });
280
281 // Editor: Show/Hide fields based on the selected theme
282 $( document ).on( 'change', '.ayg-editor-field-theme', function( e ) {
283 var theme = $( this ).val();
284 var $elem = $( this ).closest( '.ayg-editor' );
285
286 $elem.removeClass(function( index, classes ) {
287 var matches = classes.match( /\ayg-editor-field-theme-\S+/ig );
288 return ( matches ) ? matches.join(' ') : '';
289 });
290
291 $elem.addClass( 'ayg-editor-field-theme-' + theme );
292 });
293
294 // Editor: Show/Hide fields based on the selected pagination type
295 $( document ).on( 'change', '.ayg-editor-field-pagination_type', function( e ) {
296 var pagination_type = $( this ).val();
297 var $elem = $( this ).closest( '.ayg-editor' );
298
299 $elem.removeClass(function( index, classes ) {
300 var matches = classes.match( /\ayg-editor-field-pagination_type-\S+/ig );
301 return ( matches ) ? matches.join(' ') : '';
302 });
303
304 $elem.addClass( 'ayg-editor-field-pagination_type-' + pagination_type );
305 });
306
307 // Settings: Show/Hide fields based on the selected theme
308 $( 'tr.theme', '#ayg-settings' ).find( 'select' ).on( 'change', function() {
309 var theme = $( this ).val();
310 var $elem = $( '#ayg-settings' );
311
312 $elem.removeClass(function( index, classes ) {
313 var matches = classes.match( /\ayg-settings-theme-\S+/ig );
314 return ( matches ) ? matches.join(' ') : '';
315 });
316
317 $elem.addClass( 'ayg-settings-theme-' + theme );
318 });
319
320 // Settings: Show/Hide fields based on the selected pagination type
321 $( 'tr.pagination_type', '#ayg-settings' ).find( 'select' ).on( 'change', function() {
322 var pagination_type = $( this ).val();
323 var $elem = $( '#ayg-settings' );
324
325 $elem.removeClass(function( index, classes ) {
326 var matches = classes.match( /\ayg-settings-pagination_type-\S+/ig );
327 return ( matches ) ? matches.join(' ') : '';
328 });
329
330 $elem.addClass( 'ayg-settings-pagination_type-' + pagination_type );
331 });
332
333 // Settings: Browse button
334 $( '.ayg-settings-browse' ).on( 'click', function( e ) {
335 e.preventDefault();
336 ayg_render_media_uploader( $( this ) );
337 });
338
339 // Settings: Delete cache
340 $( '#ayg-button-delete-cache' ).on( 'click', function( e ) {
341 e.preventDefault();
342
343 $( this ).prop( 'disabled', true );
344 $( '.ayg-ajax-status', '#ayg-table-delete-cache' ).html( '<span class="spinner"></span>' );
345
346 var data = {
347 'action': 'ayg_delete_cache',
348 'security': ayg_admin.ajax_nonce
349 };
350
351 $.post( ajaxurl, data, function( response ) {
352 $( this ).prop( 'disabled', false );
353 $( '.ayg-ajax-status', '#ayg-table-delete-cache' ).html( '<span class="ayg-text-success">' + ayg_admin.i18n.cleared + '</span>' );
354 });
355 });
356
357 // Widget: Initiate color picker
358 $( '#widgets-right .widget:has(.ayg-color-picker)' ).each(function() {
359 ayg_widget_color_picker( $( this ) );
360 });
361
362 $( document ).on( 'widget-added widget-updated', on_ayg_widget_update );
363 });
364
365 })( jQuery );
366