PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 7.6.2
Jetpack – WP Security, Backup, Speed, & Growth v7.6.2
16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 All 504 releases
jetpack / modules / widgets / gallery / js / admin.js

admin.js in Jetpack – WP Security, Backup, Speed, & Growth 7.6.2, at modules/widgets/gallery/js/admin.js

237 lines 6.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /* jshint onevar: false, multistr: true */
2 /* global _wpMediaViewsL10n, _wpGalleryWidgetAdminSettings */
3
4 ( function( $ ) {
5 var $ids;
6 var $thumbs;
7
8 $( function() {
9 $( document.body ).on( 'click', '.gallery-widget-choose-images', function( event ) {
10 event.preventDefault();
11
12 var widget_form = $( this ).closest( 'form, .form' );
13
14 $ids = widget_form.find( '.gallery-widget-ids' );
15 $thumbs = widget_form.find( '.gallery-widget-thumbs' );
16
17 var idsString = $ids.val();
18
19 var attachments = getAttachments( idsString );
20
21 var selection = null;
22 var editing = false;
23
24 if ( attachments ) {
25 selection = getSelection( attachments );
26
27 editing = true;
28 }
29
30 var options = {
31 state: 'gallery-edit',
32 title: wp.media.view.l10n.addMedia,
33 multiple: true,
34 editing: editing,
35 selection: selection,
36 };
37
38 var workflow = getWorkflow( options );
39
40 workflow.open();
41 } );
42
43 // Setup an onchange handler to toggle various options when changing style. The different style options
44 // require different form inputs to be presented in the widget; this event will keep the UI in sync
45 // with the selected style
46 $( '.widget-inside' ).on( 'change', '.gallery-widget-style', setupStyleOptions );
47
48 // Setup the Link To options for all forms currently on the page. Does the same as the onChange handler, but
49 // is called once to display the correct form inputs for each widget on the page
50 setupStyleOptions();
51 } );
52
53 var media = wp.media,
54 l10n;
55
56 // Link any localized strings.
57 l10n = media.view.l10n = typeof _wpMediaViewsL10n === 'undefined' ? {} : _wpMediaViewsL10n;
58
59 /**
60 * wp.media.view.MediaFrame.GalleryWidget
61 *
62 * This behavior can be very nearly had by setting the workflow's state to 'gallery-edit', but
63 * we cannot use the custom WidgetGalleryEdit controller with it (must overide createStates(),
64 * which is necessary to disable the sidebar gallery settings in the media browser)
65 */
66 media.view.MediaFrame.GalleryWidget = media.view.MediaFrame.Post.extend( {
67 createStates: function() {
68 var options = this.options;
69
70 // `CollectionEdit` and `CollectionAdd` were only introduced in r27214-core,
71 // so they may not be available yet.
72 if ( 'CollectionEdit' in media.controller ) {
73 this.states.add( [
74 new media.controller.CollectionEdit( {
75 type: 'image',
76 collectionType: 'gallery',
77 title: l10n.editGalleryTitle,
78 SettingsView: media.view.Settings.Gallery,
79 library: options.selection,
80 editing: options.editing,
81 menu: 'gallery',
82 } ),
83 new media.controller.CollectionAdd( {
84 type: 'image',
85 collectionType: 'gallery',
86 title: l10n.addToGalleryTitle,
87 } ),
88 ] );
89 } else {
90 // If `CollectionEdit` is not available, then use the old approach.
91
92 if ( ! ( 'WidgetGalleryEdit' in media.controller ) ) {
93 // Remove the gallery settings sidebar when editing widgets.
94 media.controller.WidgetGalleryEdit = media.controller.GalleryEdit.extend( {
95 gallerySettings: function(/*browser*/) {
96 return;
97 },
98 } );
99 }
100
101 this.states.add( [
102 new media.controller.WidgetGalleryEdit( {
103 library: options.selection,
104 editing: options.editing,
105 menu: 'gallery',
106 } ),
107 new media.controller.GalleryAdd( {} ),
108 ] );
109 }
110 },
111 } );
112
113 function setupStyleOptions() {
114 $( '.widget-inside .gallery-widget-style' ).each( function(/*i*/) {
115 var style = $( this ).val();
116
117 var form = $( this ).parents( 'form' );
118
119 switch ( style ) {
120 case 'slideshow':
121 form.find( '.gallery-widget-link-wrapper' ).hide();
122 form.find( '.gallery-widget-columns-wrapper' ).hide();
123
124 break;
125
126 default:
127 form.find( '.gallery-widget-link-wrapper' ).show();
128 form.find( '.gallery-widget-columns-wrapper' ).show();
129 }
130 } );
131 }
132
133 /**
134 * Take a given Selection of attachments and a thumbs wrapper div (jQuery object)
135 * and fill it with thumbnails
136 */
137 function setupThumbs( selection, wrapper ) {
138 wrapper.empty();
139
140 var imageSize = _wpGalleryWidgetAdminSettings.thumbSize;
141
142 selection.each( function( model ) {
143 var sizedUrl = model.get( 'url' ) + '?w=' + imageSize + '&h=' + imageSize + '&crop=true';
144
145 var thumb = jQuery( '<img>', {
146 src: sizedUrl,
147 alt: model.get( 'title' ),
148 title: model.get( 'title' ),
149 width: imageSize,
150 height: imageSize,
151 class: 'thumb',
152 } );
153
154 wrapper.append( thumb );
155 } );
156 }
157
158 /**
159 * Take a csv string of ids (as stored in db) and fetch a full Attachments collection
160 */
161 function getAttachments( idsString ) {
162 if ( ! idsString ) {
163 return null;
164 }
165
166 // Found in /wp-includes/js/media-editor.js
167 var shortcode = wp.shortcode.next( 'gallery', '[gallery ids="' + idsString + '"]' );
168
169 // Ignore the rest of the match object, to give attachments() below what it expects
170 shortcode = shortcode.shortcode;
171
172 var attachments = wp.media.gallery.attachments( shortcode );
173
174 return attachments;
175 }
176
177 /**
178 * Take an Attachments collection and return a corresponding Selection model that can be
179 * passed to a MediaFrame to prepopulate the gallery picker
180 */
181 function getSelection( attachments ) {
182 var selection = new wp.media.model.Selection( attachments.models, {
183 props: attachments.props.toJSON(),
184 multiple: true,
185 } );
186
187 selection.gallery = attachments.gallery;
188
189 // Fetch the query's attachments, and then break ties from the
190 // query to allow for sorting.
191 selection.more().done( function() {
192 // Break ties with the query.
193 selection.props.set( { query: false } );
194 selection.unmirror();
195 selection.props.unset( 'orderby' );
196 } );
197
198 return selection;
199 }
200
201 /**
202 * Create a media 'workflow' (MediaFrame). This is the main entry point for the media picker
203 */
204 function getWorkflow( options ) {
205 var workflow = new wp.media.view.MediaFrame.GalleryWidget( options );
206
207 workflow.on(
208 'update',
209 function( selection ) {
210 var state = workflow.state();
211
212 selection = selection || state.get( 'selection' );
213
214 if ( ! selection ) {
215 return;
216 }
217
218 // Map the Models down into a simple array of ids that can be easily imploded to a csv string
219 var ids = selection.map( function( model ) {
220 return model.get( 'id' );
221 } );
222
223 var id_string = ids.join( ',' );
224
225 $ids.val( id_string ).trigger( 'change' );
226
227 setupThumbs( selection, $thumbs );
228 },
229 this
230 );
231
232 workflow.setState( workflow.options.state );
233
234 return workflow;
235 }
236 } )( jQuery );
237