PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / trunk
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF vtrunk
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
← All changes | assets/js/jquery.twentytwenty.js +562 -490 1.4.6 → trunk View file →
@@ -1,66 +1,53 @@
1 -(function(window, $, undefined){
1 +(function($, d, w, undefined) { // eslint-disable-line no-unused-vars, no-shadow, no-shadow-restricted-names
2 2
3 - $.fn.twentytwenty = function(options, callback) {
4 - var options = $.extend({
5 - handlePosition : 0.5,
6 - orientation : 'horizontal',
7 - labelBefore : 'Before',
8 - labelAfter : 'After'
9 - }
10 - , options);
11 - return this.each(function() {
3 + $.fn.twentytwenty = function( options, callback ) {
4 + options = $.extend( {
5 + handlePosition: 0.5,
6 + orientation: 'horizontal',
7 + labelBefore: 'Before',
8 + labelAfter: 'After'
9 + }, options );
12 10
13 - var sliderPct = options.handlePosition,
14 - $container = $(this),
15 - sliderOrientation = options.orientation,
16 - beforeDirection = (sliderOrientation === 'vertical') ? 'down' : 'left',
17 - afterDirection = (sliderOrientation === 'vertical') ? 'up' : 'right',
18 - $beforeImg = $container.find('img:first'),
19 - $afterImg = $container.find('img:last');
20 -
21 -
22 - $container.wrap('<div class="twentytwenty-wrapper twentytwenty-' + sliderOrientation + '"></div>');
23 - $container.append('<div class="twentytwenty-overlay"></div>');
24 - $container.append('<div class="twentytwenty-handle"></div>');
25 -
26 - var $slider = $container.find('.twentytwenty-handle');
11 + return this.each( function() {
12 + var sliderPct = options.handlePosition,
13 + $container = $( this ),
14 + sliderOrientation = options.orientation,
15 + beforeDirection = ( 'vertical' === sliderOrientation ) ? 'down' : 'left',
16 + afterDirection = ( 'vertical' === sliderOrientation ) ? 'up' : 'right',
17 + $beforeImg = $container.find( 'img:first' ),
18 + $afterImg = $container.find( 'img:last' ),
19 + offsetX = 0,
20 + offsetY = 0,
21 + imgWidth = 0,
22 + imgHeight = 0,
23 + $slider, $overlay,
24 + calcOffset = function( dimensionPct ) {
25 + var width = parseInt( $beforeImg.width(), 10 ),
26 + height = parseInt( $beforeImg.height(), 10 );
27 27
28 - $slider.append('<span class="twentytwenty-' + beforeDirection + '-arrow"></span>');
29 - $slider.append('<span class="twentytwenty-' + afterDirection + '-arrow"></span>');
30 - $container.addClass('twentytwenty-container');
31 - $beforeImg.addClass('twentytwenty-before');
32 - $afterImg.addClass('twentytwenty-after');
33 -
34 - var $overlay = $container.find('.twentytwenty-overlay');
28 + if ( ! width || ! height ) {
29 + width = parseInt( $beforeImg.attr( 'width' ), 10 );
30 + height = parseInt( $beforeImg.attr( 'height' ), 10 );
31 + }
35 32
36 - $overlay.append('<div class="twentytwenty-labels twentytwenty-before-label"><span class="twentytwenty-label-content">' + options.labelBefore + '</span></div>');
37 - $overlay.append('<div class="twentytwenty-labels twentytwenty-after-label"><span class="twentytwenty-label-content">' + options.labelAfter + '</span></div>');
38 -
39 -
40 - // some usefull function and vars declarations
41 -
42 - var calcOffset = function(dimensionPct) {
43 - var w = $beforeImg.width();
44 - var h = $beforeImg.height();
45 33 return {
46 - w: w+"px",
47 - h: h+"px",
48 - cw: (dimensionPct*w)+"px",
49 - ch: (dimensionPct*h)+"px"
34 + w: width + "px",
35 + h: height + "px",
36 + cw: ( dimensionPct * width ) + "px",
37 + ch: ( dimensionPct * height ) + "px"
50 38 };
51 39 },
52 -
53 40 adjustContainer = function( offset ) {
54 - // make it dynamic, in case "before" image change
55 - var $beforeImg = $container.find('.twentytwenty-before');
41 + // Make it dynamic, in case the "before" image changes.
42 + var $beforeImage = $container.find( '.twentytwenty-before' );
56 43
57 - if ( sliderOrientation === 'vertical' ) {
58 - $beforeImg.css( 'clip', 'rect(0,' + offset.w + ',' + offset.ch + ',0)' );
44 + if ( 'vertical' === sliderOrientation ) {
45 + $beforeImage.css( 'clip', 'rect(0,' + offset.w + ',' + offset.ch + ',0)' );
46 + } else {
47 + $beforeImage.css( 'clip', 'rect(0,' + offset.cw + ',' + offset.h + ',0)' );
59 48 }
60 - else {
61 - $beforeImg.css( 'clip', 'rect(0,' + offset.cw + ',' + offset.h + ',0)' );
62 - }
49 +
63 50 $container.css( 'height', offset.h );
64 51
65 52 if ( typeof callback === 'function' ) {
66 53 callback();
@@ -66,571 +53,656 @@
66 53 callback();
67 54 }
68 55 },
69 56 adjustSlider = function( pct ) {
70 - var offset = calcOffset(pct);
71 - $slider.css( ( sliderOrientation === 'vertical' ) ? 'top' : 'left', ( sliderOrientation === 'vertical' ) ? offset.ch : offset.cw );
57 + var offset = calcOffset( pct );
58 +
59 + if ( 'vertical' === sliderOrientation ) {
60 + $slider.css( 'top', offset.ch );
61 + } else {
62 + $slider.css( 'left', offset.cw );
63 + }
64 +
72 65 adjustContainer( offset );
73 - },
74 - offsetX = 0,
75 - offsetY = 0,
76 - imgWidth = 0,
77 - imgHeight = 0;
66 + };
78 67
79 - $(window).on('resize.twentytwenty', function(e) {
68 +
69 + if ( $container.parent( '.twentytwenty-wrapper' ).length ) {
70 + $container.unwrap();
71 + }
72 + $container.wrap( '<div class="twentytwenty-wrapper twentytwenty-' + sliderOrientation + '"></div>' );
73 +
74 + $container.children( '.twentytwenty-overlay, .twentytwenty-handle' ).remove();
75 + $container.append( '<div class="twentytwenty-overlay"></div>' );
76 + $container.append( '<div class="twentytwenty-handle"></div>' );
77 +
78 + $slider = $container.find( '.twentytwenty-handle' );
79 +
80 + $slider.append( '<span class="twentytwenty-' + beforeDirection + '-arrow"></span>' );
81 + $slider.append( '<span class="twentytwenty-' + afterDirection + '-arrow"></span>' );
82 + $container.addClass( 'twentytwenty-container' );
83 + $beforeImg.addClass( 'twentytwenty-before' );
84 + $afterImg.addClass( 'twentytwenty-after' );
85 +
86 + $overlay = $container.find( '.twentytwenty-overlay' );
87 +
88 + $overlay.append( '<div class="twentytwenty-labels twentytwenty-before-label"><span class="twentytwenty-label-content">' + options.labelBefore + '</span></div>' );
89 + $overlay.append( '<div class="twentytwenty-labels twentytwenty-after-label"><span class="twentytwenty-label-content">' + options.labelAfter + '</span></div>' );
90 +
91 + $( w ).on( 'resize.twentytwenty', function() {
80 92 adjustSlider( sliderPct );
81 - });
82 -
83 - $slider.on('movestart', function(e) {
84 - if ( ( ( e.distX > e.distY && e.distX < -e.distY ) || ( e.distX < e.distY && e.distX > -e.distY ) ) && sliderOrientation !== 'vertical' ) {
93 + } );
94 +
95 + $slider.on( 'movestart', function( e ) {
96 + if ( 'vertical' !== sliderOrientation && ( ( e.distX > e.distY && e.distX < -e.distY ) || ( e.distX < e.distY && e.distX > -e.distY ) ) ) {
85 97 e.preventDefault();
86 - }
87 - else if ( ( ( e.distX < e.distY && e.distX < -e.distY ) || ( e.distX > e.distY && e.distX > -e.distY ) ) && sliderOrientation === 'vertical' ) {
98 + } else if ( 'vertical' === sliderOrientation && ( ( e.distX < e.distY && e.distX < -e.distY ) || ( e.distX > e.distY && e.distX > -e.distY ) ) ) {
88 99 e.preventDefault();
89 100 }
90 - $container.addClass('active');
91 - offsetX = $container.offset().left;
92 - offsetY = $container.offset().top;
93 - imgWidth = $beforeImg.width();
94 - imgHeight = $beforeImg.height();
95 - });
96 101
97 - $slider.on('moveend', function(e) {
98 - $container.removeClass('active');
99 - });
102 + $container.addClass( 'active' );
100 103
101 - $slider.on('move', function(e) {
104 + offsetX = $container.offset().left;
105 + offsetY = $container.offset().top;
106 + imgWidth = $beforeImg.width();
107 + imgHeight = $beforeImg.height();
108 + } );
109 +
110 + $slider.on( 'moveend', function() {
111 + $container.removeClass( 'active' );
112 + } );
113 +
114 + $slider.on( 'move', function( e ) {
102 115 if ( $container.hasClass('active') ) {
116 + sliderPct = 'vertical' === sliderOrientation ? ( e.pageY - offsetY ) / imgHeight : ( e.pageX - offsetX ) / imgWidth;
103 117
104 - sliderPct = ( sliderOrientation === 'vertical' ) ? ( e.pageY-offsetY )/imgHeight : ( e.pageX-offsetX )/imgWidth;
105 -
106 118 if ( sliderPct < 0 ) {
107 119 sliderPct = 0;
108 - }
109 - if ( sliderPct > 1 ) {
120 + } else if ( sliderPct > 1 ) {
110 121 sliderPct = 1;
111 122 }
123 +
112 124 adjustSlider( sliderPct );
113 125 }
114 - });
126 + } );
115 127
116 - $container.find('img').on('mousedown', function(event) {
117 - event.preventDefault();
118 - });
128 + $container.find( 'img' ).on( 'mousedown', function( e ) {
129 + e.preventDefault();
130 + } );
119 131
120 - $(window).trigger('resize.twentytwenty');
121 - });
132 + $( w ).trigger( 'resize.twentytwenty' );
133 + } );
122 134 };
123 135
124 -})(window, jQuery);
136 +} )(jQuery, document, window);
125 137
126 138 /**
127 139 * Twentytwenty Imagify Init
128 140 */
129 -(function($, window, document, undefined){
141 +(function($, d, w, undefined) { // eslint-disable-line no-unused-vars, no-shadow, no-shadow-restricted-names
130 142
131 143 /*
132 144 * Mini chart
133 145 *
134 146 * @param {element} canvas
135 - */
136 - var draw_me_a_chart = function ( canvas ) {
137 - canvas.each(function(){
138 - var the_value = parseInt( $(this).closest('.imagify-chart').next('.imagify-chart-value').text() ),
139 - overviewData = [
140 - {
141 - value: the_value,
142 - color: '#00B3D3'
143 - },
144 - {
145 - value: 100 - the_value,
146 - color: '#D8D8D8'
147 - }
148 - ],
149 - overviewDoughnut = new Chart( $(this)[0].getContext('2d')).Doughnut(overviewData, {
150 - segmentStrokeColor : '#2A2E3C',
151 - segmentStrokeWidth : 1,
152 - animateRotate : true,
153 - percentageInnerCutout: 60,
154 - tooltipEvents : []
155 - });
156 - });
157 - };
158 -
159 - /**
160 - * Dynamic modal
161 - *
162 - * @param {object} Parameters to build modal with datas
163 147 */
164 - var imagify_open_modal = function( $the_link ){
148 + var drawMeAChart = function ( canvas ) {
149 + canvas.each( function() {
150 + var value = parseInt( $( this ).closest( '.imagify-chart' ).next( '.imagify-chart-value' ).text(), 10 );
165 151
166 - var the_target = $the_link.data('target') || $the_link.attr('href');
167 -
168 - $( the_target ).css('display', 'flex').hide().fadeIn(400).attr('aria-hidden', 'false').attr('tabindex', '0').focus().removeAttr('tabindex').addClass('modal-is-open');
169 - $('body').addClass('imagify-modal-is-open');
170 -
152 + new w.imagify.Chart( this, { // eslint-disable-line no-new
153 + type: 'doughnut',
154 + data: {
155 + datasets: [{
156 + data: [ value, 100 - value ],
157 + backgroundColor: [ '#00B3D3', '#D8D8D8' ],
158 + borderColor: '#2A2E3C',
159 + borderWidth: 1
160 + }]
161 + },
162 + options: {
163 + legend: {
164 + display: false
165 + },
166 + events: [],
167 + animation: {
168 + easing: 'easeOutBounce'
169 + },
170 + tooltips: {
171 + enabled: false
172 + },
173 + responsive: false,
174 + cutoutPercentage: 60
175 + }
176 + } );
177 + } );
171 178 },
172 - imagify_twenty_modal = function( options ) {
173 -
174 - var defaults = {
175 - width: 0, //px
176 - height: 0, //px
177 - original_url: '', //url
178 - optimized_url: '', //url
179 - original_size: 0, //mb
180 - optimized_size: 0, // mb
181 - saving: 0, //percent
182 - modal_append_to: $('body'), // jQuery element
183 - trigger: $('[data-target="imagify-visual-comparison"]'), // jQuery element (button, link) with data-target="modal_id"
184 - modal_id: 'imagify-visual-comparison', // should be dynamic if multiple modals
185 - open_modal: false
186 - },
187 - settings = $.extend({}, defaults, options);
179 + /**
180 + * Dynamic modal
181 + *
182 + * @param {object} Parameters to build modal with datas
183 + */
184 + imagifyTwentyModal = function( options ) {
185 + var defaults = {
186 + width: 0, //px
187 + height: 0, //px
188 + originalUrl: '', //url
189 + optimizedUrl: '', //url
190 + originalSize: 0, //mb
191 + optimizedSize: 0, // mb
192 + saving: 0, //percent
193 + modalAppendTo: $( 'body' ), // jQuery element
194 + trigger: $( '[data-target="imagify-visual-comparison"]' ), // jQuery element (button, link) with data-target="modalId"
195 + modalId: 'imagify-visual-comparison', // should be dynamic if multiple modals
196 + openModal: false
197 + },
198 + settings = $.extend( {}, defaults, options ),
199 + modalHtml;
188 200
189 - if ( settings.width === 0 || settings.height === 0 || settings.original_url === ''|| settings.optimized_url === '' || settings.original_size === 0 || settings.optimized_size === 0 || settings.saving === 0 ) {
190 - return 'error';
191 - }
201 + if ( 0 === settings.width || 0 === settings.height || '' === settings.originalUrl || '' === settings.optimizedUrl || 0 === settings.originalSize || 0 === settings.optimizedSize || 0 === settings.saving ) {
202 + return 'error';
203 + }
192 204
193 - // create modal box
194 - settings.modal_append_to.append(''
195 - + '<div id="' + settings.modal_id + '" class="imagify-modal imagify-visual-comparison" aria-hidden="true">'
196 - + '<div class="imagify-modal-content loading">'
197 - + '<div class="twentytwenty-container">'
198 - + '<img class="imagify-img-before" alt="" width="' + settings.width + '" height="' + settings.height + '">'
199 - + '<img class="imagify-img-after" alt="" width="' + settings.width + '" height="' + settings.height + '">'
200 - + '</div>'
201 - + '<div class="imagify-comparison-levels">'
202 - + '<div class="imagify-c-level imagify-level-original go-left">'
203 - + '<p class="imagify-c-level-row">'
204 - + '<span class="label">' + imagifyTTT.labels.filesize + '</span>'
205 - + '<span class="value level">' + settings.original_size + '</span>'
206 - + '</p>'
207 - + '</div>'
208 - + '<div class="imagify-c-level imagify-level-optimized go-right">'
209 - + '<p class="imagify-c-level-row">'
210 - + '<span class="label">' + imagifyTTT.labels.filesize + '</span>'
211 - + '<span class="value level">' + settings.optimized_size + '</span>'
212 - + '</p>'
213 - + '<p class="imagify-c-level-row">'
214 - + '<span class="label">' + imagifyTTT.labels.saving + '</span>'
215 - + '<span class="value"><span class="imagify-chart"><span class="imagify-chart-container"><canvas id="imagify-consumption-chart-normal" width="15" height="15"></canvas></span></span><span class="imagify-chart-value">' + settings.saving + '</span>%</span>'
216 - + '</p>'
217 - +'</div>'
218 - + '</div>'
219 - + '<button class="close-btn absolute" type="button"><i aria-hidden="true" class="dashicons dashicons-no-alt"></i><span class="screen-reader-text">' + imagifyTTT.labels.close + '</span></button>'
220 - + '</div>'
221 - + '</div>'
222 - );
205 + // create modal box
206 + modalHtml = '<div id="' + settings.modalId + '" class="imagify-modal imagify-visual-comparison" aria-hidden="true">';
207 + /* eslint-disable indent */
208 + modalHtml += '<div class="imagify-modal-content loading">';
209 + modalHtml += '<div class="twentytwenty-container">';
210 + modalHtml += '<img class="imagify-img-before" alt="" width="' + settings.width + '" height="' + settings.height + '">';
211 + modalHtml += '<img class="imagify-img-after" alt="" width="' + settings.width + '" height="' + settings.height + '">';
212 + modalHtml += '</div>';
213 + modalHtml += '<div class="imagify-comparison-levels">';
214 + modalHtml += '<div class="imagify-c-level imagify-level-original go-left imagify-original-fize-size">';
215 + modalHtml += '<p class="imagify-c-level-row">';
216 + modalHtml += '<span class="label">' + imagifyTTT.labels.filesize + '</span>';
217 + modalHtml += '<span class="value level">' + settings.originalSize + '</span>';
218 + modalHtml += '</p>';
219 + modalHtml += '</div>';
220 + modalHtml += '<div class="imagify-c-level imagify-level-optimized go-right">';
221 + modalHtml += '<p class="imagify-c-level-row">';
222 + modalHtml += '<span class="label">' + imagifyTTT.labels.filesize + '</span>';
223 + modalHtml += '<span class="value level">' + settings.optimizedSize + '</span>';
224 + modalHtml += '</p>';
225 + modalHtml += '<p class="imagify-c-level-row">';
226 + modalHtml += '<span class="label">' + imagifyTTT.labels.saving + '</span>';
227 + modalHtml += '<span class="value"><span class="imagify-chart"><span class="imagify-chart-container"><canvas id="imagify-consumption-chart-normal" width="15" height="15"></canvas></span></span><span class="imagify-chart-value">' + settings.saving + '</span>%</span>';
228 + modalHtml += '</p>';
229 + modalHtml += '</div>';
230 + modalHtml += '</div>';
231 + modalHtml += '<button class="close-btn absolute" type="button"><i aria-hidden="true" class="dashicons dashicons-no-alt"></i><span class="screen-reader-text">' + imagifyTTT.labels.close + '</span></button>';
232 + modalHtml += '</div>';
233 + /* eslint-enable indent */
234 + modalHtml += '</div>';
223 235
224 - settings.trigger.on('click.imagify', function(){
236 + settings.modalAppendTo.append( modalHtml );
225 237
226 - var $modal = $( $(this).data('target') ),
227 - imgs_loaded = 0;
238 + settings.trigger.on( 'click.imagify', function( e ) {
239 + var $modal = $( $( this ).data( 'target' ) ),
240 + imgsLoaded = 0,
241 + $tt, checkLoad;
228 242
229 - if ( typeof imagify_open_modal === 'function' && settings.open_modal ) {
230 - imagify_open_modal( $(this) );
231 - }
243 + e.preventDefault();
232 244
233 - $modal.find('.imagify-modal-content').css({
234 - 'width' : ($(window).outerWidth()*0.85) + 'px',
235 - 'max-width' : settings.width
236 - });
245 + if ( settings.openModal ) {
246 + w.imagify.openModal( $( this ) );
247 + }
237 248
238 - // load before img
239 - $modal.find('.imagify-img-before').on('load', function(){
240 - imgs_loaded++;
241 - }).attr('src', settings.original_url);
249 + $modal.find( '.imagify-modal-content' ).css( {
250 + 'width': ( $( w ).outerWidth() * 0.85 ) + 'px',
251 + 'max-width': settings.width
252 + } );
242 253
243 - // load after img
244 - $modal.find('.imagify-img-after').on('load', function(){
245 - imgs_loaded++;
246 - }).attr('src', settings.optimized_url);
254 + // Load before img.
255 + $modal.find( '.imagify-img-before' ).on( 'load', function() {
256 + imgsLoaded++;
257 + } ).attr( 'src', settings.originalUrl );
247 258
248 - var $tt = $modal.find('.twentytwenty-container'),
249 - check_load = setInterval( function(){
259 + // Load after img.
260 + $modal.find( '.imagify-img-after' ).on( 'load', function() {
261 + imgsLoaded++;
262 + } ).attr( 'src', settings.optimizedUrl + ( settings.optimizedUrl.indexOf( '?' ) > 0 ? '&' : '?' ) + 'v=' + Date.now() );
250 263
251 - if ( imgs_loaded === 2 ) {
252 - $tt.twentytwenty({
253 - handlePosition: 0.3,
254 - orientation: 'horizontal',
255 - labelBefore: imagifyTTT.labels.original_l,
256 - labelAfter: imagifyTTT.labels.optimized_l
257 - }, function(){
264 + $tt = $modal.find( '.twentytwenty-container' );
265 + checkLoad = setInterval( function() {
266 + if ( 2 !== imgsLoaded ) {
267 + return;
268 + }
258 269
259 - var windowH = $(window).height(),
260 - ttH = $modal.find('.twentytwenty-container').height(),
261 - ttTop = $modal.find('.twentytwenty-wrapper').position().top;
270 + $tt.twentytwenty( {
271 + handlePosition: 0.3,
272 + orientation: 'horizontal',
273 + labelBefore: imagifyTTT.labels.originalL,
274 + labelAfter: imagifyTTT.labels.optimizedL
275 + }, function() {
276 + var windowH = $( w ).height(),
277 + ttH = $modal.find( '.twentytwenty-container' ).height(),
278 + ttTop = $modal.find( '.twentytwenty-wrapper' ).position().top,
279 + $handle, $labels, $datas, datasH, handlePos, labelsPos;
262 280
263 - if ( ! $tt.closest('.imagify-modal-content').hasClass('loaded') ) {
264 - $tt.closest('.imagify-modal-content').removeClass('loading').addClass('loaded');
265 - draw_me_a_chart( $modal.find('.imagify-level-optimized').find('.imagify-chart').find('canvas') );
266 - }
281 + if ( ! $tt.closest( '.imagify-modal-content' ).hasClass( 'loaded' ) ) {
282 + $tt.closest( '.imagify-modal-content' ).removeClass( 'loading' ).addClass( 'loaded' );
283 + drawMeAChart( $modal.find( '.imagify-level-optimized .imagify-chart canvas' ) );
284 + }
267 285
268 - // check if image height is to big
269 - if ( windowH < ttH && ! $modal.hasClass('modal-is-too-high') ) {
270 - $modal.addClass('modal-is-too-high');
286 + // Check if image height is to big.
287 + if ( windowH < ttH && ! $modal.hasClass( 'modal-is-too-high' ) ) {
288 + $modal.addClass( 'modal-is-too-high' );
271 289
272 - var $handle = $modal.find('.twentytwenty-handle'),
273 - $labels = $modal.find('.twentytwenty-label-content'),
274 - $datas = $modal.find('.imagify-comparison-levels'),
275 - datasH = $datas.outerHeight(),
276 - handle_pos = ( windowH - ttTop - $handle.height() ) / 2,
277 - labels_pos = ( windowH - ttTop * 3 - datasH );
290 + $handle = $modal.find( '.twentytwenty-handle' );
291 + $labels = $modal.find( '.twentytwenty-label-content' );
292 + $datas = $modal.find( '.imagify-comparison-levels' );
293 + datasH = $datas.outerHeight();
294 + handlePos = ( windowH - ttTop - $handle.height() ) / 2;
295 + labelsPos = ( windowH - ttTop * 3 - datasH );
278 296
279 - $handle.css({
280 - top: handle_pos
281 - });
282 - $labels.css({
283 - top: labels_pos,
284 - bottom: 'auto'
285 - });
286 - $modal.find('.twentytwenty-wrapper').css({
287 - paddingBottom: datasH
288 - });
297 + $handle.css( {
298 + top: handlePos
299 + } );
300 + $labels.css( {
301 + top: labelsPos,
302 + bottom: 'auto'
303 + } );
304 + $modal.find( '.twentytwenty-wrapper' ).css( {
305 + paddingBottom: datasH
306 + } );
307 + $modal.find( '.imagify-modal-content' ).on( 'scroll.imagify', function() {
308 + var scrollTop = $( this ).scrollTop();
289 309
290 - $modal.find('.imagify-modal-content').on('scroll.imagify', function(){
291 - $handle.css({
292 - top: handle_pos + $(this).scrollTop()
293 - });
294 - $labels.css({
295 - top: labels_pos + $(this).scrollTop()
296 - });
297 - $datas.css({
298 - bottom: - ( $(this).scrollTop() )
299 - });
300 - });
301 - }
310 + $handle.css( {
311 + top: handlePos + scrollTop
312 + } );
313 + $labels.css( {
314 + top: labelsPos + scrollTop
315 + } );
316 + $datas.css( {
317 + bottom: -scrollTop
318 + } );
319 + } );
320 + }
321 + } );
302 322
303 - });
304 - clearInterval( check_load );
305 - check_load = null;
306 - return 'done';
307 - }
323 + clearInterval( checkLoad );
324 + checkLoad = null;
325 + return 'done';
308 326 }, 75 );
327 + } );
328 + }; // imagifyTwentyModal( options );
309 329
310 - return false;
311 - });
312 - }; // imagify_twenty_modal( options );
313 330
314 -
315 331 /**
316 332 * The complexe visual comparison
317 333 */
318 - $('.imagify-visual-comparison-btn').on('click', function(){
334 + $( '.imagify-visual-comparison-btn' ).on( 'click', function() {
335 + var $tt, imgsLoaded, loader,
336 + labelOriginal, labelNormal, labelAggressive, labelUltra,
337 + originalLabel, originalAlt, originalSrc, originalDim,
338 + normalAlt, normalSrc, normalDim,
339 + aggressiveAlt, aggressiveSrc, aggressiveDim,
340 + ultraLabel, ultraAlt, ultraSrc, ultraDim,
341 + ttBeforeButtons, ttAfterButtons, image50, twentyMe;
319 342
320 - if ( $('.twentytwenty-wrapper').length === 1) {
343 + if ( $( '.twentytwenty-wrapper' ).length === 1 ) {
321 344 return;
322 345 }
323 -
324 - $( $(this).data('target') ).find('.imagify-modal-content').css('width', ($(window).outerWidth()*0.95) + 'px');
325 346
326 - if ( $('.twentytwenty-container').length > 0 && $(window).outerWidth() > 800 ) {
347 + $( $( this ).data( 'target' ) ).find( '.imagify-modal-content' ).css( 'width', ( $( w ).outerWidth() * 0.95 ) + 'px' );
327 348
328 - var $tt = $('.twentytwenty-container'),
329 - imgs_loaded = 0,
330 - loader = $tt.data('loader'),
331 - label_original = $tt.data('label-original'),
332 - label_normal = $tt.data('label-normal'),
333 - label_aggressive = $tt.data('label-aggressive'),
334 - label_ultra = $tt.data('label-ultra'),
349 + if ( $( '.twentytwenty-container' ).length > 0 && $( w ).outerWidth() <= 800 ) {
350 + return;
351 + }
335 352
336 - original_label = $tt.data('original-label').replace(/\*\*/, '<strong>').replace(/\*\*/, '</strong>'),
337 - original_alt = $tt.data('original-alt'),
338 - original_src = $tt.data('original-img'),
339 - original_dim = $tt.data('original-dim').split('x'),
353 + $tt = $( '.twentytwenty-container' );
354 + imgsLoaded = 0;
355 + loader = $tt.data( 'loader' );
356 + labelOriginal = $tt.data( 'label-original' );
357 + labelNormal = $tt.data( 'label-normal' );
358 + labelAggressive = $tt.data( 'label-aggressive' );
359 + labelUltra = $tt.data( 'label-ultra' );
340 360
341 - normal_label = $tt.data('normal-label').replace(/\*\*/, '<strong>').replace(/\*\*/, '</strong>'),
342 - normal_alt = $tt.data('normal-alt'),
343 - normal_src = $tt.data('normal-img'),
344 - normal_dim = $tt.data('normal-dim').split('x'),
361 + originalLabel = $tt.data( 'original-label' ).replace( /\*\*/, '<strong>' ).replace( /\*\*/, '</strong>' );
362 + originalAlt = $tt.data( 'original-alt' );
363 + originalSrc = $tt.data( 'original-img' );
364 + originalDim = $tt.data( 'original-dim' ).split( 'x' );
345 365
346 - aggressive_label = $tt.data('aggressive-label').replace(/\*\*/, '<strong>').replace(/\*\*/, '</strong>'),
347 - aggressive_alt = $tt.data('aggressive-alt'),
348 - aggressive_src = $tt.data('aggressive-img'),
349 - aggressive_dim = $tt.data('aggressive-dim').split('x'),
366 + normalAlt = $tt.data( 'normal-alt' );
367 + normalSrc = $tt.data( 'normal-img' );
368 + normalDim = $tt.data( 'normal-dim' ).split( 'x' );
350 369
351 - ultra_label = $tt.data('ultra-label').replace(/\*\*/, '<strong>').replace(/\*\*/, '</strong>'),
352 - ultra_alt = $tt.data('ultra-alt'),
353 - ultra_src = $tt.data('ultra-img'),
354 - ultra_dim = $tt.data('ultra-dim').split('x'),
370 + aggressiveAlt = $tt.data( 'aggressive-alt' );
371 + aggressiveSrc = $tt.data( 'aggressive-img' );
372 + aggressiveDim = $tt.data( 'aggressive-dim' ).split( 'x' );
355 373
356 - tt_before_buttons = '<span class="twentytwenty-duo-buttons twentytwenty-duo-left">' +
357 - '<button type="button" class="imagify-comparison-original selected" data-img="original">' + label_original + '</button>' +
358 - '<button type="button" class="imagify-comparison-normal" data-img="normal">' + label_normal + '</button>' +
359 - '<button type="button" class="imagify-comparison-aggressive" data-img="aggressive">' + label_aggressive + '</button>' +
360 - '</span>',
361 - tt_after_buttons = '<span class="twentytwenty-duo-buttons twentytwenty-duo-right">' +
362 - '<button type="button" class="imagify-comparison-normal" data-img="normal">' + label_normal + '</button>' +
363 - '<button type="button" class="imagify-comparison-aggressive" data-img="aggressive">' + label_aggressive + '</button>' +
364 - '<button type="button" class="imagify-comparison-ultra selected" data-img="ultra">' + label_ultra + '</button>' +
365 - '</span>';
374 + ultraLabel = $tt.data( 'ultra-label' ).replace( /\*\*/, '<strong>' ).replace( /\*\*/, '</strong>' );
375 + ultraAlt = $tt.data( 'ultra-alt' );
376 + ultraSrc = $tt.data( 'ultra-img' );
377 + ultraDim = $tt.data( 'ultra-dim' ).split( 'x' );
366 378
367 - // loader
368 - $tt.before('<img class="loader" src="' + loader + '" alt="Loading…" width="64" height="64">')
379 + ttBeforeButtons = '<span class="twentytwenty-duo-buttons twentytwenty-duo-left">';
380 + /* eslint-disable indent */
381 + ttBeforeButtons += '<button type="button" class="imagify-comparison-original selected" data-img="original">' + labelOriginal + '</button>';
382 + ttBeforeButtons += '<button type="button" class="imagify-comparison-normal" data-img="normal">' + labelNormal + '</button>';
383 + ttBeforeButtons += '<button type="button" class="imagify-comparison-aggressive" data-img="aggressive">' + labelAggressive + '</button>';
384 + /* eslint-enable indent */
385 + ttBeforeButtons += '</span>';
386 + ttAfterButtons = '<span class="twentytwenty-duo-buttons twentytwenty-duo-right">';
387 + /* eslint-disable indent */
388 + ttAfterButtons += '<button type="button" class="imagify-comparison-normal" data-img="normal">' + labelNormal + '</button>';
389 + ttAfterButtons += '<button type="button" class="imagify-comparison-aggressive" data-img="aggressive">' + labelAggressive + '</button>';
390 + ttAfterButtons += '<button type="button" class="imagify-comparison-ultra selected" data-img="ultra">' + labelUltra + '</button>';
391 + /* eslint-enable indent */
392 + ttAfterButtons += '</span>';
369 393
370 - // add switchers button only if needed
371 - // should be more locally integrate...
372 - var duo_buttons = ( $('.twentytwenty-left-buttons').lenght ) ? tt_before_buttons + tt_after_buttons : '';
373 - // should be more locally integrate...
374 - $('.twentytwenty-left-buttons').append(tt_before_buttons);
375 - $('.twentytwenty-right-buttons').append(tt_after_buttons);
394 + // Loader.
395 + $tt.before( '<img class="loader" src="' + loader + '" alt="Loading…" width="64" height="64">' );
376 396
377 - // add images to 50/50 area
378 - $tt.closest('.imagify-modal-content').addClass('loading').find('.twentytwenty-container').append(
379 - '<img class="img-original" alt="' + original_alt + '" width="' + original_dim[0] + '" height="' + original_dim[1] + '">' +
380 - '<img class="img-normal" alt="' + normal_alt + '" width="' + normal_dim[0] + '" height="' + normal_dim[1] + '">' +
381 - '<img class="img-aggressive" alt="' + aggressive_alt + '" width="' + aggressive_dim[0] + '" height="' + aggressive_dim[1] + '">' +
382 - '<img class="img-ultra" alt="' + ultra_alt + '" width="' + ultra_dim[0] + '" height="' + ultra_dim[1] + '">' +
383 - duo_buttons
384 - );
397 + // Should be more locally integrated...
398 + $( '.twentytwenty-left-buttons' ).append( ttBeforeButtons );
399 + $( '.twentytwenty-right-buttons' ).append( ttAfterButtons );
385 400
386 - // load image original
387 - $('.img-original').on('load', function(){
388 - imgs_loaded++;
389 - }).attr('src', original_src);
401 + image50 = '<img class="img-original" alt="' + originalAlt + '" width="' + originalDim[0] + '" height="' + originalDim[1] + '">';
402 + image50 += '<img class="img-normal" alt="' + normalAlt + '" width="' + normalDim[0] + '" height="' + normalDim[1] + '">';
403 + image50 += '<img class="img-aggressive" alt="' + aggressiveAlt + '" width="' + aggressiveDim[0] + '" height="' + aggressiveDim[1] + '">';
404 + image50 += '<img class="img-ultra" alt="' + ultraAlt + '" width="' + ultraDim[0] + '" height="' + ultraDim[1] + '">';
405 + // Add switchers button only if needed.
406 + // Should be more locally integrated...
407 + image50 += $( '.twentytwenty-left-buttons' ).lenght ? ttBeforeButtons + ttAfterButtons : '';
390 408
391 - // load image normal
392 - $('.img-normal').on('load', function(){
393 - imgs_loaded++;
394 - }).attr('src', normal_src);
409 + // Add images to 50/50 area.
410 + $tt.closest( '.imagify-modal-content' ).addClass( 'loading' ).find( '.twentytwenty-container' ).append( image50 );
395 411
396 - // load image aggressive
397 - $('.img-aggressive').on('load', function(){
398 - imgs_loaded++;
399 - }).attr('src', aggressive_src);
412 + // Load image original.
413 + $( '.img-original' ).on( 'load', function() {
414 + imgsLoaded++;
415 + } ).attr( 'src', originalSrc );
400 416
401 - // load image ultra
402 - $('.img-ultra').on('load', function(){
403 - imgs_loaded++;
404 - }).attr('src', ultra_src);
417 + // Load image normal.
418 + $( '.img-normal' ).on( 'load', function() {
419 + imgsLoaded++;
420 + } ).attr( 'src', normalSrc );
405 421
406 - var twenty_me = setInterval(function(){
407 - if ( imgs_loaded === 4 ) {
408 - $tt.twentytwenty({
409 - handlePosition: 0.6,
410 - orientation: 'horizontal',
411 - labelBefore: original_label,
412 - labelAfter: ultra_label
413 - }, function(){
414 - // fires on initialisation & each time the handle is moving
415 - if ( ! $tt.closest('.imagify-modal-content').hasClass('loaded') ) {
416 - $tt.closest('.imagify-modal-content').removeClass('loading').addClass('loaded');
417 - draw_me_a_chart( $('.imagify-level-ultra').find('.imagify-chart').find('canvas') );
418 - }
419 - });
420 - clearInterval( twenty_me );
421 - twenty_me = null;
422 - }
423 - }, 75);
422 + // Load image aggressive.
423 + $( '.img-aggressive' ).on( 'load', function() {
424 + imgsLoaded++;
425 + } ).attr( 'src', aggressiveSrc );
424 426
425 - // on click on button choices
426 -
427 - $('.imagify-comparison-title').on('click', '.twentytwenty-duo-buttons button:not(.selected)', function(e){
427 + // Load image ultra.
428 + $( '.img-ultra' ).on( 'load', function() {
429 + imgsLoaded++;
430 + } ).attr( 'src', ultraSrc );
428 431
429 - e.stopPropagation();
432 + twentyMe = setInterval( function() {
433 + if ( 4 !== imgsLoaded ) {
434 + return;
435 + }
430 436
431 - var $_this = $(this),
432 - $container = $_this.closest('.imagify-comparison-title').nextAll('.twentytwenty-wrapper').find('.twentytwenty-container'),
433 - side = $_this.closest('.twentytwenty-duo-buttons').hasClass('twentytwenty-duo-left') ? 'left' : 'right',
434 - $other_side = side === 'left' ? $_this.closest('.imagify-comparison-title').find('.twentytwenty-duo-right') : $_this.closest('.imagify-comparison-title').find('.twentytwenty-duo-left'),
435 - $duo = $_this.closest('.twentytwenty-duo-buttons').find('button'),
436 - $img_before = $container.find('.twentytwenty-before'),
437 - $img_after = $container.find('.twentytwenty-after'),
438 - image = $_this.data('img');
437 + $tt.twentytwenty({
438 + handlePosition: 0.6,
439 + orientation: 'horizontal',
440 + labelBefore: originalLabel,
441 + labelAfter: ultraLabel
442 + }, function() {
443 + // Fires on initialisation & each time the handle is moving.
444 + if ( ! $tt.closest( '.imagify-modal-content' ).hasClass( 'loaded' ) ) {
445 + $tt.closest( '.imagify-modal-content' ).removeClass( 'loading' ).addClass( 'loaded' );
446 + drawMeAChart( $( '.imagify-level-ultra .imagify-chart canvas' ) );
447 + }
448 + } );
439 449
440 - // button coloration
441 - $duo.removeClass('selected');
442 - $_this.addClass('selected');
450 + clearInterval( twentyMe );
451 + twentyMe = null;
452 + }, 75);
443 453
444 - // other side action (to not compare same images)
445 - if ( $other_side.find('.selected').data('img') === image ) {
446 - $other_side.find('button:not(.selected)').eq(0).trigger('click');
447 - }
454 + // On click on button choices.
455 + $( '.imagify-comparison-title' ).on( 'click', '.twentytwenty-duo-buttons button:not(.selected)', function( e ) {
456 + var $this = $( this ),
457 + $container = $this.closest( '.imagify-comparison-title' ).nextAll( '.twentytwenty-wrapper' ).find( '.twentytwenty-container' ),
458 + side = $this.closest( '.twentytwenty-duo-buttons' ).hasClass( 'twentytwenty-duo-left' ) ? 'left' : 'right',
459 + $otherSide = 'left' === side ? $this.closest( '.imagify-comparison-title' ).find( '.twentytwenty-duo-right' ) : $this.closest( '.imagify-comparison-title' ).find( '.twentytwenty-duo-left' ),
460 + $duo = $this.closest( '.twentytwenty-duo-buttons' ).find( 'button' ),
461 + $imgBefore = $container.find( '.twentytwenty-before' ),
462 + $imgAfter = $container.find( '.twentytwenty-after' ),
463 + image = $this.data( 'img' ),
464 + clipStyles;
448 465
449 - // left buttons
450 - if ( side === 'left' ) {
451 - var clip_styles = $img_before.css('clip');
452 - $img_before.attr('style', '');
453 - $img_before.removeClass('twentytwenty-before');
454 - $container.find( '.img-' + image ).addClass('twentytwenty-before').css('clip', clip_styles);
455 - $('.twentytwenty-before-label').find('.twentytwenty-label-content').text( $container.data( image + '-label' ) );
456 - $('.imagify-c-level.go-left').attr('aria-hidden', 'true').removeClass('go-left go-right');
457 - $('.imagify-level-' + image).attr('aria-hidden', 'false').addClass('go-left');
458 - }
466 + e.stopPropagation();
467 + e.preventDefault();
459 468
460 - // right buttons
461 - if ( side === 'right' ) {
462 - $img_after.removeClass('twentytwenty-after')
463 - $container.find( '.img-' + image ).addClass('twentytwenty-after');
464 - $('.twentytwenty-after-label').find('.twentytwenty-label-content').text( $container.data( image + '-label' ) );
465 - $('.imagify-c-level.go-right').attr('aria-hidden', 'true').removeClass('go-left go-right');
466 - $('.imagify-level-' + image).attr('aria-hidden', 'false').addClass('go-right');
467 - }
469 + // Button coloration.
470 + $duo.removeClass( 'selected' );
471 + $this.addClass( 'selected' );
468 472
469 - draw_me_a_chart( $('.imagify-level-' + image).find('.imagify-chart').find('canvas') );
473 + // Other side action (to not compare same images).
474 + if ( $otherSide.find( '.selected' ).data( 'img' ) === image ) {
475 + $otherSide.find( 'button:not(.selected)' ).eq( 0 ).trigger( 'click' );
476 + }
470 477
471 - return false;
478 + // Left buttons.
479 + if ( 'left' === side ) {
480 + clipStyles = $imgBefore.css( 'clip' );
481 + $imgBefore.attr( 'style', '' );
482 + $imgBefore.removeClass( 'twentytwenty-before' );
483 + $container.find( '.img-' + image ).addClass( 'twentytwenty-before' ).css( 'clip', clipStyles );
484 + $( '.twentytwenty-before-label .twentytwenty-label-content' ).text( $container.data( image + '-label' ) );
485 + $( '.imagify-c-level.go-left' ).attr( 'aria-hidden', 'true' ).removeClass( 'go-left go-right' );
486 + $( '.imagify-level-' + image ).attr( 'aria-hidden', 'false' ).addClass( 'go-left' );
487 + }
472 488
473 - });
474 - }
489 + // Right buttons.
490 + if ( 'right' === side ) {
491 + $imgAfter.removeClass( 'twentytwenty-after' );
492 + $container.find( '.img-' + image ).addClass( 'twentytwenty-after' );
493 + $( '.twentytwenty-after-label .twentytwenty-label-content' ).text( $container.data( image + '-label' ) );
494 + $( '.imagify-c-level.go-right' ).attr( 'aria-hidden', 'true' ).removeClass( 'go-left go-right' );
495 + $( '.imagify-level-' + image ).attr( 'aria-hidden', 'false' ).addClass( 'go-right' );
496 + }
475 497
476 - });
498 + drawMeAChart( $( '.imagify-level-' + image + ' .imagify-chart canvas' ) );
499 + } );
500 + } );
477 501
478 502
479 503 /**
480 - * Imagify comparison inside Media post visualization
504 + * Imagify comparison inside Media post edition.
481 505 */
482 - if ( $('.post-php').find('.wp_attachment_image').find('.thumbnail').length > 0 ) {
483 -
484 - var $ori_parent = $('.post-php').find('.wp_attachment_image'),
485 - $thumbnail = $ori_parent.find('.thumbnail'),
486 - thumb = { src: $thumbnail.prop('src'), width: $thumbnail.width(), height: $thumbnail.height() },
487 - ori_source = { src: $('#imagify-full-original').val(), size: $('#imagify-full-original-size').val() },
488 - $optimize_btn = $('#misc-publishing-actions').find('.misc-pub-imagify').find('.button-primary'),
489 - width_limit = 360;
506 + if ( imagifyTTT.imageWidth && $( '.post-php .wp_attachment_image .thumbnail' ).length > 0 ) {
490 507
491 - // if shown image > 360, use twentytwenty
492 - if ( thumb.width > width_limit && $('#imagify-full-original').length > 0 && $('#imagify-full-original').val() !== '' ) {
508 + var $oriParent = $( '.post-php .wp_attachment_image' ),
509 + oriSource = { src: $( '#imagify-full-original' ).val(), size: $( '#imagify-full-original-size' ).val() },
510 + $optimizeBtn = $( '#misc-publishing-actions' ).find( '.misc-pub-imagify .button-primary' ),
511 + filesize, saving;
493 512
494 - var imgs_loaded = 0,
495 - filesize = $('.misc-pub-filesize').find('strong').text(),
496 - saving = $('.imagify-data-item').find('.imagify-chart-value').text();
513 + imagifyTTT.widthLimit = parseInt( imagifyTTT.widthLimit, 10 );
497 514
498 - // create button to trigger
499 - $('[id^="imgedit-open-btn-"]').before('<button type="button" class="imagify-button-primary button-primary imagify-modal-trigger" data-target="#imagify-visual-comparison" id="imagify-start-comparison">' + imagifyTTT.labels.compare + '</button>')
515 + // If shown image > 360, use twentytwenty.
516 + if ( imagifyTTT.imageWidth > imagifyTTT.widthLimit && oriSource.src ) {
500 517
501 - // Modal and trigger event creation
502 - var is_modalified = imagify_twenty_modal({
503 - width: thumb.width,
504 - height: thumb.height,
505 - original_url: ori_source.src,
506 - optimized_url: thumb.src,
507 - original_size: ori_source.size,
508 - optimized_size: filesize,
509 - saving: saving,
510 - modal_append_to: $ori_parent,
511 - trigger: $('#imagify-start-comparison'),
512 - modal_id: 'imagify-visual-comparison'
513 - });
518 + filesize = $( '#imagify-full-optimized-size' ).val();
519 + saving = $( '.imagify-data-item .imagify-chart-value' ).text();
520 +
521 + // Create button to trigger.
522 + $( '[id^="imgedit-open-btn-"]' ).before( '<button type="button" class="imagify-button-primary button-primary imagify-modal-trigger" data-target="#imagify-visual-comparison" id="imagify-start-comparison">' + imagifyTTT.labels.compare + '</button>' );
523 +
524 + // Modal and trigger event creation.
525 + imagifyTwentyModal( {
526 + width: parseInt( imagifyTTT.imageWidth, 10 ),
527 + height: parseInt( imagifyTTT.imageHeight, 10 ),
528 + originalUrl: oriSource.src,
529 + optimizedUrl: imagifyTTT.imageSrc,
530 + originalSize: oriSource.size,
531 + optimizedSize: filesize,
532 + saving: saving,
533 + modalAppendTo: $oriParent,
534 + trigger: $( '#imagify-start-comparison' ),
535 + modalId: 'imagify-visual-comparison'
536 + } );
514 537 }
515 - // else put images next to next
516 - else if ( thumb.width < width_limit && $('#imagify-full-original').length > 0 && $('#imagify-full-original').val() !== '' ) {
517 -
538 + // Else put images next to next.
539 + else if ( imagifyTTT.imageWidth < imagifyTTT.widthLimit && oriSource.src ) {
540 + // TODO
518 541 }
519 - // if image has no backup
520 - else if ( $('#imagify-full-original').length > 0 && $('#imagify-full-original').val() === '' ) {
542 + // If image has no backup.
543 + else if ( $( '#imagify-full-original' ).length > 0 && '' === oriSource.src ) {
521 544 // do nothing ?
522 545 }
523 - // in case image is not optimized
546 + // In case image is not optimized.
524 547 else {
525 - $('[id^="imgedit-open-btn-"]').before('<span class="spinner imagify-hidden"></span><a class="imagify-button-primary button-primary imagify-optimize-trigger" id="imagify-optimize-trigger" href="' + $optimize_btn.attr('href') + '">' + imagifyTTT.labels.optimize + '</a>');
548 + // If is not in optimizing process, propose the Optimize button trigger.
549 + if ( $( '#misc-publishing-actions' ).find( '.misc-pub-imagify .button-primary' ).length === 1 ) {
550 + $( '[id^="imgedit-open-btn-"]' ).before( '<span class="spinner imagify-hidden"></span><a class="imagify-button-primary button-primary imagify-optimize-trigger" id="imagify-optimize-trigger" href="' + $optimizeBtn.attr( 'href' ) + '">' + imagifyTTT.labels.optimize + '</a>' );
526 551
527 - $('#imagify-optimize-trigger').on('click', function(){
528 - $(this).prev('.spinner').removeClass('imagify-hidden').addClass('is-active');
529 - });
552 + $( '#imagify-optimize-trigger' ).on( 'click', function() {
553 + $( this ).prev( '.spinner' ).removeClass( 'imagify-hidden' ).addClass( 'is-active' );
554 + } );
555 + }
530 556 }
531 557
532 558 }
533 559
534 560 /**
535 - * Images comparison in attachments list page (upload.php)
561 + * Images comparison in attachments list page (upload.php).
536 562 */
537 - if ( $('.upload-php').find('.imagify-compare-images').length > 0 ) {
563 + if ( $( '.upload-php .imagify-compare-images' ).length > 0 ) {
538 564
539 - $('.imagify-compare-images').each(function(){
565 + $( '.imagify-compare-images' ).each( function() {
566 + var $this = $( this ),
567 + id = $this.data( 'id' ),
568 + $datas = $this.closest( '#post-' + id ).find( '.column-imagify_optimized_file' );
540 569
541 - var $_this = $(this),
542 - id = $_this.data('id'),
543 - $datas = $_this.closest('#post-' + id ).find('.column-imagify_optimized_file'),
544 -
545 - // Modal and trigger event creation
546 - is_modalified = imagify_twenty_modal({
547 - width: $_this.data('full-width'),
548 - height: $_this.data('full-height'),
549 - original_url: $_this.data('backup-src'),
550 - optimized_url: $_this.data('full-src'),
551 - original_size: $datas.find('.original').text(),
552 - optimized_size: $datas.find('.imagify-data-item').find('.big').text(),
553 - saving: $datas.find('.imagify-chart-value').text(),
554 - modal_append_to: $_this.closest('.column-primary'),
555 - trigger: $_this,
556 - modal_id: 'imagify-comparison-' + id
557 - });
558 -
559 - });
570 + // Modal and trigger event creation.
571 + imagifyTwentyModal( {
572 + width: parseInt( $this.data( 'full-width' ), 10 ),
573 + height: parseInt( $this.data( 'full-height' ), 10 ),
574 + originalUrl: $this.data( 'backup-src' ),
575 + optimizedUrl: $this.data( 'full-src' ),
576 + originalSize: $datas.find( '.original' ).text(),
577 + optimizedSize: $datas.find( '#imagify_data_sum .big' ).text(),
578 + saving: $datas.find( '.imagify-chart-value' ).text(),
579 + modalAppendTo: $this.closest( '.column-primary' ),
580 + trigger: $this,
581 + modalId: 'imagify-comparison-' + id
582 + } );
583 + } );
560 584 }
561 585
562 586 /**
563 - * Images Comparison in Grid View modal
587 + * Images Comparison in Grid View modal.
564 588 */
565 - if ( $('.upload-php').length > 0 ) {
589 + if ( $( '.upload-php' ).length > 0 ) {
566 590
591 + var getVar = function( param ) {
592 + var vars = {};
567 593
568 - var get_var = function (param) {
569 - var vars = {};
570 - window.location.href.replace(
571 - /[?&]+([^=&]+)=?([^&]*)?/gi, // regexp
572 - function( m, key, value ) { // callback
573 - vars[key] = value !== undefined ? value : '';
594 + w.location.href.replace(
595 + /[?&]+([^=&]+)=?([^&]*)?/gi,
596 + function( m, key, value ) {
597 + vars[ key ] = undefined !== value ? value : '';
574 598 }
575 599 );
576 600
577 601 if ( param ) {
578 - return vars[param] ? vars[param] : null;
602 + return vars[ param ] ? vars[ param ] : null;
579 603 }
580 604 return vars;
581 605 },
582 - imagify_content_in_modal = function() {
583 -
584 - var tempTimer = setInterval( function(){
585 - if ( $('.media-modal').find('.imagify-datas-details').length ) {
586 - if ( $('#imagify-original-src').length > 0 && $('#imagify-original-src') !== '' ) {
606 + imagifyContentInModal = function() {
607 + var tempTimer = setInterval( function() {
608 + var $datas, originalSrc, $actions;
587 609
588 - // trigger creation
589 - $('.media-frame-content').find('.attachment-actions').prepend( '<button type="button" class="imagify-button-primary button-primary imagify-modal-trigger" data-target="#imagify-comparison-modal" id="imagify-media-frame-comparison-btn">' + imagifyTTT.labels.compare + '</button>' );
610 + if ( ! $( '.media-modal .imagify-datas-details' ).length ) {
611 + return;
612 + }
590 613
591 - // get datas
592 - var $datas = $('.media-frame-content').find('.compat-field-imagify');
614 + originalSrc = $( '#imagify-original-src' ).val();
593 615
594 - // Modal and trigger event creation
595 - is_modalified = imagify_twenty_modal({
596 - width: $('#imagify-full-width').val(),
597 - height: $('#imagify-full-height').val(),
598 - original_url: $('#imagify-original-src').val(),
599 - optimized_url: $('#imagify-full-src').val(),
600 - original_size: $('#imagify-original-size').val(),
601 - optimized_size: $datas.find('.imagify-data-item').find('.big').text(),
602 - saving: $datas.find('.imagify-chart-value').text(),
603 - modal_append_to: $('.media-frame-content').find('.thumbnail-image'),
604 - trigger: $('#imagify-media-frame-comparison-btn'),
605 - modal_id: 'imagify-comparison-modal',
606 - open_modal: true
607 - });
608 - }
616 + if ( originalSrc ) {
617 + // Trigger creation.
618 + $actions = $( '.media-frame-content .attachment-actions' );
609 619
610 - clearInterval(tempTimer);
611 - tempTimer = null;
612 - }
613 - }, 20 );
614 - },
615 - waitContent = setInterval( function() {
616 - if ( $('.upload-php').find('.media-frame.mode-grid').find('.attachments').length > 0 ) {
617 -
618 - // if attachment is clicked, build the modal inside the modal
619 - $('.upload-php').find('.media-frame.mode-grid').on('click', '.attachment', function(){
620 - imagify_content_in_modal();
621 - });
620 + $actions.find( '#imagify-media-frame-comparison-btn' ).remove();
621 + $actions.prepend( '<button type="button" class="imagify-button-primary button-primary imagify-modal-trigger" data-target="#imagify-comparison-modal" id="imagify-media-frame-comparison-btn">' + imagifyTTT.labels.compare + '</button>' );
622 622
623 - // if attachment is mentionned in URL, build the modal inside the modal
624 - if ( get_var('item') ){
625 - imagify_content_in_modal();
623 + // Get datas.
624 + $datas = $( '.media-frame-content .compat-field-imagify' );
625 +
626 + // Modal and trigger event creation.
627 + imagifyTwentyModal( {
628 + width: parseInt( $( '#imagify-full-width' ).val(), 10 ),
629 + height: parseInt( $( '#imagify-full-height' ).val(), 10 ),
630 + originalUrl: originalSrc,
631 + optimizedUrl: $( '#imagify-full-src' ).val(),
632 + originalSize: $( '#imagify-original-size' ).val(),
633 + optimizedSize: $datas.find( '#imagify_data_sum .big' ).text(),
634 + saving: $datas.find( '.imagify-chart-value' ).text(),
635 + modalAppendTo: $( '.media-frame-content .thumbnail-image' ),
636 + trigger: $( '#imagify-media-frame-comparison-btn' ),
637 + modalId: 'imagify-comparison-modal',
638 + openModal: true
639 + } );
626 640 }
627 641
628 - clearInterval(waitContent);
629 - waitContent = null;
630 - }
631 - }, 100);
642 + clearInterval( tempTimer );
643 + tempTimer = null;
644 + }, 20 );
645 + };
632 646
633 - // if URL contain item, that will open the WP Modal View
647 + // If attachment is clicked, or the "Previous" and "Next" buttons, build the modal inside the modal.
648 + $( '.upload-php' ).on( 'click', '.media-frame.mode-grid .attachment, .edit-media-header .left, .edit-media-header .right', function() {
649 + imagifyContentInModal();
650 + } );
651 +
652 + // If attachment is mentionned in URL, build the modal inside the modal.
653 + if ( getVar( 'item' ) ) {
654 + imagifyContentInModal();
655 + }
634 656 }
635 657
636 -})(jQuery, window, document);
658 + /**
659 + * Images comparison in custom folders list page.
660 + */
661 + if ( $( '#imagify-files-list-form' ).length > 0 ) {
662 +
663 + var buildComparisonModal = function( $buttons ) {
664 + $buttons.each( function() {
665 + var $this = $( this ),
666 + id = $this.data( 'id' ),
667 + $datas = $this.closest( 'tr' ).find( '.column-optimization .imagify-data-item' );
668 +
669 + $( '#imagify-comparison-' + id ).remove();
670 +
671 + // Modal and trigger event creation.
672 + imagifyTwentyModal( {
673 + width: parseInt( $this.data( 'full-width' ), 10 ),
674 + height: parseInt( $this.data( 'full-height' ), 10 ),
675 + originalUrl: $this.data( 'backup-src' ),
676 + optimizedUrl: $this.data( 'full-src' ),
677 + originalSize: $datas.find( '.original' ).text(),
678 + optimizedSize: $datas.find( '.optimized' ).text(),
679 + saving: $datas.find( '.imagify-chart-value' ).text(),
680 + modalAppendTo: $this.closest( '.column-primary' ),
681 + trigger: $this,
682 + modalId: 'imagify-comparison-' + id
683 + } );
684 + } );
685 + };
686 +
687 + /**
688 + * Update the comparison tool window when a file row is updated via ajax, and the ones already printed.
689 + */
690 + $( w ).on( 'comparisonprinted.imagify', function( e, id ) {
691 + var $buttons;
692 +
693 + id = id || 0;
694 +
695 + if ( id ) {
696 + $buttons = $( '#imagify-files-list-form' ).find( '.imagify-compare-images[data-id="' + id + '"]' );
697 + } else {
698 + $buttons = $( '#imagify-files-list-form' ).find( '.imagify-compare-images' );
699 + }
700 +
701 + if ( $buttons.length ) {
702 + buildComparisonModal( $buttons );
703 + }
704 + } )
705 + .trigger( 'comparisonprinted.imagify' );
706 + }
707 +
708 +} )(jQuery, document, window);