mediawiki-extensions-Multim.../resources/multilightbox/lightboximage.js
Mark Holmquist 5aaf1e73fe Reorganize UI to be...way sexier.
There's just no other adjective for this, it is _super_ nice.

There may be refinements later as the designers get things together.

Bug: 56499
Change-Id: I200684a19c190444587f520595186e9ae5afbea6
2013-11-21 09:47:05 -08:00

94 lines
2 KiB
JavaScript

( 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;
};
// Assumes that the parent element's size is the maximum size.
LIP.autoResize = function ( ele, $parent ) {
function updateRatios() {
if ( imgHeight ) {
imgHeightRatio = parentHeight / imgHeight;
}
if ( imgWidth ) {
imgWidthRatio = parentWidth / imgWidth;
}
}
var imgWidthRatio, imgHeightRatio, parentWidth, parentHeight,
$img = $( ele ),
imgWidth = $img.width(),
imgHeight = $img.height();
$parent = $parent || $img.parent();
parentWidth = $parent.width();
parentHeight = $parent.height();
if ( this.globalMaxWidth && parentWidth > this.globalMaxWidth ) {
parentWidth = this.globalMaxWidth;
}
if ( this.globalMaxHeight && parentHeight > this.globalMaxHeight ) {
parentHeight = this.globalMaxHeight;
}
updateRatios();
if ( imgWidth > parentWidth ) {
imgHeight *= imgWidthRatio || 1;
imgWidth = parentWidth;
updateRatios();
}
if ( imgHeight > parentHeight ) {
imgWidth *= imgHeightRatio || 1;
imgHeight = parentHeight;
updateRatios();
}
if ( imgWidth < parentWidth && imgHeight < parentHeight ) {
if ( imgWidth === 0 && imgHeight === 0 ) {
// Only set one
imgWidth = parentWidth;
imgHeight = null;
} else {
if ( imgHeightRatio > imgWidthRatio ) {
imgWidth *= imgHeightRatio;
imgHeight = parentHeight;
} else {
imgHeight *= imgWidthRatio;
imgWidth = parentWidth;
}
updateRatios();
}
}
$img.width( imgWidth ).height( imgHeight );
};
window.LightboxImage = LightboxImage;
}( jQuery ) );