2014-02-17 15:09:23 +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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
( function ( mw, $ ) {
|
2014-03-04 11:53:53 +00:00
|
|
|
var MMVB;
|
2014-02-17 15:09:23 +00:00
|
|
|
|
|
|
|
/**
|
2014-02-21 11:16:30 +00:00
|
|
|
* Bootstrap code listening to thumb clicks checking the initial location.hash
|
2014-02-17 15:09:23 +00:00
|
|
|
* Loads the mmv and opens it if necessary
|
2014-02-19 02:27:30 +00:00
|
|
|
* @class mw.mmv.MultimediaViewerBootstrap
|
2014-02-17 15:09:23 +00:00
|
|
|
*/
|
|
|
|
function MultimediaViewerBootstrap () {
|
2015-08-14 18:15:57 +00:00
|
|
|
var localStorage = false;
|
|
|
|
|
|
|
|
try {
|
|
|
|
localStorage = window.localStorage || false;
|
|
|
|
} catch ( e ) { }
|
|
|
|
|
2014-02-17 15:09:23 +00:00
|
|
|
this.validExtensions = {
|
|
|
|
'jpg' : true,
|
|
|
|
'jpeg' : true,
|
|
|
|
'gif' : true,
|
|
|
|
'svg' : true,
|
|
|
|
'png' : true,
|
|
|
|
'tiff' : true,
|
|
|
|
'tif' : true
|
|
|
|
};
|
|
|
|
|
2014-02-24 17:06:11 +00:00
|
|
|
// Exposed for tests
|
|
|
|
this.readinessCSSClass = 'mw-mmv-has-been-loaded';
|
|
|
|
this.readinessWaitDuration = 100;
|
2014-04-24 13:47:59 +00:00
|
|
|
this.hoverWaitDuration = 200;
|
2014-03-26 23:59:04 +00:00
|
|
|
|
2014-06-10 23:29:50 +00:00
|
|
|
// TODO lazy-load config and htmlUtils
|
|
|
|
|
|
|
|
/** @property {mw.mmv.Config} config - */
|
|
|
|
this.config = new mw.mmv.Config(
|
|
|
|
mw.config.get( 'wgMultimediaViewer', {} ),
|
|
|
|
mw.config,
|
|
|
|
mw.user,
|
|
|
|
new mw.Api(),
|
2015-08-14 18:15:57 +00:00
|
|
|
localStorage
|
2014-06-10 23:29:50 +00:00
|
|
|
);
|
|
|
|
|
2014-03-26 23:59:04 +00:00
|
|
|
/** @property {mw.mmv.HtmlUtils} htmlUtils - */
|
|
|
|
this.htmlUtils = new mw.mmv.HtmlUtils();
|
|
|
|
|
2014-04-29 01:35:07 +00:00
|
|
|
/**
|
|
|
|
* This flag is set to true when we were unable to load the viewer.
|
|
|
|
* @property {boolean}
|
|
|
|
*/
|
|
|
|
this.viewerIsBroken = false;
|
|
|
|
|
2014-03-28 22:39:36 +00:00
|
|
|
this.thumbsReadyDeferred = $.Deferred();
|
2014-03-26 23:59:04 +00:00
|
|
|
this.thumbs = [];
|
2015-08-12 19:02:36 +00:00
|
|
|
this.$thumbs = null; // will be set by processThumbs
|
2014-04-24 13:47:59 +00:00
|
|
|
|
2015-08-12 19:02:36 +00:00
|
|
|
// find and setup all thumbs on this page
|
2015-09-01 02:47:00 +00:00
|
|
|
// this will run initially and then every time the content changes,
|
|
|
|
// e.g. via a VE edit or pagination in a multipage file
|
|
|
|
mw.hook( 'wikipage.content' ).add( $.proxy( this, 'processThumbs' ) );
|
2014-04-11 09:31:38 +00:00
|
|
|
|
|
|
|
this.browserHistory = window.history;
|
2014-02-17 15:09:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MMVB = MultimediaViewerBootstrap.prototype;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads the mmv module asynchronously and passes the thumb data to it
|
|
|
|
* @returns {jQuery.Promise}
|
|
|
|
*/
|
|
|
|
MMVB.loadViewer = function () {
|
|
|
|
var deferred = $.Deferred(),
|
|
|
|
bs = this;
|
|
|
|
|
2014-03-14 22:19:26 +00:00
|
|
|
// Don't load if someone has specifically stopped us from doing so
|
|
|
|
if ( mw.config.get( 'wgMediaViewer' ) !== true ) {
|
|
|
|
return deferred.reject();
|
|
|
|
}
|
|
|
|
|
2014-03-28 08:06:53 +00:00
|
|
|
bs.setupOverlay();
|
|
|
|
|
2015-01-23 12:48:27 +00:00
|
|
|
mw.loader.using( 'mmv', function () {
|
2014-02-14 00:36:10 +00:00
|
|
|
bs.isCSSReady( deferred );
|
|
|
|
}, function ( error ) {
|
|
|
|
deferred.reject( error.message );
|
|
|
|
} );
|
2014-02-24 17:06:11 +00:00
|
|
|
|
2014-02-25 02:28:49 +00:00
|
|
|
return deferred.done( function ( viewer ) {
|
2014-02-17 15:09:23 +00:00
|
|
|
if ( !bs.viewerInitialized ) {
|
|
|
|
if ( bs.thumbs.length ) {
|
2014-02-25 02:28:49 +00:00
|
|
|
viewer.initWithThumbs( bs.thumbs );
|
2014-02-17 15:09:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bs.viewerInitialized = true;
|
|
|
|
}
|
2015-01-23 12:48:27 +00:00
|
|
|
} ).fail( function ( message ) {
|
2014-04-09 00:22:18 +00:00
|
|
|
mw.log.warn( message );
|
|
|
|
bs.cleanupOverlay();
|
2014-04-29 01:35:07 +00:00
|
|
|
bs.viewerIsBroken = true;
|
2014-04-09 21:56:19 +00:00
|
|
|
mw.notify( 'Error loading MediaViewer: ' + message );
|
2014-02-17 15:09:23 +00:00
|
|
|
} );
|
2014-02-24 17:06:11 +00:00
|
|
|
};
|
2014-02-17 15:09:23 +00:00
|
|
|
|
2014-02-24 17:06:11 +00:00
|
|
|
/**
|
|
|
|
* Checks if the mmv CSS has been correctly added to the page
|
|
|
|
* This is a workaround for core bug 61852
|
2014-02-25 02:28:49 +00:00
|
|
|
* @param {jQuery.Promise.<mw.mmv.MultimediaViewer>} deferred
|
2014-02-24 17:06:11 +00:00
|
|
|
*/
|
|
|
|
MMVB.isCSSReady = function ( deferred ) {
|
|
|
|
var $dummy = $( '<div class="' + this.readinessCSSClass + '">' )
|
|
|
|
.appendTo( $( document.body ) ),
|
2014-04-09 00:22:18 +00:00
|
|
|
bs = this,
|
2014-04-21 21:23:28 +00:00
|
|
|
viewer,
|
|
|
|
message;
|
2014-02-24 17:06:11 +00:00
|
|
|
|
|
|
|
if ( $dummy.css( 'display' ) === 'inline' ) {
|
|
|
|
// Let's be clean and remove the test item before resolving the deferred
|
|
|
|
$dummy.remove();
|
2014-04-09 00:22:18 +00:00
|
|
|
try {
|
|
|
|
viewer = bs.getViewer();
|
|
|
|
} catch ( e ) {
|
2014-04-21 21:23:28 +00:00
|
|
|
message = e.message;
|
|
|
|
if ( e.stack ) {
|
|
|
|
message += '\n' + e.stack;
|
|
|
|
}
|
|
|
|
deferred.reject( message );
|
2014-04-09 00:22:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
deferred.resolve( viewer );
|
2014-02-24 17:06:11 +00:00
|
|
|
} else {
|
|
|
|
$dummy.remove();
|
|
|
|
setTimeout( function () { bs.isCSSReady( deferred ); }, this.readinessWaitDuration );
|
|
|
|
}
|
2014-02-17 15:09:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Processes all thumbs found on the page
|
|
|
|
*/
|
|
|
|
MMVB.processThumbs = function () {
|
|
|
|
var bs = this;
|
|
|
|
|
2015-08-12 19:02:36 +00:00
|
|
|
this.$thumbs = $( '.gallery .image img, a.image img, #file a img' );
|
|
|
|
|
2014-04-28 23:20:57 +00:00
|
|
|
// if this breaks in IE8, see https://github.com/ebryn/backburner.js/pull/50
|
|
|
|
// but it probably won't since there is a catch further up the chain
|
|
|
|
try {
|
|
|
|
this.$thumbs.each( function ( i, thumb ) {
|
|
|
|
bs.processThumb( thumb );
|
|
|
|
} );
|
|
|
|
} finally {
|
|
|
|
this.thumbsReadyDeferred.resolve();
|
|
|
|
// now that we have set up our real click handler we can we can remove the temporary
|
|
|
|
// handler added in mmv.head.js which just replays clicks to the real handler
|
|
|
|
$( document ).off( 'click.mmv-head' );
|
|
|
|
}
|
2014-02-17 15:09:23 +00:00
|
|
|
};
|
|
|
|
|
2014-06-11 18:42:29 +00:00
|
|
|
/**
|
|
|
|
* Check if this thumbnail should be handled by MediaViewer
|
|
|
|
* @param {jQuery} $thumb the thumbnail (an `<img>` element) in question
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
|
|
|
MMVB.isAllowedThumb = function ( $thumb ) {
|
|
|
|
// .metadata means this is inside an informational template like {{refimprove}} on enwiki.
|
|
|
|
// .noviewer means MediaViewer has been specifically disabled for this image
|
|
|
|
// .noarticletext means we are on an error page for a non-existing article, the image is part of some
|
|
|
|
// template // FIXME this should be handled by .metadata
|
2014-11-20 15:02:19 +00:00
|
|
|
return $thumb.closest( '.metadata, .noviewer, .noarticletext, #siteNotice' ).length === 0;
|
2014-06-11 18:42:29 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2014-02-17 15:09:23 +00:00
|
|
|
/**
|
|
|
|
* Processes a thumb
|
|
|
|
* @param {Object} thumb
|
|
|
|
*/
|
|
|
|
MMVB.processThumb = function ( thumb ) {
|
2014-04-25 23:16:48 +00:00
|
|
|
var bs = this,
|
2014-02-17 15:09:23 +00:00
|
|
|
$thumb = $( thumb ),
|
|
|
|
$link = $thumb.closest( 'a.image' ),
|
|
|
|
$thumbContain = $link.closest( '.thumb' ),
|
|
|
|
$enlarge = $thumbContain.find( '.magnify a' ),
|
|
|
|
title = mw.Title.newFromImg( $thumb ),
|
2014-12-30 05:10:27 +00:00
|
|
|
link = $link.prop( 'href' ),
|
|
|
|
alt = $thumb.attr( 'alt' );
|
2014-02-17 15:09:23 +00:00
|
|
|
|
|
|
|
if ( !bs.validExtensions[ title.getExtension().toLowerCase() ] ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-06-11 18:42:29 +00:00
|
|
|
if ( !bs.isAllowedThumb( $thumb ) ) {
|
2014-03-06 23:26:25 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-17 15:09:23 +00:00
|
|
|
if ( $thumbContain.length !== 0 && $thumbContain.is( '.thumb' ) ) {
|
2014-04-24 13:47:59 +00:00
|
|
|
// If this is a thumb, we preload JS/CSS when the mouse cursor hovers the thumb container (thumb image + caption + border)
|
2015-01-23 12:48:27 +00:00
|
|
|
$thumbContain.mouseenter( function () {
|
2014-04-24 13:47:59 +00:00
|
|
|
// There is no point preloading if clicking the thumb won't open Media Viewer
|
2014-06-10 23:29:50 +00:00
|
|
|
if ( !bs.config.isMediaViewerEnabledOnClick() ) {
|
2014-04-24 13:47:59 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-01-23 12:48:27 +00:00
|
|
|
bs.preloadOnHoverTimer = setTimeout( function () {
|
2014-04-24 13:47:59 +00:00
|
|
|
mw.loader.load( 'mmv' );
|
|
|
|
}, bs.hoverWaitDuration );
|
2015-01-23 12:48:27 +00:00
|
|
|
} ).mouseleave( function () {
|
2014-04-24 13:47:59 +00:00
|
|
|
if ( bs.preloadOnHoverTimer ) {
|
|
|
|
clearTimeout( bs.preloadOnHoverTimer );
|
|
|
|
}
|
|
|
|
} );
|
2014-02-17 15:09:23 +00:00
|
|
|
}
|
|
|
|
|
2014-04-08 21:01:07 +00:00
|
|
|
if ( $thumb.closest( '#file' ).length > 0 ) {
|
2014-10-19 19:02:49 +00:00
|
|
|
// main thumbnail of a file page
|
|
|
|
this.processFilePageThumb( $thumb, title );
|
|
|
|
return;
|
2014-04-08 21:01:07 +00:00
|
|
|
}
|
|
|
|
|
2014-02-17 15:09:23 +00:00
|
|
|
// This is the data that will be passed onto the mmv
|
|
|
|
this.thumbs.push( {
|
|
|
|
thumb : thumb,
|
|
|
|
$thumb : $thumb,
|
|
|
|
title : title,
|
|
|
|
link : link,
|
2014-12-30 05:10:27 +00:00
|
|
|
alt : alt,
|
2014-04-25 23:16:48 +00:00
|
|
|
caption : this.findCaption( $thumbContain, $link ) } );
|
2014-02-17 15:09:23 +00:00
|
|
|
|
|
|
|
$link.add( $enlarge ).click( function ( e ) {
|
2014-10-19 19:02:49 +00:00
|
|
|
return bs.click( this, e, title );
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2015-09-01 02:47:00 +00:00
|
|
|
* Processes the main thumbnail of a file page by adding some buttons
|
|
|
|
* below to open MediaViewer.
|
2014-10-19 19:02:49 +00:00
|
|
|
* @param {jQuery} $thumb
|
|
|
|
* @param {mw.Title} title
|
|
|
|
*/
|
|
|
|
MMVB.processFilePageThumb = function ( $thumb, title ) {
|
|
|
|
var $link,
|
|
|
|
$configLink,
|
2014-10-03 09:15:28 +00:00
|
|
|
$filepageButtons,
|
2014-10-19 19:02:49 +00:00
|
|
|
bs = this,
|
|
|
|
link = $thumb.closest( 'a' ).prop( 'href' );
|
|
|
|
|
2015-09-01 02:47:00 +00:00
|
|
|
// remove the buttons (and the clearing element) if they are already there
|
|
|
|
// this should not happen (at least until we support paged media) but just in case
|
|
|
|
$( '.mw-mmv-filepage-buttons' ).next().addBack().remove();
|
|
|
|
|
2014-10-19 19:02:49 +00:00
|
|
|
$link = $( '<a>' )
|
|
|
|
// It won't matter because we catch the click event anyway, but
|
|
|
|
// give the user some URL to see.
|
|
|
|
.prop( 'href', link )
|
|
|
|
.addClass( 'mw-mmv-view-expanded mw-ui-button mw-ui-icon mw-ui-icon-before' )
|
|
|
|
.text( mw.message( 'multimediaviewer-view-expanded' ).text() );
|
|
|
|
|
|
|
|
$configLink = $( '<a>' )
|
|
|
|
.prop( 'href', $thumb.closest( 'a' ).prop( 'href' ) )
|
|
|
|
.addClass( 'mw-mmv-view-config mw-ui-button mw-ui-icon mw-ui-icon-element' )
|
|
|
|
.text( mw.message( 'multimediaviewer-view-config' ).text() );
|
|
|
|
|
2014-10-03 09:15:28 +00:00
|
|
|
$filepageButtons = $( '<div>' )
|
|
|
|
.addClass( 'mw-ui-button-group mw-mmv-filepage-buttons' )
|
|
|
|
.append( $link, $configLink );
|
2014-10-19 19:02:49 +00:00
|
|
|
|
|
|
|
$( '.fullMedia' ).append(
|
2014-10-03 09:15:28 +00:00
|
|
|
$filepageButtons,
|
2014-10-19 19:02:49 +00:00
|
|
|
$( '<div>' )
|
|
|
|
.css( 'clear', 'both' )
|
|
|
|
);
|
|
|
|
|
|
|
|
this.thumbs.push( {
|
|
|
|
thumb : $thumb.get( 0 ),
|
|
|
|
$thumb : $thumb,
|
|
|
|
title : title,
|
|
|
|
link : link
|
|
|
|
} );
|
|
|
|
|
|
|
|
$link.click( function () {
|
2014-10-03 09:15:28 +00:00
|
|
|
if ( bs.statusInfoDialog ) {
|
|
|
|
bs.statusInfoDialog.close();
|
|
|
|
}
|
2014-10-19 19:02:49 +00:00
|
|
|
bs.openImage( this, title );
|
|
|
|
return false;
|
|
|
|
} );
|
|
|
|
|
|
|
|
$configLink.click( function () {
|
2014-10-03 09:15:28 +00:00
|
|
|
if ( bs.statusInfoDialog ) {
|
|
|
|
bs.statusInfoDialog.close();
|
|
|
|
}
|
2014-10-19 19:02:49 +00:00
|
|
|
bs.openImage( this, title ).then( function () {
|
|
|
|
$( document ).trigger( 'mmv-options-open' );
|
|
|
|
} );
|
|
|
|
return false;
|
2014-02-17 15:09:23 +00:00
|
|
|
} );
|
2014-10-03 09:15:28 +00:00
|
|
|
|
|
|
|
if ( this.config.shouldShowStatusInfo() ) {
|
|
|
|
this.config.disableStatusInfo();
|
|
|
|
this.showStatusInfo();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shows a popup notifying the user
|
|
|
|
*/
|
|
|
|
MMVB.showStatusInfo = function () {
|
|
|
|
var bs = this;
|
|
|
|
|
|
|
|
mw.loader.using( 'mmv.ui.tipsyDialog' ).done( function () {
|
|
|
|
/** @property {mw.mmv.ui.TipsyDialog} statusInfoDialog popup on the file page explaining how to re-enable */
|
|
|
|
bs.statusInfoDialog = new mw.mmv.ui.TipsyDialog( $( '.mw-mmv-view-expanded' ), { gravity: 'sw' } );
|
|
|
|
bs.statusInfoDialog.setContent(
|
|
|
|
mw.message( 'multimediaviewer-disable-info-title' ).plain(),
|
2015-01-23 12:48:27 +00:00
|
|
|
mw.message( 'multimediaviewer-disable-info' ).escaped()
|
2014-10-03 09:15:28 +00:00
|
|
|
);
|
|
|
|
// tipsy mispositions the tooltip, probably because it does the positioning before the buttons are
|
|
|
|
// displayed and the page is reflown. Adding some delay seems to help.
|
|
|
|
window.setTimeout( function () {
|
|
|
|
bs.statusInfoDialog.open();
|
|
|
|
}, 1000 );
|
|
|
|
} );
|
2014-02-17 15:09:23 +00:00
|
|
|
};
|
|
|
|
|
2014-04-25 23:16:48 +00:00
|
|
|
/**
|
|
|
|
* Finds the caption for an image.
|
|
|
|
* @param {jQuery} $thumbContain The container for the thumbnail.
|
|
|
|
* @param {jQuery} $link The link that encompasses the thumbnail.
|
|
|
|
* @returns {string|undefined} Unsafe HTML may be present - caution
|
|
|
|
*/
|
|
|
|
MMVB.findCaption = function ( $thumbContain, $link ) {
|
|
|
|
var $thumbCaption;
|
|
|
|
|
|
|
|
if ( $thumbContain.length !== 0 && $thumbContain.is( '.thumb' ) ) {
|
2014-12-30 01:26:17 +00:00
|
|
|
// try to find closest caption to the image
|
|
|
|
$thumbCaption = $link.closest( ':has(> .thumbcaption)', $thumbContain )
|
|
|
|
.find( '> .thumbcaption' )
|
|
|
|
.clone();
|
|
|
|
if ( !$thumbCaption.length ) {
|
|
|
|
// if nothing is found, look for the caption in whole container
|
|
|
|
$thumbCaption = $thumbContain.find( '.thumbcaption' ).clone();
|
|
|
|
}
|
2014-04-25 23:16:48 +00:00
|
|
|
$thumbCaption.find( '.magnify' ).remove();
|
|
|
|
if ( !$thumbCaption.length ) { // gallery, maybe
|
|
|
|
$thumbCaption = $thumbContain
|
|
|
|
.closest( '.gallerybox' )
|
|
|
|
.not( function () {
|
|
|
|
// do not treat categories as galleries - the autogenerated caption they have is not helpful
|
|
|
|
return $thumbContain.closest( '#mw-category-media' ).length;
|
|
|
|
} )
|
2014-12-24 17:49:01 +00:00
|
|
|
.not( function () {
|
|
|
|
// do not treat special file related pages as galleries
|
|
|
|
var $specialFileRelatedPages = $( '.page-Special_NewFiles, .page-Special_MostLinkedFiles\
|
|
|
|
, .page-Special_MostGloballyLinkedFiles\
|
|
|
|
, .page-Special_UncategorizedFiles, .page-Special_UnusedFiles'
|
|
|
|
);
|
|
|
|
return $thumbContain.closest( $specialFileRelatedPages ).length;
|
|
|
|
} )
|
2014-04-25 23:16:48 +00:00
|
|
|
.find( '.gallerytext' )
|
|
|
|
.clone();
|
|
|
|
}
|
2014-12-30 22:00:37 +00:00
|
|
|
return this.htmlUtils.htmlToTextWithTags( $thumbCaption.html() || '' );
|
2014-04-25 23:16:48 +00:00
|
|
|
} else if ( $link.prop( 'title' ) ) {
|
|
|
|
return $link.prop( 'title' );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-02-17 15:09:23 +00:00
|
|
|
/**
|
2014-10-19 19:02:49 +00:00
|
|
|
* Opens MediaViewer and loads the given thumbnail. Requires processThumb() to be called first.
|
2014-04-29 01:35:07 +00:00
|
|
|
* @param {HTMLElement} element Clicked element
|
2014-02-17 15:09:23 +00:00
|
|
|
* @param {string} title File title
|
|
|
|
*/
|
2014-10-19 19:02:49 +00:00
|
|
|
MMVB.openImage = function ( element, title ) {
|
2014-02-17 15:09:23 +00:00
|
|
|
var $element = $( element );
|
|
|
|
|
2014-05-05 07:52:34 +00:00
|
|
|
mw.mmv.durationLogger.start( [ 'click-to-first-image', 'click-to-first-metadata' ] );
|
2014-04-28 16:11:55 +00:00
|
|
|
|
2014-02-17 15:09:23 +00:00
|
|
|
if ( $element.is( 'a.image' ) ) {
|
2014-05-19 09:24:54 +00:00
|
|
|
mw.mmv.actionLogger.log( 'thumbnail' );
|
2014-02-17 15:09:23 +00:00
|
|
|
} else if ( $element.is( '.magnify a' ) ) {
|
2014-05-19 09:24:54 +00:00
|
|
|
mw.mmv.actionLogger.log( 'enlarge' );
|
2014-02-17 15:09:23 +00:00
|
|
|
}
|
|
|
|
|
2014-09-12 17:13:42 +00:00
|
|
|
this.ensureEventHandlersAreSetUp();
|
|
|
|
|
2014-10-19 19:02:49 +00:00
|
|
|
return this.loadViewer().then( function ( viewer ) {
|
2014-04-14 23:22:28 +00:00
|
|
|
viewer.loadImageByTitle( title, true );
|
2014-02-17 15:09:23 +00:00
|
|
|
} );
|
2014-10-19 19:02:49 +00:00
|
|
|
};
|
2014-02-17 15:09:23 +00:00
|
|
|
|
2014-10-19 19:02:49 +00:00
|
|
|
/**
|
|
|
|
* Handles a click event on a link
|
|
|
|
* @param {HTMLElement} element Clicked element
|
|
|
|
* @param {jQuery.Event} e jQuery event object
|
|
|
|
* @param {string} title File title
|
|
|
|
* @returns {boolean} a value suitable for an event handler (ie. true if the click should be handled
|
|
|
|
* by the browser).
|
|
|
|
*/
|
|
|
|
MMVB.click = function ( element, e, title ) {
|
|
|
|
// Do not interfere with non-left clicks or if modifier keys are pressed.
|
|
|
|
if ( ( e.button !== 0 && e.which !== 1 ) || e.altKey || e.ctrlKey || e.shiftKey || e.metaKey ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't load if someone has specifically stopped us from doing so
|
|
|
|
if ( !this.config.isMediaViewerEnabledOnClick() ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't load if we already tried loading and it failed
|
|
|
|
if ( this.viewerIsBroken ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.openImage( element, title );
|
|
|
|
|
|
|
|
// calling this late so that in case of errors users at least get to the file page
|
2014-02-17 15:09:23 +00:00
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2014-10-19 19:02:49 +00:00
|
|
|
|
|
|
|
|
2014-02-17 15:09:23 +00:00
|
|
|
/**
|
2014-02-24 09:29:14 +00:00
|
|
|
* Handles the browser location hash on pageload or hash change
|
2014-04-15 16:14:06 +00:00
|
|
|
* @param {boolean} log Whether this is called for the hash that came with the pageload
|
2014-02-17 15:09:23 +00:00
|
|
|
*/
|
2014-04-15 16:14:06 +00:00
|
|
|
MMVB.hash = function ( initialHash ) {
|
2014-04-09 00:22:18 +00:00
|
|
|
var bootstrap = this;
|
|
|
|
|
2014-02-21 11:16:30 +00:00
|
|
|
// There is no point loading the mmv if it isn't loaded yet for hash changes unrelated to the mmv
|
|
|
|
// Such as anchor links on the page
|
2015-02-25 14:55:37 +00:00
|
|
|
if ( !this.viewerInitialized
|
|
|
|
&& window.location.hash.indexOf( '#mediaviewer/' ) !== 0
|
|
|
|
&& window.location.hash.indexOf( '#/media/' ) !== 0 ) {
|
2014-02-21 11:16:30 +00:00
|
|
|
return;
|
2014-02-17 15:09:23 +00:00
|
|
|
}
|
2014-02-21 11:16:30 +00:00
|
|
|
|
2014-02-24 09:29:14 +00:00
|
|
|
if ( this.skipNextHashHandling ) {
|
|
|
|
this.skipNextHashHandling = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-25 02:28:49 +00:00
|
|
|
this.loadViewer().then( function ( viewer ) {
|
|
|
|
viewer.hash();
|
2014-04-09 00:22:18 +00:00
|
|
|
// this is an ugly temporary fix to avoid a black screen of death when
|
|
|
|
// the page is loaded with an invalid MMV url
|
|
|
|
if ( !viewer.isOpen ) {
|
|
|
|
bootstrap.cleanupOverlay();
|
2014-04-15 16:14:06 +00:00
|
|
|
} else if ( initialHash ) {
|
2014-05-19 09:24:54 +00:00
|
|
|
mw.mmv.actionLogger.log( 'hash-load' );
|
2014-04-15 16:14:06 +00:00
|
|
|
} else {
|
2014-05-19 09:24:54 +00:00
|
|
|
mw.mmv.actionLogger.log( 'history-navigation' );
|
2014-04-09 00:22:18 +00:00
|
|
|
}
|
2014-02-21 11:16:30 +00:00
|
|
|
} );
|
2014-02-17 15:09:23 +00:00
|
|
|
};
|
|
|
|
|
2014-02-24 09:29:14 +00:00
|
|
|
/**
|
|
|
|
* Handles hash change requests coming from mmv
|
2014-04-11 09:31:38 +00:00
|
|
|
* @param {jQuery.Event} e Custom mmv-hash event
|
2014-02-24 09:29:14 +00:00
|
|
|
*/
|
|
|
|
MMVB.internalHashChange = function ( e ) {
|
2014-07-01 22:22:06 +00:00
|
|
|
var hash = e.hash,
|
|
|
|
title = e.title;
|
2014-04-11 09:31:38 +00:00
|
|
|
|
|
|
|
// The advantage of using pushState when it's available is that it has to ability to truly
|
|
|
|
// clear the hash, not leaving "#" in the history
|
|
|
|
// An entry with "#" in the history has the side-effect of resetting the scroll position when navigating the history
|
2014-05-14 10:02:39 +00:00
|
|
|
if ( this.browserHistory && this.browserHistory.pushState ) {
|
2014-04-11 09:31:38 +00:00
|
|
|
// In order to truly clear the hash, we need to reconstruct the hash-free URL
|
|
|
|
if ( hash === '#' ) {
|
|
|
|
hash = window.location.href.replace( /#.*$/, '' );
|
|
|
|
}
|
2014-05-14 10:02:39 +00:00
|
|
|
|
2015-07-15 22:52:46 +00:00
|
|
|
window.history.pushState( null, title, hash );
|
2014-04-11 09:31:38 +00:00
|
|
|
} else {
|
|
|
|
// Since we voluntarily changed the hash, we don't want MMVB.hash (which will trigger on hashchange event) to treat it
|
|
|
|
this.skipNextHashHandling = true;
|
2014-02-24 09:29:14 +00:00
|
|
|
|
2014-04-11 09:31:38 +00:00
|
|
|
window.location.hash = hash;
|
|
|
|
}
|
2014-07-01 22:22:06 +00:00
|
|
|
|
|
|
|
document.title = title;
|
2014-02-24 09:29:14 +00:00
|
|
|
};
|
|
|
|
|
2014-03-04 11:53:53 +00:00
|
|
|
/**
|
|
|
|
* Instantiates a new viewer if necessary
|
|
|
|
* @returns {mw.mmv.MultimediaViewer}
|
|
|
|
*/
|
|
|
|
MMVB.getViewer = function () {
|
|
|
|
if ( this.viewer === undefined ) {
|
2014-11-20 23:39:29 +00:00
|
|
|
this.viewer = new mw.mmv.MultimediaViewer( mw.config );
|
2014-03-04 11:53:53 +00:00
|
|
|
this.viewer.setupEventHandlers();
|
2014-12-30 07:13:22 +00:00
|
|
|
mw.mmv.viewer = this.viewer;
|
2014-03-04 11:53:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return this.viewer;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Listens to events on the window/document
|
|
|
|
*/
|
|
|
|
MMVB.setupEventHandlers = function () {
|
|
|
|
var self = this;
|
2014-02-17 15:09:23 +00:00
|
|
|
|
2014-09-12 17:13:42 +00:00
|
|
|
/** @property {boolean} eventHandlersHaveBeenSetUp tracks domready event handler state */
|
|
|
|
this.eventHandlersHaveBeenSetUp = true;
|
|
|
|
|
2014-05-16 09:06:40 +00:00
|
|
|
$( window ).on( this.browserHistory && this.browserHistory.pushState ? 'popstate.mmvb' : 'hashchange', function () {
|
2014-03-04 11:53:53 +00:00
|
|
|
self.hash();
|
2014-04-11 09:31:38 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
// Interpret any hash that might already be in the url
|
2014-04-15 16:14:06 +00:00
|
|
|
self.hash( true );
|
2014-03-04 11:53:53 +00:00
|
|
|
|
2014-04-11 09:31:38 +00:00
|
|
|
$( document ).on( 'mmv-hash', function ( e ) {
|
2014-03-04 11:53:53 +00:00
|
|
|
self.internalHashChange( e );
|
2014-04-11 09:31:38 +00:00
|
|
|
} ).on( 'mmv-cleanup-overlay', function () {
|
2014-03-28 08:06:53 +00:00
|
|
|
self.cleanupOverlay();
|
2014-03-04 11:53:53 +00:00
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cleans up event handlers, used for tests
|
|
|
|
*/
|
|
|
|
MMVB.cleanupEventHandlers = function () {
|
2014-04-11 09:31:38 +00:00
|
|
|
$( window ).off( 'hashchange popstate.mmvb' );
|
|
|
|
$( document ).off( 'mmv-hash' );
|
2014-09-12 17:13:42 +00:00
|
|
|
this.eventHandlersHaveBeenSetUp = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Makes sure event handlers are set up properly via MultimediaViewerBootstrap.setupEventHandlers().
|
|
|
|
* Called before loading the main mmv module. At this point, event handers for MultimediaViewerBootstrap
|
|
|
|
* should have been set up, but due to bug 70756 it cannot be guaranteed.
|
|
|
|
*/
|
|
|
|
MMVB.ensureEventHandlersAreSetUp = function () {
|
|
|
|
if ( !this.eventHandlersHaveBeenSetUp ) {
|
|
|
|
this.setupEventHandlers();
|
|
|
|
}
|
2014-03-04 11:53:53 +00:00
|
|
|
};
|
|
|
|
|
2014-03-28 08:06:53 +00:00
|
|
|
/**
|
|
|
|
* Sets up the overlay while the viewer loads
|
|
|
|
*/
|
|
|
|
MMVB.setupOverlay = function () {
|
2014-04-11 09:31:38 +00:00
|
|
|
var $scrollTo = $.scrollTo(),
|
|
|
|
$body = $( document.body );
|
|
|
|
|
|
|
|
// There are situations where we can call setupOverlay while the overlay is already there,
|
|
|
|
// such as inside this.hash(). In that case, do nothing
|
|
|
|
if ( $body.hasClass( 'mw-mmv-lightbox-open' ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-04-09 00:22:18 +00:00
|
|
|
if ( !this.$overlay ) {
|
|
|
|
this.$overlay = $( '<div>' )
|
|
|
|
.addClass( 'mw-mmv-overlay' );
|
|
|
|
}
|
2014-03-28 08:06:53 +00:00
|
|
|
|
2014-04-11 09:31:38 +00:00
|
|
|
this.savedScroll = { top : $scrollTo.scrollTop(), left : $scrollTo.scrollLeft() };
|
|
|
|
|
|
|
|
$body.addClass( 'mw-mmv-lightbox-open' )
|
2014-03-28 08:06:53 +00:00
|
|
|
.append( this.$overlay );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cleans up the overlay
|
|
|
|
*/
|
|
|
|
MMVB.cleanupOverlay = function () {
|
2014-04-23 00:26:07 +00:00
|
|
|
var bootstrap = this;
|
|
|
|
|
2014-03-31 21:33:12 +00:00
|
|
|
$( document.body ).removeClass( 'mw-mmv-lightbox-open' );
|
2014-03-28 08:06:53 +00:00
|
|
|
|
|
|
|
if ( this.$overlay ) {
|
|
|
|
this.$overlay.remove();
|
|
|
|
}
|
2014-04-11 09:31:38 +00:00
|
|
|
|
|
|
|
if ( this.savedScroll ) {
|
2014-04-23 00:26:07 +00:00
|
|
|
// setTimeout because otherwise Chrome will scroll back to top after the popstate event handlers run
|
2015-01-23 12:48:27 +00:00
|
|
|
setTimeout( function () { $.scrollTo( bootstrap.savedScroll, 0 ); bootstrap.savedScroll = undefined; }, 0 );
|
2014-04-11 09:31:38 +00:00
|
|
|
}
|
2014-03-28 08:06:53 +00:00
|
|
|
};
|
|
|
|
|
2014-03-28 22:39:36 +00:00
|
|
|
MMVB.whenThumbsReady = function () {
|
|
|
|
return this.thumbsReadyDeferred.promise();
|
|
|
|
};
|
|
|
|
|
2014-03-04 11:53:53 +00:00
|
|
|
mw.mmv.MultimediaViewerBootstrap = MultimediaViewerBootstrap;
|
2014-02-17 15:09:23 +00:00
|
|
|
}( mediaWiki, jQuery ) );
|