| 1 |
(function(window, $, undefined){ |
| 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() { |
| 12 |
|
| 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'); |
| 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'); |
| 35 |
|
| 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 |
return { |
| 46 |
w: w+"px", |
| 47 |
h: h+"px", |
| 48 |
cw: (dimensionPct*w)+"px", |
| 49 |
ch: (dimensionPct*h)+"px" |
| 50 |
}; |
| 51 |
}, |
| 52 |
|
| 53 |
adjustContainer = function( offset ) { |
| 54 |
// make it dynamic, in case "before" image change |
| 55 |
var $beforeImg = $container.find('.twentytwenty-before'); |
| 56 |
|
| 57 |
if ( sliderOrientation === 'vertical' ) { |
| 58 |
$beforeImg.css( 'clip', 'rect(0,' + offset.w + ',' + offset.ch + ',0)' ); |
| 59 |
} |
| 60 |
else { |
| 61 |
$beforeImg.css( 'clip', 'rect(0,' + offset.cw + ',' + offset.h + ',0)' ); |
| 62 |
} |
| 63 |
$container.css( 'height', offset.h ); |
| 64 |
|
| 65 |
if ( typeof callback === 'function' ) { |
| 66 |
callback(); |
| 67 |
} |
| 68 |
}, |
| 69 |
adjustSlider = function( pct ) { |
| 70 |
var offset = calcOffset(pct); |
| 71 |
$slider.css( ( sliderOrientation === 'vertical' ) ? 'top' : 'left', ( sliderOrientation === 'vertical' ) ? offset.ch : offset.cw ); |
| 72 |
adjustContainer( offset ); |
| 73 |
}, |
| 74 |
offsetX = 0, |
| 75 |
offsetY = 0, |
| 76 |
imgWidth = 0, |
| 77 |
imgHeight = 0; |
| 78 |
|
| 79 |
$(window).on('resize.twentytwenty', function(e) { |
| 80 |
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' ) { |
| 85 |
e.preventDefault(); |
| 86 |
} |
| 87 |
else if ( ( ( e.distX < e.distY && e.distX < -e.distY ) || ( e.distX > e.distY && e.distX > -e.distY ) ) && sliderOrientation === 'vertical' ) { |
| 88 |
e.preventDefault(); |
| 89 |
} |
| 90 |
$container.addClass('active'); |
| 91 |
offsetX = $container.offset().left; |
| 92 |
offsetY = $container.offset().top; |
| 93 |
imgWidth = $beforeImg.width(); |
| 94 |
imgHeight = $beforeImg.height(); |
| 95 |
}); |
| 96 |
|
| 97 |
$slider.on('moveend', function(e) { |
| 98 |
$container.removeClass('active'); |
| 99 |
}); |
| 100 |
|
| 101 |
$slider.on('move', function(e) { |
| 102 |
if ( $container.hasClass('active') ) { |
| 103 |
|
| 104 |
sliderPct = ( sliderOrientation === 'vertical' ) ? ( e.pageY-offsetY )/imgHeight : ( e.pageX-offsetX )/imgWidth; |
| 105 |
|
| 106 |
if ( sliderPct < 0 ) { |
| 107 |
sliderPct = 0; |
| 108 |
} |
| 109 |
if ( sliderPct > 1 ) { |
| 110 |
sliderPct = 1; |
| 111 |
} |
| 112 |
adjustSlider( sliderPct ); |
| 113 |
} |
| 114 |
}); |
| 115 |
|
| 116 |
$container.find('img').on('mousedown', function(event) { |
| 117 |
event.preventDefault(); |
| 118 |
}); |
| 119 |
|
| 120 |
$(window).trigger('resize.twentytwenty'); |
| 121 |
}); |
| 122 |
}; |
| 123 |
|
| 124 |
})(window, jQuery); |
| 125 |
|
| 126 |
/** |
| 127 |
* Twentytwenty Imagify Init |
| 128 |
*/ |
| 129 |
(function($, window, document, undefined){ |
| 130 |
|
| 131 |
/* |
| 132 |
* Mini chart |
| 133 |
* |
| 134 |
* @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 |
*/ |
| 164 |
var imagify_open_modal = function( $the_link ){ |
| 165 |
|
| 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 |
|
| 171 |
}, |
| 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); |
| 188 |
|
| 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 |
} |
| 192 |
|
| 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 |
); |
| 223 |
|
| 224 |
settings.trigger.on('click.imagify', function(){ |
| 225 |
|
| 226 |
var $modal = $( $(this).data('target') ), |
| 227 |
imgs_loaded = 0; |
| 228 |
|
| 229 |
if ( typeof imagify_open_modal === 'function' && settings.open_modal ) { |
| 230 |
imagify_open_modal( $(this) ); |
| 231 |
} |
| 232 |
|
| 233 |
$modal.find('.imagify-modal-content').css({ |
| 234 |
'width' : ($(window).outerWidth()*0.85) + 'px', |
| 235 |
'max-width' : settings.width |
| 236 |
}); |
| 237 |
|
| 238 |
// load before img |
| 239 |
$modal.find('.imagify-img-before').on('load', function(){ |
| 240 |
imgs_loaded++; |
| 241 |
}).attr('src', settings.original_url); |
| 242 |
|
| 243 |
// load after img |
| 244 |
$modal.find('.imagify-img-after').on('load', function(){ |
| 245 |
imgs_loaded++; |
| 246 |
}).attr('src', settings.optimized_url); |
| 247 |
|
| 248 |
var $tt = $modal.find('.twentytwenty-container'), |
| 249 |
check_load = setInterval( function(){ |
| 250 |
|
| 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(){ |
| 258 |
|
| 259 |
var windowH = $(window).height(), |
| 260 |
ttH = $modal.find('.twentytwenty-container').height(), |
| 261 |
ttTop = $modal.find('.twentytwenty-wrapper').position().top; |
| 262 |
|
| 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 |
} |
| 267 |
|
| 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'); |
| 271 |
|
| 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 ); |
| 278 |
|
| 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 |
}); |
| 289 |
|
| 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 |
} |
| 302 |
|
| 303 |
}); |
| 304 |
clearInterval( check_load ); |
| 305 |
check_load = null; |
| 306 |
return 'done'; |
| 307 |
} |
| 308 |
}, 75 ); |
| 309 |
|
| 310 |
return false; |
| 311 |
}); |
| 312 |
}; // imagify_twenty_modal( options ); |
| 313 |
|
| 314 |
|
| 315 |
/** |
| 316 |
* The complexe visual comparison |
| 317 |
*/ |
| 318 |
$('.imagify-visual-comparison-btn').on('click', function(){ |
| 319 |
|
| 320 |
if ( $('.twentytwenty-wrapper').length === 1) { |
| 321 |
return; |
| 322 |
} |
| 323 |
|
| 324 |
$( $(this).data('target') ).find('.imagify-modal-content').css('width', ($(window).outerWidth()*0.95) + 'px'); |
| 325 |
|
| 326 |
if ( $('.twentytwenty-container').length > 0 && $(window).outerWidth() > 800 ) { |
| 327 |
|
| 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'), |
| 335 |
|
| 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'), |
| 340 |
|
| 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'), |
| 345 |
|
| 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'), |
| 350 |
|
| 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'), |
| 355 |
|
| 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>'; |
| 366 |
|
| 367 |
// loader |
| 368 |
$tt.before('<img class="loader" src="' + loader + '" alt="Loading…" width="64" height="64">') |
| 369 |
|
| 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); |
| 376 |
|
| 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 |
); |
| 385 |
|
| 386 |
// load image original |
| 387 |
$('.img-original').on('load', function(){ |
| 388 |
imgs_loaded++; |
| 389 |
}).attr('src', original_src); |
| 390 |
|
| 391 |
// load image normal |
| 392 |
$('.img-normal').on('load', function(){ |
| 393 |
imgs_loaded++; |
| 394 |
}).attr('src', normal_src); |
| 395 |
|
| 396 |
// load image aggressive |
| 397 |
$('.img-aggressive').on('load', function(){ |
| 398 |
imgs_loaded++; |
| 399 |
}).attr('src', aggressive_src); |
| 400 |
|
| 401 |
// load image ultra |
| 402 |
$('.img-ultra').on('load', function(){ |
| 403 |
imgs_loaded++; |
| 404 |
}).attr('src', ultra_src); |
| 405 |
|
| 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); |
| 424 |
|
| 425 |
// on click on button choices |
| 426 |
|
| 427 |
$('.imagify-comparison-title').on('click', '.twentytwenty-duo-buttons button:not(.selected)', function(e){ |
| 428 |
|
| 429 |
e.stopPropagation(); |
| 430 |
|
| 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'); |
| 439 |
|
| 440 |
// button coloration |
| 441 |
$duo.removeClass('selected'); |
| 442 |
$_this.addClass('selected'); |
| 443 |
|
| 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 |
} |
| 448 |
|
| 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 |
} |
| 459 |
|
| 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 |
} |
| 468 |
|
| 469 |
draw_me_a_chart( $('.imagify-level-' + image).find('.imagify-chart').find('canvas') ); |
| 470 |
|
| 471 |
return false; |
| 472 |
|
| 473 |
}); |
| 474 |
} |
| 475 |
|
| 476 |
}); |
| 477 |
|
| 478 |
|
| 479 |
/** |
| 480 |
* Imagify comparison inside Media post visualization |
| 481 |
*/ |
| 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; |
| 490 |
|
| 491 |
// if shown image > 360, use twentytwenty |
| 492 |
if ( thumb.width > width_limit && $('#imagify-full-original').length > 0 && $('#imagify-full-original').val() !== '' ) { |
| 493 |
|
| 494 |
var imgs_loaded = 0, |
| 495 |
filesize = $('.misc-pub-filesize').find('strong').text(), |
| 496 |
saving = $('.imagify-data-item').find('.imagify-chart-value').text(); |
| 497 |
|
| 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>') |
| 500 |
|
| 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 |
}); |
| 514 |
} |
| 515 |
// else put images next to next |
| 516 |
else if ( thumb.width < width_limit && $('#imagify-full-original').length > 0 && $('#imagify-full-original').val() !== '' ) { |
| 517 |
|
| 518 |
} |
| 519 |
// if image has no backup |
| 520 |
else if ( $('#imagify-full-original').length > 0 && $('#imagify-full-original').val() === '' ) { |
| 521 |
// do nothing ? |
| 522 |
} |
| 523 |
// in case image is not optimized |
| 524 |
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>'); |
| 526 |
|
| 527 |
$('#imagify-optimize-trigger').on('click', function(){ |
| 528 |
$(this).prev('.spinner').removeClass('imagify-hidden').addClass('is-active'); |
| 529 |
}); |
| 530 |
} |
| 531 |
|
| 532 |
} |
| 533 |
|
| 534 |
/** |
| 535 |
* Images comparison in attachments list page (upload.php) |
| 536 |
*/ |
| 537 |
if ( $('.upload-php').find('.imagify-compare-images').length > 0 ) { |
| 538 |
|
| 539 |
$('.imagify-compare-images').each(function(){ |
| 540 |
|
| 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 |
}); |
| 560 |
} |
| 561 |
|
| 562 |
/** |
| 563 |
* Images Comparison in Grid View modal |
| 564 |
*/ |
| 565 |
if ( $('.upload-php').length > 0 ) { |
| 566 |
|
| 567 |
|
| 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 : ''; |
| 574 |
} |
| 575 |
); |
| 576 |
|
| 577 |
if ( param ) { |
| 578 |
return vars[param] ? vars[param] : null; |
| 579 |
} |
| 580 |
return vars; |
| 581 |
}, |
| 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') !== '' ) { |
| 587 |
|
| 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>' ); |
| 590 |
|
| 591 |
// get datas |
| 592 |
var $datas = $('.media-frame-content').find('.compat-field-imagify'); |
| 593 |
|
| 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 |
} |
| 609 |
|
| 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 |
}); |
| 622 |
|
| 623 |
// if attachment is mentionned in URL, build the modal inside the modal |
| 624 |
if ( get_var('item') ){ |
| 625 |
imagify_content_in_modal(); |
| 626 |
} |
| 627 |
|
| 628 |
clearInterval(waitContent); |
| 629 |
waitContent = null; |
| 630 |
} |
| 631 |
}, 100); |
| 632 |
|
| 633 |
// if URL contain item, that will open the WP Modal View |
| 634 |
} |
| 635 |
|
| 636 |
})(jQuery, window, document); |
| 637 |
|