PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 2.2.9
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v2.2.9
2.3.4 2.3.3 2.3.2 2.3.1 2.3.0 2.2.9 2.2.8 trunk 1.10 1.3.3 1.3.4 1.3.5 1.3.5.1 1.3.5.2 1.3.6 1.3.6.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.5 All 103 releases
imagify / assets / js / media-modal.js

media-modal.js in Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF 2.2.9, at assets/js/media-modal.js

335 lines 9.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /**
2 * Mini chart.
3 *
4 * @param {element} canvas The canvas element.
5 */
6 window.imagify.drawMeAChart = function( canvas ) {
7 canvas.each( function() {
8 var value = parseInt( jQuery( this ).closest( '.imagify-chart' ).next( '.imagify-chart-value' ).text(), 10 );
9
10 new window.imagify.Chart( this, { // eslint-disable-line no-new
11 type: 'doughnut',
12 data: {
13 datasets: [ {
14 data: [ value, 100 - value ],
15 backgroundColor: [ '#00B3D3', '#D8D8D8' ],
16 borderColor: '#fff',
17 borderWidth: 1
18 } ]
19 },
20 options: {
21 legend: {
22 display: false
23 },
24 events: [],
25 animation: {
26 easing: 'easeOutBounce'
27 },
28 tooltips: {
29 enabled: false
30 },
31 responsive: false
32 }
33 } );
34 } );
35 };
36
37 (function($, d, w, undefined) { // eslint-disable-line no-unused-vars, no-shadow, no-shadow-restricted-names
38
39 w.imagify.modal = {
40
41 working: [],
42
43 /*
44 * Init.
45 */
46 init: function () {
47 var $document = $( d ),
48 $processing;
49
50 // Update the chart in the media modal when a media is selected, and the ones already printed.
51 $( w ).on( 'canvasprinted.imagify', this.updateChart ).trigger( 'canvasprinted.imagify' );
52
53 // Toggle slide in custom column.
54 $( '.imagify-datas-details' ).hide();
55
56 $document.on( 'click', '.imagify-datas-more-action a', this.toggleSlide );
57
58 // Optimize, restore, etc.
59 $document.on( 'click', '.button-imagify-restore, .button-imagify-optimize, .button-imagify-manual-reoptimize, .button-imagify-optimize-missing-sizes, .button-imagify-generate-webp, .button-imagify-delete-webp', this.processOptimization );
60
61 $document.on( 'imagifybeat-send', this.addToImagifybeat );
62 $document.on( 'imagifybeat-tick', this.processImagifybeat );
63
64 // Some items may be processed in background on page load.
65 $processing = $( '.imagify-data-actions-container .button-imagify-processing' );
66
67 if ( $processing.length ) {
68 // Some media are already being processed.
69 // Lock the items, so we can check their status with Imagifybeat.
70 $processing.closest( '.imagify-data-actions-container' ).each( function() {
71 var $this = $( this ),
72 id = w.imagify.modal.sanitizeId( $this.data( 'id' ) ),
73 context = w.imagify.modal.sanitizeContext( $this.data( 'context' ) );
74
75 w.imagify.modal.lockItem( context, id );
76 } );
77
78 // Fasten Imagifybeat.
79 w.imagify.beat.interval( 15 );
80 }
81 },
82
83 // Charts ==================================================================================
84
85 /**
86 * Update the chart in the media modal when a media is selected, and the ones already printed.
87 *
88 * @param {object} e Event.
89 * @param {string} selector A CSS selector.
90 */
91 updateChart: function( e, selector ) {
92 var $canvas;
93
94 selector = selector || '.imagify-consumption-chart';
95 $canvas = $( selector );
96
97 w.imagify.drawMeAChart( $canvas );
98
99 $canvas.closest( '.imagify-datas-list' ).siblings( '.imagify-datas-details' ).hide();
100 },
101
102 // Optimization ============================================================================
103
104 /**
105 * Process to one of these actions: restore, optimize, re-optimize, or optimize missing sizes.
106 *
107 * @param {object} e Event.
108 */
109 processOptimization: function( e ) {
110 var $obj = $( this ),
111 $container = $obj.parents( '.imagify-data-actions-container' ),
112 id = w.imagify.modal.sanitizeId( $container.data( 'id' ) ),
113 context = w.imagify.modal.sanitizeContext( $container.data( 'context' ) ),
114 href, processingTemplate;
115
116 e.preventDefault();
117
118 if ( w.imagify.modal.isItemLocked( context, id ) ) {
119 return;
120 }
121
122 w.imagify.modal.lockItem( context, id );
123
124 href = $obj.attr( 'href' );
125 processingTemplate = w.imagify.template( 'imagify-button-processing' );
126
127 $container.html( processingTemplate( {
128 label: $obj.data( 'processing-label' )
129 } ) );
130
131 $.get( href.replace( 'admin-post.php', 'admin-ajax.php' ) )
132 .done( function( response ) {
133 if ( response.data && response.data.html ) {
134 // The work is done.
135 w.imagify.modal.displayProcessResult( context, id, response.data.html );
136 } else {
137 // Still processing in background: we're waiting for the result by poking Imagifybeat.
138 // Set the Imagifybeat interval to 15 seconds.
139 w.imagify.beat.interval( 15 );
140 }
141 } );
142 },
143
144 // Imagifybeat =============================================================================
145
146 /**
147 * Send the media IDs and their status to Imagifybeat.
148 *
149 * @param {object} e Event object.
150 * @param {object} data Object containing all Imagifybeat IDs.
151 */
152 addToImagifybeat: function ( e, data ) {
153 var $containers = $( '.imagify-data-actions-container' );
154
155 if ( ! $containers.length ) {
156 return;
157 }
158
159 data[ w.imagifyModal.imagifybeatID ] = {};
160
161 $containers.each( function() {
162 var $this = $( this ),
163 id = w.imagify.modal.sanitizeId( $this.data( 'id' ) ),
164 context = w.imagify.modal.sanitizeContext( $this.data( 'context' ) ),
165 locked = w.imagify.modal.isItemLocked( context, id ) ? 1 : 0;
166
167 data[ w.imagifyModal.imagifybeatID ][ context ] = data[ w.imagifyModal.imagifybeatID ][ context ] || {};
168 data[ w.imagifyModal.imagifybeatID ][ context ][ '_' + id ] = locked;
169 } );
170 },
171
172 /**
173 * Listen for the custom event "imagifybeat-tick" on $(document).
174 *
175 * @param {object} e Event object.
176 * @param {object} data Object containing all Imagifybeat IDs.
177 */
178 processImagifybeat: function ( e, data ) {
179 if ( typeof data[ w.imagifyModal.imagifybeatID ] === 'undefined' ) {
180 return;
181 }
182
183 $.each( data[ w.imagifyModal.imagifybeatID ], function( contextId, htmlContent ) {
184 var context, id;
185
186 context = String( contextId ).trim().match( /^(.+)_(\d+)$/ );
187
188 if ( ! context ) {
189 return;
190 }
191
192 id = w.imagify.modal.sanitizeId( context[2] );
193 context = w.imagify.modal.sanitizeContext( context[1] );
194
195 w.imagify.modal.displayProcessResult( context, id, htmlContent );
196 } );
197 },
198
199 // DOM manipulation tools ==================================================================
200
201 /**
202 * Display the process result.
203 *
204 * @param {string} context The media context.
205 * @param {int} id The media ID.
206 * @param {string} htmlContent The HTML to insert.
207 */
208 displayProcessResult: function( context, id, htmlContent ) {
209 var $containers = w.imagify.modal.getContainers( context, id );
210
211 $containers.html( htmlContent );
212 w.imagify.modal.unlockItem( context, id );
213
214 if ( ! w.imagify.modal.working.length ) {
215 // Work is done.
216 // Open the last container being processed.
217 w.imagify.modal.openSlide( $containers );
218 // Reset Imagifybeat interval.
219 w.imagify.beat.resetInterval();
220 }
221 },
222
223 /**
224 * Open a slide rapidly.
225 *
226 * @param {object} $containers A jQuery collection.
227 */
228 openSlide: function( $containers ) {
229 $containers.each( function() {
230 var $container = $( this ),
231 text = $container.find( '.imagify-datas-more-action a' ).data( 'close' );
232
233 $container.find( '.imagify-datas-more-action a' ).addClass( 'is-open' ).find( '.the-text' ).text( text );
234 $container.find( '.imagify-datas-details' ).show().addClass( 'is-open' );
235 } );
236 },
237
238 /**
239 * Toggle slide in custom column.
240 *
241 * @param {object} e Event.
242 */
243 toggleSlide: function( e ) {
244 var $this = $( this );
245
246 e.preventDefault();
247
248 if ( $this.hasClass( 'is-open' ) ) {
249 $( $this.attr( 'href' ) ).slideUp( 300 ).removeClass( 'is-open' );
250 $this.removeClass( 'is-open' ).find( '.the-text' ).text( $this.data( 'open' ) );
251 } else {
252 $( $this.attr( 'href' ) ).slideDown( 300 ).addClass( 'is-open' );
253 $this.addClass( 'is-open' ).find( '.the-text' ).text( $this.data( 'close' ) );
254 }
255 },
256
257 /**
258 * Get all containers matching the given context and id.
259 *
260 * @param {string} context The media context.
261 * @param {int} id The media ID.
262 * @return {object} A jQuery collection.
263 */
264 getContainers: function( context, id ) {
265 return $( '.imagify-data-actions-container[data-id="' + id + '"][data-context="' + context + '"]' );
266 },
267
268 // Sanitization ============================================================================
269
270 /**
271 * Sanitize a media ID.
272 *
273 * @param {int|string} id A media ID.
274 * @return {int}
275 */
276 sanitizeId: function( id ) {
277 return parseInt( id, 10 );
278 },
279
280 /**
281 * Sanitize a media context.
282 *
283 * @param {string} context A media context.
284 * @return {string}
285 */
286 sanitizeContext: function( context ) {
287 context = context.replace( '/[^a-z0-9_-]/gi', '' ).toLowerCase();
288 return context ? context : 'wp';
289 },
290
291 // Locks ===================================================================================
292
293 /**
294 * Lock an item.
295 *
296 * @param {string} context The media context.
297 * @param {int} id The media ID.
298 */
299 lockItem: function( context, id ) {
300 if ( ! this.isItemLocked( context, id ) ) {
301 this.working.push( context + '_' + id );
302 }
303 },
304
305 /**
306 * Unlock an item.
307 *
308 * @param {string} context The media context.
309 * @param {int} id The media ID.
310 */
311 unlockItem: function( context, id ) {
312 var name = context + '_' + id,
313 i = _.indexOf( this.working, name );
314
315 if ( i > -1 ) {
316 this.working.splice( i, 1 );
317 }
318 },
319
320 /**
321 * Tell if an item is locked.
322 *
323 * @param {string} context The media context.
324 * @param {int} id The media ID.
325 * @return {bool}
326 */
327 isItemLocked: function( context, id ) {
328 return _.indexOf( this.working, context + '_' + id ) > -1;
329 }
330 };
331
332 w.imagify.modal.init();
333
334 } )(jQuery, document, window);
335