From 2080e28a83111509c9add78134d3644d44319900 Mon Sep 17 00:00:00 2001 From: Gilles Dubuc Date: Fri, 28 Mar 2014 11:06:17 +0100 Subject: [PATCH] Display thumbnail placeholder ASAP - Exposes file width and height through data attributes - If the attributes are present, display the placeholder immediately Change-Id: I3dfa80b02073984dc2d3d7784cb3744b7e4e2cc8 Mingle: https://wikimedia.mingle.thoughtworks.com/projects/multimedia/cards/293 --- MultimediaViewer.php | 1 + MultimediaViewerHooks.php | 26 ++++++++++++++++++++++++++ resources/mmv/mmv.js | 28 ++++++++++++++++++---------- 3 files changed, 45 insertions(+), 10 deletions(-) diff --git a/MultimediaViewer.php b/MultimediaViewer.php index cdb927884..36cf40864 100644 --- a/MultimediaViewer.php +++ b/MultimediaViewer.php @@ -691,6 +691,7 @@ call_user_func( function() { $wgHooks['ResourceLoaderGetConfigVars'][] = 'MultimediaViewerHooks::resourceLoaderGetConfigVars'; $wgHooks['MakeGlobalVariablesScript'][] = 'MultimediaViewerHooks::makeGlobalVariablesScript'; $wgHooks['ResourceLoaderTestModules'][] = 'MultimediaViewerHooks::getTestModules'; + $wgHooks['ThumbnailBeforeProduceHTML'][] = 'MultimediaViewerHooks::thumbnailBeforeProduceHTML'; $section = 'other'; diff --git a/MultimediaViewerHooks.php b/MultimediaViewerHooks.php index a6ad791b0..50645590a 100644 --- a/MultimediaViewerHooks.php +++ b/MultimediaViewerHooks.php @@ -220,4 +220,30 @@ class MultimediaViewerHooks { return true; } + + /** + * Modify thumbnail DOM + * @param ThumbnailImage $thumbnail + * @param array $attribs Attributes of the element + * @param array $linkAttribs Attributes of the wrapping element + */ + public static function thumbnailBeforeProduceHTML( ThumbnailImage $thumbnail, array &$attribs, array &$linkAttribs ) { + $file = $thumbnail->getFile(); + + if ( $file ) { + // At the moment all classes that extend File have getWidth() and getHeight() + // but since the File class doesn't have these methods defined, this check + // is more future-proof + + if ( method_exists( $file, 'getWidth' ) ) { + $attribs['data-file-width'] = $file->getWidth(); + } + + if ( method_exists( $file, 'getHeight' ) ) { + $attribs['data-file-height'] = $file->getHeight(); + } + } + + return true; + } } diff --git a/resources/mmv/mmv.js b/resources/mmv/mmv.js index 4932569d3..6740635b0 100755 --- a/resources/mmv/mmv.js +++ b/resources/mmv/mmv.js @@ -195,11 +195,13 @@ */ MMVP.loadImage = function ( image, initialImage ) { var imageWidths, - viewer = this, imagePromise, metadataPromise, start, - $initialImage = $( initialImage ); + viewer = this, + $initialImage = $( initialImage ), + fileWidth = parseInt( $initialImage.data( 'file-width' ), 10 ), + fileHeight = parseInt( $initialImage.data( 'file-height' ), 10 ); this.currentIndex = image.index; @@ -246,6 +248,20 @@ viewer.ui.panel.percent( 5 ); } + if ( fileWidth > 0 && fileHeight > 0 ) { + viewer.displayPlaceholderThumbnail( { width : fileWidth , height : fileHeight }, + $initialImage, + imageWidths ); + } else { + this.imageInfoProvider.get( image.filePageTitle ).done( function ( imageInfo ) { + if ( viewer.currentIndex !== image.index ) { + return; + } + + viewer.displayPlaceholderThumbnail( imageInfo, $initialImage, imageWidths ); + } ); + } + imagePromise.progress( function ( thumbnailInfoResponse, imageResponse ) { if ( viewer.currentIndex !== image.index ) { return; @@ -267,14 +283,6 @@ viewer.ui.canvas.showError( error ); } ); - this.imageInfoProvider.get( image.filePageTitle ).done( function ( imageInfo ) { - if ( viewer.currentIndex !== image.index ) { - return; - } - - viewer.displayPlaceholderThumbnail( imageInfo, $initialImage, imageWidths ); - } ); - metadataPromise = this.fetchSizeIndependentLightboxInfo( image.filePageTitle ).done( function ( imageInfo, repoInfo, localUsage, globalUsage, userInfo ) {