2013-08-07 08:59:08 +00:00
|
|
|
( function ( $ ) {
|
|
|
|
/**
|
2014-02-03 23:20:54 +00:00
|
|
|
* @class mlb.LightboxImage
|
2013-08-07 08:59:08 +00:00
|
|
|
* @constructor
|
|
|
|
* @param {string} src The URL (possibly relative) to the image
|
|
|
|
*/
|
|
|
|
function LightboxImage( src ) {
|
|
|
|
this.src = src;
|
|
|
|
|
|
|
|
lightboxHooks.callAll( 'modifyImageObject', this );
|
|
|
|
}
|
|
|
|
|
|
|
|
var LIP = LightboxImage.prototype;
|
|
|
|
|
2013-12-10 21:04:45 +00:00
|
|
|
/**
|
|
|
|
* The URL of the image (in the size we intend use to display the it in the lightbox)
|
|
|
|
* @type {String}
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
LIP.src = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The URL of a placeholder while the image loads. Typically a smaller version of the image, which is already
|
|
|
|
* loaded in the browser.
|
|
|
|
* @type {String}
|
2014-02-03 23:20:54 +00:00
|
|
|
* @return {jQuery.Promise.<mlb.LightboxImage, HTMLImageElement>}
|
2013-12-10 21:04:45 +00:00
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
LIP.initialSrc = null;
|
|
|
|
|
2014-01-15 23:33:16 +00:00
|
|
|
LIP.getImageElement = function () {
|
|
|
|
var ele,
|
|
|
|
$deferred = $.Deferred(),
|
|
|
|
image = this;
|
2013-08-07 08:59:08 +00:00
|
|
|
|
|
|
|
lightboxHooks.callAll( 'beforeFetchImage', this );
|
|
|
|
|
|
|
|
ele = new Image();
|
2014-01-15 23:33:16 +00:00
|
|
|
ele.addEventListener( 'error', $deferred.reject );
|
|
|
|
ele.addEventListener( 'load', function() { $deferred.resolve( image, ele ); } );
|
2014-02-06 16:17:38 +00:00
|
|
|
|
|
|
|
if ( this.src !== this.initialSrc ) {
|
|
|
|
ele.src = this.src;
|
|
|
|
} else {
|
|
|
|
// Don't display the thumb, pretend that we did load the image
|
|
|
|
// This is a workaround until we decide whether we want to display a nicer version of the thumb or not
|
|
|
|
$deferred.resolve( image, ele );
|
|
|
|
}
|
2013-08-07 08:59:08 +00:00
|
|
|
|
|
|
|
lightboxHooks.callAll( 'modifyImageElement', ele );
|
|
|
|
|
2014-01-15 23:33:16 +00:00
|
|
|
return $deferred;
|
2013-08-07 08:59:08 +00:00
|
|
|
};
|
|
|
|
|
2013-11-13 00:43:46 +00:00
|
|
|
// Assumes that the parent element's size is the maximum size.
|
|
|
|
LIP.autoResize = function ( ele, $parent ) {
|
2013-08-07 08:59:08 +00:00
|
|
|
function updateRatios() {
|
2013-09-04 01:01:57 +00:00
|
|
|
if ( imgHeight ) {
|
2013-11-13 00:43:46 +00:00
|
|
|
imgHeightRatio = parentHeight / imgHeight;
|
2013-08-07 08:59:08 +00:00
|
|
|
}
|
|
|
|
|
2013-09-04 01:01:57 +00:00
|
|
|
if ( imgWidth ) {
|
2013-11-13 00:43:46 +00:00
|
|
|
imgWidthRatio = parentWidth / imgWidth;
|
2013-08-07 08:59:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-13 00:43:46 +00:00
|
|
|
var imgWidthRatio, imgHeightRatio, parentWidth, parentHeight,
|
2013-08-07 08:59:08 +00:00
|
|
|
$img = $( ele ),
|
|
|
|
imgWidth = $img.width(),
|
|
|
|
imgHeight = $img.height();
|
|
|
|
|
2013-11-13 00:43:46 +00:00
|
|
|
$parent = $parent || $img.parent();
|
|
|
|
parentWidth = $parent.width();
|
|
|
|
parentHeight = $parent.height();
|
|
|
|
|
|
|
|
if ( this.globalMaxWidth && parentWidth > this.globalMaxWidth ) {
|
|
|
|
parentWidth = this.globalMaxWidth;
|
2013-09-04 01:01:57 +00:00
|
|
|
}
|
|
|
|
|
2013-11-13 00:43:46 +00:00
|
|
|
if ( this.globalMaxHeight && parentHeight > this.globalMaxHeight ) {
|
|
|
|
parentHeight = this.globalMaxHeight;
|
2013-09-04 01:01:57 +00:00
|
|
|
}
|
|
|
|
|
2013-08-07 08:59:08 +00:00
|
|
|
updateRatios();
|
|
|
|
|
2013-11-13 00:43:46 +00:00
|
|
|
if ( imgWidth > parentWidth ) {
|
2013-08-07 08:59:08 +00:00
|
|
|
imgHeight *= imgWidthRatio || 1;
|
2013-11-13 00:43:46 +00:00
|
|
|
imgWidth = parentWidth;
|
2013-08-07 08:59:08 +00:00
|
|
|
updateRatios();
|
|
|
|
}
|
|
|
|
|
2013-11-13 00:43:46 +00:00
|
|
|
if ( imgHeight > parentHeight ) {
|
2013-08-07 08:59:08 +00:00
|
|
|
imgWidth *= imgHeightRatio || 1;
|
2013-11-13 00:43:46 +00:00
|
|
|
imgHeight = parentHeight;
|
2013-08-07 08:59:08 +00:00
|
|
|
updateRatios();
|
|
|
|
}
|
|
|
|
|
2013-11-13 00:43:46 +00:00
|
|
|
if ( imgWidth < parentWidth && imgHeight < parentHeight ) {
|
2013-08-07 08:59:08 +00:00
|
|
|
if ( imgWidth === 0 && imgHeight === 0 ) {
|
|
|
|
// Only set one
|
2013-11-13 00:43:46 +00:00
|
|
|
imgWidth = parentWidth;
|
2013-08-07 08:59:08 +00:00
|
|
|
imgHeight = null;
|
|
|
|
} else {
|
|
|
|
if ( imgHeightRatio > imgWidthRatio ) {
|
|
|
|
imgWidth *= imgHeightRatio;
|
2013-11-13 00:43:46 +00:00
|
|
|
imgHeight = parentHeight;
|
2013-08-07 08:59:08 +00:00
|
|
|
} else {
|
|
|
|
imgHeight *= imgWidthRatio;
|
2013-11-13 00:43:46 +00:00
|
|
|
imgWidth = parentWidth;
|
2013-08-07 08:59:08 +00:00
|
|
|
}
|
|
|
|
updateRatios();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$img.width( imgWidth ).height( imgHeight );
|
|
|
|
};
|
|
|
|
|
|
|
|
window.LightboxImage = LightboxImage;
|
|
|
|
}( jQuery ) );
|