PluginProbe ʕ •ᴥ•ʔ
Responsive Lightbox & Gallery / 2.5.4
Responsive Lightbox & Gallery v2.5.4
2.7.8 trunk 1.0.0 1.0.1 1.0.1.1 1.0.2 1.0.3 1.0.4 1.1.0 1.1.1 1.1.2 1.2.0 1.2.1 1.2.2 1.2.3 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.4.0 1.4.0.1 1.4.1 1.4.11 1.4.12 1.4.13 1.4.14 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.9 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.6.0 1.6.1 1.6.10 1.6.11 1.6.12 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.6.9 1.7.0 1.7.1 1.7.2 2.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.1 2.2.0 2.2.1 2.2.2 2.2.3 2.2.3.1 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.3.5 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.4.8 2.4.9 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 2.6.0 2.6.1 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7
responsive-lightbox / js / admin-widgets.js
responsive-lightbox / js Last commit date
admin-folders.js 7 months ago admin-galleries.js 3 years ago admin-gallery.js 4 years ago admin-media.js 1 year ago admin-plugins.js 3 years ago admin-widgets.js 3 years ago admin.js 3 years ago front-basicmasonry.js 3 years ago front-basicslider.js 1 year ago front.js 7 months ago gutenberg.min.js 7 months ago
admin-widgets.js
255 lines
1 ( function( window, $ ) {
2
3 /* Gallery widget */
4 var rl_galleries = [];
5 var rl_gallery_frames = [];
6 var rl_gallery_ids = [];
7 var rl_gallery_images = [];
8
9 /* Single image widget */
10 var rl_image_frame;
11 var rl_image_container;
12 var rl_image_input;
13
14 // ready event
15 $( function() {
16 init_rl_gallery_widget();
17 init_rl_image_widget();
18 } );
19
20 // custom events trigger
21 $( document ).on( 'widget-updated widget-added', function() {
22 init_rl_gallery_widget();
23 init_rl_image_widget();
24 } );
25
26 function init_rl_gallery_widget() {
27 rl_galleries = $( '.rl-gallery-widget' );
28
29 if ( rl_galleries.length > 0 ) {
30 $.each( rl_galleries, function( index, gallery ) {
31 var gallery_id_attr = $( gallery ).attr( 'id' );
32 var gallery_id_arr = gallery_id_attr.match( /\-\d*\-/ );
33
34 if ( gallery_id_arr != null ) {
35 var gallery_id = gallery_id_arr.shift().replace( /-/g, '' );
36
37 rl_gallery_frames[gallery_id] = [ ];
38 rl_gallery_ids[gallery_id] = $( gallery ).find( '.rl-gallery-ids' );
39 rl_gallery_images[gallery_id] = $( gallery ).find( '.rl-gallery-images' );
40
41 // image ordering
42 rl_gallery_images[gallery_id].sortable( {
43 items: 'li.rl-gallery-image',
44 cursor: 'move',
45 scrollSensitivity: 40,
46 forcePlaceholderSize: true,
47 forceHelperSize: false,
48 helper: 'clone',
49 opacity: 0.65,
50 placeholder: 'rl-gallery-sortable-placeholder',
51 start: function( event, ui ) {
52 ui.item.css( 'border-color', '#f6f6f6' );
53 },
54 stop: function( event, ui ) {
55 ui.item.removeAttr( 'style' );
56 },
57 update: function( event, ui ) {
58 var attachment_ids = [ ];
59
60 $( rl_gallery_images[gallery_id] ).find( 'li.rl-gallery-image' ).each( function() {
61 var attachment_id = jQuery( this ).attr( 'data-attachment_id' );
62
63 attachment_ids.push( attachment_id );
64 } );
65
66 rl_gallery_ids[gallery_id].val( attachment_ids.join( ',' ) );
67 rl_gallery_ids[gallery_id].trigger( 'change' );
68 }
69 } );
70
71 // removing images
72 $( rl_gallery_images[gallery_id] ).on( 'click', '.rl-gallery-image-remove', function() {
73 var li = $( this ).closest( 'li.rl-gallery-image' );
74 var attachment_ids = rl_gallery_ids[gallery_id].val().split( ',' ).map( function( el ) {
75 return parseInt( el );
76 } );
77
78 // remove id
79 attachment_ids = _.without( attachment_ids, parseInt( li.data( 'attachment_id' ) ) );
80
81 // remove attachment
82 li.remove();
83
84 // update attachment ids
85 rl_gallery_ids[gallery_id].val( attachment_ids.join( ',' ) );
86 rl_gallery_ids[gallery_id].trigger( 'change' );
87
88 return false;
89 } );
90 }
91 } );
92 }
93
94 // open the modal on click
95 $( '.rl-gallery-widget-select' ).on( 'click', function( event ) {
96 event.preventDefault();
97
98 var gallery = $( this ).closest( '.rl-gallery-widget' );
99 var gallery_id = 0;
100 var gallery_id_attr = $( gallery ).attr( 'id' );
101 var gallery_id_arr = gallery_id_attr.match( /\-\d*\-/ );
102
103 if ( gallery_id_arr != null ) {
104 gallery_id = gallery_id_arr.shift().replace( /-/g, '' );
105
106 var attachment_ids = rl_gallery_ids[gallery_id].val();
107
108 // if the media frame already exists, reopen it.
109 if ( rl_gallery_frames[gallery_id].length != 0 ) {
110 rl_gallery_frames[gallery_id].open();
111
112 return;
113 }
114
115 // create the media frame.
116 rl_gallery_frames[gallery_id] = wp.media( {
117 // Set the title of the modal.
118 title: rlArgsWidgets.textSelectImages,
119 multiple: true,
120 button: {
121 text: rlArgsWidgets.textUseImages
122 },
123 library: { type: 'image' },
124 multiple: true
125 } );
126
127 rl_gallery_frames[gallery_id].on( 'open', function() {
128 var selection = rl_gallery_frames[gallery_id].state().get( 'selection' );
129 var attachment_ids = rl_gallery_ids[gallery_id].val().split( ',' );
130
131 $.each( attachment_ids, function() {
132 let _this = this;
133
134 if ( ! isNaN( parseFloat( _this ) ) && isFinite( _this ) ) {
135 attachment = wp.media.attachment( this );
136 attachment.fetch();
137 selection.add( attachment ? [ attachment ] : [ ] );
138 }
139 } );
140 } );
141
142 // when an image is selected, run a callback.
143 rl_gallery_frames[gallery_id].on( 'select', function() {
144 var selection = rl_gallery_frames[gallery_id].state().get( 'selection' );
145 var selected_ids = [ ];
146 var attachment_ids = rl_gallery_ids[gallery_id].val().split( ',' ).map( function( el ) {
147 return parseInt( el );
148 } );
149
150 if ( selection ) {
151 selection.map( function( attachment ) {
152 if ( attachment.id ) {
153 selected_ids.push( attachment.id );
154
155 // is image already in gallery?
156 if ( $.inArray( attachment.id, attachment_ids ) !== -1 )
157 return;
158
159 attachment_ids.push( attachment.id );
160 attachment = attachment.toJSON();
161
162 // is preview size available?
163 if ( attachment.sizes && attachment.sizes['thumbnail'] ) {
164 attachment.url = attachment.sizes['thumbnail'].url;
165 }
166
167 rl_gallery_images[gallery_id].append( '\
168 <li class="rl-gallery-image" data-attachment_id="' + attachment.id + '">\
169 <div class="rl-gallery-inner"><div class="centered"><img src="' + attachment.url + '" /></div></div>\
170 <div class="rl-gallery-actions"><a href="#" class="rl-gallery-image-remove dashicons-before dashicons-no" title="' + rlArgsWidgets.textRemoveImage + '"></a></div>\
171 </li>'
172 );
173 }
174 } );
175 }
176
177 // assign copy of attachments ids
178 var copy = attachment_ids;
179
180 for ( var i = 0; i < attachment_ids.length; i++ ) {
181 // unselected image?
182 if ( $.inArray( attachment_ids[i], selected_ids ) === -1 ) {
183 $( rl_gallery_images[gallery_id] ).find( 'li.rl-gallery-image[data-attachment_id="' + attachment_ids[i] + '"]' ).remove();
184
185 copy = _.without( copy, attachment_ids[i] )
186 }
187 }
188
189 rl_gallery_ids[gallery_id].val( _.uniq( copy ).join( ',' ) );
190 rl_gallery_ids[gallery_id].trigger( 'change' );
191 } );
192
193 // finally, open the modal.
194 rl_gallery_frames[gallery_id].open();
195 }
196 } );
197 }
198
199 function init_rl_image_widget() {
200 $( '.rl-image-widget-select' ).on( 'click', function( event ) {
201 var _this = $( this );
202
203 rl_image_container = _this.parent().find( '.rl-image-widget-content' );
204 rl_image_input = _this.parent().find( '.rl-image-widget-image-id' );
205
206 event.preventDefault();
207
208 // create a new media frame
209 rl_image_frame = wp.media( {
210 title: rlArgsWidgets.textSelectImage,
211 button: {
212 text: rlArgsWidgets.textUseImage
213 },
214 library: { type: 'image' },
215 multiple: false
216 } );
217
218 // when an image is selected in the media frame...
219 rl_image_frame.on( 'select', function() {
220 // get media attachment details from the frame state
221 var attachment = rl_image_frame.state().get( 'selection' ).first().toJSON();
222
223 // send the attachment URL to our custom image input field.
224 rl_image_container.html( '<img src="' + attachment.url + '" alt="" />' );
225
226 // send the attachment id to our hidden input
227 rl_image_input.val( attachment.id );
228 rl_image_input.trigger( 'change' );
229 } );
230
231 // when an image is already selected in the media frame...
232 rl_image_frame.on( 'open', function() {
233 var selection = rl_image_frame.state().get( 'selection' );
234
235 // get current image
236 var attachment = wp.media.attachment( rl_image_input.val() );
237
238 attachment.fetch();
239
240 // preselect in media frame
241 selection.add( attachment ? [ attachment ] : [ ] );
242 } );
243
244 rl_image_frame.open();
245 } );
246
247 $( '.rl-image-link-to' ).on( 'change', function() {
248 if ( $( this ).val() === 'custom' )
249 $( this ).parent().next().slideDown( 'fast' );
250 else
251 $( this ).parent().next().slideUp( 'fast' );
252 } );
253 }
254
255 } )( this, jQuery );