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

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

316 lines 8.1 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_close_modal() {
50 $( '#ayg-shortcode-builder' ).find( '.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 // Admin: Dismiss admin notice
138 $( '#ayg-admin-notice' ).on( 'click', '.notice-dismiss', function( e ) {
139 e.preventDefault();
140
141 var data = {
142 'action': 'ayg_dismiss_admin_notice',
143 'security': $( '#ayg-admin-notice' ).data( 'security' )
144 };
145
146 $.post( ajaxurl, data );
147 });
148
149 // Settings: Initialize the color picker
150 $( '.ayg-color-picker', '#ayg-settings' ).wpColorPicker();
151
152 // Settings: Show/Hide fields based on the selected theme
153 $( 'tr.theme', '#ayg-settings' ).find( 'select' ).on( 'change', function() {
154 var theme = $( this ).val();
155 var $elem = $( '#ayg-settings' );
156
157 $elem.removeClass(function( index, classes ) {
158 var matches = classes.match( /\ayg-settings-theme-\S+/ig );
159 return ( matches ) ? matches.join(' ') : '';
160 });
161
162 $elem.addClass( 'ayg-settings-theme-' + theme );
163 });
164
165 // Settings: Browse button
166 $( '.ayg-settings-browse' ).on( 'click', function( e ) {
167 e.preventDefault();
168 ayg_render_media_uploader( $( this ) );
169 });
170
171 // Editor: Toggle between field sections
172 $( document ).on( 'click', '.ayg-editor-section-header', function( e ) {
173 var $elem = $( this ).parent();
174
175 if ( ! $elem.hasClass( 'ayg-active' ) ) {
176 $( this ).closest( '.ayg-editor' )
177 .find( '.ayg-editor-section.ayg-active' )
178 .toggleClass( 'ayg-active' )
179 .find( '.ayg-editor-controls' )
180 .slideToggle();
181 }
182
183 $elem.toggleClass( 'ayg-active' )
184 .find( '.ayg-editor-controls' )
185 .slideToggle();
186 });
187
188 // Editor: Show/Hide fields based on the selected source 'type'
189 $( document ).on( 'change', '.ayg-editor-field-type', function( e ) {
190 var type = $( this ).val();
191 var $elem = $( this ).closest( '.ayg-editor' );
192
193 $elem.removeClass(function( index, classes ) {
194 var matches = classes.match( /\ayg-editor-field-type-\S+/ig );
195 return ( matches ) ? matches.join(' ') : '';
196 });
197
198 $elem.addClass( 'ayg-editor-field-type-' + type );
199 });
200
201
202 // Editor: Show/Hide fields based on the selected theme
203 $( document ).on( 'change', '.ayg-editor-field-theme', function( e ) {
204 var theme = $( this ).val();
205 var $elem = $( this ).closest( '.ayg-editor' );
206
207 $elem.removeClass(function( index, classes ) {
208 var matches = classes.match( /\ayg-editor-field-theme-\S+/ig );
209 return ( matches ) ? matches.join(' ') : '';
210 });
211
212 $elem.addClass( 'ayg-editor-field-theme-' + theme );
213 });
214
215 // Shortcode Builder: Initialize the color picker
216 $( '.ayg-color-picker', '#ayg-shortcode-builder' ).wpColorPicker();
217
218 // Shortcode Builder: Initialize the popup
219 $( '#ayg-media-button' ).on( 'click', function() {
220 $( 'html' ).addClass( 'ayg-no-scroll' );
221 $( '#ayg-shortcode-builder' ).find( '.ayg-modal' ).show();
222 });
223
224 // Shortcode Builder: Insert shortcode
225 $( '#ayg-insert-shortcode' ).on( 'click', function( e ) {
226 e.preventDefault();
227
228 // Attributes
229 var props = {};
230
231 $( '.ayg-editor-control', '#ayg-shortcode-builder' ).each(function() {
232 var $elem = $( this ).find( '.ayg-editor-field' );
233 var type = $elem.attr( 'type' );
234 var key = $elem.attr( 'name' );
235 var value = $elem.val();
236 var def = $elem.data( 'default' );
237
238 // field type = checkbox
239 if ( 'checkbox' == type ) {
240 value = $elem.is( ':checked' ) ? 1 : 0;
241 }
242
243 // source = playlist
244 if ( 'playlist' == key ) {
245 value = ayg_get_youtube_playlist_id( value );
246 }
247
248 // source = channel
249 if ( 'channel' == key ) {
250 var result = ayg_get_youtube_channel_id( value );
251
252 key = result.type;
253 value = result.id;
254
255 if ( props.hasOwnProperty( 'type' ) && 'channel' == props.type ) {
256 props.type = key;
257 }
258 }
259
260 // source = video
261 if ( 'video' == key ) {
262 value = ayg_get_youtube_video_id( value );
263 }
264
265 // source = videos
266 if ( 'videos' == key ) {
267 var lines = value.split( '\n' ),
268 ids = [];
269
270 lines.map(function( url ) {
271 ids.push( ayg_get_youtube_video_id( url ) );
272 });
273
274 value = ids.join( ',' );
275 }
276
277 // Add only if the user input differ from the global configuration
278 if ( value != def ) {
279 props[ key ] = value;
280 }
281 });
282
283 var attrs = '';
284 for ( var key in props ) {
285 if ( props.hasOwnProperty( key ) ) {
286 attrs += ( ' ' + key + '="' + props[ key ] + '"' );
287 }
288 }
289
290 // Shortcode output
291 window.send_to_editor( '[automatic_youtube_gallery' + attrs + ']' );
292 ayg_close_modal();
293 });
294
295 // Shortcode Builder: Close the popup
296 $( '.ayg-modal-close', '#ayg-shortcode-builder' ).on( 'click', function( e ) {
297 e.preventDefault();
298 ayg_close_modal();
299 });
300
301 $( '.ayg-modal-content', '#ayg-shortcode-builder' ).on( 'click', function( e ) {
302 if ( $( e.target ).hasClass( 'ayg-modal-content' ) ) {
303 ayg_close_modal();
304 };
305 });
306
307 // Widget: Initiate color picker
308 $( '#widgets-right .widget:has(.ayg-color-picker)' ).each(function() {
309 ayg_widget_color_picker( $( this ) );
310 });
311
312 $( document ).on( 'widget-added widget-updated', on_ayg_widget_update );
313 });
314
315 })( jQuery );
316