2013-08-07 08:59:08 +00:00
|
|
|
( function ( $ ) {
|
|
|
|
/**
|
|
|
|
* @class
|
|
|
|
* @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;
|
|
|
|
|
|
|
|
LIP.getImageElement = function ( loadcb ) {
|
|
|
|
var ele;
|
|
|
|
|
|
|
|
lightboxHooks.callAll( 'beforeFetchImage', this );
|
|
|
|
|
|
|
|
ele = new Image();
|
|
|
|
ele.addEventListener( 'load', loadcb );
|
|
|
|
ele.src = this.src;
|
|
|
|
|
|
|
|
lightboxHooks.callAll( 'modifyImageElement', ele );
|
|
|
|
|
|
|
|
return ele;
|
|
|
|
};
|
|
|
|
|
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 ) );
|