PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 1.3.6.1
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v1.3.6.1
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.3.6.1, at assets/js/bulk.js

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