mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/MultimediaViewer
synced 2024-11-17 21:04:11 +00:00
4dd724838d
Bug: 56054 Bug: 56059 Change-Id: I549ba702c3b9bf9adec9fab692664eec1ff90005
95 lines
2 KiB
JavaScript
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 ) );
|