mediawiki-extensions-Multim.../resources/multilightbox/lightboximage.js
Mark Holmquist c0c9e7037f ResourceLoader consistency file moves
Keep modules in separate dirs, move images to their module dirs, and fix
up the module declarations so they still work.

Also moved viewer.svg into the root.

Bug: 56421
Change-Id: Ia84ddfd3b91c784c42d9ba243c5cfd46354f139f
2013-11-14 13:38:40 -08:00

95 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;
};
LIP.autoResize = function ( ele, ratio ) {
function updateRatios() {
if ( imgHeight ) {
imgHeightRatio = imgMaxHeight / imgHeight;
}
if ( imgWidth ) {
imgWidthRatio = imgMaxWidth / imgWidth;
}
}
var imgWidthRatio, imgHeightRatio,
multRatio = ratio || 0.5,
$window = $( window ),
winWidth = $window.width(),
winHeight = $window.height(),
$img = $( ele ),
imgMaxWidth = winWidth * multRatio,
imgMaxHeight = winHeight * multRatio,
imgWidth = $img.width(),
imgHeight = $img.height();
if ( this.globalMaxWidth && imgMaxWidth > this.globalMaxWidth ) {
imgMaxWidth = this.globalMaxWidth;
}
if ( this.globalMaxHeight && imgMaxHeight > this.globalMaxHeight ) {
imgMaxHeight = this.globalMaxHeight;
}
updateRatios();
if ( imgWidth > imgMaxWidth ) {
imgHeight *= imgWidthRatio || 1;
imgWidth = imgMaxWidth;
updateRatios();
}
if ( imgHeight > imgMaxHeight ) {
imgWidth *= imgHeightRatio || 1;
imgHeight = imgMaxHeight;
updateRatios();
}
if ( imgWidth < imgMaxWidth && imgHeight < imgMaxHeight ) {
if ( imgWidth === 0 && imgHeight === 0 ) {
// Only set one
imgWidth = imgMaxWidth;
imgHeight = null;
} else {
if ( imgHeightRatio > imgWidthRatio ) {
imgWidth *= imgHeightRatio;
imgHeight = imgMaxHeight;
} else {
imgHeight *= imgWidthRatio;
imgWidth = imgMaxWidth;
}
updateRatios();
}
}
$img.width( imgWidth ).height( imgHeight );
};
window.LightboxImage = LightboxImage;
}( jQuery ) );