2013-11-13 23:01:16 +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, $, oo, MLBInterface ) {
|
2013-11-25 23:51:40 +00:00
|
|
|
var LIP;
|
|
|
|
|
2014-01-11 00:05:03 +00:00
|
|
|
/**
|
|
|
|
* @class mw.LightboxInterface
|
|
|
|
* @extends mlb.LightboxInterface
|
2014-01-22 00:22:43 +00:00
|
|
|
* Represents the main interface of the lightbox
|
2014-01-11 00:05:03 +00:00
|
|
|
* @constructor
|
|
|
|
*/
|
2014-01-06 21:53:42 +00:00
|
|
|
function LightboxInterface( viewer ) {
|
2013-11-13 23:01:16 +00:00
|
|
|
MLBInterface.call( this );
|
2013-11-25 23:51:40 +00:00
|
|
|
|
2014-01-06 21:53:42 +00:00
|
|
|
this.viewer = viewer;
|
|
|
|
|
2013-12-12 21:31:14 +00:00
|
|
|
this.eventsRegistered = {};
|
|
|
|
|
2014-02-06 23:18:47 +00:00
|
|
|
/**
|
2014-02-13 09:52:40 +00:00
|
|
|
* @type {mw.mmv.ThumbnailWidthCalculator}
|
|
|
|
* @private
|
2014-02-06 23:18:47 +00:00
|
|
|
*/
|
2014-02-13 09:52:40 +00:00
|
|
|
this.thumbnailWidthCalculator = new mw.mmv.ThumbnailWidthCalculator();
|
|
|
|
|
2014-02-06 23:18:47 +00:00
|
|
|
|
2013-11-25 23:51:40 +00:00
|
|
|
this.initializeInterface();
|
2013-11-13 23:01:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
oo.inheritClass( LightboxInterface, MLBInterface );
|
|
|
|
|
2013-11-25 23:51:40 +00:00
|
|
|
LIP = LightboxInterface.prototype;
|
|
|
|
|
2013-12-06 20:10:51 +00:00
|
|
|
LIP.empty = function () {
|
2013-12-12 21:31:14 +00:00
|
|
|
this.clearEvents();
|
2013-12-06 20:10:51 +00:00
|
|
|
|
2014-02-07 14:47:00 +00:00
|
|
|
this.panel.empty();
|
2013-12-06 20:10:51 +00:00
|
|
|
|
|
|
|
this.$imageDiv.addClass( 'empty' );
|
|
|
|
|
|
|
|
MLBInterface.prototype.empty.call( this );
|
|
|
|
};
|
|
|
|
|
2013-12-12 21:31:14 +00:00
|
|
|
/**
|
|
|
|
* Add event handler in a way that will be auto-cleared on lightbox close
|
2014-02-07 14:47:00 +00:00
|
|
|
* NOTE: If you're changing this method you should probably do it in the
|
|
|
|
* mw.mmv.ui.Element version, which is where we'll be handling events
|
|
|
|
* from now on.
|
2013-12-12 21:31:14 +00:00
|
|
|
* @param {string} name Name of event, like 'keydown'
|
|
|
|
* @param {Function} handler Callback for the event
|
|
|
|
*/
|
|
|
|
LIP.handleEvent = function ( name, handler ) {
|
|
|
|
if ( this.eventsRegistered[name] === undefined ) {
|
|
|
|
this.eventsRegistered[name] = [];
|
|
|
|
}
|
|
|
|
this.eventsRegistered[name].push( handler );
|
|
|
|
$( document ).on( name, handler );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove all events that have been registered.
|
|
|
|
*/
|
|
|
|
LIP.clearEvents = function () {
|
|
|
|
var i, handlers, thisevent,
|
|
|
|
events = Object.keys( this.eventsRegistered );
|
|
|
|
|
|
|
|
for ( i = 0; i < events.length; i++ ) {
|
|
|
|
thisevent = events[i];
|
|
|
|
handlers = this.eventsRegistered[thisevent];
|
|
|
|
while ( handlers.length > 0 ) {
|
|
|
|
$( document ).off( thisevent, handlers.pop() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-01-08 15:14:52 +00:00
|
|
|
LIP.attach = function ( parentId ) {
|
|
|
|
// Advanced description needs to be below the fold when the lightbox opens
|
|
|
|
// regardless of what the scroll value was prior to opening the lightbox
|
|
|
|
|
2014-01-09 16:44:00 +00:00
|
|
|
// Only scroll and save the position if it's the first attach
|
|
|
|
// Otherwise it could be an attach event happening because of prev/next
|
|
|
|
if ( this.scrollTopBeforeAttach === undefined ) {
|
|
|
|
// Save the scrollTop value because we want below to be back to where they were
|
|
|
|
// before opening the lightbox
|
2014-01-23 00:38:01 +00:00
|
|
|
this.scrollTopBeforeAttach = $.scrollTo().scrollTop();
|
|
|
|
$.scrollTo( 0, 0 );
|
2014-01-09 16:44:00 +00:00
|
|
|
}
|
2014-01-08 15:14:52 +00:00
|
|
|
|
2014-01-31 10:35:50 +00:00
|
|
|
// Make sure that the metadata is going to be at the bottom when it appears
|
|
|
|
// 83 is the height of the top metadata area. Which can't be measured by
|
|
|
|
// reading the DOM at this point of the execution, unfortunately
|
|
|
|
this.$postDiv.css( 'top', ( $( window ).height() - 83 ) + 'px' );
|
|
|
|
|
2014-01-08 15:14:52 +00:00
|
|
|
MLBInterface.prototype.attach.call( this, parentId );
|
2014-02-03 11:23:31 +00:00
|
|
|
|
2014-02-13 20:50:05 +00:00
|
|
|
this.panel.attach();
|
|
|
|
|
2014-02-03 11:23:31 +00:00
|
|
|
// Buttons fading might not had been reset properly after a hard fullscreen exit
|
|
|
|
// This needs to happen after the parent attach() because the buttons need to be attached
|
|
|
|
// to the DOM for $.fn.stop() to work
|
2014-02-04 23:44:36 +00:00
|
|
|
this.buttons.stopFade();
|
2014-01-08 15:14:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
LIP.unattach = function () {
|
|
|
|
MLBInterface.prototype.unattach.call( this );
|
|
|
|
|
2014-02-13 20:50:05 +00:00
|
|
|
this.panel.unattach();
|
|
|
|
|
2014-01-08 15:14:52 +00:00
|
|
|
// Restore the scrollTop as it was before opening the lightbox
|
|
|
|
if ( this.scrollTopBeforeAttach !== undefined ) {
|
2014-01-23 00:38:01 +00:00
|
|
|
$.scrollTo( this.scrollTopBeforeAttach, 0 );
|
2014-01-09 16:44:00 +00:00
|
|
|
this.scrollTopBeforeAttach = undefined;
|
2014-01-08 15:14:52 +00:00
|
|
|
}
|
2014-02-04 23:44:36 +00:00
|
|
|
|
|
|
|
this.panel.fileReuse.closeDialog();
|
2014-01-08 15:14:52 +00:00
|
|
|
};
|
|
|
|
|
2014-02-13 09:52:40 +00:00
|
|
|
/**
|
|
|
|
* FIXME A bunch of stuff ripped out of load, because load tries to actually load the image
|
|
|
|
* and causes the small-thumbnail-for-a-moment bug in the process. Needs severe refactoring.
|
|
|
|
*/
|
|
|
|
LIP.setupForLoad = function() {
|
2014-01-23 00:38:01 +00:00
|
|
|
var hashFragment = '#mediaviewer/' + this.viewer.currentImageFilename + '/' + this.viewer.lightbox.currentIndex,
|
|
|
|
ui = this;
|
2013-11-25 23:51:40 +00:00
|
|
|
|
2014-01-06 21:53:42 +00:00
|
|
|
this.viewer.ui = this;
|
2013-11-25 23:51:40 +00:00
|
|
|
|
|
|
|
if ( !this.comingFromPopstate ) {
|
|
|
|
history.pushState( {}, '', hashFragment );
|
|
|
|
}
|
|
|
|
|
2014-01-23 00:38:01 +00:00
|
|
|
this.handleEvent( 'keydown', function( e ) { ui.keydown( e ); } );
|
2013-12-12 21:31:14 +00:00
|
|
|
|
2014-02-03 11:23:31 +00:00
|
|
|
// mousemove generates a ton of events, which is why we throttle it
|
|
|
|
this.handleEvent( 'mousemove.lip', $.throttle( 250, function( e ) {
|
|
|
|
ui.mousemove( e );
|
|
|
|
} ) );
|
2014-02-13 09:52:40 +00:00
|
|
|
};
|
2014-02-03 11:23:31 +00:00
|
|
|
|
2014-02-13 09:52:40 +00:00
|
|
|
LIP.load = function ( image ) {
|
|
|
|
this.setupForLoad();
|
2013-11-25 23:51:40 +00:00
|
|
|
MLBInterface.prototype.load.call( this, image );
|
|
|
|
};
|
|
|
|
|
|
|
|
LIP.initializeInterface = function () {
|
2014-02-07 14:47:00 +00:00
|
|
|
this.panel = new mw.mmv.ui.MetadataPanel( this.$postDiv, this.$controlBar );
|
2014-02-04 23:44:36 +00:00
|
|
|
this.buttons = new mw.mmv.ui.Buttons( this.$imageWrapper, this.$closeButton, this.$fullscreenButton );
|
2013-11-25 23:51:40 +00:00
|
|
|
this.initializeImage();
|
|
|
|
};
|
|
|
|
|
|
|
|
LIP.initializeImage = function () {
|
|
|
|
this.$imageDiv
|
|
|
|
.addClass( 'empty' );
|
|
|
|
};
|
|
|
|
|
2013-12-10 21:04:45 +00:00
|
|
|
LIP.replaceImageWith = function ( imageEle ) {
|
2014-02-12 01:50:56 +00:00
|
|
|
if ( this.$image.is( imageEle ) ) { // http://bugs.jquery.com/ticket/4087
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-10 21:04:45 +00:00
|
|
|
var $image = $( imageEle );
|
|
|
|
|
|
|
|
this.currentImage.src = imageEle.src;
|
|
|
|
|
|
|
|
this.$image.replaceWith( $image );
|
|
|
|
this.$image = $image;
|
|
|
|
|
|
|
|
this.$image.css( {
|
|
|
|
maxHeight: $image.parent().height(),
|
|
|
|
maxWidth: $image.parent().width()
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
2014-02-03 11:23:31 +00:00
|
|
|
LIP.fullscreenChange = function( e ) {
|
|
|
|
MLBInterface.prototype.fullscreenChange.call( this, e );
|
|
|
|
|
|
|
|
// Fullscreen change events can happen after unattach(), in which
|
|
|
|
// case we shouldn't do anything UI-related
|
|
|
|
if ( !this.currentlyAttached ) {
|
|
|
|
return;
|
|
|
|
}
|
2014-01-06 21:53:42 +00:00
|
|
|
|
2014-02-03 11:23:31 +00:00
|
|
|
if ( this.isFullscreen ) {
|
|
|
|
// When entering fullscreen without a mousemove, the browser
|
|
|
|
// still thinks that the cursor is where it was prior to entering
|
|
|
|
// fullscreen. I.e. on top of the fullscreen button
|
|
|
|
// Thus, we purposefully reset the saved position, so that
|
|
|
|
// the fade out really takes place (otherwise it's cancelled
|
|
|
|
// by updateControls which is called a few times when fullscreen opens)
|
|
|
|
this.mousePosition = { x: 0, y: 0 };
|
2014-02-04 23:44:36 +00:00
|
|
|
this.buttons.fadeOut();
|
2014-02-03 11:23:31 +00:00
|
|
|
}
|
2014-01-06 21:53:42 +00:00
|
|
|
};
|
|
|
|
|
2014-01-23 00:38:01 +00:00
|
|
|
/**
|
|
|
|
* @method
|
|
|
|
* Handles keydown events on the document
|
|
|
|
* @param {jQuery.Event} e The jQuery keypress event object
|
|
|
|
*/
|
|
|
|
LIP.keydown = function ( e ) {
|
|
|
|
var isRtl = $( document.body ).hasClass( 'rtl' );
|
|
|
|
|
|
|
|
switch ( e.which ) {
|
|
|
|
case 37:
|
|
|
|
// Left arrow
|
|
|
|
if ( isRtl ) {
|
|
|
|
this.viewer.nextImage();
|
|
|
|
} else {
|
|
|
|
this.viewer.prevImage();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 39:
|
|
|
|
// Right arrow
|
|
|
|
if ( isRtl ) {
|
|
|
|
this.viewer.prevImage();
|
|
|
|
} else {
|
|
|
|
this.viewer.nextImage();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-02-03 11:23:31 +00:00
|
|
|
/**
|
|
|
|
* @method
|
|
|
|
* Handles mousemove events on the document
|
|
|
|
*/
|
|
|
|
LIP.mousemove = function ( e ) {
|
|
|
|
if ( e ) {
|
|
|
|
// Saving the mouse position is useful whenever we need to
|
|
|
|
// run LIP.mousemove manually, such as when going to the next/prev
|
|
|
|
// element
|
2014-02-04 23:44:36 +00:00
|
|
|
this.mousePosition = { x: e.pageX, y: e.pageY };
|
2014-02-03 11:23:31 +00:00
|
|
|
}
|
|
|
|
|
2014-02-04 23:44:36 +00:00
|
|
|
if ( this.isFullscreen ) {
|
|
|
|
this.buttons.revealAndFade( this.mousePosition );
|
2014-02-03 11:23:31 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @method
|
|
|
|
* Updates the next and prev buttons
|
2014-02-03 23:20:54 +00:00
|
|
|
* @param {boolean} showPrevButton Whether the prev button should be revealed or not
|
|
|
|
* @param {boolean} showNextButton Whether the next button should be revealed or not
|
2014-02-03 11:23:31 +00:00
|
|
|
*/
|
|
|
|
LIP.updateControls = function ( showPrevButton, showNextButton ) {
|
|
|
|
var prevNextTop = ( ( this.$imageWrapper.height() / 2 ) - 60 ) + 'px';
|
|
|
|
|
2014-02-06 10:23:03 +00:00
|
|
|
if ( this.$main.data( 'isFullscreened' ) ) {
|
|
|
|
this.$postDiv.css( 'top', '' );
|
|
|
|
} else {
|
|
|
|
this.$postDiv.css( 'top', this.$imageWrapper.height() );
|
|
|
|
}
|
2014-02-03 11:23:31 +00:00
|
|
|
|
2014-02-04 23:44:36 +00:00
|
|
|
this.buttons.setOffset( prevNextTop );
|
|
|
|
this.buttons.toggle( showPrevButton, showNextButton );
|
2014-02-03 11:23:31 +00:00
|
|
|
|
2014-02-04 23:44:36 +00:00
|
|
|
if ( this.isFullscreen ) {
|
|
|
|
this.buttons.revealAndFade( this.mousePosition );
|
|
|
|
}
|
2014-02-03 11:23:31 +00:00
|
|
|
};
|
|
|
|
|
2014-02-06 23:18:47 +00:00
|
|
|
/**
|
2014-02-13 09:52:40 +00:00
|
|
|
* Gets the widths for a given lightbox image.
|
|
|
|
* @param {mlb.LightboxImage} image
|
|
|
|
* @returns {mw.mmv.model.ThumbnailWidth}
|
2014-02-06 23:18:47 +00:00
|
|
|
*/
|
2014-02-13 09:52:40 +00:00
|
|
|
LIP.getLightboxImageWidths = function ( image ) {
|
|
|
|
var thumb = image.thumbnail;
|
2014-02-06 23:18:47 +00:00
|
|
|
|
|
|
|
return this.thumbnailWidthCalculator.calculateWidths(
|
|
|
|
this.$imageWrapper.width(), this.$imageWrapper.height(), thumb.width, thumb.height );
|
|
|
|
};
|
|
|
|
|
2014-02-13 09:52:40 +00:00
|
|
|
/**
|
|
|
|
* Gets the widths for the current lightbox image.
|
|
|
|
* @returns {mw.mmv.model.ThumbnailWidth}
|
|
|
|
*/
|
|
|
|
LIP.getCurrentImageWidths = function () {
|
|
|
|
return this.getLightboxImageWidths( this.currentImage );
|
|
|
|
};
|
2014-02-06 23:18:47 +00:00
|
|
|
|
2013-12-11 02:08:07 +00:00
|
|
|
mw.LightboxInterface = LightboxInterface;
|
2013-12-02 19:43:02 +00:00
|
|
|
}( mediaWiki, jQuery, OO, window.LightboxInterface ) );
|