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

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