2014-02-04 23:44:36 +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/>.
|
|
|
|
*/
|
|
|
|
|
2018-11-12 16:33:24 +00:00
|
|
|
( function () {
|
2014-03-28 00:10:04 +00:00
|
|
|
var CBP;
|
|
|
|
|
2014-02-04 23:44:36 +00:00
|
|
|
/**
|
2014-03-28 00:10:04 +00:00
|
|
|
* Represents the buttons which are displayed over the image - next, previous, close
|
|
|
|
* and fullscreen.
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
2014-03-28 00:10:04 +00:00
|
|
|
* @class mw.mmv.ui.CanvasButtons
|
2014-02-04 23:44:36 +00:00
|
|
|
* @extends mw.mmv.ui.Element
|
|
|
|
* @constructor
|
2014-02-19 17:38:55 +00:00
|
|
|
* @param {jQuery} $container The parent element we should put the buttons into.
|
|
|
|
* @param {jQuery} $closeButton The close button element from the parent class.
|
|
|
|
* @param {jQuery} $fullscreenButton The fullscreen button from the parent class.
|
2014-02-04 23:44:36 +00:00
|
|
|
*/
|
2014-03-28 00:10:04 +00:00
|
|
|
function CanvasButtons( $container, $closeButton, $fullscreenButton ) {
|
2019-06-11 18:51:23 +00:00
|
|
|
var buttons = this;
|
2014-02-04 23:44:36 +00:00
|
|
|
|
|
|
|
mw.mmv.ui.Element.call( this, $container );
|
|
|
|
|
|
|
|
this.$close = $closeButton;
|
|
|
|
this.$fullscreen = $fullscreenButton;
|
|
|
|
|
2016-08-05 10:44:07 +00:00
|
|
|
this.$reuse = $( '<a>' )
|
|
|
|
.attr( 'role', 'button' )
|
2014-09-12 18:05:13 +00:00
|
|
|
.addClass( 'mw-mmv-reuse-button' )
|
2023-04-24 18:37:07 +00:00
|
|
|
.text( '\u00A0' )
|
2019-06-11 18:51:23 +00:00
|
|
|
.prop( 'title', mw.message( 'multimediaviewer-reuse-link' ).text() );
|
2014-09-16 20:33:05 +00:00
|
|
|
|
2017-03-18 06:38:33 +00:00
|
|
|
this.$options = $( '<button>' )
|
2014-07-23 23:00:37 +00:00
|
|
|
.text( ' ' )
|
2014-10-23 07:56:48 +00:00
|
|
|
.prop( 'title', mw.message( 'multimediaviewer-options-tooltip' ).text() )
|
2019-06-11 18:51:23 +00:00
|
|
|
.addClass( 'mw-mmv-options-button' );
|
2014-07-23 23:00:37 +00:00
|
|
|
|
2016-08-05 10:44:07 +00:00
|
|
|
this.$download = $( '<a>' )
|
|
|
|
.attr( 'role', 'button' )
|
2014-09-16 20:33:05 +00:00
|
|
|
.addClass( 'mw-mmv-download-button' )
|
2023-04-24 18:37:07 +00:00
|
|
|
.text( '\u00A0' )
|
2019-06-11 18:51:23 +00:00
|
|
|
.prop( 'title', mw.message( 'multimediaviewer-download-link' ).text() );
|
2014-09-12 18:05:13 +00:00
|
|
|
|
2017-03-18 06:38:33 +00:00
|
|
|
this.$next = $( '<button>' )
|
|
|
|
.prop( 'title', mw.message( 'multimediaviewer-next-image-alt-text' ).text() )
|
2014-03-31 21:33:12 +00:00
|
|
|
.addClass( 'mw-mmv-next-image disabled' )
|
2023-04-24 18:37:07 +00:00
|
|
|
.text( '\u00A0' );
|
2014-02-04 23:44:36 +00:00
|
|
|
|
2017-03-18 06:38:33 +00:00
|
|
|
this.$prev = $( '<button>' )
|
|
|
|
.prop( 'title', mw.message( 'multimediaviewer-prev-image-alt-text' ).text() )
|
2014-03-31 21:33:12 +00:00
|
|
|
.addClass( 'mw-mmv-prev-image disabled' )
|
2023-04-24 18:37:07 +00:00
|
|
|
.text( '\u00A0' );
|
2014-02-04 23:44:36 +00:00
|
|
|
|
|
|
|
this.$nav = this.$next
|
|
|
|
.add( this.$prev );
|
|
|
|
|
|
|
|
this.$buttons = this.$close
|
2014-09-16 20:33:05 +00:00
|
|
|
.add( this.$download )
|
2014-09-12 18:05:13 +00:00
|
|
|
.add( this.$reuse )
|
2014-02-04 23:44:36 +00:00
|
|
|
.add( this.$fullscreen )
|
2014-07-23 23:00:37 +00:00
|
|
|
.add( this.$options )
|
2014-02-04 23:44:36 +00:00
|
|
|
.add( this.$next )
|
2014-09-04 21:50:14 +00:00
|
|
|
.add( this.$prev );
|
2014-02-04 23:44:36 +00:00
|
|
|
|
|
|
|
this.$buttons.appendTo( this.$container );
|
|
|
|
|
2014-02-11 13:25:39 +00:00
|
|
|
$( document ).on( 'mmv-close', function () {
|
2014-02-04 23:44:36 +00:00
|
|
|
buttons.$nav.addClass( 'disabled' );
|
|
|
|
} );
|
|
|
|
|
2018-04-24 14:47:41 +00:00
|
|
|
this.$close.on( 'click', function () {
|
2014-02-04 23:44:36 +00:00
|
|
|
$container.trigger( $.Event( 'mmv-close' ) );
|
|
|
|
} );
|
|
|
|
|
2018-04-24 14:47:41 +00:00
|
|
|
this.$next.on( 'click', function () {
|
2015-09-25 19:55:00 +00:00
|
|
|
buttons.emit( 'next' );
|
2014-02-04 23:44:36 +00:00
|
|
|
} );
|
|
|
|
|
2018-04-24 14:47:41 +00:00
|
|
|
this.$prev.on( 'click', function () {
|
2015-09-25 19:55:00 +00:00
|
|
|
buttons.emit( 'prev' );
|
2014-02-04 23:44:36 +00:00
|
|
|
} );
|
|
|
|
}
|
2018-11-12 16:33:24 +00:00
|
|
|
OO.inheritClass( CanvasButtons, mw.mmv.ui.Element );
|
2014-03-28 00:10:04 +00:00
|
|
|
CBP = CanvasButtons.prototype;
|
2014-02-04 23:44:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the top offset for the navigation buttons.
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
2014-02-04 23:44:36 +00:00
|
|
|
* @param {number} offset
|
|
|
|
*/
|
2014-03-28 00:10:04 +00:00
|
|
|
CBP.setOffset = function ( offset ) {
|
2014-02-04 23:44:36 +00:00
|
|
|
this.$nav.css( {
|
|
|
|
top: offset
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stops the fading animation of the buttons and cancel any opacity value
|
|
|
|
*/
|
2014-03-28 00:10:04 +00:00
|
|
|
CBP.stopFade = function () {
|
2014-02-04 23:44:36 +00:00
|
|
|
this.$buttons
|
|
|
|
.stop( true )
|
2014-02-18 08:25:48 +00:00
|
|
|
.removeClass( 'hidden' )
|
2014-02-04 23:44:36 +00:00
|
|
|
.css( 'opacity', '' );
|
2014-02-18 08:25:48 +00:00
|
|
|
|
|
|
|
this.$container.trigger( $.Event( 'mmv-fade-stopped' ) );
|
2014-02-04 23:44:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Toggles buttons being disabled or not
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
2014-02-04 23:44:36 +00:00
|
|
|
* @param {boolean} showPrevButton
|
|
|
|
* @param {boolean} showNextButton
|
|
|
|
*/
|
2014-05-15 20:24:40 +00:00
|
|
|
CBP.toggle = function ( showPrevButton, showNextButton ) {
|
2014-02-04 23:44:36 +00:00
|
|
|
this.$next.toggleClass( 'disabled', !showPrevButton );
|
|
|
|
this.$prev.toggleClass( 'disabled', !showNextButton );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fades out the active buttons
|
|
|
|
*/
|
2014-03-28 00:10:04 +00:00
|
|
|
CBP.fadeOut = function () {
|
2014-02-04 23:44:36 +00:00
|
|
|
var buttons = this;
|
|
|
|
|
|
|
|
// We don't use animation chaining because delay() can't be stop()ed
|
2015-01-23 12:48:27 +00:00
|
|
|
this.buttonsFadeTimeout = setTimeout( function () {
|
2019-02-06 01:56:53 +00:00
|
|
|
// FIXME: Use CSS transition
|
2019-02-20 19:22:21 +00:00
|
|
|
// eslint-disable-next-line no-jquery/no-animate
|
2014-02-18 08:25:48 +00:00
|
|
|
buttons.$buttons.not( '.disabled' ).animate( { opacity: 0 }, 1000, 'swing',
|
|
|
|
function () {
|
|
|
|
buttons.$buttons.addClass( 'hidden' );
|
|
|
|
buttons.$container.trigger( $.Event( 'mmv-faded-out' ) );
|
|
|
|
} );
|
2014-02-04 23:44:36 +00:00
|
|
|
}, 1500 );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if any active buttons are currently hovered, given a position
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
2014-02-04 23:44:36 +00:00
|
|
|
* @param {number} x The horizontal coordinate of the position
|
|
|
|
* @param {number} y The vertical coordinate of the position
|
2016-07-18 13:49:27 +00:00
|
|
|
* @return {boolean}
|
2014-02-04 23:44:36 +00:00
|
|
|
*/
|
2014-03-28 00:10:04 +00:00
|
|
|
CBP.isAnyActiveButtonHovered = function ( x, y ) {
|
2014-02-04 23:44:36 +00:00
|
|
|
// We don't use mouseenter/mouseleave events because content is subject
|
|
|
|
// to change underneath the cursor, eg. when entering fullscreen or
|
|
|
|
// when going prev/next (the button can disappear when reaching ends)
|
|
|
|
var hovered = false;
|
|
|
|
|
2015-01-23 12:48:27 +00:00
|
|
|
this.$buttons.not( '.disabled' ).each( function ( idx, e ) {
|
2014-02-04 23:44:36 +00:00
|
|
|
var $e = $( e ),
|
|
|
|
offset = $e.offset();
|
|
|
|
|
2016-07-18 13:49:27 +00:00
|
|
|
if ( y >= offset.top &&
|
2017-06-07 14:52:52 +00:00
|
|
|
// using css( 'height' ) & css( 'width' ) instead of .height()
|
|
|
|
// and .width() since those don't include padding, and as a
|
|
|
|
// result can return a smaller size than is actually the button
|
|
|
|
y <= offset.top + parseInt( $e.css( 'height' ) ) &&
|
2016-07-18 13:49:27 +00:00
|
|
|
x >= offset.left &&
|
2017-06-07 14:52:52 +00:00
|
|
|
x <= offset.left + parseInt( $e.css( 'width' ) ) ) {
|
2014-02-04 23:44:36 +00:00
|
|
|
hovered = true;
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
return hovered;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reveals all active buttons and schedule a fade out if needed
|
2016-12-14 13:09:10 +00:00
|
|
|
*
|
|
|
|
* @param {Object} [mousePosition] Mouse position containing 'x' and 'y' properties
|
2014-02-04 23:44:36 +00:00
|
|
|
*/
|
2014-03-28 00:10:04 +00:00
|
|
|
CBP.revealAndFade = function ( mousePosition ) {
|
2014-02-04 23:44:36 +00:00
|
|
|
if ( this.buttonsFadeTimeout ) {
|
|
|
|
clearTimeout( this.buttonsFadeTimeout );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop ongoing animations and make sure the buttons that need to be displayed are displayed
|
|
|
|
this.stopFade();
|
|
|
|
|
|
|
|
// mousePosition can be empty, for instance when we enter fullscreen and haven't
|
|
|
|
// recorded a real mousemove event yet
|
2016-07-18 13:49:27 +00:00
|
|
|
if ( !mousePosition ||
|
|
|
|
!this.isAnyActiveButtonHovered( mousePosition.x, mousePosition.y ) ) {
|
2014-02-04 23:44:36 +00:00
|
|
|
this.fadeOut();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-09-12 18:05:13 +00:00
|
|
|
/**
|
|
|
|
* @event mmv-reuse-open
|
|
|
|
* Fired when the button to open the reuse dialog is clicked.
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* Registers listeners.
|
|
|
|
*/
|
|
|
|
CBP.attach = function () {
|
|
|
|
var buttons = this;
|
|
|
|
|
|
|
|
this.$reuse.on( 'click.mmv-canvasButtons', function ( e ) {
|
2014-07-23 23:00:37 +00:00
|
|
|
$( document ).trigger( 'mmv-reuse-open', e );
|
2016-08-05 10:44:07 +00:00
|
|
|
return false;
|
2014-09-12 18:05:13 +00:00
|
|
|
} );
|
|
|
|
this.handleEvent( 'mmv-reuse-opened', function () {
|
|
|
|
buttons.$reuse.addClass( 'open' );
|
|
|
|
} );
|
|
|
|
this.handleEvent( 'mmv-reuse-closed', function () {
|
|
|
|
buttons.$reuse.removeClass( 'open' );
|
|
|
|
} );
|
2014-09-16 20:33:05 +00:00
|
|
|
|
|
|
|
this.$download.on( 'click.mmv-canvasButtons', function ( e ) {
|
2014-07-23 23:00:37 +00:00
|
|
|
$( document ).trigger( 'mmv-download-open', e );
|
2016-08-05 10:44:07 +00:00
|
|
|
return false;
|
2014-09-16 20:33:05 +00:00
|
|
|
} );
|
|
|
|
this.handleEvent( 'mmv-download-opened', function () {
|
|
|
|
buttons.$download.addClass( 'open' );
|
|
|
|
} );
|
|
|
|
this.handleEvent( 'mmv-download-closed', function () {
|
|
|
|
buttons.$download.removeClass( 'open' );
|
|
|
|
} );
|
2014-07-23 23:00:37 +00:00
|
|
|
|
|
|
|
this.$options.on( 'click.mmv-canvasButtons', function ( e ) {
|
|
|
|
$( document ).trigger( 'mmv-options-open', e );
|
|
|
|
e.stopPropagation();
|
|
|
|
} );
|
|
|
|
this.handleEvent( 'mmv-options-opened', function () {
|
|
|
|
buttons.$options.addClass( 'open' );
|
|
|
|
} );
|
|
|
|
this.handleEvent( 'mmv-options-closed', function () {
|
|
|
|
buttons.$options.removeClass( 'open' );
|
|
|
|
} );
|
2014-11-19 10:46:39 +00:00
|
|
|
|
|
|
|
this.$download
|
|
|
|
.add( this.$reuse )
|
|
|
|
.add( this.$options )
|
|
|
|
.add( this.$close )
|
2019-06-11 18:51:23 +00:00
|
|
|
.add( this.$fullscreen );
|
2014-09-12 18:05:13 +00:00
|
|
|
};
|
|
|
|
|
2014-06-27 19:45:45 +00:00
|
|
|
/**
|
|
|
|
* Removes all UI things from the DOM, or hides them
|
|
|
|
*/
|
|
|
|
CBP.unattach = function () {
|
2019-05-28 21:11:18 +00:00
|
|
|
mw.mmv.ui.Element.prototype.unattach.call( this );
|
|
|
|
|
2014-11-19 10:46:39 +00:00
|
|
|
this.$download
|
|
|
|
.add( this.$reuse )
|
|
|
|
.add( this.$options )
|
|
|
|
.add( this.$close )
|
|
|
|
.add( this.$fullscreen )
|
2019-06-11 18:51:23 +00:00
|
|
|
.off( 'click.mmv-canvasButtons' );
|
2014-06-27 19:45:45 +00:00
|
|
|
};
|
|
|
|
|
2016-08-05 10:44:07 +00:00
|
|
|
/**
|
|
|
|
* @param {mw.mmv.model.Image} image
|
|
|
|
*/
|
|
|
|
CBP.set = function ( image ) {
|
|
|
|
this.$reuse.prop( 'href', image.descriptionUrl );
|
|
|
|
this.$download.prop( 'href', image.url );
|
|
|
|
};
|
|
|
|
|
2014-09-12 18:05:13 +00:00
|
|
|
CBP.empty = function () {
|
2016-08-05 10:44:07 +00:00
|
|
|
this.$reuse
|
|
|
|
.removeClass( 'open' )
|
|
|
|
.prop( 'href', null );
|
|
|
|
this.$download
|
|
|
|
.prop( 'href', null );
|
2014-09-12 18:05:13 +00:00
|
|
|
};
|
|
|
|
|
2014-03-28 00:10:04 +00:00
|
|
|
mw.mmv.ui.CanvasButtons = CanvasButtons;
|
2018-11-12 16:33:24 +00:00
|
|
|
}() );
|