2020-11-18 09:11:12 +00:00
|
|
|
/**
|
|
|
|
* Module containing presentation logic for the revision pointers
|
|
|
|
*
|
|
|
|
* @class PointerView
|
|
|
|
* @param {Pointer} pointer
|
|
|
|
* @param {string} name
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
function PointerView( pointer, name ) {
|
|
|
|
this.pointer = pointer;
|
|
|
|
this.name = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
$.extend( PointerView.prototype, {
|
2016-06-17 13:06:12 +00:00
|
|
|
/**
|
2020-11-18 09:11:12 +00:00
|
|
|
* @type {string}
|
2016-06-17 13:06:12 +00:00
|
|
|
*/
|
2020-11-18 09:11:12 +00:00
|
|
|
name: '',
|
2016-05-03 11:26:42 +00:00
|
|
|
|
2020-11-18 09:11:12 +00:00
|
|
|
/**
|
|
|
|
* @type {Pointer}
|
|
|
|
*/
|
|
|
|
pointer: null,
|
2016-05-10 12:42:05 +00:00
|
|
|
|
2020-11-18 09:11:12 +00:00
|
|
|
/**
|
2023-10-23 12:46:10 +00:00
|
|
|
* @type {jQuery|null}
|
2020-11-18 09:11:12 +00:00
|
|
|
*/
|
|
|
|
$html: null,
|
2016-05-03 11:26:42 +00:00
|
|
|
|
2020-11-18 09:11:12 +00:00
|
|
|
/**
|
|
|
|
* Initializes the DOM element
|
|
|
|
*/
|
|
|
|
initialize: function () {
|
|
|
|
// eslint-disable-next-line mediawiki/class-doc
|
|
|
|
this.$html = $( '<div>' )
|
|
|
|
.addClass( 'mw-revslider-pointer mw-revslider-pointer-cursor ' + this.name );
|
|
|
|
},
|
2016-05-03 11:26:42 +00:00
|
|
|
|
2020-11-18 09:11:12 +00:00
|
|
|
/**
|
|
|
|
* @return {jQuery}
|
|
|
|
*/
|
|
|
|
render: function () {
|
|
|
|
this.initialize();
|
2023-10-23 12:46:10 +00:00
|
|
|
return this.$html;
|
2020-11-18 09:11:12 +00:00
|
|
|
},
|
2016-05-03 11:26:42 +00:00
|
|
|
|
2020-11-18 09:11:12 +00:00
|
|
|
/**
|
2023-10-23 12:46:10 +00:00
|
|
|
* @return {jQuery|null} Null if not initialized
|
2020-11-18 09:11:12 +00:00
|
|
|
*/
|
|
|
|
getElement: function () {
|
|
|
|
return this.$html;
|
|
|
|
},
|
2016-05-03 11:26:42 +00:00
|
|
|
|
2020-11-18 09:11:12 +00:00
|
|
|
/**
|
|
|
|
* Returns whether a pointer is the newer revision pointer based on its CSS class
|
|
|
|
*
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
|
|
|
isNewerPointer: function () {
|
2023-10-23 12:46:10 +00:00
|
|
|
return this.$html.hasClass( 'mw-revslider-pointer-newer' );
|
2020-11-18 09:11:12 +00:00
|
|
|
},
|
2016-05-10 12:42:05 +00:00
|
|
|
|
2020-11-18 09:11:12 +00:00
|
|
|
// For correct positioning of the pointer in the RTL mode the left position is flipped in the container.
|
|
|
|
// 30 pixel have to be added to cover the arrow and its margin.
|
|
|
|
getAdjustedLeftPositionWhenRtl: function ( pos ) {
|
2023-10-23 12:46:10 +00:00
|
|
|
return this.$html.offsetParent().width() - pos - 30;
|
2020-11-18 09:11:12 +00:00
|
|
|
},
|
2016-05-21 00:10:08 +00:00
|
|
|
|
2020-11-18 09:11:12 +00:00
|
|
|
/**
|
|
|
|
* Sets the HTML attribute for the position
|
|
|
|
*
|
|
|
|
* @param {number} pos
|
|
|
|
*/
|
|
|
|
setDataPositionAttribute: function ( pos ) {
|
2023-10-23 12:46:10 +00:00
|
|
|
if ( !this.$html ) {
|
2020-11-18 09:11:12 +00:00
|
|
|
this.initialize();
|
|
|
|
}
|
2023-10-23 12:46:10 +00:00
|
|
|
this.$html.attr( 'data-pos', pos );
|
2020-11-18 09:11:12 +00:00
|
|
|
},
|
2017-07-11 11:41:16 +00:00
|
|
|
|
2020-11-18 09:11:12 +00:00
|
|
|
/**
|
|
|
|
* Moves the pointer to a position
|
|
|
|
*
|
2023-10-24 13:45:31 +00:00
|
|
|
* @private
|
2020-11-18 09:11:12 +00:00
|
|
|
* @param {number} posInPx
|
|
|
|
* @param {number} revisionWidth
|
|
|
|
* @param {number} [baseDuration] Duration per revisionWidth, is adjusted by log() distance
|
|
|
|
* @return {jQuery}
|
|
|
|
*/
|
|
|
|
animateTo: function ( posInPx, revisionWidth, baseDuration ) {
|
2023-06-22 09:41:37 +00:00
|
|
|
const animatePos = { left: posInPx },
|
2023-10-23 12:46:10 +00:00
|
|
|
currentPos = this.$html.position();
|
2021-10-07 13:55:44 +00:00
|
|
|
|
2020-11-18 09:11:12 +00:00
|
|
|
baseDuration = typeof baseDuration !== 'undefined' ? baseDuration : 100;
|
2023-10-23 12:46:10 +00:00
|
|
|
if ( this.$html.css( 'direction' ) === 'rtl' ) {
|
2020-11-18 09:11:12 +00:00
|
|
|
animatePos.left = this.getAdjustedLeftPositionWhenRtl( animatePos.left );
|
|
|
|
}
|
2023-06-22 09:41:37 +00:00
|
|
|
const distance = Math.abs( animatePos.left - currentPos.left ) / revisionWidth;
|
|
|
|
const duration = baseDuration * Math.log( 5 + distance );
|
2023-10-23 12:46:10 +00:00
|
|
|
// eslint-disable-next-line no-jquery/no-animate
|
|
|
|
return this.$html.animate( animatePos, duration, 'linear' );
|
2020-11-18 09:11:12 +00:00
|
|
|
},
|
2016-05-10 12:42:05 +00:00
|
|
|
|
2020-11-18 09:11:12 +00:00
|
|
|
/**
|
|
|
|
* Slides the pointer to the revision it's pointing at
|
|
|
|
*
|
2023-10-24 13:45:31 +00:00
|
|
|
* @private
|
2020-11-18 09:11:12 +00:00
|
|
|
* @param {Slider} slider
|
|
|
|
* @param {number} [duration]
|
|
|
|
* @return {jQuery}
|
|
|
|
*/
|
|
|
|
slideToPosition: function ( slider, duration ) {
|
2023-06-22 09:41:37 +00:00
|
|
|
const relativePos = this.pointer.getPosition() - slider.getOldestVisibleRevisionIndex();
|
2020-11-18 09:11:12 +00:00
|
|
|
return this.animateTo( ( relativePos - 1 ) * slider.getView().revisionWidth, slider.getView().revisionWidth, duration );
|
|
|
|
},
|
2016-05-10 12:42:05 +00:00
|
|
|
|
2020-11-18 09:11:12 +00:00
|
|
|
/**
|
|
|
|
* Slides the pointer to the side of the slider when it's not in the current range of revisions
|
|
|
|
*
|
2023-10-24 13:45:31 +00:00
|
|
|
* @private
|
2020-11-18 09:11:12 +00:00
|
|
|
* @param {Slider} slider
|
|
|
|
* @param {boolean} posBeforeSlider
|
|
|
|
* @param {number} [duration]
|
|
|
|
* @return {jQuery}
|
|
|
|
*/
|
|
|
|
slideToSide: function ( slider, posBeforeSlider, duration ) {
|
2023-10-23 12:46:10 +00:00
|
|
|
const margin = this.isNewerPointer() ? 16 : 0;
|
|
|
|
let x = slider.getView().revisionWidth;
|
|
|
|
x *= posBeforeSlider ? -2 : slider.getRevisionsPerWindow();
|
|
|
|
return this.animateTo( x + margin, slider.getView().revisionWidth, duration );
|
2020-11-18 09:11:12 +00:00
|
|
|
},
|
2016-05-10 12:42:05 +00:00
|
|
|
|
2020-11-18 09:11:12 +00:00
|
|
|
/**
|
|
|
|
* Decides based on its position whether the pointer should be sliding to the side or to its position
|
|
|
|
*
|
|
|
|
* @param {Slider} slider
|
|
|
|
* @param {number} [duration]
|
|
|
|
* @return {jQuery}
|
|
|
|
*/
|
|
|
|
slideToSideOrPosition: function ( slider, duration ) {
|
2023-06-22 09:41:37 +00:00
|
|
|
const firstVisibleRev = slider.getOldestVisibleRevisionIndex(),
|
2020-11-18 09:11:12 +00:00
|
|
|
posBeforeSlider = this.pointer.getPosition() < firstVisibleRev,
|
|
|
|
isVisible = !posBeforeSlider && this.pointer.getPosition() <= firstVisibleRev + slider.getRevisionsPerWindow();
|
|
|
|
if ( isVisible ) {
|
|
|
|
return this.slideToPosition( slider, duration );
|
|
|
|
} else {
|
|
|
|
return this.slideToSide( slider, posBeforeSlider, duration );
|
2016-05-03 11:26:42 +00:00
|
|
|
}
|
2020-11-18 09:11:12 +00:00
|
|
|
}
|
|
|
|
} );
|
2016-05-03 11:26:42 +00:00
|
|
|
|
2020-11-18 09:11:12 +00:00
|
|
|
module.exports = PointerView;
|