mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/MultimediaViewer
synced 2024-09-30 05:37:46 +00:00
6e127b25e2
When the lightbox is opened, or prev/next pressed, preloads the previous/next N images. Technical debt introduced: * initialization is a mess, with the viewer and the interface randomly setting properties on each other in different phases of execution. That got in the way and I shuffled things around until they worked, which is obviously not the way to have a robust system, but hopefully it will get scrapped soon anyway in favor of a clean top-down dependency injection. Change-Id: Idcb5c40de1ac0b3e482decd66e56c4de8ec71b6b Mingle: https://wikimedia.mingle.thoughtworks.com/projects/multimedia/cards/155
141 lines
2.9 KiB
JavaScript
141 lines
2.9 KiB
JavaScript
( function () {
|
|
var lightboxHooks, MLBP, HRP;
|
|
|
|
/**
|
|
* @class mlb.MultiLightbox
|
|
* @constructor
|
|
* @param {mlb.LightboxImage[]} images
|
|
* @param {number} [start=0]
|
|
* @param {Function} [InterfaceClass] type of interface to use
|
|
*/
|
|
function MultiLightbox( images, start, InterfaceClass ) {
|
|
this.images = images;
|
|
this.currentIndex = start || 0;
|
|
this.onInterfaceReady = [];
|
|
this.initializeInterface( InterfaceClass );
|
|
this.interfaceReady();
|
|
}
|
|
|
|
MLBP = MultiLightbox.prototype;
|
|
|
|
/**
|
|
* Instantiates and initializes the interface object
|
|
* @param {Function} [InterfaceClass] type of interface to use
|
|
*/
|
|
MLBP.initializeInterface = function ( InterfaceClass ) {
|
|
InterfaceClass = InterfaceClass || window.LightboxInterface;
|
|
this.iface = new InterfaceClass();
|
|
};
|
|
|
|
MLBP.onInterface = function ( func ) {
|
|
if ( this.onInterfaceReady !== undefined ) {
|
|
this.onInterfaceReady.push( func );
|
|
} else {
|
|
func();
|
|
}
|
|
};
|
|
|
|
MLBP.interfaceReady = function () {
|
|
var i;
|
|
|
|
for ( i = 0; i < this.onInterfaceReady.length; i++ ) {
|
|
this.onInterfaceReady[i]();
|
|
}
|
|
|
|
this.onInterfaceReady = undefined;
|
|
};
|
|
|
|
MLBP.next = function () {
|
|
var result;
|
|
|
|
if ( this.currentIndex >= this.images.length - 1 ) {
|
|
result = lightboxHooks.callAll( 'noNextImage', this );
|
|
|
|
if ( result === true ) {
|
|
return;
|
|
}
|
|
}
|
|
|
|
result = lightboxHooks.callAll( 'nextImage', this );
|
|
|
|
if ( result === true ) {
|
|
this.iface.load( this.images[++this.currentIndex] );
|
|
}
|
|
};
|
|
|
|
MLBP.prev = function () {
|
|
var result;
|
|
|
|
if ( this.currentIndex <= 0 ) {
|
|
result = lightboxHooks.callAll( 'noPrevImage', this );
|
|
|
|
if ( result === true ) {
|
|
return;
|
|
}
|
|
}
|
|
|
|
result = lightboxHooks.callAll( 'prevImage', this );
|
|
|
|
if ( result === true ) {
|
|
this.iface.load( this.images[--this.currentIndex] );
|
|
}
|
|
};
|
|
|
|
MLBP.open = function () {
|
|
this.iface.empty();
|
|
this.iface.attach();
|
|
};
|
|
|
|
/**
|
|
* @class
|
|
* Simple hook registry
|
|
* @constructor
|
|
*/
|
|
function LightboxHookRegistry() {
|
|
this.hooks = {};
|
|
}
|
|
|
|
HRP = LightboxHookRegistry.prototype;
|
|
|
|
/**
|
|
* Call all hooks of a type, with the provided arguments.
|
|
* @param {string} type
|
|
* @param {Mixed} thisArg
|
|
* @return {boolean} true if all hooks ran, false if one of them is overriding the default behaviour
|
|
*/
|
|
HRP.callAll = function ( type, thisArg ) {
|
|
var result, i,
|
|
hooks = this.hooks[type],
|
|
otherArgs = Array.prototype.slice.call( arguments, 2 );
|
|
|
|
if ( hooks !== undefined ) {
|
|
for ( i = 0; i < hooks.length; i++ ) {
|
|
result = hooks[i].apply( thisArg, otherArgs );
|
|
if ( result === false ) {
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
return true;
|
|
};
|
|
|
|
/**
|
|
* Register a hook of a type.
|
|
* @param {string} type
|
|
* @param {Function} hook
|
|
*/
|
|
HRP.register = function ( type, hook ) {
|
|
if ( this.hooks[type] === undefined ) {
|
|
this.hooks[type] = [];
|
|
}
|
|
|
|
this.hooks[type].push( hook );
|
|
};
|
|
|
|
lightboxHooks = new LightboxHookRegistry();
|
|
|
|
window.lightboxHooks = lightboxHooks;
|
|
window.MultiLightbox = MultiLightbox;
|
|
}() );
|