mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/MultimediaViewer
synced 2024-12-18 02:40:56 +00:00
a25d117b43
Fixes for resizing the image - should make things a little nicer Also fix for bind - should fix the library for IE users. Change-Id: I52ea4fcf0cb8b4ee3a6e2ca835f846264fffae3b
94 lines
2 KiB
JavaScript
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;
|
|
};
|
|
|
|
LIP.autoResize = function ( ele ) {
|
|
function updateRatios() {
|
|
if ( imgHeight ) {
|
|
imgHeightRatio = imgMaxHeight / imgHeight;
|
|
}
|
|
|
|
if ( imgWidth ) {
|
|
imgWidthRatio = imgMaxWidth / imgWidth;
|
|
}
|
|
}
|
|
|
|
var imgWidthRatio, imgHeightRatio,
|
|
$window = $( window ),
|
|
winWidth = $window.width(),
|
|
winHeight = $window.height(),
|
|
$img = $( ele ),
|
|
imgMaxWidth = winWidth * 0.5,
|
|
imgMaxHeight = winHeight * 0.5,
|
|
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 ) );
|