2014-05-05 21:44:27 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the MediaWiki extension MediaViewer.
|
|
|
|
*
|
|
|
|
* MediaViewer 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.
|
|
|
|
*
|
|
|
|
* MediaViewer 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 MediaViewer. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2018-11-12 16:33:24 +00:00
|
|
|
( function () {
|
2014-05-05 21:44:27 +00:00
|
|
|
var MPSP;
|
|
|
|
|
|
|
|
/**
|
2016-07-18 13:49:27 +00:00
|
|
|
* Handles scrolling behavior of the metadata panel.
|
|
|
|
*
|
2014-05-05 21:44:27 +00:00
|
|
|
* @class mw.mmv.ui.MetadataPanelScroller
|
|
|
|
* @extends mw.mmv.ui.Element
|
|
|
|
* @constructor
|
2014-07-16 23:15:38 +00:00
|
|
|
* @param {jQuery} $container The container for the panel (.mw-mmv-post-image).
|
|
|
|
* @param {jQuery} $aboveFold The control bar element (.mw-mmv-above-fold).
|
2017-07-10 09:42:42 +00:00
|
|
|
* @param {mw.SafeStorage} localStorage the localStorage object, for dependency injection
|
2014-05-05 21:44:27 +00:00
|
|
|
*/
|
2014-07-16 23:15:38 +00:00
|
|
|
function MetadataPanelScroller( $container, $aboveFold, localStorage ) {
|
2014-05-05 21:44:27 +00:00
|
|
|
mw.mmv.ui.Element.call( this, $container );
|
|
|
|
|
2014-07-16 23:15:38 +00:00
|
|
|
this.$aboveFold = $aboveFold;
|
2014-05-05 21:44:27 +00:00
|
|
|
|
2017-07-10 09:42:42 +00:00
|
|
|
/** @property {mw.SafeStorage} localStorage */
|
2014-05-05 21:44:27 +00:00
|
|
|
this.localStorage = localStorage;
|
|
|
|
|
2014-11-11 15:21:36 +00:00
|
|
|
/** @property {boolean} panelWasOpen state flag which will be used to detect open <-> closed transitions */
|
|
|
|
this.panelWasOpen = null;
|
2014-07-16 23:15:38 +00:00
|
|
|
|
2014-06-17 22:23:18 +00:00
|
|
|
/**
|
|
|
|
* Whether this user has ever opened the metadata panel.
|
|
|
|
* Based on a localstorage flag; will be set to true if the client does not support localstorage.
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
|
|
|
this.hasOpenedMetadata = undefined;
|
|
|
|
|
2014-05-05 21:44:27 +00:00
|
|
|
/**
|
|
|
|
* Whether we've already fired an animation for the metadata div in this lightbox session.
|
|
|
|
* @property {boolean}
|
|
|
|
* @private
|
|
|
|
*/
|
2014-06-17 22:23:18 +00:00
|
|
|
this.hasAnimatedMetadata = false;
|
2014-05-05 21:44:27 +00:00
|
|
|
|
|
|
|
this.initialize();
|
|
|
|
}
|
2018-11-12 16:33:24 +00:00
|
|
|
OO.inheritClass( MetadataPanelScroller, mw.mmv.ui.Element );
|
2014-05-05 21:44:27 +00:00
|
|
|
MPSP = MetadataPanelScroller.prototype;
|
|
|
|
|
2015-01-23 12:48:27 +00:00
|
|
|
MPSP.attach = function () {
|
2014-05-05 21:44:27 +00:00
|
|
|
var panel = this;
|
|
|
|
|
|
|
|
this.handleEvent( 'keydown', function ( e ) {
|
|
|
|
panel.keydown( e );
|
|
|
|
} );
|
|
|
|
|
2017-09-13 18:04:22 +00:00
|
|
|
$( window ).on( 'scroll.mmvp', $.throttle( 250, function () {
|
2014-05-05 21:44:27 +00:00
|
|
|
panel.scroll();
|
|
|
|
} ) );
|
|
|
|
|
2014-07-16 23:15:38 +00:00
|
|
|
this.$container.on( 'mmv-metadata-open', function () {
|
2017-06-02 09:29:41 +00:00
|
|
|
if ( !panel.hasOpenedMetadata && panel.localStorage.store ) {
|
2014-07-16 23:15:38 +00:00
|
|
|
panel.hasOpenedMetadata = true;
|
2017-06-02 09:29:41 +00:00
|
|
|
panel.localStorage.set( 'mmv.hasOpenedMetadata', '1' );
|
2014-07-16 23:15:38 +00:00
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
2014-05-05 21:44:27 +00:00
|
|
|
// reset animation flag when the viewer is reopened
|
2014-06-17 22:23:18 +00:00
|
|
|
this.hasAnimatedMetadata = false;
|
2014-05-05 21:44:27 +00:00
|
|
|
};
|
|
|
|
|
2015-01-23 12:48:27 +00:00
|
|
|
MPSP.unattach = function () {
|
2014-05-05 21:44:27 +00:00
|
|
|
this.clearEvents();
|
2017-09-13 18:04:22 +00:00
|
|
|
$( window ).off( 'scroll.mmvp' );
|
2014-07-16 23:15:38 +00:00
|
|
|
this.$container.off( 'mmv-metadata-open' );
|
2014-05-05 21:44:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
MPSP.empty = function () {
|
|
|
|
// need to remove this to avoid animating again when reopening lightbox on same page
|
|
|
|
this.$container.removeClass( 'invite' );
|
2014-07-16 23:15:38 +00:00
|
|
|
|
2014-11-11 15:21:36 +00:00
|
|
|
this.panelWasOpen = this.panelIsOpen();
|
2014-05-05 21:44:27 +00:00
|
|
|
};
|
|
|
|
|
2014-11-11 23:44:53 +00:00
|
|
|
/**
|
|
|
|
* Returns scroll top position when the panel is fully open.
|
|
|
|
* (In other words, the height of the area that is outside the screen, in pixels.)
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
2014-11-11 23:44:53 +00:00
|
|
|
* @return {number}
|
|
|
|
*/
|
|
|
|
MPSP.getScrollTopWhenOpen = function () {
|
2016-07-18 13:49:27 +00:00
|
|
|
return this.$container.outerHeight() - parseInt( this.$aboveFold.css( 'min-height' ), 10 ) -
|
|
|
|
parseInt( this.$aboveFold.css( 'padding-bottom' ), 10 );
|
2014-11-11 23:44:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Makes sure the panel does not contract when it is emptied and thus keeps its position as much as possible.
|
|
|
|
* This should be called when switching images, before the panel is emptied, and should be undone with
|
|
|
|
* unfreezeHeight after the panel has been populeted with the new metadata.
|
|
|
|
*/
|
|
|
|
MPSP.freezeHeight = function () {
|
2016-07-18 13:49:27 +00:00
|
|
|
var scrollTop, scrollTopWhenOpen;
|
|
|
|
|
2014-11-13 08:55:12 +00:00
|
|
|
if ( !this.$container.is( ':visible' ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-13 18:04:22 +00:00
|
|
|
scrollTop = $( window ).scrollTop();
|
2016-07-18 13:49:27 +00:00
|
|
|
scrollTopWhenOpen = this.getScrollTopWhenOpen();
|
2014-11-11 23:44:53 +00:00
|
|
|
|
|
|
|
this.panelWasFullyOpen = ( scrollTop === scrollTopWhenOpen );
|
|
|
|
this.$container.css( 'min-height', this.$container.height() );
|
|
|
|
};
|
|
|
|
|
|
|
|
MPSP.unfreezeHeight = function () {
|
2014-11-13 08:55:12 +00:00
|
|
|
if ( !this.$container.is( ':visible' ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-11-11 23:44:53 +00:00
|
|
|
this.$container.css( 'min-height', '' );
|
|
|
|
if ( this.panelWasFullyOpen ) {
|
2017-09-13 18:04:22 +00:00
|
|
|
$( window ).scrollTop( this.getScrollTopWhenOpen() );
|
2014-11-11 23:44:53 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-05-05 21:44:27 +00:00
|
|
|
MPSP.initialize = function () {
|
2017-06-02 09:29:41 +00:00
|
|
|
var value = this.localStorage.get( 'mmv.hasOpenedMetadata' );
|
|
|
|
|
|
|
|
// localStorage will only store strings; if values `null`, `false` or
|
|
|
|
// `0` are set, they'll come out as `"null"`, `"false"` or `"0"`, so we
|
|
|
|
// can be certain that an actual null is a failure to locate the item,
|
|
|
|
// and false is an issue with localStorage itself
|
|
|
|
if ( value !== false ) {
|
|
|
|
this.hasOpenedMetadata = value !== null;
|
|
|
|
} else {
|
|
|
|
// if there was an issue with localStorage, treat it as opened
|
2015-04-02 12:15:24 +00:00
|
|
|
this.hasOpenedMetadata = true;
|
|
|
|
}
|
2014-05-05 21:44:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Animates the metadata area when the viewer is first opened.
|
|
|
|
*/
|
|
|
|
MPSP.animateMetadataOnce = function () {
|
2014-06-17 22:23:18 +00:00
|
|
|
if ( !this.hasOpenedMetadata && !this.hasAnimatedMetadata ) {
|
2014-05-05 21:44:27 +00:00
|
|
|
this.hasAnimatedMetadata = true;
|
|
|
|
this.$container.addClass( 'invite' );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Toggles the metadata div being totally visible.
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
2014-06-26 01:26:49 +00:00
|
|
|
* @param {string} [forceDirection] 'up' or 'down' makes the panel move on that direction (and is a noop
|
|
|
|
* if the panel is already at the upmost/bottommost position); without the parameter, the panel position
|
|
|
|
* is toggled. (Partially open counts as open.)
|
2017-09-13 18:04:22 +00:00
|
|
|
* @return {jQuery.Promise} A promise which resolves after the animation has finished.
|
2014-05-05 21:44:27 +00:00
|
|
|
*/
|
2014-11-22 00:16:36 +00:00
|
|
|
MPSP.toggle = function ( forceDirection ) {
|
2017-09-13 18:04:22 +00:00
|
|
|
var scrollTopWhenOpen = this.getScrollTopWhenOpen(),
|
2014-05-05 21:44:27 +00:00
|
|
|
scrollTopWhenClosed = 0,
|
2017-09-13 18:04:22 +00:00
|
|
|
scrollTop = $( window ).scrollTop(),
|
2014-05-05 21:44:27 +00:00
|
|
|
panelIsOpen = scrollTop > scrollTopWhenClosed,
|
2014-11-22 00:16:36 +00:00
|
|
|
direction = forceDirection || ( panelIsOpen ? 'down' : 'up' ),
|
|
|
|
scrollTopTarget = ( direction === 'up' ) ? scrollTopWhenOpen : scrollTopWhenClosed;
|
2014-05-05 21:44:27 +00:00
|
|
|
|
2014-06-26 01:26:49 +00:00
|
|
|
// don't log / animate if the panel is already in the end position
|
|
|
|
if ( scrollTopTarget === scrollTop ) {
|
2017-09-13 18:04:22 +00:00
|
|
|
return $.Deferred().resolve().promise();
|
2014-06-26 01:26:49 +00:00
|
|
|
} else {
|
2014-11-22 00:16:36 +00:00
|
|
|
mw.mmv.actionLogger.log( direction === 'up' ? 'metadata-open' : 'metadata-close' );
|
|
|
|
if ( direction === 'up' && !panelIsOpen ) {
|
|
|
|
// FIXME nasty. This is not really an event but a command sent to the metadata panel;
|
|
|
|
// child UI elements should not send commands to their parents. However, there is no way
|
|
|
|
// to calculate the target scrollTop accurately without revealing the text, and the event
|
|
|
|
// which does that (metadata-open) is only triggered later in the process, when the panel
|
|
|
|
// actually scrolled, so we cannot use it here without risking triggering it multiple times.
|
|
|
|
this.$container.trigger( 'mmv-metadata-reveal-truncated-text' );
|
|
|
|
scrollTopTarget = this.getScrollTopWhenOpen();
|
|
|
|
}
|
2019-02-20 19:22:21 +00:00
|
|
|
// eslint-disable-next-line no-jquery/no-global-selector
|
2017-09-13 18:04:22 +00:00
|
|
|
return $( 'html, body' ).animate( { scrollTop: scrollTopTarget }, 'fast' ).promise();
|
2014-06-26 01:26:49 +00:00
|
|
|
}
|
2014-05-05 21:44:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles keydown events for this element.
|
2016-12-14 13:09:10 +00:00
|
|
|
*
|
|
|
|
* @param {jQuery.Event} e Key down event
|
2014-05-05 21:44:27 +00:00
|
|
|
*/
|
|
|
|
MPSP.keydown = function ( e ) {
|
2014-12-29 06:51:23 +00:00
|
|
|
if ( e.altKey || e.shiftKey || e.ctrlKey || e.metaKey ) {
|
|
|
|
return;
|
|
|
|
}
|
2014-05-05 21:44:27 +00:00
|
|
|
switch ( e.which ) {
|
2014-06-10 20:01:19 +00:00
|
|
|
case 40: // Down arrow
|
|
|
|
// fall through
|
|
|
|
case 38: // Up arrow
|
|
|
|
this.toggle();
|
2014-05-05 21:44:27 +00:00
|
|
|
e.preventDefault();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-11-11 15:21:36 +00:00
|
|
|
/**
|
|
|
|
* Returns whether the metadata panel is open. (Partially open is considered to be open.)
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
2014-11-11 15:21:36 +00:00
|
|
|
* @return {boolean}
|
|
|
|
*/
|
|
|
|
MPSP.panelIsOpen = function () {
|
2017-09-13 18:04:22 +00:00
|
|
|
return $( window ).scrollTop() > 0;
|
2014-11-11 15:21:36 +00:00
|
|
|
};
|
|
|
|
|
2014-05-05 21:44:27 +00:00
|
|
|
/**
|
2014-10-21 20:29:53 +00:00
|
|
|
* Receives the window's scroll events and and turns them into business logic events
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
2014-10-21 20:29:53 +00:00
|
|
|
* @fires mmv-metadata-open
|
|
|
|
* @fires mmv-metadata-close
|
2014-05-05 21:44:27 +00:00
|
|
|
*/
|
|
|
|
MPSP.scroll = function () {
|
2014-11-11 15:21:36 +00:00
|
|
|
var panelIsOpen = this.panelIsOpen();
|
2014-07-16 23:15:38 +00:00
|
|
|
|
2014-11-11 15:21:36 +00:00
|
|
|
if ( panelIsOpen && !this.panelWasOpen ) { // just opened
|
2014-07-16 23:15:38 +00:00
|
|
|
this.$container.trigger( 'mmv-metadata-open' );
|
2014-11-25 00:52:15 +00:00
|
|
|
// This will include keyboard- and mouseclick-initiated open events as well,
|
|
|
|
// since the panel is anomated, which counts as scrolling.
|
|
|
|
// Filtering these seems too much trouble to be worth it.
|
|
|
|
mw.mmv.actionLogger.log( 'metadata-scroll-open' );
|
2014-11-11 15:21:36 +00:00
|
|
|
} else if ( !panelIsOpen && this.panelWasOpen ) { // just closed
|
2014-07-16 23:15:38 +00:00
|
|
|
this.$container.trigger( 'mmv-metadata-close' );
|
2014-11-25 00:52:15 +00:00
|
|
|
mw.mmv.actionLogger.log( 'metadata-scroll-close' );
|
2014-05-05 21:44:27 +00:00
|
|
|
}
|
2014-11-11 15:21:36 +00:00
|
|
|
this.panelWasOpen = panelIsOpen;
|
2014-05-05 21:44:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
mw.mmv.ui.MetadataPanelScroller = MetadataPanelScroller;
|
2018-11-12 16:33:24 +00:00
|
|
|
}() );
|