2013-08-07 08:59:08 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the MediaWiki extension MultimediaViewer.
|
|
|
|
*
|
|
|
|
* MultimediaViewer is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* MultimediaViewer is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with MultimediaViewer. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2014-02-07 14:47:00 +00:00
|
|
|
( function ( mw, $ ) {
|
2014-02-17 15:09:23 +00:00
|
|
|
var MMVP,
|
2014-02-24 09:29:14 +00:00
|
|
|
comingFromHashChange = false;
|
2013-08-07 08:59:08 +00:00
|
|
|
|
2013-11-27 21:57:45 +00:00
|
|
|
/**
|
2014-01-11 00:11:37 +00:00
|
|
|
* Analyses the page, looks for image content and sets up the hooks
|
2013-11-27 21:57:45 +00:00
|
|
|
* to manage the viewing experience of such content.
|
2014-02-19 02:27:30 +00:00
|
|
|
* @class mw.MultimediaViewer
|
2013-11-27 21:57:45 +00:00
|
|
|
* @constructor
|
|
|
|
*/
|
2013-08-07 08:59:08 +00:00
|
|
|
function MultimediaViewer() {
|
2013-11-27 21:57:45 +00:00
|
|
|
/**
|
|
|
|
* MultiLightbox object used to display the pictures in the page.
|
2014-01-11 00:11:37 +00:00
|
|
|
* @property {mlb.MultiLightbox}
|
2013-11-27 21:57:45 +00:00
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this.lightbox = null;
|
|
|
|
|
2013-12-31 00:19:20 +00:00
|
|
|
/**
|
|
|
|
* Whether we've fired an animation for the metadata div.
|
|
|
|
* @property {boolean}
|
|
|
|
* @private
|
|
|
|
*/
|
2014-02-18 09:01:37 +00:00
|
|
|
this.hasAnimatedMetadata = window.localStorage !== undefined &&
|
|
|
|
localStorage.getItem( 'mmv.hasOpenedMetadata' );
|
2013-12-31 00:19:20 +00:00
|
|
|
|
2013-11-27 21:57:45 +00:00
|
|
|
/**
|
2014-02-13 09:52:40 +00:00
|
|
|
* @property {mw.mmv.provider.Image}
|
2013-11-27 21:57:45 +00:00
|
|
|
* @private
|
|
|
|
*/
|
2014-02-13 09:52:40 +00:00
|
|
|
this.imageProvider = new mw.mmv.provider.Image();
|
2013-11-27 21:57:45 +00:00
|
|
|
|
2014-02-03 20:42:17 +00:00
|
|
|
/**
|
|
|
|
* @property {mw.mmv.provider.ImageInfo}
|
|
|
|
* @private
|
|
|
|
*/
|
2014-02-15 02:15:54 +00:00
|
|
|
this.imageInfoProvider = new mw.mmv.provider.ImageInfo( new mw.mmv.Api( 'imageinfo' ),
|
2014-02-03 20:42:17 +00:00
|
|
|
// Short-circuit, don't fallback, to save some tiny amount of time
|
2014-02-15 02:15:54 +00:00
|
|
|
{ language: mw.config.get( 'wgUserLanguage', false ) || mw.config.get( 'wgContentLanguage', 'en' ) }
|
|
|
|
);
|
2014-02-03 20:42:17 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {mw.mmv.provider.FileRepoInfo}
|
|
|
|
* @private
|
|
|
|
*/
|
2014-02-15 02:15:54 +00:00
|
|
|
this.fileRepoInfoProvider = new mw.mmv.provider.FileRepoInfo( new mw.mmv.Api( 'filerepoinfo' ) );
|
2014-02-03 20:42:17 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {mw.mmv.provider.ThumbnailInfo}
|
|
|
|
* @private
|
|
|
|
*/
|
2014-02-15 02:15:54 +00:00
|
|
|
this.thumbnailInfoProvider = new mw.mmv.provider.ThumbnailInfo( new mw.mmv.Api( 'thumbnailinfo' ) );
|
2014-02-03 20:42:17 +00:00
|
|
|
|
2014-02-06 00:12:50 +00:00
|
|
|
/**
|
|
|
|
* @property {mw.mmv.provider.UserInfo}
|
|
|
|
* @private
|
|
|
|
*/
|
2014-02-15 02:15:54 +00:00
|
|
|
this.userInfoProvider = new mw.mmv.provider.UserInfo( new mw.mmv.Api( 'userinfo' ) );
|
2014-02-06 00:12:50 +00:00
|
|
|
|
2014-01-30 11:28:43 +00:00
|
|
|
/**
|
2014-01-31 00:06:33 +00:00
|
|
|
* @property {mw.mmv.provider.ImageUsage}
|
2014-01-30 11:28:43 +00:00
|
|
|
* @private
|
|
|
|
*/
|
2014-02-15 02:15:54 +00:00
|
|
|
this.imageUsageProvider = new mw.mmv.provider.ImageUsage( new mw.mmv.Api( 'imageusage' ) );
|
2014-01-30 11:28:43 +00:00
|
|
|
|
|
|
|
/**
|
2014-01-31 00:06:33 +00:00
|
|
|
* @property {mw.mmv.provider.GlobalUsage}
|
2014-01-30 11:28:43 +00:00
|
|
|
* @private
|
|
|
|
*/
|
2014-02-19 18:06:11 +00:00
|
|
|
this.globalUsageProvider = new mw.mmv.provider.GlobalUsage( new mw.mmv.Api( 'globalusage' ),
|
2014-02-15 02:15:54 +00:00
|
|
|
{ doNotUseApi: !mw.config.get( 'wgMultimediaViewer' ).globalUsageAvailable }
|
|
|
|
);
|
2014-01-30 11:28:43 +00:00
|
|
|
// replace with this one to test global usage on a local wiki without going through all the
|
|
|
|
// hassle required for installing the extension:
|
2014-02-03 20:42:17 +00:00
|
|
|
//this.globalUsageProvider = new mw.mmv.provider.GlobalUsage(
|
2014-02-19 18:06:11 +00:00
|
|
|
// new mw.mmv.Api( {ajax: { url: 'http://commons.wikimedia.org/w/api.php', dataType: 'jsonp' } } )
|
2014-01-30 11:28:43 +00:00
|
|
|
//);
|
|
|
|
|
2014-02-17 15:09:23 +00:00
|
|
|
this.setupEventHandlers();
|
|
|
|
}
|
2013-08-07 08:59:08 +00:00
|
|
|
|
2014-02-17 15:09:23 +00:00
|
|
|
MMVP = MultimediaViewer.prototype;
|
2013-08-07 08:59:08 +00:00
|
|
|
|
2014-02-19 02:27:30 +00:00
|
|
|
/**
|
|
|
|
* Initialize the lightbox interface given an array of thumbnail
|
|
|
|
* objects.
|
|
|
|
* @param {Object[]} thumbs Complex structure...TODO, document this better.
|
|
|
|
*/
|
2014-02-17 15:09:23 +00:00
|
|
|
MMVP.initWithThumbs = function ( thumbs ) {
|
|
|
|
var i, thumb;
|
2013-08-07 08:59:08 +00:00
|
|
|
|
2013-11-27 21:57:45 +00:00
|
|
|
// Only if we find legit images, create a MultiLightbox object
|
2014-02-17 15:09:23 +00:00
|
|
|
this.lightbox = new mw.MultiLightbox( 0, mw.LightboxInterface, this );
|
2013-11-27 21:57:45 +00:00
|
|
|
|
2014-02-17 15:09:23 +00:00
|
|
|
this.thumbs = thumbs;
|
2013-10-29 20:26:07 +00:00
|
|
|
|
2014-02-17 15:09:23 +00:00
|
|
|
for ( i = 0; i < this.thumbs.length; i++ ) {
|
|
|
|
thumb = this.thumbs[ i ];
|
|
|
|
// Create a LightboxImage object for each legit image
|
|
|
|
thumb.image = mw.mediaViewer.createNewImage( thumb.$thumb.prop( 'src' ),
|
|
|
|
thumb.link,
|
|
|
|
thumb.title,
|
|
|
|
i,
|
|
|
|
thumb.thumb,
|
|
|
|
thumb.caption );
|
|
|
|
}
|
|
|
|
};
|
2013-12-10 19:54:07 +00:00
|
|
|
|
2013-12-02 19:46:52 +00:00
|
|
|
/**
|
|
|
|
* Create an image object for the lightbox to use.
|
|
|
|
* @protected
|
|
|
|
* @param {string} fileLink Link to the file - generally a thumb URL
|
|
|
|
* @param {string} filePageLink Link to the File: page
|
|
|
|
* @param {mw.Title} fileTitle Represents the File: page
|
|
|
|
* @param {number} index Which number file this is
|
2013-11-13 21:16:49 +00:00
|
|
|
* @param {HTMLImageElement} thumb The thumbnail that represents this image on the page
|
2013-12-10 19:54:07 +00:00
|
|
|
* @param {string} [caption] The caption, if any.
|
2013-12-02 19:46:52 +00:00
|
|
|
* @returns {mw.LightboxImage}
|
|
|
|
*/
|
2013-12-10 19:54:07 +00:00
|
|
|
MMVP.createNewImage = function ( fileLink, filePageLink, fileTitle, index, thumb, caption ) {
|
|
|
|
var thisImage = new mw.LightboxImage( fileLink, filePageLink, fileTitle, index, thumb, caption );
|
2013-12-02 19:46:52 +00:00
|
|
|
thisImage.filePageLink = filePageLink;
|
|
|
|
thisImage.filePageTitle = fileTitle;
|
|
|
|
thisImage.index = index;
|
2013-11-13 21:16:49 +00:00
|
|
|
thisImage.thumbnail = thumb;
|
2013-12-02 19:46:52 +00:00
|
|
|
|
|
|
|
return thisImage;
|
|
|
|
};
|
|
|
|
|
2013-12-03 01:58:09 +00:00
|
|
|
/**
|
|
|
|
* Handles resize events in viewer.
|
|
|
|
* @protected
|
|
|
|
* @param {mw.LightboxInterface} ui lightbox that got resized
|
|
|
|
*/
|
2013-11-26 03:37:01 +00:00
|
|
|
MMVP.resize = function ( ui ) {
|
2013-12-03 01:58:09 +00:00
|
|
|
var viewer = this,
|
2014-02-05 01:06:10 +00:00
|
|
|
fileTitle = this.currentImageFileTitle,
|
|
|
|
imageWidths;
|
2013-11-13 21:16:49 +00:00
|
|
|
|
2014-02-13 09:52:40 +00:00
|
|
|
this.preloadThumbnails();
|
|
|
|
|
2014-01-30 07:22:26 +00:00
|
|
|
if ( fileTitle ) {
|
2014-02-13 09:52:40 +00:00
|
|
|
imageWidths = ui.getCurrentImageWidths();
|
|
|
|
this.fetchThumbnail(
|
|
|
|
fileTitle, imageWidths.real
|
|
|
|
).then( function( thumbnail, image ) {
|
|
|
|
viewer.setImage( ui, thumbnail, image, imageWidths );
|
2014-01-30 07:22:26 +00:00
|
|
|
} );
|
|
|
|
}
|
2014-01-06 21:53:42 +00:00
|
|
|
|
|
|
|
this.updateControls();
|
2013-12-03 01:58:09 +00:00
|
|
|
};
|
|
|
|
|
2014-02-19 02:27:30 +00:00
|
|
|
/**
|
|
|
|
* Updates positioning of controls, usually after a resize event.
|
|
|
|
*/
|
2013-11-13 00:43:46 +00:00
|
|
|
MMVP.updateControls = function () {
|
2014-02-17 15:09:23 +00:00
|
|
|
var numImages = this.thumbs ? this.thumbs.length : 0,
|
2014-02-03 11:23:31 +00:00
|
|
|
showNextButton = this.lightbox.currentIndex < (numImages - 1),
|
|
|
|
showPreviousButton = this.lightbox.currentIndex > 0;
|
2013-11-13 00:43:46 +00:00
|
|
|
|
2014-02-03 11:23:31 +00:00
|
|
|
this.ui.updateControls( showNextButton, showPreviousButton );
|
2013-11-13 00:43:46 +00:00
|
|
|
};
|
2013-11-06 00:36:06 +00:00
|
|
|
|
2014-01-28 23:57:05 +00:00
|
|
|
/**
|
2014-02-13 09:52:40 +00:00
|
|
|
* Loads and sets the specified image. It also updates the controls.
|
2014-01-28 23:57:05 +00:00
|
|
|
* @param {mw.LightboxInterface} ui image container
|
2014-02-13 09:52:40 +00:00
|
|
|
* @param {mw.mmv.model.Thumbnail} thumbnail thumbnail information
|
|
|
|
* @param {HTMLImageElement} image
|
2014-02-06 23:18:47 +00:00
|
|
|
* @param {mw.mmv.model.ThumbnailWidth} imageWidths
|
2014-01-28 23:57:05 +00:00
|
|
|
*/
|
2014-02-13 09:52:40 +00:00
|
|
|
MMVP.setImage = function ( ui, thumbnail, image, imageWidths ) {
|
|
|
|
// we downscale larger images but do not scale up smaller ones, that would look ugly
|
|
|
|
if ( thumbnail.width > imageWidths.screen ) {
|
|
|
|
image.width = imageWidths.css;
|
2014-02-06 23:18:47 +00:00
|
|
|
}
|
2014-01-28 23:57:05 +00:00
|
|
|
|
2014-02-13 09:52:40 +00:00
|
|
|
ui.replaceImageWith( image );
|
|
|
|
this.updateControls();
|
2014-01-28 23:57:05 +00:00
|
|
|
};
|
|
|
|
|
2013-12-10 19:54:07 +00:00
|
|
|
/**
|
|
|
|
* Loads a specified image.
|
|
|
|
* @param {mw.LightboxImage} image
|
2014-02-13 09:52:40 +00:00
|
|
|
* @param {HTMLImageElement} initialImage A thumbnail to use as placeholder while the image loads
|
2013-12-10 19:54:07 +00:00
|
|
|
*/
|
2014-02-13 09:52:40 +00:00
|
|
|
MMVP.loadImage = function ( image, initialImage ) {
|
2014-02-06 23:18:47 +00:00
|
|
|
var imageWidths,
|
2014-02-13 09:52:40 +00:00
|
|
|
viewer = this,
|
|
|
|
imagePromise,
|
|
|
|
metadataPromise;
|
|
|
|
|
|
|
|
// FIXME dependency injection happens in completely random order and location, needs cleanup
|
|
|
|
this.ui = this.lightbox.iface;
|
2013-11-25 23:51:40 +00:00
|
|
|
|
|
|
|
this.lightbox.currentIndex = image.index;
|
|
|
|
|
|
|
|
this.currentImageFilename = image.filePageTitle.getPrefixedText();
|
2014-01-06 20:02:39 +00:00
|
|
|
this.currentImageFileTitle = image.filePageTitle;
|
2014-02-24 09:29:14 +00:00
|
|
|
this.lightbox.iface.comingFromHashChange = this.comingFromHashChange;
|
2014-01-06 21:53:42 +00:00
|
|
|
|
|
|
|
if ( !this.isOpen ) {
|
|
|
|
this.lightbox.open();
|
|
|
|
this.isOpen = true;
|
|
|
|
} else {
|
|
|
|
this.lightbox.iface.empty();
|
|
|
|
}
|
2014-02-13 09:52:40 +00:00
|
|
|
this.lightbox.iface.setupForLoad();
|
|
|
|
this.lightbox.iface.showImage( image, initialImage );
|
|
|
|
|
|
|
|
this.preloadImagesMetadata();
|
|
|
|
this.preloadThumbnails();
|
2014-02-20 09:34:48 +00:00
|
|
|
this.preloadFullscreenThumbnail( image );
|
2014-01-06 21:53:42 +00:00
|
|
|
|
2013-11-25 23:51:40 +00:00
|
|
|
$( document.body ).addClass( 'mw-mlb-lightbox-open' );
|
|
|
|
|
2014-02-13 09:52:40 +00:00
|
|
|
imageWidths = this.ui.getCurrentImageWidths();
|
|
|
|
imagePromise = this.fetchThumbnail(
|
2014-02-06 23:18:47 +00:00
|
|
|
image.filePageTitle, imageWidths.real
|
2014-02-13 09:52:40 +00:00
|
|
|
).done( function( thumbnail, image ) {
|
|
|
|
viewer.setImage( viewer.lightbox.iface, thumbnail, image, imageWidths );
|
|
|
|
viewer.lightbox.iface.$imageDiv.removeClass( 'empty' );
|
|
|
|
} );
|
|
|
|
|
|
|
|
metadataPromise = this.fetchSizeIndependentLightboxInfo(
|
|
|
|
image.filePageTitle
|
|
|
|
).done( function ( imageInfo, repoInfo, localUsage, globalUsage, userInfo ) {
|
|
|
|
viewer.lightbox.iface.panel.setImageInfo(image, imageInfo, repoInfo,
|
|
|
|
localUsage, globalUsage, userInfo );
|
|
|
|
} );
|
2014-01-28 23:57:05 +00:00
|
|
|
|
2014-02-13 09:52:40 +00:00
|
|
|
$.when( imagePromise, metadataPromise ).then( function() {
|
2014-01-23 00:38:01 +00:00
|
|
|
viewer.stopListeningToScroll();
|
|
|
|
viewer.animateMetadataDivOnce()
|
|
|
|
// We need to wait until the animation is finished before we listen to scroll
|
|
|
|
.then( function() { viewer.startListeningToScroll(); } );
|
2014-02-13 09:52:40 +00:00
|
|
|
} );
|
2013-11-25 23:51:40 +00:00
|
|
|
|
2014-02-24 09:29:14 +00:00
|
|
|
this.comingFromHashChange = false;
|
2014-02-17 15:09:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads an image by its title
|
|
|
|
* @param {string} title
|
|
|
|
* @param {boolean} updateHash Viewer should update the location hash when true
|
|
|
|
*/
|
|
|
|
MMVP.loadImageByTitle = function ( title, updateHash ) {
|
|
|
|
var viewer = this;
|
|
|
|
|
|
|
|
if ( !this.thumbs || !this.thumbs.length ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-24 09:29:14 +00:00
|
|
|
this.comingFromHashChange = !updateHash;
|
2014-02-17 15:09:23 +00:00
|
|
|
|
|
|
|
$.each( this.thumbs, function ( idx, thumb ) {
|
|
|
|
if ( thumb.title.getPrefixedText() === title ) {
|
|
|
|
viewer.loadImage( thumb.image, thumb.$thumb.clone()[ 0 ], true );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} );
|
2014-02-13 09:52:40 +00:00
|
|
|
};
|
2013-12-10 21:04:45 +00:00
|
|
|
|
2014-02-13 09:52:40 +00:00
|
|
|
/**
|
|
|
|
* Preload this many prev/next images to speed up navigation.
|
|
|
|
* (E.g. preloadDistance = 3 means that the previous 3 and the next 3 images will be loaded.)
|
|
|
|
* Preloading only happens when the viewer is open.
|
|
|
|
* @property {number}
|
|
|
|
*/
|
|
|
|
MMVP.preloadDistance = 1;
|
2014-02-07 14:47:00 +00:00
|
|
|
|
2014-02-13 09:52:40 +00:00
|
|
|
/**
|
|
|
|
* Stores image metadata preloads, so they can be cancelled.
|
2014-02-19 02:27:30 +00:00
|
|
|
* @property {mw.mmv.model.TaskQueue}
|
2014-02-13 09:52:40 +00:00
|
|
|
*/
|
|
|
|
MMVP.metadataPreloadQueue = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stores image thumbnail preloads, so they can be cancelled.
|
2014-02-19 02:27:30 +00:00
|
|
|
* @property {mw.mmv.model.TaskQueue}
|
2014-02-13 09:52:40 +00:00
|
|
|
*/
|
|
|
|
MMVP.thumbnailPreloadQueue = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Orders lightboximage indexes for preloading. Works similar to $.each, except it only takes
|
|
|
|
* the callback argument. Calls the callback with each lightboximage index in some sequence
|
|
|
|
* that is ideal for preloading.
|
|
|
|
* @private
|
|
|
|
* @param {function(number, mw.LightboxImage)} callback
|
|
|
|
*/
|
|
|
|
MMVP.eachPrealoadableLightboxIndex = function( callback ) {
|
|
|
|
for ( var i = 0; i <= this.preloadDistance; i++ ) {
|
2014-02-17 15:09:23 +00:00
|
|
|
if ( this.lightbox.currentIndex + i < this.thumbs.length ) {
|
2014-02-13 09:52:40 +00:00
|
|
|
callback(
|
|
|
|
this.lightbox.currentIndex + i,
|
2014-02-17 15:09:23 +00:00
|
|
|
this.thumbs[ this.lightbox.currentIndex + i ].image
|
2014-02-13 09:52:40 +00:00
|
|
|
);
|
2014-02-07 14:47:00 +00:00
|
|
|
}
|
2014-02-13 09:52:40 +00:00
|
|
|
if ( i && this.lightbox.currentIndex - i >= 0 ) { // skip duplicate for i==0
|
|
|
|
callback(
|
|
|
|
this.lightbox.currentIndex - i,
|
2014-02-17 15:09:23 +00:00
|
|
|
this.thumbs[ this.lightbox.currentIndex - i ].image
|
2014-02-13 09:52:40 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A helper function to fill up the preload queues.
|
|
|
|
* taskFactory(lightboxImage) should return a preload task for the given lightboximage.
|
|
|
|
* @private
|
|
|
|
* @param {function(mw.LightboxImage): function()} taskFactory
|
|
|
|
* @return {mw.mmv.model.TaskQueue}
|
|
|
|
*/
|
|
|
|
MMVP.pushLightboxImagesIntoQueue = function( taskFactory ) {
|
|
|
|
var queue = new mw.mmv.model.TaskQueue();
|
|
|
|
|
|
|
|
this.eachPrealoadableLightboxIndex( function( i, lightboxImage ) {
|
|
|
|
queue.push( taskFactory( lightboxImage ) );
|
2013-11-25 23:51:40 +00:00
|
|
|
} );
|
|
|
|
|
2014-02-13 09:52:40 +00:00
|
|
|
return queue;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cancels in-progress image metadata preloading.
|
|
|
|
*/
|
|
|
|
MMVP.cancelImageMetadataPreloading = function() {
|
|
|
|
if ( this.metadataPreloadQueue ) {
|
|
|
|
this.metadataPreloadQueue.cancel();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cancels in-progress image thumbnail preloading.
|
|
|
|
*/
|
|
|
|
MMVP.cancelThumbnailsPreloading = function() {
|
|
|
|
if ( this.thumbnailPreloadQueue ) {
|
|
|
|
this.thumbnailPreloadQueue.cancel();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Preload metadata for next and prev N image (N = MMVP.preloadDistance).
|
|
|
|
* Two images will be loaded at a time (one forward, one backward), with closer images
|
|
|
|
* being loaded sooner.
|
|
|
|
*/
|
|
|
|
MMVP.preloadImagesMetadata = function() {
|
|
|
|
var viewer = this;
|
|
|
|
|
|
|
|
this.cancelImageMetadataPreloading();
|
|
|
|
|
|
|
|
this.metadataPreloadQueue = this.pushLightboxImagesIntoQueue( function( lightboxImage ) {
|
|
|
|
return function() {
|
|
|
|
return viewer.fetchSizeIndependentLightboxInfo( lightboxImage.filePageTitle );
|
|
|
|
};
|
|
|
|
} );
|
|
|
|
|
|
|
|
this.metadataPreloadQueue.execute();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Preload thumbnails for next and prev N image (N = MMVP.preloadDistance).
|
|
|
|
* Two images will be loaded at a time (one forward, one backward), with closer images
|
|
|
|
* being loaded sooner.
|
|
|
|
*/
|
|
|
|
MMVP.preloadThumbnails = function() {
|
|
|
|
var viewer = this,
|
|
|
|
ui = this.lightbox.iface;
|
|
|
|
|
|
|
|
this.cancelThumbnailsPreloading();
|
|
|
|
|
|
|
|
this.thumbnailPreloadQueue = this.pushLightboxImagesIntoQueue( function( lightboxImage ) {
|
|
|
|
return function() {
|
|
|
|
return viewer.fetchThumbnail(
|
|
|
|
lightboxImage.filePageTitle,
|
|
|
|
ui.getLightboxImageWidths( lightboxImage ).real
|
|
|
|
);
|
|
|
|
};
|
|
|
|
} );
|
|
|
|
|
|
|
|
this.thumbnailPreloadQueue.execute();
|
2013-11-25 23:51:40 +00:00
|
|
|
};
|
|
|
|
|
2014-02-20 08:43:10 +00:00
|
|
|
/**
|
|
|
|
* Preload the fullscreen size of the current image.
|
|
|
|
*/
|
2014-02-20 09:34:48 +00:00
|
|
|
MMVP.preloadFullscreenThumbnail = function( image ) {
|
2014-02-20 08:43:10 +00:00
|
|
|
this.fetchThumbnail(
|
2014-02-20 09:34:48 +00:00
|
|
|
image.filePageTitle,
|
|
|
|
this.lightbox.iface.getLightboxImageWidthsForFullscreen( image ).real
|
2014-02-20 08:43:10 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2014-01-23 00:38:01 +00:00
|
|
|
/**
|
|
|
|
* Animates the metadata area when the viewer is first opened.
|
|
|
|
* @return {jQuery.Promise} an empty promise which resolves when the animation is finished
|
|
|
|
*/
|
2014-01-07 19:58:29 +00:00
|
|
|
MMVP.animateMetadataDivOnce = function () {
|
|
|
|
if ( !this.hasAnimatedMetadata ) {
|
|
|
|
this.hasAnimatedMetadata = true;
|
2014-02-18 09:01:37 +00:00
|
|
|
$.scrollTo( 20, 300 )
|
|
|
|
.scrollTo( 0, 300 );
|
2014-01-07 19:58:29 +00:00
|
|
|
}
|
2014-01-23 00:38:01 +00:00
|
|
|
return $.scrollTo.window().promise();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stop listening to the page's scroll events
|
|
|
|
*/
|
|
|
|
MMVP.stopListeningToScroll = function () {
|
|
|
|
$.scrollTo().off( 'scroll.mmvp' );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start listening to the page's scroll events
|
|
|
|
* Will call MMVP.scroll(), throttled so it is not triggered on every pixel.
|
|
|
|
*/
|
|
|
|
MMVP.startListeningToScroll = function () {
|
|
|
|
var viewer = this;
|
|
|
|
|
|
|
|
$.scrollTo().on( 'scroll.mmvp', $.throttle( 250, function() { viewer.scroll(); } ) );
|
|
|
|
|
|
|
|
// Trigger a check in case the user scrolled manually during the animation
|
|
|
|
viewer.scroll();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Receives the window's scroll events and flips the chevron if necessary.
|
|
|
|
*/
|
|
|
|
MMVP.scroll = function () {
|
2014-02-18 09:01:37 +00:00
|
|
|
var scrolled = !!$.scrollTo().scrollTop();
|
|
|
|
|
|
|
|
this.ui.panel.$dragIcon.toggleClass( 'pointing-down', scrolled );
|
|
|
|
|
|
|
|
if ( !this.savedHasOpenedMetadata &&
|
|
|
|
scrolled &&
|
|
|
|
window.localStorage !== undefined ) {
|
|
|
|
localStorage.setItem( 'mmv.hasOpenedMetadata', true );
|
|
|
|
this.savedHasOpenedMetadata = true;
|
|
|
|
}
|
2013-12-31 00:19:20 +00:00
|
|
|
};
|
|
|
|
|
2014-01-06 20:02:39 +00:00
|
|
|
/**
|
2014-02-13 09:52:40 +00:00
|
|
|
* Loads all the size-independent information needed by the lightbox (image metadata, repo
|
|
|
|
* information, file usage, uploader data).
|
|
|
|
* @param {mw.Title} fileTitle Title of the file page for the image.
|
|
|
|
* @returns {jQuery.Promise.<mw.mmv.model.Image, mw.mmv.model.Repo, mw.mmv.model.FileUsage,
|
|
|
|
* mw.mmv.model.FileUsage, mw.mmv.model.User>}
|
2014-01-06 20:02:39 +00:00
|
|
|
*/
|
2014-02-13 09:52:40 +00:00
|
|
|
MMVP.fetchSizeIndependentLightboxInfo = function ( fileTitle ) {
|
|
|
|
var viewer = this,
|
|
|
|
imageInfoPromise = this.imageInfoProvider.get( fileTitle ),
|
|
|
|
repoInfoPromise = this.fileRepoInfoProvider.get( fileTitle ),
|
|
|
|
imageUsagePromise = this.imageUsageProvider.get( fileTitle ),
|
|
|
|
globalUsagePromise = this.globalUsageProvider.get( fileTitle ),
|
|
|
|
userInfoPromise;
|
|
|
|
|
|
|
|
userInfoPromise = $.when(
|
|
|
|
imageInfoPromise, repoInfoPromise
|
|
|
|
).then( function( imageInfo, repoInfoHash ) {
|
|
|
|
if ( imageInfo.lastUploader ) {
|
|
|
|
return viewer.userInfoProvider.get( imageInfo.lastUploader, repoInfoHash[imageInfo.repo] );
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
2014-02-03 20:42:17 +00:00
|
|
|
return $.when(
|
2014-02-13 09:52:40 +00:00
|
|
|
imageInfoPromise, repoInfoPromise, imageUsagePromise, globalUsagePromise, userInfoPromise
|
|
|
|
).then( function( imageInfo, repoInfoHash, imageUsage, globalUsage, userInfo ) {
|
|
|
|
return $.Deferred().resolve( imageInfo, repoInfoHash[imageInfo.repo], imageUsage, globalUsage, userInfo );
|
2014-02-03 20:42:17 +00:00
|
|
|
} );
|
2013-09-26 01:47:59 +00:00
|
|
|
};
|
|
|
|
|
2014-01-30 11:28:43 +00:00
|
|
|
/**
|
2014-02-13 09:52:40 +00:00
|
|
|
* Loads size-dependent components of a lightbox - the thumbnail model and the image itself.
|
|
|
|
* @param {mw.Title} fileTitle
|
|
|
|
* @param {number} width
|
|
|
|
* @returns {jQuery.Promise.<mw.mmv.model.Thumbnail, HTMLImageElement>}
|
2014-01-30 11:28:43 +00:00
|
|
|
*/
|
2014-02-13 09:52:40 +00:00
|
|
|
MMVP.fetchThumbnail = function ( fileTitle, width ) {
|
|
|
|
var viewer = this,
|
|
|
|
thumbnailPromise,
|
|
|
|
imagePromise;
|
|
|
|
|
|
|
|
thumbnailPromise = this.thumbnailInfoProvider.get( fileTitle, width );
|
|
|
|
imagePromise = thumbnailPromise.then( function( thumbnail ) {
|
|
|
|
return viewer.imageProvider.get( thumbnail.url );
|
2014-01-30 11:28:43 +00:00
|
|
|
} );
|
2014-02-13 09:52:40 +00:00
|
|
|
|
|
|
|
return $.when( thumbnailPromise, imagePromise );
|
2014-01-30 11:28:43 +00:00
|
|
|
};
|
|
|
|
|
2014-02-19 02:27:30 +00:00
|
|
|
/**
|
|
|
|
* Loads an image at a specified index in the viewer's thumbnail array.
|
|
|
|
* @param {number} index
|
|
|
|
*/
|
2013-11-26 03:37:01 +00:00
|
|
|
MMVP.loadIndex = function ( index ) {
|
2014-02-17 15:09:23 +00:00
|
|
|
var thumb;
|
|
|
|
|
|
|
|
if ( index < this.thumbs.length && index >= 0 ) {
|
|
|
|
thumb = this.thumbs[ index ];
|
|
|
|
this.loadImage( thumb.image, thumb.$thumb.clone()[0] );
|
2013-11-26 03:37:01 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-02-11 13:25:39 +00:00
|
|
|
/**
|
|
|
|
* Opens the next image
|
|
|
|
*/
|
2013-11-26 03:37:01 +00:00
|
|
|
MMVP.nextImage = function () {
|
|
|
|
this.loadIndex( this.lightbox.currentIndex + 1 );
|
|
|
|
};
|
|
|
|
|
2014-02-11 13:25:39 +00:00
|
|
|
/**
|
|
|
|
* Opens the previous image
|
|
|
|
*/
|
2013-11-26 03:37:01 +00:00
|
|
|
MMVP.prevImage = function () {
|
|
|
|
this.loadIndex( this.lightbox.currentIndex - 1 );
|
|
|
|
};
|
|
|
|
|
2014-02-11 13:25:39 +00:00
|
|
|
/**
|
|
|
|
* Handles close event coming from the lightbox
|
|
|
|
*/
|
|
|
|
MMVP.close = function () {
|
|
|
|
$( document.body ).removeClass( 'mw-mlb-lightbox-open' );
|
2014-02-24 09:29:14 +00:00
|
|
|
if ( comingFromHashChange === false ) {
|
|
|
|
$( document ).trigger( $.Event( 'mmv.hash', { hash : '#' } ) );
|
2014-02-11 13:25:39 +00:00
|
|
|
} else {
|
2014-02-24 09:29:14 +00:00
|
|
|
comingFromHashChange = false;
|
2014-02-11 13:25:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.isOpen = false;
|
|
|
|
};
|
|
|
|
|
2014-02-17 15:09:23 +00:00
|
|
|
/**
|
2014-02-21 11:16:30 +00:00
|
|
|
* Handles a hash change coming from the browser
|
2014-02-17 15:09:23 +00:00
|
|
|
*/
|
2014-02-21 11:16:30 +00:00
|
|
|
MMVP.hash = function () {
|
2014-02-24 09:29:14 +00:00
|
|
|
var hash = decodeURIComponent( window.location.hash ),
|
2014-02-21 11:16:30 +00:00
|
|
|
linkState = hash.split( '/' );
|
|
|
|
|
|
|
|
if ( linkState[0] === '#mediaviewer' ) {
|
|
|
|
this.loadImageByTitle( linkState[ 1 ] );
|
|
|
|
} else if ( this.isOpen ) {
|
2014-02-24 09:29:14 +00:00
|
|
|
// This allows us to avoid the mmv.hash event that normally happens on close
|
|
|
|
comingFromHashChange = true;
|
2014-02-21 11:16:30 +00:00
|
|
|
|
|
|
|
if ( this.lightbox && this.lightbox.iface ) {
|
|
|
|
this.lightbox.iface.unattach();
|
|
|
|
} else {
|
|
|
|
this.close();
|
|
|
|
}
|
2014-02-17 15:09:23 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-02-19 02:27:30 +00:00
|
|
|
/**
|
2014-02-21 11:16:30 +00:00
|
|
|
* Registers all event handlers
|
2014-02-19 02:27:30 +00:00
|
|
|
*/
|
2014-02-11 13:25:39 +00:00
|
|
|
MMVP.setupEventHandlers = function () {
|
2014-02-11 01:12:31 +00:00
|
|
|
var viewer = this;
|
|
|
|
|
2014-02-21 11:16:30 +00:00
|
|
|
$( document ).on( 'mmv-close.mmvp', function () {
|
2014-02-11 13:25:39 +00:00
|
|
|
viewer.close();
|
2014-02-21 11:16:30 +00:00
|
|
|
} ).on( 'mmv-next.mmvp', function () {
|
2014-02-11 01:12:31 +00:00
|
|
|
viewer.nextImage();
|
2014-02-21 11:16:30 +00:00
|
|
|
} ).on( 'mmv-prev.mmvp', function () {
|
2014-02-11 01:12:31 +00:00
|
|
|
viewer.prevImage();
|
2014-02-21 11:16:30 +00:00
|
|
|
} ).on( 'mmv-resize.mmvp', function () {
|
2014-02-11 13:25:39 +00:00
|
|
|
viewer.resize( viewer.lightbox.iface );
|
|
|
|
});
|
2014-02-11 01:12:31 +00:00
|
|
|
};
|
|
|
|
|
2013-08-07 08:59:08 +00:00
|
|
|
mw.MultimediaViewer = MultimediaViewer;
|
2014-02-17 15:09:23 +00:00
|
|
|
mw.mediaViewer = new mw.MultimediaViewer();
|
2014-02-07 14:47:00 +00:00
|
|
|
}( mediaWiki, jQuery ) );
|