2020-11-18 09:11:12 +00:00
|
|
|
/**
|
|
|
|
* Module containing presentation logic for the revision pointer lines
|
|
|
|
*
|
|
|
|
* @class PointerLine
|
|
|
|
* @param {Pointer} pointer
|
|
|
|
* @param {string} name
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
function PointerLine( pointer, name ) {
|
|
|
|
this.pointer = pointer;
|
|
|
|
this.name = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
$.extend( PointerLine.prototype, {
|
|
|
|
/**
|
|
|
|
* @type {string}
|
|
|
|
*/
|
|
|
|
name: '',
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @type {Pointer}
|
|
|
|
*/
|
|
|
|
pointer: null,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @type {jQuery}
|
|
|
|
*/
|
|
|
|
$html: null,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate the relative distance in between the given pointer and column
|
|
|
|
*
|
2023-10-24 13:45:31 +00:00
|
|
|
* @private
|
2020-11-18 09:11:12 +00:00
|
|
|
* @param {jQuery} $sourcePointer
|
|
|
|
* @param {jQuery} $targetColumn
|
|
|
|
* @return {number} distance between the given elements
|
|
|
|
*/
|
|
|
|
calculateDistance: function ( $sourcePointer, $targetColumn ) {
|
|
|
|
return ( $targetColumn.offset().left + $targetColumn.width() / 2 ) -
|
|
|
|
( $sourcePointer.offset().left + ( $sourcePointer.width() ) / 2 );
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate and set line's width and position with the given pointer and column
|
|
|
|
*
|
2023-10-24 13:45:31 +00:00
|
|
|
* @private
|
2023-11-03 10:11:30 +00:00
|
|
|
* @param {jQuery} $sliderView
|
2020-11-18 09:11:12 +00:00
|
|
|
* @param {jQuery} $sourcePointer
|
|
|
|
* @param {jQuery} $targetColumn
|
|
|
|
*/
|
2023-11-03 10:11:30 +00:00
|
|
|
setCssProperties: function ( $sliderView, $sourcePointer, $targetColumn ) {
|
2023-06-22 09:41:37 +00:00
|
|
|
const distance = this.calculateDistance( $sourcePointer, $targetColumn );
|
2020-11-18 09:11:12 +00:00
|
|
|
|
2023-06-22 09:41:37 +00:00
|
|
|
const widthToSet = Math.abs( distance );
|
|
|
|
let leftToSet = ( $targetColumn.offset().left + $targetColumn.width() / 2 ) -
|
2023-11-03 10:11:30 +00:00
|
|
|
$sliderView.offset().left;
|
2020-11-18 09:11:12 +00:00
|
|
|
|
|
|
|
if ( distance > 0 ) {
|
|
|
|
// targetColumn is right relative to sourcePointer
|
|
|
|
leftToSet -= widthToSet;
|
|
|
|
leftToSet -= 1;
|
|
|
|
} else {
|
|
|
|
// targetColumn is left relative to sourcePointer
|
|
|
|
leftToSet += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.$html.css( {
|
|
|
|
width: widthToSet + 'px',
|
|
|
|
left: leftToSet + 'px'
|
|
|
|
} );
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the target column is located right form the source pointer
|
|
|
|
*
|
2023-10-24 13:45:31 +00:00
|
|
|
* @private
|
2020-11-18 09:11:12 +00:00
|
|
|
* @param {jQuery} $sourcePointer
|
|
|
|
* @param {jQuery} $targetColumn
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
|
|
|
targetColumnIsRightFromPointer: function ( $sourcePointer, $targetColumn ) {
|
|
|
|
return this.calculateDistance( $sourcePointer, $targetColumn ) > 0;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Draws the line between pointer and column by setting borders, position and width of the line box
|
|
|
|
*/
|
|
|
|
drawLine: function () {
|
2023-10-24 08:28:28 +00:00
|
|
|
const isNewer = this.pointer.getView().isNewerPointer();
|
2023-11-03 10:11:30 +00:00
|
|
|
const $sliderView = $( '.mw-revslider-revision-slider' );
|
2023-10-24 08:28:28 +00:00
|
|
|
const $targetColumn = $( isNewer ? '.diff-ntitle' : '.diff-otitle' );
|
|
|
|
|
2023-11-06 21:14:58 +00:00
|
|
|
if ( !$sliderView.length || !$targetColumn.length ) {
|
2023-08-25 15:49:59 +00:00
|
|
|
return;
|
2021-10-07 13:55:44 +00:00
|
|
|
}
|
|
|
|
|
2023-06-22 09:41:37 +00:00
|
|
|
const $upperLineDiv = this.$html.find( '.mw-revslider-pointer-line-upper' ),
|
2020-11-18 09:11:12 +00:00
|
|
|
$lowerLineDiv = this.$html.find( '.mw-revslider-pointer-line-lower' ),
|
2023-08-25 15:49:59 +00:00
|
|
|
$underline = this.$html.find( '.mw-revslider-pointer-line-underline' ),
|
2023-10-24 08:28:28 +00:00
|
|
|
$sourcePointer = this.pointer.getView().getElement();
|
2020-11-18 09:11:12 +00:00
|
|
|
|
2023-08-25 15:49:59 +00:00
|
|
|
$lowerLineDiv.add( $upperLineDiv ).add( $underline )
|
2021-07-01 07:15:46 +00:00
|
|
|
.toggleClass( 'mw-revslider-lower-color', !isNewer )
|
|
|
|
.toggleClass( 'mw-revslider-upper-color', isNewer );
|
2020-11-18 09:11:12 +00:00
|
|
|
|
2023-11-03 10:11:30 +00:00
|
|
|
this.setCssProperties( $sliderView, $sourcePointer, $targetColumn );
|
2020-11-18 09:11:12 +00:00
|
|
|
|
2023-10-24 08:28:28 +00:00
|
|
|
const columnWidth = $targetColumn.width();
|
2020-11-18 09:11:12 +00:00
|
|
|
$upperLineDiv.addClass( 'mw-revslider-bottom-line' );
|
2023-10-24 08:28:28 +00:00
|
|
|
$underline.css( 'width', columnWidth + 'px' );
|
2020-11-18 09:11:12 +00:00
|
|
|
|
|
|
|
if ( this.targetColumnIsRightFromPointer( $sourcePointer, $targetColumn ) ) {
|
|
|
|
$upperLineDiv.addClass( 'mw-revslider-left-line' );
|
|
|
|
$lowerLineDiv.addClass( 'mw-revslider-right-line' );
|
|
|
|
|
2023-08-25 15:49:59 +00:00
|
|
|
$underline.css( {
|
2023-10-24 08:28:28 +00:00
|
|
|
'margin-right': -columnWidth / 2 + 'px',
|
2020-11-18 09:11:12 +00:00
|
|
|
'margin-left': 0,
|
|
|
|
float: 'right'
|
|
|
|
} );
|
|
|
|
} else {
|
|
|
|
$upperLineDiv.addClass( 'mw-revslider-right-line' );
|
|
|
|
$lowerLineDiv.addClass( 'mw-revslider-left-line' );
|
|
|
|
|
2023-08-25 15:49:59 +00:00
|
|
|
$underline.css( {
|
2023-10-24 08:28:28 +00:00
|
|
|
'margin-left': -columnWidth / 2 + 'px',
|
2020-11-18 09:11:12 +00:00
|
|
|
'margin-right': 0,
|
|
|
|
float: 'left'
|
2016-11-02 18:22:40 +00:00
|
|
|
} );
|
|
|
|
}
|
2020-11-18 09:11:12 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes the DOM element with the line-box for drawing the lines
|
|
|
|
*/
|
|
|
|
initialize: function () {
|
|
|
|
// eslint-disable-next-line mediawiki/class-doc
|
|
|
|
this.$html = $( '<div>' )
|
|
|
|
.addClass( 'mw-revslider-pointer-line ' + this.name )
|
|
|
|
.append(
|
|
|
|
// eslint-disable-next-line mediawiki/class-doc
|
|
|
|
$( '<div>' ).addClass( 'mw-revslider-pointer-line-upper ' + this.name ),
|
|
|
|
// eslint-disable-next-line mediawiki/class-doc
|
|
|
|
$( '<div>' ).addClass( 'mw-revslider-pointer-line-lower ' + this.name ),
|
|
|
|
// eslint-disable-next-line mediawiki/class-doc
|
|
|
|
$( '<div>' ).addClass( 'mw-revslider-pointer-line-underline ' + this.name )
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds colored top-borders for the diff columns fitting the line colors between pointers and columns
|
2023-10-24 14:08:50 +00:00
|
|
|
*
|
|
|
|
* @param {boolean} [show=true]
|
2023-10-24 13:45:31 +00:00
|
|
|
* @private
|
2020-11-18 09:11:12 +00:00
|
|
|
*/
|
2023-10-24 14:08:50 +00:00
|
|
|
addColoredColumnBorders: function ( show ) {
|
|
|
|
show = show !== false;
|
|
|
|
$( '#mw-diff-otitle1' ).toggleClass( 'mw-revslider-older-diff-column', show );
|
|
|
|
$( '#mw-diff-ntitle1' ).toggleClass( 'mw-revslider-newer-diff-column', show );
|
2020-11-18 09:11:12 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove colored top-borders for the diff columns fitting the line colors between pointers and columns
|
2023-10-24 13:45:31 +00:00
|
|
|
*
|
|
|
|
* @private
|
2020-11-18 09:11:12 +00:00
|
|
|
*/
|
|
|
|
removeColoredColumnBorders: function () {
|
2023-10-24 14:08:50 +00:00
|
|
|
this.addColoredColumnBorders( false );
|
2020-11-18 09:11:12 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the hooks to draw the column borders
|
2023-10-24 13:45:31 +00:00
|
|
|
*
|
|
|
|
* @private
|
2020-11-18 09:11:12 +00:00
|
|
|
*/
|
|
|
|
setColumnBorderHooks: function () {
|
|
|
|
mw.hook( 'wikipage.diff' ).add( this.addColoredColumnBorders );
|
|
|
|
mw.hook( 'revslider.expand' ).add( this.addColoredColumnBorders );
|
2023-11-03 09:34:42 +00:00
|
|
|
mw.hook( 'revslider.collapse' ).add( this.removeColoredColumnBorders.bind( this ) );
|
2020-11-18 09:11:12 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {jQuery}
|
|
|
|
*/
|
|
|
|
render: function () {
|
|
|
|
this.initialize();
|
|
|
|
this.setColumnBorderHooks();
|
|
|
|
return this.getElement();
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {jQuery}
|
|
|
|
*/
|
|
|
|
getElement: function () {
|
|
|
|
return this.$html;
|
|
|
|
}
|
|
|
|
|
|
|
|
} );
|
2016-11-02 18:22:40 +00:00
|
|
|
|
2020-11-18 09:11:12 +00:00
|
|
|
module.exports = PointerLine;
|