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/>.
|
|
|
|
*/
|
|
|
|
|
2023-05-20 06:42:20 +00:00
|
|
|
const { getMediaHash } = require( 'mmv.head' );
|
2023-05-19 20:26:45 +00:00
|
|
|
const ViewLogger = require( './logging/mmv.logging.ViewLogger.js' );
|
2023-05-20 08:30:52 +00:00
|
|
|
const {
|
|
|
|
Api,
|
|
|
|
FileRepoInfo,
|
|
|
|
GuessedThumbnailInfo,
|
|
|
|
ImageInfo,
|
|
|
|
ImageProvider,
|
|
|
|
ThumbnailInfo
|
|
|
|
} = require( './provider/mmv.provider.js' );
|
|
|
|
const {
|
|
|
|
ImageModel,
|
|
|
|
IwTitle,
|
|
|
|
License,
|
|
|
|
Repo,
|
|
|
|
ForeignApiRepo,
|
|
|
|
ForeignDbRepo,
|
|
|
|
TaskQueue,
|
|
|
|
Thumbnail,
|
|
|
|
ThumbnailWidth
|
|
|
|
} = require( './model/mmv.model.js' );
|
|
|
|
const {
|
|
|
|
Canvas,
|
|
|
|
CanvasButtons,
|
|
|
|
Description,
|
|
|
|
Dialog,
|
|
|
|
DownloadDialog,
|
|
|
|
UiElement,
|
|
|
|
MetadataPanel,
|
|
|
|
MetadataPanelScroller,
|
|
|
|
Permission,
|
|
|
|
ProgressBar,
|
|
|
|
ReuseDialog,
|
|
|
|
StripeButtons,
|
|
|
|
TruncatableTextField,
|
|
|
|
OptionsDialog
|
|
|
|
} = require( './ui/index.js' );
|
2023-05-19 20:26:45 +00:00
|
|
|
const LightboxInterface = require( './mmv.lightboxinterface.js' );
|
|
|
|
const ThumbnailWidthCalculator = require( './mmv.ThumbnailWidthCalculator.js' );
|
|
|
|
|
2018-11-12 16:33:24 +00:00
|
|
|
( function () {
|
2023-05-20 12:38:59 +00:00
|
|
|
const router = require( 'mediawiki.router' );
|
|
|
|
let 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.
|
|
|
|
*/
|
2023-05-20 12:38:59 +00:00
|
|
|
class MultimediaViewer {
|
2014-02-03 20:42:17 +00:00
|
|
|
/**
|
2023-05-20 12:38:59 +00:00
|
|
|
* @param {Config} config Config object
|
2014-02-03 20:42:17 +00:00
|
|
|
*/
|
2023-05-20 12:38:59 +00:00
|
|
|
constructor( config ) {
|
|
|
|
const apiCacheMaxAge = 86400; // one day (24 hours * 60 min * 60 sec)
|
|
|
|
const apiCacheFiveMinutes = 300; // 5 min * 60 sec
|
|
|
|
const api = new mw.Api();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {Config}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this.config = config;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {ImageProvider}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this.imageProvider = new ImageProvider( this.config.imageQueryParameter() );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {ImageInfo}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this.imageInfoProvider = new ImageInfo( api, {
|
|
|
|
language: this.config.language(),
|
|
|
|
maxage: apiCacheFiveMinutes
|
|
|
|
} );
|
2014-02-03 20:42:17 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
/**
|
|
|
|
* @property {FileRepoInfo}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this.fileRepoInfoProvider = new FileRepoInfo( api,
|
|
|
|
{ maxage: apiCacheMaxAge } );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {ThumbnailInfo}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this.thumbnailInfoProvider = new ThumbnailInfo( api,
|
|
|
|
{ maxage: apiCacheMaxAge } );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {ThumbnailInfo}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this.guessedThumbnailInfoProvider = new GuessedThumbnailInfo();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Image index on page.
|
|
|
|
*
|
|
|
|
* @property {number}
|
|
|
|
*/
|
|
|
|
this.currentIndex = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {OO.Router} router
|
|
|
|
*/
|
|
|
|
this.router = router;
|
|
|
|
this.setupRouter();
|
|
|
|
comingFromHashChange = false;
|
2014-02-03 20:42:17 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
/**
|
|
|
|
* UI object used to display the pictures in the page.
|
|
|
|
*
|
|
|
|
* @property {LightboxInterface}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this.ui = new LightboxInterface();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* How many sharp images have been displayed in Media Viewer since the pageload
|
|
|
|
*
|
|
|
|
* @property {number}
|
|
|
|
*/
|
|
|
|
this.imageDisplayedCount = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* How many data-filled metadata panels have been displayed in Media Viewer since the pageload
|
|
|
|
*
|
|
|
|
* @property {number}
|
|
|
|
*/
|
|
|
|
this.metadataDisplayedCount = 0;
|
|
|
|
|
|
|
|
/** @property {string} documentTitle base document title, MediaViewer will expand this */
|
|
|
|
this.documentTitle = document.title;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {ViewLogger} view -
|
|
|
|
*/
|
|
|
|
this.viewLogger = new ViewLogger( this.config, window );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stores whether the real image was loaded and displayed already.
|
|
|
|
* This is reset when paging, so it is not necessarily accurate.
|
|
|
|
*
|
|
|
|
* @property {boolean}
|
|
|
|
*/
|
|
|
|
this.realThumbnailShown = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stores whether the a blurred placeholder is being displayed in place of the real image.
|
|
|
|
* When a placeholder is displayed, but it is not blurred, this is false.
|
|
|
|
* This is reset when paging, so it is not necessarily accurate.
|
|
|
|
*
|
|
|
|
* @property {boolean}
|
|
|
|
*/
|
|
|
|
this.blurredThumbnailShown = false;
|
|
|
|
}
|
2014-04-10 01:08:36 +00:00
|
|
|
|
2014-03-05 02:24:18 +00:00
|
|
|
/**
|
2023-05-20 12:38:59 +00:00
|
|
|
* Initialize the lightbox interface given an array of thumbnail
|
|
|
|
* objects.
|
2020-06-26 10:21:04 +00:00
|
|
|
*
|
2024-05-21 18:24:39 +00:00
|
|
|
* @param {LightboxImage[]} thumbs
|
2014-04-14 23:22:28 +00:00
|
|
|
*/
|
2023-05-20 12:38:59 +00:00
|
|
|
initWithThumbs( thumbs ) {
|
|
|
|
this.thumbs = thumbs;
|
|
|
|
}
|
2014-04-28 16:11:55 +00:00
|
|
|
|
|
|
|
/**
|
2023-05-20 12:38:59 +00:00
|
|
|
* Handles resize events in viewer.
|
2020-06-26 10:21:04 +00:00
|
|
|
*
|
2023-05-20 12:38:59 +00:00
|
|
|
* @protected
|
|
|
|
* @param {LightboxInterface} ui lightbox that got resized
|
2014-04-28 16:11:55 +00:00
|
|
|
*/
|
2023-05-20 12:38:59 +00:00
|
|
|
resize( ui ) {
|
2024-05-21 18:24:39 +00:00
|
|
|
const image = this.thumbs[ this.currentIndex ];
|
|
|
|
const ext = this.thumbs[ this.currentIndex ].filePageTitle.getExtension().toLowerCase();
|
2023-05-20 12:38:59 +00:00
|
|
|
|
|
|
|
this.preloadThumbnails();
|
|
|
|
|
|
|
|
if ( image ) {
|
2023-10-23 19:28:57 +00:00
|
|
|
const imageWidths = ui.canvas.getCurrentImageWidths();
|
2023-05-20 12:38:59 +00:00
|
|
|
|
|
|
|
this.fetchThumbnailForLightboxImage(
|
|
|
|
image, imageWidths.real
|
|
|
|
).then( ( thumbnail, image2 ) => {
|
|
|
|
// eslint-disable-next-line mediawiki/class-doc
|
|
|
|
image2.className = ext;
|
|
|
|
this.setImage( ui, thumbnail, image2, imageWidths );
|
|
|
|
}, ( error ) => {
|
|
|
|
this.ui.canvas.showError( error );
|
|
|
|
} );
|
|
|
|
}
|
2014-07-01 22:22:06 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
this.updateControls();
|
|
|
|
}
|
2014-11-15 15:32:04 +00:00
|
|
|
|
|
|
|
/**
|
2023-05-20 12:38:59 +00:00
|
|
|
* Updates positioning of controls, usually after a resize event.
|
2014-11-15 15:32:04 +00:00
|
|
|
*/
|
2023-05-20 12:38:59 +00:00
|
|
|
updateControls() {
|
|
|
|
const numImages = this.thumbs ? this.thumbs.length : 0;
|
|
|
|
const showNextButton = this.currentIndex < ( numImages - 1 );
|
|
|
|
const showPreviousButton = this.currentIndex > 0;
|
|
|
|
|
|
|
|
this.ui.updateControls( showNextButton, showPreviousButton );
|
|
|
|
}
|
2018-06-06 18:23:25 +00:00
|
|
|
|
|
|
|
/**
|
2023-05-20 12:38:59 +00:00
|
|
|
* Loads and sets the specified image. It also updates the controls.
|
2020-06-26 10:21:04 +00:00
|
|
|
*
|
2023-05-20 12:38:59 +00:00
|
|
|
* @param {LightboxInterface} ui image container
|
|
|
|
* @param {Thumbnail} thumbnail thumbnail information
|
|
|
|
* @param {HTMLImageElement} imageElement
|
|
|
|
* @param {ThumbnailWidth} imageWidths
|
2018-06-06 18:23:25 +00:00
|
|
|
*/
|
2023-05-20 12:38:59 +00:00
|
|
|
setImage( ui, thumbnail, imageElement, imageWidths ) {
|
|
|
|
ui.canvas.setImageAndMaxDimensions( thumbnail, imageElement, imageWidths );
|
|
|
|
this.updateControls();
|
|
|
|
}
|
2018-06-06 18:23:25 +00:00
|
|
|
|
|
|
|
/**
|
2023-05-20 12:38:59 +00:00
|
|
|
* Loads a specified image.
|
2020-06-26 10:21:04 +00:00
|
|
|
*
|
2023-05-20 12:38:59 +00:00
|
|
|
* @param {LightboxImage} image
|
2018-06-06 18:23:25 +00:00
|
|
|
*/
|
2024-05-21 18:24:39 +00:00
|
|
|
loadImage( image ) {
|
|
|
|
const $initialImage = $( image.thumbnail ).clone();
|
2013-08-07 08:59:08 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
const pluginsPromise = this.loadExtensionPlugins( image.filePageTitle.getExtension().toLowerCase() );
|
2013-08-07 08:59:08 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
this.currentIndex = image.index;
|
2014-12-02 10:05:12 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
this.currentImageFileTitle = image.filePageTitle;
|
2013-12-02 19:46:52 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
if ( !this.isOpen ) {
|
|
|
|
$( document ).trigger( $.Event( 'mmv-setup-overlay' ) );
|
|
|
|
this.ui.open();
|
|
|
|
this.isOpen = true;
|
|
|
|
} else {
|
|
|
|
this.ui.empty();
|
|
|
|
}
|
2014-01-06 21:53:42 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
this.setTitle();
|
2013-12-03 01:58:09 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
// At this point we can't show the thumbnail because we don't
|
|
|
|
// know what size it should be. We still assign it to allow for
|
|
|
|
// size calculations in getCurrentImageWidths, which needs to know
|
|
|
|
// the aspect ratio
|
|
|
|
// eslint-disable-next-line mediawiki/class-doc
|
|
|
|
$initialImage.hide()
|
|
|
|
.removeAttr( 'style' )
|
|
|
|
.removeClass()
|
2024-02-13 00:44:51 +00:00
|
|
|
.addClass( `mw-mmv-placeholder-image ${ image.filePageTitle.getExtension().toLowerCase() }` );
|
2013-11-13 00:43:46 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
this.ui.canvas.set( image, $initialImage );
|
2013-11-06 00:36:06 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
this.preloadImagesMetadata();
|
|
|
|
this.preloadThumbnails();
|
|
|
|
// this.preloadFullscreenThumbnail( image ); // disabled - #474
|
|
|
|
const imageWidths = this.ui.canvas.getCurrentImageWidths();
|
2014-01-28 23:57:05 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
const start = Date.now();
|
2019-05-16 13:19:44 +00:00
|
|
|
|
2024-05-15 18:59:58 +00:00
|
|
|
const imagePromise = this.fetchThumbnailForLightboxImage( image, imageWidths.real );
|
2014-02-25 13:43:22 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
this.resetBlurredThumbnailStates();
|
|
|
|
if ( imagePromise.state() === 'pending' ) {
|
|
|
|
this.displayPlaceholderThumbnail( image, $initialImage, imageWidths );
|
|
|
|
}
|
2016-04-03 09:18:26 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
this.setupProgressBar( image, imagePromise, imageWidths.real );
|
2014-02-13 09:52:40 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
const metadataPromise = this.fetchSizeIndependentLightboxInfo( image.filePageTitle );
|
2014-01-06 21:53:42 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
imagePromise.then(
|
|
|
|
// done
|
|
|
|
( thumbnail, imageElement ) => {
|
|
|
|
if ( this.currentIndex !== image.index ) {
|
|
|
|
return;
|
|
|
|
}
|
2014-02-25 13:43:22 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
// eslint-disable-next-line mediawiki/class-doc
|
2024-02-13 00:44:51 +00:00
|
|
|
imageElement.className = `mw-mmv-final-image ${ image.filePageTitle.getExtension().toLowerCase() }`;
|
2023-05-20 12:38:59 +00:00
|
|
|
imageElement.alt = image.alt;
|
2014-02-25 13:43:22 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
$.when( metadataPromise, pluginsPromise ).then( ( metadata ) => {
|
|
|
|
$( document ).trigger( $.Event( 'mmv-metadata', { viewer: this, image: image, imageInfo: metadata[ 0 ] } ) );
|
|
|
|
} );
|
2014-02-27 00:40:39 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
this.displayRealThumbnail( thumbnail, imageElement, imageWidths, Date.now() - start );
|
2014-02-27 00:40:39 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
return $.Deferred().resolve( thumbnail, imageElement );
|
|
|
|
},
|
|
|
|
// fail
|
|
|
|
( error ) => {
|
|
|
|
this.ui.canvas.showError( error );
|
|
|
|
return $.Deferred().reject( error );
|
|
|
|
}
|
|
|
|
);
|
2014-04-01 07:56:18 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
metadataPromise.then(
|
|
|
|
// done
|
|
|
|
( imageInfo, repoInfo ) => {
|
|
|
|
if ( this.currentIndex !== image.index ) {
|
|
|
|
return;
|
|
|
|
}
|
2014-02-27 00:40:39 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
this.ui.panel.setImageInfo( image, imageInfo, repoInfo );
|
2017-05-11 15:03:52 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
// File reuse steals a bunch of information from the DOM, so do it last
|
|
|
|
this.ui.setFileReuseData( imageInfo, repoInfo, image.caption, image.alt );
|
2014-12-30 05:10:27 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
return $.Deferred().resolve( imageInfo, repoInfo );
|
|
|
|
},
|
|
|
|
// fail
|
|
|
|
( error ) => {
|
|
|
|
if ( this.currentIndex === image.index ) {
|
|
|
|
// Set title to caption or file name if caption is not available;
|
|
|
|
// see setTitle() in mmv.ui.metadataPanel for extended caption fallback
|
|
|
|
this.ui.panel.showError( image.caption || image.filePageTitle.getNameText(), error );
|
|
|
|
}
|
2016-04-03 09:18:26 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
return $.Deferred().reject( error );
|
|
|
|
}
|
|
|
|
);
|
2014-02-25 13:43:22 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
$.when( imagePromise, metadataPromise ).then( () => {
|
|
|
|
if ( this.currentIndex !== image.index ) {
|
2017-05-11 15:03:52 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-12-02 10:05:12 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
this.ui.panel.scroller.animateMetadataOnce();
|
|
|
|
this.preloadDependencies();
|
|
|
|
} );
|
|
|
|
}
|
2014-11-05 10:40:31 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
/**
|
|
|
|
* Loads an image by its title
|
|
|
|
*
|
|
|
|
* @param {mw.Title} title
|
2024-05-21 18:24:39 +00:00
|
|
|
* @param {number} [position]
|
2023-05-20 12:38:59 +00:00
|
|
|
*/
|
2024-05-21 18:24:39 +00:00
|
|
|
loadImageByTitle( title, position ) {
|
2023-05-20 12:38:59 +00:00
|
|
|
if ( !this.thumbs || !this.thumbs.length ) {
|
|
|
|
return;
|
|
|
|
}
|
2014-12-02 10:05:12 +00:00
|
|
|
|
2024-05-21 18:24:39 +00:00
|
|
|
const thumb = this.thumbs.find( ( t ) =>
|
|
|
|
t.filePageTitle.getPrefixedText() === title.getPrefixedText() &&
|
|
|
|
( !position || t.position === position )
|
|
|
|
);
|
2023-05-20 20:20:49 +00:00
|
|
|
|
|
|
|
if ( !thumb ) {
|
|
|
|
this.onTitleNotFound( title );
|
|
|
|
return;
|
2017-05-11 15:03:52 +00:00
|
|
|
}
|
2023-05-20 20:20:49 +00:00
|
|
|
|
2024-05-21 18:24:39 +00:00
|
|
|
this.loadImage( thumb );
|
2023-05-20 20:20:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* When the image to load is not present on the current page,
|
|
|
|
* a notification is shown to the user and the MMV is closed.
|
|
|
|
*
|
|
|
|
* @param {mw.Title} title
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
onTitleNotFound( title ) {
|
|
|
|
this.close();
|
|
|
|
const text = mw.message( 'multimediaviewer-file-not-found-error', title.getMainText() ).text();
|
|
|
|
const $link = $( '<a>' ).text( mw.message( 'multimediaviewer-file-page' ).text() ).prop( 'href', title.getUrl() );
|
|
|
|
const $message = $( '<div>' ).text( text ).append( $( '<br>' ) ).append( $link );
|
|
|
|
mw.notify( $message );
|
2023-05-20 12:38:59 +00:00
|
|
|
}
|
2014-01-28 23:57:05 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
/**
|
|
|
|
* Resets the cross-request states needed to handle the blurred thumbnail logic.
|
|
|
|
*/
|
|
|
|
resetBlurredThumbnailStates() {
|
|
|
|
this.realThumbnailShown = false;
|
|
|
|
this.blurredThumbnailShown = false;
|
|
|
|
}
|
2014-02-27 00:40:39 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
/**
|
|
|
|
* Display the real, full-resolution, thumbnail that was fetched with fetchThumbnail
|
|
|
|
*
|
|
|
|
* @param {Thumbnail} thumbnail
|
|
|
|
* @param {HTMLImageElement} imageElement
|
|
|
|
* @param {ThumbnailWidth} imageWidths
|
|
|
|
* @param {number} loadTime Time it took to load the thumbnail
|
|
|
|
*/
|
|
|
|
displayRealThumbnail( thumbnail, imageElement, imageWidths, loadTime ) {
|
|
|
|
this.realThumbnailShown = true;
|
2014-02-17 15:09:23 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
this.setImage( this.ui, thumbnail, imageElement, imageWidths );
|
|
|
|
|
|
|
|
// We only animate unblurWithAnimation if the image wasn't loaded from the cache
|
|
|
|
// A load in < 100ms is fast enough (maybe even browser cache hit) that
|
|
|
|
// using a 300ms animation would needlessly deter from a fast experience.
|
|
|
|
if ( this.blurredThumbnailShown && loadTime > 100 ) {
|
|
|
|
this.ui.canvas.unblurWithAnimation();
|
|
|
|
} else {
|
|
|
|
this.ui.canvas.unblur();
|
|
|
|
}
|
2014-02-17 15:09:23 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
this.viewLogger.attach( thumbnail.url );
|
2014-02-17 15:09:23 +00:00
|
|
|
}
|
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
/**
|
|
|
|
* Display the blurred thumbnail from the page
|
|
|
|
*
|
|
|
|
* @param {LightboxImage} image
|
|
|
|
* @param {jQuery} $initialImage The thumbnail from the page
|
|
|
|
* @param {ThumbnailWidth} imageWidths
|
|
|
|
* @param {boolean} [recursion=false] for internal use, never set this when calling from outside
|
|
|
|
*/
|
|
|
|
displayPlaceholderThumbnail( image, $initialImage, imageWidths, recursion ) {
|
|
|
|
const size = { width: image.originalWidth, height: image.originalHeight };
|
|
|
|
|
|
|
|
// If the actual image has already been displayed, there's no point showing the blurry one.
|
|
|
|
// This can happen if the API request to get the original image size needed to show the
|
|
|
|
// placeholder thumbnail takes longer then loading the actual thumbnail.
|
|
|
|
if ( this.realThumbnailShown ) {
|
2018-09-13 20:09:43 +00:00
|
|
|
return;
|
2014-02-17 15:09:23 +00:00
|
|
|
}
|
2023-05-20 12:38:59 +00:00
|
|
|
|
|
|
|
// Width/height of the original image are added to the HTML by MediaViewer via a PHP hook,
|
|
|
|
// and can be missing in exotic circumstances, e. g. when the extension has only been
|
|
|
|
// enabled recently and the HTML cache has not cleared yet. If that is the case, we need
|
|
|
|
// to fetch the size from the API first.
|
|
|
|
if ( !size.width || !size.height ) {
|
|
|
|
if ( recursion ) {
|
|
|
|
// this should not be possible, but an infinite recursion is nasty
|
|
|
|
// business, so we make a sense check
|
|
|
|
throw new Error( 'MediaViewer internal error: displayPlaceholderThumbnail recursion' );
|
|
|
|
}
|
|
|
|
this.imageInfoProvider.get( image.filePageTitle ).done( ( imageInfo ) => {
|
|
|
|
// Make sure the user has not navigated away while we were waiting for the size
|
|
|
|
if ( this.currentIndex === image.index ) {
|
|
|
|
image.originalWidth = imageInfo.width;
|
|
|
|
image.originalHeight = imageInfo.height;
|
|
|
|
this.displayPlaceholderThumbnail( image, $initialImage, imageWidths, true );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
} else {
|
|
|
|
this.blurredThumbnailShown = this.ui.canvas.maybeDisplayPlaceholder(
|
|
|
|
size, $initialImage, imageWidths );
|
|
|
|
}
|
2018-09-13 20:09:43 +00:00
|
|
|
}
|
2013-12-10 21:04:45 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
/**
|
|
|
|
* Displays a progress bar for the image loading, if necessary, and sets up handling of
|
|
|
|
* all the related callbacks.
|
|
|
|
*
|
|
|
|
* @param {LightboxImage} image
|
|
|
|
* @param {jQuery.Promise.<Thumbnail, HTMLImageElement>} imagePromise
|
|
|
|
* @param {number} imageWidth needed for caching progress (FIXME)
|
|
|
|
*/
|
|
|
|
setupProgressBar( image, imagePromise, imageWidth ) {
|
|
|
|
const progressBar = this.ui.panel.progressBar;
|
2024-02-13 00:44:51 +00:00
|
|
|
const key = `${ image.filePageTitle.getPrefixedDb() }|${ imageWidth }`;
|
2023-05-20 12:38:59 +00:00
|
|
|
|
|
|
|
if ( !this.progressCache[ key ] ) {
|
|
|
|
// Animate progress bar to 5 to give a sense that something is happening, and make sure
|
|
|
|
// the progress bar is noticeable, even if we're sitting at 0% stuck waiting for
|
|
|
|
// server-side processing, such as thumbnail (re)generation
|
|
|
|
progressBar.jumpTo( 0 );
|
|
|
|
progressBar.animateTo( 5 );
|
|
|
|
this.progressCache[ key ] = 5;
|
|
|
|
} else {
|
|
|
|
progressBar.jumpTo( this.progressCache[ key ] );
|
|
|
|
}
|
Refactor progressbar & blur handling
This tries to fix a number of related issues:
* the blurred thumbnail was visible for a split-second sometimes
when switching back to an already-loaded image. (Presumably when
JS was sluggish enough to take more than 10 ms to execute.) We
now check whether the promise is pending before showing a placeholder.
(More generally, a lot of unnecessary logic was executed when paging
through already loaded images, like displaying the placeholder, so
this might make the UI a bit more responsive.)
* the blur could get stuck sometimes - I have seen this a few times,
but have never been able to reproduce it, so I'm only guessing, but
maybe the timing was really unfortunate, and we switched back less
than 10 ms before loading finished. We now remove the blur on every
branch, just to be sure.
* adding a progress handler to a promise might not have any immediate
effect, so when switching to an image which was loading, the progress
bar reacted too late. We now store the progress state per thumbnail
so it is always available immediately.
* the progress would animate from 0 to its actual state whenever we
navigated to the image. The change on paging is now instant; the
progress bar only animates when we are looking at it.
* switching quickly back and forthe between a loaded and a loading
image resulted in the loading image becoming unblurred. This seems
fixed now, I'm not sure why. Maybe the "skip on non-pending promise"
logic affects it somehow.
Also removes some unused things / renames some things which were
confusing, and makes an unrelated fix in the image provider, which kept
amassing fail handlers.
Change-Id: I580becff246f197ec1bc65e82acd422620e35578
Mingle: https://wikimedia.mingle.thoughtworks.com/projects/multimedia/cards/489
2014-04-25 22:26:34 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
// FIXME would be nice to have a "filtered" promise which does not fire when the image is not visible
|
|
|
|
imagePromise.then(
|
|
|
|
// done
|
|
|
|
( thumbnail, imageElement ) => {
|
|
|
|
this.progressCache[ key ] = 100;
|
|
|
|
if ( this.currentIndex === image.index ) {
|
|
|
|
// Fallback in case the browser doesn't have fancy progress updates
|
|
|
|
progressBar.animateTo( 100 );
|
|
|
|
|
|
|
|
// Hide progress bar, we're done
|
|
|
|
progressBar.hide();
|
|
|
|
}
|
2014-02-25 13:43:22 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
return $.Deferred().resolve( thumbnail, imageElement );
|
|
|
|
},
|
|
|
|
// fail
|
|
|
|
( error ) => {
|
|
|
|
this.progressCache[ key ] = 100;
|
2014-02-28 10:44:26 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
if ( this.currentIndex === image.index ) {
|
|
|
|
// Hide progress bar on error
|
|
|
|
progressBar.hide();
|
|
|
|
}
|
2014-02-25 13:43:22 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
return $.Deferred().reject( error );
|
|
|
|
},
|
|
|
|
// progress
|
|
|
|
( progress ) => {
|
|
|
|
// We pretend progress is always at least 5%, so progress events below 5% should be ignored
|
|
|
|
// 100 will be handled by the done handler, do not mix two animations
|
|
|
|
if ( progress >= 5 && progress < 100 ) {
|
|
|
|
this.progressCache[ key ] = progress;
|
|
|
|
|
|
|
|
// Touch the UI only if the user is looking at this image
|
|
|
|
if ( this.currentIndex === image.index ) {
|
|
|
|
progressBar.animateTo( progress );
|
|
|
|
}
|
|
|
|
}
|
2014-02-25 13:43:22 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
return progress;
|
2014-04-24 01:03:24 +00:00
|
|
|
}
|
2023-05-20 12:38:59 +00:00
|
|
|
);
|
2014-04-24 01:03:24 +00:00
|
|
|
}
|
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
/**
|
|
|
|
* 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, LightboxImage)} callback
|
|
|
|
*/
|
|
|
|
eachPreloadableLightboxIndex( callback ) {
|
2023-10-23 19:28:57 +00:00
|
|
|
for ( let i = 0; i <= this.preloadDistance; i++ ) {
|
2023-05-20 12:38:59 +00:00
|
|
|
if ( this.currentIndex + i < this.thumbs.length ) {
|
|
|
|
callback(
|
|
|
|
this.currentIndex + i,
|
2024-05-21 18:24:39 +00:00
|
|
|
this.thumbs[ this.currentIndex + i ]
|
2023-05-20 12:38:59 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
if ( i && this.currentIndex - i >= 0 ) { // skip duplicate for i==0
|
|
|
|
callback(
|
|
|
|
this.currentIndex - i,
|
2024-05-21 18:24:39 +00:00
|
|
|
this.thumbs[ this.currentIndex - i ]
|
2023-05-20 12:38:59 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
Refactor progressbar & blur handling
This tries to fix a number of related issues:
* the blurred thumbnail was visible for a split-second sometimes
when switching back to an already-loaded image. (Presumably when
JS was sluggish enough to take more than 10 ms to execute.) We
now check whether the promise is pending before showing a placeholder.
(More generally, a lot of unnecessary logic was executed when paging
through already loaded images, like displaying the placeholder, so
this might make the UI a bit more responsive.)
* the blur could get stuck sometimes - I have seen this a few times,
but have never been able to reproduce it, so I'm only guessing, but
maybe the timing was really unfortunate, and we switched back less
than 10 ms before loading finished. We now remove the blur on every
branch, just to be sure.
* adding a progress handler to a promise might not have any immediate
effect, so when switching to an image which was loading, the progress
bar reacted too late. We now store the progress state per thumbnail
so it is always available immediately.
* the progress would animate from 0 to its actual state whenever we
navigated to the image. The change on paging is now instant; the
progress bar only animates when we are looking at it.
* switching quickly back and forthe between a loaded and a loading
image resulted in the loading image becoming unblurred. This seems
fixed now, I'm not sure why. Maybe the "skip on non-pending promise"
logic affects it somehow.
Also removes some unused things / renames some things which were
confusing, and makes an unrelated fix in the image provider, which kept
amassing fail handlers.
Change-Id: I580becff246f197ec1bc65e82acd422620e35578
Mingle: https://wikimedia.mingle.thoughtworks.com/projects/multimedia/cards/489
2014-04-25 22:26:34 +00:00
|
|
|
}
|
2014-04-24 01:03:24 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
/**
|
|
|
|
* A helper function to fill up the preload queues.
|
|
|
|
* taskFactory(lightboxImage) should return a preload task for the given lightboximage.
|
|
|
|
*
|
|
|
|
* @private
|
2023-05-20 13:02:55 +00:00
|
|
|
* @param {function(LightboxImage)} taskFactory
|
2023-05-20 12:38:59 +00:00
|
|
|
* @return {TaskQueue}
|
|
|
|
*/
|
|
|
|
pushLightboxImagesIntoQueue( taskFactory ) {
|
|
|
|
const queue = new TaskQueue();
|
Refactor progressbar & blur handling
This tries to fix a number of related issues:
* the blurred thumbnail was visible for a split-second sometimes
when switching back to an already-loaded image. (Presumably when
JS was sluggish enough to take more than 10 ms to execute.) We
now check whether the promise is pending before showing a placeholder.
(More generally, a lot of unnecessary logic was executed when paging
through already loaded images, like displaying the placeholder, so
this might make the UI a bit more responsive.)
* the blur could get stuck sometimes - I have seen this a few times,
but have never been able to reproduce it, so I'm only guessing, but
maybe the timing was really unfortunate, and we switched back less
than 10 ms before loading finished. We now remove the blur on every
branch, just to be sure.
* adding a progress handler to a promise might not have any immediate
effect, so when switching to an image which was loading, the progress
bar reacted too late. We now store the progress state per thumbnail
so it is always available immediately.
* the progress would animate from 0 to its actual state whenever we
navigated to the image. The change on paging is now instant; the
progress bar only animates when we are looking at it.
* switching quickly back and forthe between a loaded and a loading
image resulted in the loading image becoming unblurred. This seems
fixed now, I'm not sure why. Maybe the "skip on non-pending promise"
logic affects it somehow.
Also removes some unused things / renames some things which were
confusing, and makes an unrelated fix in the image provider, which kept
amassing fail handlers.
Change-Id: I580becff246f197ec1bc65e82acd422620e35578
Mingle: https://wikimedia.mingle.thoughtworks.com/projects/multimedia/cards/489
2014-04-25 22:26:34 +00:00
|
|
|
|
2024-05-15 18:59:58 +00:00
|
|
|
this.eachPreloadableLightboxIndex( ( i, lightboxImage ) => {
|
|
|
|
queue.push( taskFactory( lightboxImage ) );
|
2023-05-20 12:38:59 +00:00
|
|
|
} );
|
2014-04-24 01:03:24 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
return queue;
|
|
|
|
}
|
Refactor progressbar & blur handling
This tries to fix a number of related issues:
* the blurred thumbnail was visible for a split-second sometimes
when switching back to an already-loaded image. (Presumably when
JS was sluggish enough to take more than 10 ms to execute.) We
now check whether the promise is pending before showing a placeholder.
(More generally, a lot of unnecessary logic was executed when paging
through already loaded images, like displaying the placeholder, so
this might make the UI a bit more responsive.)
* the blur could get stuck sometimes - I have seen this a few times,
but have never been able to reproduce it, so I'm only guessing, but
maybe the timing was really unfortunate, and we switched back less
than 10 ms before loading finished. We now remove the blur on every
branch, just to be sure.
* adding a progress handler to a promise might not have any immediate
effect, so when switching to an image which was loading, the progress
bar reacted too late. We now store the progress state per thumbnail
so it is always available immediately.
* the progress would animate from 0 to its actual state whenever we
navigated to the image. The change on paging is now instant; the
progress bar only animates when we are looking at it.
* switching quickly back and forthe between a loaded and a loading
image resulted in the loading image becoming unblurred. This seems
fixed now, I'm not sure why. Maybe the "skip on non-pending promise"
logic affects it somehow.
Also removes some unused things / renames some things which were
confusing, and makes an unrelated fix in the image provider, which kept
amassing fail handlers.
Change-Id: I580becff246f197ec1bc65e82acd422620e35578
Mingle: https://wikimedia.mingle.thoughtworks.com/projects/multimedia/cards/489
2014-04-25 22:26:34 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
/**
|
|
|
|
* Cancels in-progress image metadata preloading.
|
|
|
|
*/
|
|
|
|
cancelImageMetadataPreloading() {
|
|
|
|
if ( this.metadataPreloadQueue ) {
|
|
|
|
this.metadataPreloadQueue.cancel();
|
|
|
|
}
|
|
|
|
}
|
2017-05-11 15:03:52 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
/**
|
|
|
|
* Cancels in-progress image thumbnail preloading.
|
|
|
|
*/
|
|
|
|
cancelThumbnailsPreloading() {
|
|
|
|
if ( this.thumbnailPreloadQueue ) {
|
|
|
|
this.thumbnailPreloadQueue.cancel();
|
2014-04-24 01:03:24 +00:00
|
|
|
}
|
2023-05-20 12:38:59 +00:00
|
|
|
}
|
2014-02-25 13:43:22 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
preloadImagesMetadata() {
|
|
|
|
this.cancelImageMetadataPreloading();
|
|
|
|
|
2024-05-15 18:59:58 +00:00
|
|
|
this.metadataPreloadQueue = this.pushLightboxImagesIntoQueue( ( lightboxImage ) => {
|
|
|
|
return () => this.fetchSizeIndependentLightboxInfo( lightboxImage.filePageTitle );
|
2023-05-20 12:38:59 +00:00
|
|
|
} );
|
2014-02-07 14:47:00 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
this.metadataPreloadQueue.execute();
|
|
|
|
}
|
2014-02-13 09:52:40 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
preloadThumbnails() {
|
|
|
|
this.cancelThumbnailsPreloading();
|
|
|
|
|
2024-05-15 18:59:58 +00:00
|
|
|
this.thumbnailPreloadQueue = this.pushLightboxImagesIntoQueue( ( lightboxImage ) => {
|
2023-05-20 12:38:59 +00:00
|
|
|
return () => {
|
|
|
|
// viewer.ui.canvas.getLightboxImageWidths needs the viewer to be open
|
|
|
|
// because it needs to read the size of visible elements
|
|
|
|
if ( !this.isOpen ) {
|
|
|
|
return;
|
|
|
|
}
|
2014-02-13 09:52:40 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
const imageWidths = this.ui.canvas.getLightboxImageWidths( lightboxImage );
|
2014-02-13 09:52:40 +00:00
|
|
|
|
2024-05-15 18:59:58 +00:00
|
|
|
return this.fetchThumbnailForLightboxImage( lightboxImage, imageWidths.real );
|
2023-05-20 12:38:59 +00:00
|
|
|
};
|
|
|
|
} );
|
2014-02-13 09:52:40 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
this.thumbnailPreloadQueue.execute();
|
|
|
|
}
|
2013-11-25 23:51:40 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
/**
|
|
|
|
* Preload the fullscreen size of the current image.
|
|
|
|
*
|
|
|
|
* @param {LightboxImage} image
|
|
|
|
*/
|
|
|
|
preloadFullscreenThumbnail( image ) {
|
|
|
|
const imageWidths = this.ui.canvas.getLightboxImageWidthsForFullscreen( image );
|
2014-02-13 09:52:40 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
this.fetchThumbnailForLightboxImage( image, imageWidths.real );
|
2014-02-13 09:52:40 +00:00
|
|
|
}
|
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
/**
|
|
|
|
* Loads all the size-independent information needed by the lightbox (image metadata, repo
|
|
|
|
* information).
|
|
|
|
*
|
|
|
|
* @param {mw.Title} fileTitle Title of the file page for the image.
|
|
|
|
* @return {jQuery.Promise.<Image, Repo>}
|
|
|
|
*/
|
|
|
|
fetchSizeIndependentLightboxInfo( fileTitle ) {
|
|
|
|
const imageInfoPromise = this.imageInfoProvider.get( fileTitle );
|
|
|
|
const repoInfoPromise = this.fileRepoInfoProvider.get( fileTitle );
|
|
|
|
|
|
|
|
return $.when(
|
|
|
|
imageInfoPromise, repoInfoPromise
|
|
|
|
).then( ( imageInfo, repoInfoHash ) => {
|
|
|
|
return $.Deferred().resolve( imageInfo, repoInfoHash[ imageInfo.repo ] );
|
|
|
|
} );
|
2014-02-13 09:52:40 +00:00
|
|
|
}
|
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
/**
|
|
|
|
* Loads size-dependent components of a lightbox - the thumbnail model and the image itself.
|
|
|
|
*
|
|
|
|
* @param {LightboxImage} image
|
|
|
|
* @param {number} width the width of the requested thumbnail
|
|
|
|
* @return {jQuery.Promise.<Thumbnail, HTMLImageElement>}
|
|
|
|
*/
|
2024-05-15 18:59:58 +00:00
|
|
|
fetchThumbnailForLightboxImage( image, width ) {
|
2023-05-20 12:38:59 +00:00
|
|
|
return this.fetchThumbnail(
|
|
|
|
image.filePageTitle,
|
|
|
|
width,
|
|
|
|
image.src,
|
|
|
|
image.originalWidth,
|
2024-05-15 18:59:58 +00:00
|
|
|
image.originalHeight
|
2023-05-20 12:38:59 +00:00
|
|
|
);
|
|
|
|
}
|
2014-02-13 09:52:40 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
/**
|
|
|
|
* Loads size-dependent components of a lightbox - the thumbnail model and the image itself.
|
|
|
|
*
|
|
|
|
* @param {mw.Title} fileTitle
|
|
|
|
* @param {number} width the width of the requested thumbnail
|
|
|
|
* @param {string} [sampleUrl] a thumbnail URL for the same file (but with different size) (might be missing)
|
|
|
|
* @param {number} [originalWidth] the width of the original, full-sized file (might be missing)
|
|
|
|
* @param {number} [originalHeight] the height of the original, full-sized file (might be missing)
|
|
|
|
* @return {jQuery.Promise.<Thumbnail, HTMLImageElement>} A promise resolving to
|
|
|
|
* a thumbnail model and an <img> element. It might or might not have progress events which
|
|
|
|
* return a single number.
|
|
|
|
*/
|
2024-05-15 18:59:58 +00:00
|
|
|
fetchThumbnail( fileTitle, width, sampleUrl, originalWidth, originalHeight ) {
|
2023-05-20 12:38:59 +00:00
|
|
|
let guessing = false;
|
|
|
|
const combinedDeferred = $.Deferred();
|
|
|
|
let thumbnailPromise;
|
|
|
|
let imagePromise;
|
|
|
|
|
|
|
|
if ( fileTitle.getExtension().toLowerCase() !== 'svg' && originalWidth && width > originalWidth ) {
|
|
|
|
// Do not request images larger than the original image
|
|
|
|
width = originalWidth;
|
|
|
|
}
|
2014-02-13 09:52:40 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
if ( sampleUrl && originalWidth && originalHeight && this.config.useThumbnailGuessing() ) {
|
|
|
|
guessing = true;
|
|
|
|
thumbnailPromise = this.guessedThumbnailInfoProvider.get(
|
|
|
|
fileTitle, sampleUrl, width, originalWidth, originalHeight
|
|
|
|
).then( null, () => this.thumbnailInfoProvider.get( fileTitle, width ) );
|
|
|
|
} else {
|
|
|
|
thumbnailPromise = this.thumbnailInfoProvider.get( fileTitle, width );
|
|
|
|
}
|
2014-02-13 09:52:40 +00:00
|
|
|
|
2024-05-15 18:59:58 +00:00
|
|
|
imagePromise = thumbnailPromise.then( ( thumbnail ) => this.imageProvider.get( thumbnail.url ) );
|
2014-02-13 09:52:40 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
if ( guessing ) {
|
|
|
|
// If we guessed wrong, need to retry with real URL on failure.
|
|
|
|
// As a side effect this introduces an extra (harmless) retry of a failed thumbnailInfoProvider.get call
|
|
|
|
// because thumbnailInfoProvider.get is already called above when guessedThumbnailInfoProvider.get fails.
|
|
|
|
imagePromise = imagePromise
|
|
|
|
.then( null, () => this.thumbnailInfoProvider.get( fileTitle, width )
|
2024-05-15 18:59:58 +00:00
|
|
|
.then( ( thumbnail ) => this.imageProvider.get( thumbnail.url ) ) );
|
2023-05-20 12:38:59 +00:00
|
|
|
}
|
2014-02-13 09:52:40 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
// In jQuery<3, $.when used to also relay notify, but that is no longer
|
|
|
|
// the case - but we still want to pass it along...
|
|
|
|
$.when( thumbnailPromise, imagePromise ).then( combinedDeferred.resolve, combinedDeferred.reject );
|
|
|
|
imagePromise.then( null, null, ( arg, progress ) => {
|
|
|
|
combinedDeferred.notify( progress );
|
|
|
|
} );
|
|
|
|
return combinedDeferred;
|
|
|
|
}
|
2014-09-26 18:37:53 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
/**
|
|
|
|
* Loads an image at a specified index in the viewer's thumbnail array.
|
|
|
|
*
|
|
|
|
* @param {number} index
|
|
|
|
*/
|
|
|
|
loadIndex( index ) {
|
|
|
|
if ( index < this.thumbs.length && index >= 0 ) {
|
|
|
|
this.viewLogger.recordViewDuration();
|
2014-09-26 18:37:53 +00:00
|
|
|
|
2023-10-23 19:28:57 +00:00
|
|
|
const thumb = this.thumbs[ index ];
|
2024-05-21 18:24:39 +00:00
|
|
|
this.loadImage( thumb );
|
2023-05-20 12:38:59 +00:00
|
|
|
router.navigateTo( null, {
|
2024-05-21 18:24:39 +00:00
|
|
|
path: getMediaHash( thumb.filePageTitle, thumb.position ),
|
2023-05-20 12:38:59 +00:00
|
|
|
useReplaceState: true
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
}
|
2014-02-13 09:52:40 +00:00
|
|
|
|
2023-05-21 11:21:02 +00:00
|
|
|
/**
|
|
|
|
* Opens the last image
|
|
|
|
*/
|
|
|
|
firstImage() {
|
|
|
|
this.loadIndex( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Opens the last image
|
|
|
|
*/
|
|
|
|
lastImage() {
|
|
|
|
this.loadIndex( this.thumbs.length - 1 );
|
|
|
|
}
|
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
/**
|
|
|
|
* Opens the next image
|
|
|
|
*/
|
|
|
|
nextImage() {
|
|
|
|
this.loadIndex( this.currentIndex + 1 );
|
|
|
|
}
|
2013-11-25 23:51:40 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
/**
|
|
|
|
* Opens the previous image
|
|
|
|
*/
|
|
|
|
prevImage() {
|
|
|
|
this.loadIndex( this.currentIndex - 1 );
|
|
|
|
}
|
2014-09-26 18:37:53 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
/**
|
|
|
|
* Handles close event coming from the lightbox
|
|
|
|
*/
|
|
|
|
close() {
|
|
|
|
this.viewLogger.recordViewDuration();
|
|
|
|
this.viewLogger.unattach();
|
2014-02-20 08:43:10 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
if ( comingFromHashChange ) {
|
|
|
|
comingFromHashChange = false;
|
|
|
|
} else {
|
|
|
|
this.router.back();
|
|
|
|
}
|
|
|
|
// update title after route change, see T225387
|
|
|
|
document.title = this.createDocumentTitle( null );
|
2013-09-26 01:47:59 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
// This has to happen after the hash reset, because setting the hash to # will reset the page scroll
|
|
|
|
$( document ).trigger( $.Event( 'mmv-cleanup-overlay' ) );
|
2014-04-09 23:59:49 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
this.isOpen = false;
|
2014-04-15 00:01:03 +00:00
|
|
|
}
|
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
/**
|
|
|
|
* Sets up the route handlers
|
|
|
|
*/
|
|
|
|
setupRouter() {
|
|
|
|
// handle empty hashes, and anchor links (page sections)
|
|
|
|
this.router.addRoute( /^[^/]*$/, () => {
|
|
|
|
if ( this.isOpen ) {
|
|
|
|
comingFromHashChange = true;
|
|
|
|
document.title = this.createDocumentTitle( null );
|
|
|
|
if ( this.ui ) {
|
|
|
|
// FIXME triggers mmv-close event, which calls viewer.close()
|
|
|
|
this.ui.unattach();
|
|
|
|
} else {
|
|
|
|
this.close();
|
|
|
|
}
|
|
|
|
}
|
2014-04-10 01:08:36 +00:00
|
|
|
} );
|
|
|
|
}
|
2014-02-25 13:43:22 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
/**
|
|
|
|
* Updates the page title to reflect the current title.
|
|
|
|
*/
|
|
|
|
setTitle() {
|
|
|
|
// update title after route change, see T225387
|
|
|
|
document.title = this.createDocumentTitle( this.currentImageFileTitle );
|
2014-04-10 01:08:36 +00:00
|
|
|
}
|
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
/**
|
|
|
|
* Creates a string which can be shown as document title (the text at the top of the browser window).
|
|
|
|
*
|
|
|
|
* @param {mw.Title|null} imageTitle the title object for the image which is displayed; null when the
|
|
|
|
* viewer is being closed
|
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
createDocumentTitle( imageTitle ) {
|
|
|
|
if ( imageTitle ) {
|
2024-02-13 00:44:51 +00:00
|
|
|
return `${ imageTitle.getNameText() } - ${ this.documentTitle }`;
|
2023-05-20 12:38:59 +00:00
|
|
|
} else {
|
|
|
|
return this.documentTitle;
|
|
|
|
}
|
|
|
|
}
|
2014-02-17 15:09:23 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
/**
|
|
|
|
* Fired when the viewer is closed. This is used by the lightbox to notify the main app.
|
2023-05-20 13:02:55 +00:00
|
|
|
*
|
|
|
|
* @event MultimediaViewer#mmv-close
|
2023-05-20 12:38:59 +00:00
|
|
|
*/
|
2023-05-20 13:02:55 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
/**
|
|
|
|
* Fired when the user requests the next image.
|
2023-05-20 13:02:55 +00:00
|
|
|
*
|
|
|
|
* @event MultimediaViewer#mmv-next
|
2023-05-20 12:38:59 +00:00
|
|
|
*/
|
2023-05-20 13:02:55 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
/**
|
|
|
|
* Fired when the user requests the previous image.
|
2023-05-20 13:02:55 +00:00
|
|
|
*
|
|
|
|
* @event MultimediaViewer#mmv-prev
|
2023-05-20 12:38:59 +00:00
|
|
|
*/
|
2023-05-20 13:02:55 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
/**
|
|
|
|
* Fired when the screen size changes. Debounced to avoid continuous triggering while resizing with a mouse.
|
2023-05-20 13:02:55 +00:00
|
|
|
*
|
|
|
|
* @event MultimediaViewer#mmv-resize-end
|
2023-05-20 12:38:59 +00:00
|
|
|
*/
|
2023-05-20 13:02:55 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
/**
|
|
|
|
* Used by components to request a thumbnail URL for the current thumbnail, with a given size.
|
2023-05-20 13:02:55 +00:00
|
|
|
*
|
|
|
|
* @event MultimediaViewer#mmv-request-thumbnail
|
2023-05-20 12:38:59 +00:00
|
|
|
* @param {number} size
|
|
|
|
*/
|
2023-05-20 13:02:55 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
/**
|
|
|
|
* Registers all event handlers
|
|
|
|
*/
|
|
|
|
setupEventHandlers() {
|
|
|
|
this.ui.connect( this, {
|
2023-05-21 11:21:02 +00:00
|
|
|
first: 'firstImage',
|
|
|
|
last: 'lastImage',
|
2023-05-20 12:38:59 +00:00
|
|
|
next: 'nextImage',
|
|
|
|
prev: 'prevImage'
|
|
|
|
} );
|
2014-11-20 23:39:29 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
$( document ).on( 'mmv-close.mmvp', () => {
|
|
|
|
this.close();
|
|
|
|
} ).on( 'mmv-resize-end.mmvp', () => {
|
|
|
|
this.resize( this.ui );
|
|
|
|
} ).on( 'mmv-request-thumbnail.mmvp', ( e, size ) => {
|
|
|
|
if ( this.currentImageFileTitle ) {
|
|
|
|
return this.thumbnailInfoProvider.get( this.currentImageFileTitle, size );
|
|
|
|
} else {
|
|
|
|
return $.Deferred().reject();
|
|
|
|
}
|
|
|
|
} ).on( 'mmv-viewfile.mmvp', () => {
|
|
|
|
this.imageInfoProvider.get( this.currentImageFileTitle ).done( ( imageInfo ) => {
|
|
|
|
document.location = imageInfo.url;
|
|
|
|
} );
|
2023-04-28 21:49:00 +00:00
|
|
|
} );
|
2013-11-26 03:37:01 +00:00
|
|
|
}
|
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
/**
|
|
|
|
* Unregisters all event handlers. Currently only used in tests.
|
|
|
|
*/
|
|
|
|
cleanupEventHandlers() {
|
|
|
|
$( document ).off( 'mmv-close.mmvp mmv-resize-end.mmvp' );
|
2014-11-15 15:32:04 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
this.ui.disconnect( this );
|
2014-02-11 13:25:39 +00:00
|
|
|
}
|
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
/**
|
|
|
|
* Preloads JS and CSS dependencies that aren't needed to display the first image, but could be needed later
|
|
|
|
*/
|
|
|
|
preloadDependencies() {
|
|
|
|
mw.loader.load( [ 'mmv.ui.reuse.shareembed' ] );
|
|
|
|
}
|
2014-04-11 09:31:38 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
/**
|
|
|
|
* Loads the RL module defined for a given file extension, if any
|
|
|
|
*
|
|
|
|
* @param {string} extension File extension
|
|
|
|
* @return {jQuery.Promise}
|
|
|
|
*/
|
|
|
|
loadExtensionPlugins( extension ) {
|
|
|
|
const deferred = $.Deferred();
|
|
|
|
const config = this.config.extensions();
|
2014-02-11 13:25:39 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
if ( !( extension in config ) || config[ extension ] === 'default' ) {
|
|
|
|
return deferred.resolve();
|
2019-05-16 13:19:44 +00:00
|
|
|
}
|
2014-02-17 15:09:23 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
mw.loader.using( config[ extension ], () => {
|
|
|
|
deferred.resolve();
|
|
|
|
} );
|
2014-07-01 22:22:06 +00:00
|
|
|
|
2023-05-20 12:38:59 +00:00
|
|
|
return deferred;
|
2014-03-04 12:52:08 +00:00
|
|
|
}
|
2023-05-20 12:38:59 +00:00
|
|
|
}
|
2014-03-04 12:52:08 +00:00
|
|
|
|
2014-04-08 09:54:30 +00:00
|
|
|
/**
|
2023-05-20 12:38:59 +00:00
|
|
|
* Image loading progress. Keyed by image (database) name + '|' + thumbnail width in pixels,
|
|
|
|
* value is undefined, 'blurred' or 'real' (meaning respectively that no thumbnail is shown
|
|
|
|
* yet / the thumbnail that existed on the page is shown, enlarged and blurred / the real,
|
|
|
|
* correct-size thumbnail is shown).
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @property {Object.<string, string>}
|
2014-04-08 09:54:30 +00:00
|
|
|
*/
|
2023-05-20 12:38:59 +00:00
|
|
|
MultimediaViewer.prototype.thumbnailStateCache = {};
|
|
|
|
|
2014-02-19 02:27:30 +00:00
|
|
|
/**
|
2023-05-20 12:38:59 +00:00
|
|
|
* Image loading progress. Keyed by image (database) name + '|' + thumbnail width in pixels,
|
|
|
|
* value is a number between 0-100.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @property {Object.<string, number>}
|
2014-02-19 02:27:30 +00:00
|
|
|
*/
|
2023-05-20 12:38:59 +00:00
|
|
|
MultimediaViewer.prototype.progressCache = {};
|
2014-02-11 01:12:31 +00:00
|
|
|
|
2014-03-04 11:53:53 +00:00
|
|
|
/**
|
2023-05-20 12:38:59 +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}
|
2020-06-26 10:21:04 +00:00
|
|
|
*/
|
2023-05-20 12:38:59 +00:00
|
|
|
MultimediaViewer.prototype.preloadDistance = 1;
|
2014-03-04 11:53:53 +00:00
|
|
|
|
2014-04-11 14:05:58 +00:00
|
|
|
/**
|
2023-05-20 12:38:59 +00:00
|
|
|
* Stores image metadata preloads, so they can be cancelled.
|
|
|
|
*
|
|
|
|
* @property {TaskQueue}
|
2014-04-11 14:05:58 +00:00
|
|
|
*/
|
2023-05-20 12:38:59 +00:00
|
|
|
MultimediaViewer.prototype.metadataPreloadQueue = null;
|
2014-04-11 14:05:58 +00:00
|
|
|
|
2016-04-03 09:18:26 +00:00
|
|
|
/**
|
2023-05-20 12:38:59 +00:00
|
|
|
* Stores image thumbnail preloads, so they can be cancelled.
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
2023-05-20 12:38:59 +00:00
|
|
|
* @property {TaskQueue}
|
2016-04-03 09:18:26 +00:00
|
|
|
*/
|
2023-05-20 12:38:59 +00:00
|
|
|
MultimediaViewer.prototype.thumbnailPreloadQueue = null;
|
2016-04-03 09:18:26 +00:00
|
|
|
|
2023-05-20 08:30:52 +00:00
|
|
|
module.exports = {
|
|
|
|
Api,
|
|
|
|
Canvas,
|
|
|
|
CanvasButtons,
|
|
|
|
Description,
|
|
|
|
Dialog,
|
|
|
|
DownloadDialog,
|
|
|
|
FileRepoInfo,
|
|
|
|
ForeignApiRepo,
|
|
|
|
ForeignDbRepo,
|
|
|
|
GuessedThumbnailInfo,
|
|
|
|
ImageInfo,
|
|
|
|
ImageModel,
|
|
|
|
ImageProvider,
|
|
|
|
IwTitle,
|
|
|
|
License,
|
|
|
|
LightboxInterface,
|
|
|
|
MetadataPanel,
|
|
|
|
MetadataPanelScroller,
|
|
|
|
MultimediaViewer,
|
|
|
|
OptionsDialog,
|
|
|
|
Permission,
|
|
|
|
ProgressBar,
|
|
|
|
Repo,
|
|
|
|
ReuseDialog,
|
|
|
|
StripeButtons,
|
|
|
|
TaskQueue,
|
|
|
|
Thumbnail,
|
|
|
|
ThumbnailInfo,
|
|
|
|
ThumbnailWidth,
|
|
|
|
ThumbnailWidthCalculator,
|
|
|
|
TruncatableTextField,
|
|
|
|
UiElement,
|
|
|
|
ViewLogger
|
|
|
|
};
|
2018-11-12 16:33:24 +00:00
|
|
|
}() );
|