PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 1.5
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v1.5
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 / bulk.js

bulk.js in Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF 1.5, at assets/js/bulk.js

386 lines 13.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 jQuery(function($){
2
3 var concat = ajaxurl.indexOf("?") > 0 ? "&" : "?";
4
5 // avoid error on IE
6 var imagify = {
7 log: function (content) {
8 if (console !== 'undefined') console.log(content);
9 }
10 };
11
12 var overviewCanvas = document.getElementById("imagify-overview-chart"),
13 overviewData = [
14 {
15 value: imagifyBulk.labels.totalUnoptimizedAttachments,
16 color:"#D9E4EB",
17 highlight: "#D9E4EB",
18 label: imagifyBulk.labels.overviewChartLabels.unoptimized
19 },
20 {
21 value: imagifyBulk.labels.totalOptimizedAttachments,
22 color: "#46B1CE",
23 highlight: "#46B1CE",
24 label: imagifyBulk.labels.overviewChartLabels.optimized
25 },
26 {
27 value: imagifyBulk.labels.totalErrorsAttachments,
28 color: "#2E3242",
29 highlight: "#2E3242",
30 label: imagifyBulk.labels.overviewChartLabels.error
31 }
32 ];
33
34 // to avoid JS error
35 if ( overviewCanvas ) {
36 var overviewDoughnut = new Chart(overviewCanvas.getContext("2d")).Doughnut(overviewData, {
37 segmentStrokeColor : "transparent",
38 segmentStrokeWidth : 0,
39 animateRotate : true,
40 animation: true,
41 percentageInnerCutout: 85,
42 legendTemplate : "<ul class=\"imagify-<%=name.toLowerCase()%>-legend\"><% for (var i=0; i<segments.length; i++){%><li><span style=\"background-color:<%=segments[i].fillColor%>\"></span><%if(segments[i].label){%><%=segments[i].label%><%}%></li><%}%></ul>",
43 tooltipTemplate: "<%= value %>"
44 });
45
46 //then you just need to generate the legend
47 //var overviewLegend = overviewDoughnut.generateLegend();
48 //bugged `segments undefined` ?
49
50 //and append it to your page somewhere
51 overviewLegend = '<ul class="imagify-doughnut-legend">';
52
53 $(overviewData).each(function(i){
54 overviewLegend += '<li><span style="background-color:' + overviewData[i].color + '"></span>' + overviewData[i].label + '</li>';
55 });
56
57 overviewLegend += '</ul>';
58
59 document.getElementById("imagify-overview-chart-legend").innerHTML = overviewLegend;
60 }
61
62 // Heartbeat
63 $(document).on('heartbeat-send', function(e, data) {
64 data['imagify_heartbeat'] = imagifyBulk.heartbeat_id;
65 });
66
67 // Listen for the custom event "heartbeat-tick" on $(document).
68 $(document).on( 'heartbeat-tick', function(e, data) {
69 if ( ! data['imagify_bulk_data'] ) {
70 return;
71 }
72
73 data = data['imagify_bulk_data'];
74
75 // The overview chart percent
76 $('#imagify-overview-chart-percent').html(data.optimized_attachments_percent + '<span>%</span>');
77 $('.imagify-total-percent').html(data.optimized_attachments_percent + '%' );
78
79 // The comsuption bar
80 $('.imagify-unconsumed-percent').html(data.unconsumed_quota + '%');
81 $('.imagify-unconsumed-bar').animate({'width': data.unconsumed_quota + '%'});
82
83 // The total optimized images
84 $('#imagify-total-optimized-attachments').html(data.already_optimized_attachments);
85
86 // The original bar
87 $('#imagify-original-bar').find('.imagify-barnb')
88 .html(data.original_human);
89
90 // The optimized bar
91 $('#imagify-optimized-bar').animate({'width': data.optimized_percent+"%"})
92 $('#imagify-optimized-bar').find('.imagify-barnb')
93 .html(data.optimized_human);
94
95 // The Percent data
96 $('#imagify-total-optimized-attachments-pct').html( data.optimized_percent + '%' );
97
98 overviewDoughnut.segments[0].value = data.unoptimized_attachments;
99 overviewDoughnut.segments[1].value = data.optimized_attachments;
100 overviewDoughnut.segments[2].value = data.errors_attachments;
101 overviewDoughnut.update();
102 });
103
104 // Simulate a click on the "Imagif'em all" button
105 $('#imagify-simulate-bulk-action').click(function(e){
106 e.preventDefault();
107 $('#imagify-bulk-action').trigger('click');
108 });
109
110 $('#imagify-bulk-action').click(function(){
111 var $obj = $(this),
112 $optimization_level = $('[name="optimization_level"]:checked').val();
113
114 if (typeof $optimization_level === "undefined") {
115 $optimization_level = -1;
116 }
117
118 if ( $obj.attr('disabled') ) {
119 return false;
120 }
121
122 $obj.attr('disabled', 'disabled');
123 $obj.find('.dashicons').addClass('rotate');
124
125
126 confirmMessage = function(){
127 return imagifyBulk.labels.processing;
128 };
129 $(window).on('beforeunload', confirmMessage);
130
131 // Display an alert to wait
132 swal({
133 title:imagifyBulk.labels.waitTitle,
134 text: imagifyBulk.labels.waitText,
135 closeOnConfirm: false,
136 showConfirmButton: false,
137 imageUrl: imagifyBulk.labels.waitImageUrl
138 });
139
140 $.get(ajaxurl+concat+"action="+imagifyBulk.ajax_action+"&optimization_level="+$optimization_level+"&imagifybulkuploadnonce="+$('#imagifybulkuploadnonce').val())
141 .done(function(response) {
142 if( !response.success ) {
143 $obj.removeAttr('disabled');
144 $obj.find('.dashicons').removeClass('rotate');
145
146 swal_title = '';
147 swal_text = '';
148
149 // remove confirm dialog before quit the page
150 $(window).off('beforeunload', confirmMessage);
151
152 if ( response.data.message == 'invalid-api-key' ) {
153 swal_title = imagifyBulk.labels.invalidAPIKeyTitle;
154 }
155
156 if ( response.data.message == 'over-quota' ) {
157 swal_title = imagifyBulk.labels.overQuotaTitle;
158 text = imagifyBulk.labels.overQuotaText;
159 }
160
161 if ( response.data.message == 'no-images' ) {
162 swal_title = imagifyBulk.labels.noAttachmentToOptimizeTitle;
163 swal_text = imagifyBulk.labels.noAttachmentToOptimizeText;
164 }
165
166 // Display an alert to warn that all images has been optimized
167 swal({
168 title : swal_title,
169 text : swal_text,
170 type : "info",
171 customClass : "imagify-sweet-alert"
172 });
173
174 } else {
175 swal.close();
176
177 var config = {
178 'lib': ajaxurl+concat+"action=imagify_bulk_upload&imagifybulkuploadnonce="+$('#imagifybulkuploadnonce').val(),
179 'images': response.data,
180 'context': imagifyBulk.ajax_context
181 }
182
183 var table = $('.imagify-bulk-table table tbody'),
184 files = 0,
185 errors = 0,
186 original_overall_size = 0,
187 overall_saving = 0;
188
189 $('.imagify-row-progress').slideDown();
190 $('.imagify-no-uploaded-yet, .imagify-row-complete').hide(200);
191
192 Optimizer = new ImagifyGulp(config);
193
194 // before the attachment optimization
195 Optimizer.before(function(data) {
196 table.find('.imagify-row-progress').after('<tr id="attachment-'+data.id+'"><td class="imagify-cell-filename"><span class="imagiuploaded"><img src="'+data.thumbnail+'"/>"</span><span class="imagifilename">'+data.filename+'</span></td><td class="imagify-cell-status"><span class="imagistatus status-compressing"><span class="dashicons dashicons-admin-generic rotate"></span>Compressing<span></span></span></td><td class="imagify-cell-original"></td><td class="imagify-cell-optimized"></td><td class="imagify-cell-percentage"></td><td class="imagify-cell-thumbnails"></td><td class="imagify-cell-savings"></td></tr>');
197 })
198 // after the attachment optimization
199 .each(function (data) {
200 var $progress = $('#imagify-progress-bar');
201 $progress.css({'width': data.progress + '%'});
202 $progress.find('.percent').html(data.progress + '%');
203
204 if ( data.success ) {
205 $('#attachment-'+data.image+' .imagify-cell-status').html('<span class="imagistatus status-complete"><span class="dashicons dashicons-yes"></span>Complete</span>');
206 $('#attachment-'+data.image+' .imagify-cell-original').html(data.original_size_human);
207 $('#attachment-'+data.image+' .imagify-cell-optimized').html(data.new_size_human);
208 $('#attachment-'+data.image+' .imagify-cell-percentage').html('<span class="imagify-chart"><span class="imagify-chart-container"><canvas height="18" width="18" id="imagify-consumption-chart" style="width: 18px; height: 18px;"></canvas></span></span><span class="imagipercent">'+data.percent+'</span>%');
209 draw_me_a_chart( $('#attachment-'+data.image+' .imagify-cell-percentage').find('canvas') );
210 $('#attachment-'+data.image+' .imagify-cell-thumbnails').html(data.thumbnails);
211 $('#attachment-'+data.image+' .imagify-cell-savings').html(Optimizer.humanSize(data.overall_saving, 1));
212
213 // The table footer total optimized files
214 files = files + data.thumbnails + 1;
215 $('.imagify-cell-nb-files').html(files + ' file(s)');
216
217 // The table footer original size
218 original_overall_size = original_overall_size + data.original_overall_size;
219 $('.imagify-total-original').html(Optimizer.humanSize(original_overall_size, 1));
220
221 // The table footer overall saving
222 overall_saving = overall_saving + data.overall_saving;
223 $('.imagify-total-gain').html(Optimizer.humanSize(overall_saving, 1));
224
225 } else {
226 error_class = 'error';
227 error_dashicon = 'dismiss';
228 error_message = 'Error';
229
230 if ( data.error.indexOf("You've consumed all your data") >= 0 ) {
231 swal({
232 title: imagifyBulk.labels.overQuotaTitle,
233 text: imagifyBulk.labels.overQuotaText,
234 type: "error",
235 customClass: "imagify-sweet-alert",
236 html: true,
237 },
238 function(){
239 location.reload();
240 });
241 }
242
243 if ( data.error.indexOf("This image is already compressed") >=0 ) {
244 error_class = 'warning';
245 error_dashicon = 'warning';
246 error_message = 'Notice';
247 } else {
248 errors++;
249 $('.imagify-cell-errors').html(errors + ' error(s)');
250 }
251
252 $('#attachment-'+data.image).after('<tr><td colspan="7"><span class="status-'+error_class+'">'+data.error+'</span></td></tr>');
253
254 $('#attachment-'+data.image+' .imagify-cell-status').html('<span class="imagistatus status-'+error_class+'"><span class="dashicons dashicons-'+error_dashicon+'"></span>'+error_message+'</span>');
255 }
256 })
257 // after all attachments optimization
258 .done(function (data) {
259 $obj.removeAttr('disabled');
260 $obj.find('.dashicons').removeClass('rotate');
261
262 // remove confirm dialog before quit the page
263 $(window).off('beforeunload', confirmMessage);
264
265 // Hide the progress bar
266 $('.imagify-row-progress').slideUp();
267
268 if ( data.global_percent !== 'NaN' ) {
269 // Display the complete section
270 $('.imagify-row-complete').removeClass('hidden')
271 .addClass( 'done' )
272 .attr('aria-hidden', 'false');
273 $('html, body').animate({
274 scrollTop: $('.imagify-row-complete').offset().top
275 }, 200);
276
277 $('.imagify-ac-rt-total-gain').html(data.global_gain_human);
278 $('.imagify-ac-rt-total-original').html(data.global_original_size_human);
279
280 text2share = imagifyBulk.labels.textToShare;
281 text2share = text2share.replace( '%1$s', data.global_gain_human );
282 text2share = text2share.replace( '%2$s', data.global_original_size_human );
283 text2share = encodeURIComponent(text2share);
284
285 $('.imagify-sn-twitter').attr( 'href', 'https://twitter.com/intent/tweet?source=webclient&amp;original_referer=' + imagifyBulk.labels.pluginURL + '&amp;text=' + text2share + '&amp;url=' + imagifyBulk.labels.pluginURL + '&amp;related=imagify&amp;hastags=performance,web,wordpress' );
286
287 $('.imagify-ac-chart').attr('data-percent', data.global_percent);
288 draw_me_complete_chart( $('.imagify-ac-chart').find('canvas') );
289 }
290 })
291 .error(function (id) {
292 imagify.log('Can\'t optimize image with id ' + id);
293 })
294 .run();
295 }
296 })
297 .fail(function () {
298 swal({
299 title: imagifyBulk.labels.getUnoptimizedImagesErrorTitle,
300 text: imagifyBulk.labels.getUnoptimizedImagesErrorText,
301 type: "error",
302 customClass: "imagify-sweet-alert"
303 },
304 function(){
305 location.reload();
306 });
307 });
308 });
309
310 /*
311 * Mini chart
312 * You can use draw_me_a_chart() function with AJAX calls
313 *
314 * @param {element} canvas
315 */
316 function draw_me_a_chart( canvas ) {
317 canvas.each(function(){
318
319 var the_value = parseInt( $(this).closest('.imagify-chart').next('.imagipercent').text() ),
320 overviewData = [
321 {
322 value: the_value,
323 color: "#00B3D3"
324 },
325 {
326 value: 100 - the_value,
327 color:"#D8D8D8"
328 }
329 ],
330 overviewDoughnut = new Chart( $(this)[0].getContext("2d")).Doughnut(overviewData, {
331 segmentStrokeColor : "#FFF",
332 segmentStrokeWidth : 1,
333 animateRotate : true,
334 tooltipEvents: []
335 });
336 });
337 }
338
339 /*
340 * Complete Chart
341 * You can use draw_me_complete_chart() function with AJAX calls
342 *
343 * @param {element} canvas
344 */
345 function draw_me_complete_chart( canvas ) {
346 canvas.each(function(){
347
348 var the_value = parseInt( $(this).closest('.imagify-ac-chart').attr('data-percent') ),
349 overviewData = [
350 {
351 value: the_value,
352 color: "#40B1D0"
353 },
354 {
355 value: 100 - the_value,
356 color:"#FFFFFF"
357 }
358 ],
359 overviewDoughnut = new Chart( $(this)[0].getContext("2d")).Doughnut(overviewData, {
360 segmentStrokeColor : "transparent",
361 segmentStrokeWidth : 0,
362 animateRotate : true,
363 animation: true,
364 percentageInnerCutout: 70,
365 tooltipEvents: []
366 });
367 });
368 }
369 });
370
371 var width = 700, height = 290;
372 if ( window.innerWidth ) {
373 var clientLeft = (window.innerWidth-width)/2;
374 var clientTop = (window.innerHeight-height)/2;
375 }
376 else {
377 var clientLeft = (document.body.clientWidth-width)/2;
378 var clientTop = (document.body.clientHeight-height)/2;
379 }
380
381 [].forEach.call( document.querySelectorAll('.imagify-share-networks a'), function(el) {
382 el.addEventListener('click', function(evt) {
383 window.open(this.href,'',"status=no, scrollbars=no, menubar=no, top="+clientTop+", left="+clientLeft+", width="+width+", height="+height);
384 evt.preventDefault();
385 }, false);
386 });