2016-05-03 11:26:42 +00:00
|
|
|
( function ( mw, $ ) {
|
2016-06-17 13:06:12 +00:00
|
|
|
/**
|
|
|
|
* Module containing presentation logic for the revision pointers
|
|
|
|
*
|
|
|
|
* @param {Pointer} pointer
|
|
|
|
* @param {string} id
|
|
|
|
* @constructor
|
|
|
|
*/
|
2016-05-26 10:30:12 +00:00
|
|
|
var PointerView = function ( pointer, id ) {
|
2016-05-10 12:42:05 +00:00
|
|
|
this.pointer = pointer;
|
2016-05-13 10:29:05 +00:00
|
|
|
this.id = id;
|
2016-05-03 11:26:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
$.extend( PointerView.prototype, {
|
|
|
|
/**
|
|
|
|
* @type {string}
|
|
|
|
*/
|
2016-05-13 10:29:05 +00:00
|
|
|
id: '',
|
2016-05-03 11:26:42 +00:00
|
|
|
|
2016-05-10 12:42:05 +00:00
|
|
|
/**
|
|
|
|
* @type {Pointer}
|
|
|
|
*/
|
2016-06-17 13:06:12 +00:00
|
|
|
pointer: null,
|
2016-05-10 12:42:05 +00:00
|
|
|
|
2016-05-03 11:26:42 +00:00
|
|
|
/**
|
|
|
|
* @type {jQuery}
|
|
|
|
*/
|
|
|
|
$html: null,
|
|
|
|
|
2016-06-17 13:06:12 +00:00
|
|
|
/**
|
|
|
|
* Initializes the DOM element
|
|
|
|
*/
|
2016-05-03 11:26:42 +00:00
|
|
|
initialize: function () {
|
2016-06-03 08:55:17 +00:00
|
|
|
this.$html = $( '<div>' )
|
|
|
|
.attr( 'id', this.id )
|
2016-07-01 10:13:53 +00:00
|
|
|
.addClass( 'mw-revslider-pointer mw-revslider-pointer-cursor' );
|
2016-05-03 11:26:42 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {jQuery}
|
|
|
|
*/
|
|
|
|
render: function () {
|
|
|
|
this.initialize();
|
|
|
|
return this.getElement();
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {jQuery}
|
|
|
|
*/
|
|
|
|
getElement: function () {
|
|
|
|
return this.$html;
|
|
|
|
},
|
|
|
|
|
2016-06-17 13:06:12 +00:00
|
|
|
/**
|
|
|
|
* Returns whether a pointer is the upper/new revision pointer based on its CSS class
|
|
|
|
*
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
2016-05-26 10:30:12 +00:00
|
|
|
isUpperPointer: function () {
|
2016-07-01 10:13:53 +00:00
|
|
|
return this.getElement().hasClass( 'mw-revslider-pointer-upper' );
|
2016-05-26 10:30:12 +00:00
|
|
|
},
|
|
|
|
|
2016-06-17 13:06:12 +00:00
|
|
|
/**
|
|
|
|
* Returns the offset (margin-left) depending on whether its the upper or lower pointer
|
|
|
|
*
|
|
|
|
* @return {number}
|
|
|
|
*/
|
2016-05-03 11:26:42 +00:00
|
|
|
getOffset: function () {
|
2016-05-26 10:30:12 +00:00
|
|
|
return this.isUpperPointer() ? 16 : 0;
|
2016-05-10 12:42:05 +00:00
|
|
|
},
|
|
|
|
|
2016-05-21 00:10:08 +00:00
|
|
|
// For correct positioning of the pointer in the RTL mode the left position is flipped in the container.
|
|
|
|
// Additionally what must be taken into consideration is the width of the revision,
|
|
|
|
// and the extra space added on the right side of the pointer container used for correct
|
|
|
|
// restricting the pointer dragging area
|
|
|
|
getAdjustedLeftPositionWhenRtl: function ( pos ) {
|
|
|
|
return this.getElement().offsetParent().width() - pos - 16 - 15;
|
|
|
|
},
|
|
|
|
|
2016-06-17 13:06:12 +00:00
|
|
|
/**
|
|
|
|
* Moves the pointer to a position
|
|
|
|
*
|
|
|
|
* @param {number} posInPx
|
|
|
|
* @param {number} duration
|
|
|
|
* @return {jQuery}
|
|
|
|
*/
|
2016-05-10 14:01:39 +00:00
|
|
|
animateTo: function ( posInPx, duration ) {
|
2016-05-21 00:10:08 +00:00
|
|
|
var animatePos = { left: posInPx };
|
|
|
|
if ( this.getElement().css( 'direction' ) === 'rtl' ) {
|
|
|
|
animatePos.left = this.getAdjustedLeftPositionWhenRtl( animatePos.left );
|
|
|
|
}
|
|
|
|
return this.getElement().animate( animatePos, duration );
|
2016-05-10 12:42:05 +00:00
|
|
|
},
|
|
|
|
|
2016-06-17 13:06:12 +00:00
|
|
|
/**
|
|
|
|
* Slides the pointer to the revision it's pointing at
|
|
|
|
*
|
|
|
|
* @param {Slider} slider
|
|
|
|
* @param {number} duration
|
|
|
|
* @return {jQuery}
|
|
|
|
*/
|
2016-05-10 14:01:39 +00:00
|
|
|
slideToPosition: function ( slider, duration ) {
|
2016-05-12 15:19:14 +00:00
|
|
|
var relativePos = this.pointer.getPosition() - slider.getFirstVisibleRevisionIndex();
|
2016-05-26 09:50:51 +00:00
|
|
|
return this.animateTo( ( relativePos - 1 ) * slider.getView().revisionWidth, duration );
|
2016-05-10 12:42:05 +00:00
|
|
|
},
|
|
|
|
|
2016-06-17 13:06:12 +00:00
|
|
|
/**
|
|
|
|
* Slides the pointer to the side of the slider when it's not in the current range of revisions
|
|
|
|
*
|
|
|
|
* @param {Slider} slider
|
|
|
|
* @param {boolean} posBeforeSlider
|
|
|
|
* @param {number} duration
|
|
|
|
* @return {jQuery}
|
|
|
|
*/
|
2016-05-10 14:01:39 +00:00
|
|
|
slideToSide: function ( slider, posBeforeSlider, duration ) {
|
2016-05-10 12:42:05 +00:00
|
|
|
if ( posBeforeSlider ) {
|
2016-05-26 09:50:51 +00:00
|
|
|
return this.animateTo( this.getOffset() - 2 * slider.getView().revisionWidth, duration );
|
2016-05-10 12:42:05 +00:00
|
|
|
} else {
|
2016-05-26 09:50:51 +00:00
|
|
|
return this.animateTo( slider.getRevisionsPerWindow() * slider.getView().revisionWidth + this.getOffset(), duration );
|
2016-05-10 12:42:05 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-06-17 13:06: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}
|
|
|
|
*/
|
2016-05-10 14:01:39 +00:00
|
|
|
slideToSideOrPosition: function ( slider, duration ) {
|
2016-05-12 15:19:14 +00:00
|
|
|
var firstVisibleRev = slider.getFirstVisibleRevisionIndex(),
|
2016-05-10 12:42:05 +00:00
|
|
|
posBeforeSlider = this.pointer.getPosition() < firstVisibleRev,
|
2016-05-12 15:19:14 +00:00
|
|
|
isVisible = !posBeforeSlider && this.pointer.getPosition() <= firstVisibleRev + slider.getRevisionsPerWindow();
|
2016-05-10 12:42:05 +00:00
|
|
|
if ( isVisible ) {
|
2016-05-20 10:42:01 +00:00
|
|
|
return this.slideToPosition( slider, duration );
|
2016-05-10 12:42:05 +00:00
|
|
|
} else {
|
2016-05-20 10:42:01 +00:00
|
|
|
return this.slideToSide( slider, posBeforeSlider, duration );
|
2016-05-10 12:42:05 +00:00
|
|
|
}
|
2016-05-03 11:26:42 +00:00
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
mw.libs.revisionSlider = mw.libs.revisionSlider || {};
|
|
|
|
mw.libs.revisionSlider.PointerView = PointerView;
|
|
|
|
}( mediaWiki, jQuery ) );
|