2021-11-18 10:50:17 +00:00
|
|
|
/**
|
|
|
|
* @class
|
|
|
|
* @constructor
|
|
|
|
* @extends OO.ui.Element
|
|
|
|
* @param {Object} [config] Configuration options
|
|
|
|
* @param {boolean} [config.isEW] Orientation of the drag bar, East-West (true) or North-South (false).
|
|
|
|
*/
|
|
|
|
function ResizingDragBar( config ) {
|
2024-06-19 04:21:38 +00:00
|
|
|
config = Object.assign( {}, {
|
2021-11-18 10:50:17 +00:00
|
|
|
isEW: true,
|
|
|
|
classes: [ 'ext-WikiEditor-ResizingDragBar' ]
|
|
|
|
}, config );
|
|
|
|
ResizingDragBar.super.call( this, config );
|
|
|
|
|
|
|
|
var classNameDir = 'ext-WikiEditor-ResizingDragBar-' + ( config.isEW ? 'ew' : 'ns' );
|
|
|
|
// Possible class names:
|
|
|
|
// * ext-WikiEditor-ResizingDragBar-ew
|
|
|
|
// * ext-WikiEditor-ResizingDragBar-ns
|
|
|
|
this.$element.addClass( classNameDir );
|
|
|
|
|
|
|
|
var resizingDragBar = this;
|
2022-03-25 02:51:08 +00:00
|
|
|
// Determine the horizontal direction to move (flexbox automatically reverses but the offset direction doesn't).
|
|
|
|
var rtlFactor = config.isEW && OO.ui.Element.static.getDir( document ) === 'rtl' ? -1 : 1;
|
2024-11-15 10:55:47 +00:00
|
|
|
this.$element.on( 'mousedown', ( eventMousedown ) => {
|
2021-11-18 10:50:17 +00:00
|
|
|
if ( eventMousedown.button !== ResizingDragBar.static.MAIN_MOUSE_BUTTON ) {
|
|
|
|
// If not the main mouse (e.g. left) button, ignore.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Prevent selecting (or anything else) when dragging over other parts of the page.
|
|
|
|
$( document ).on( 'selectstart.' + classNameDir, false );
|
|
|
|
// Set up parameter names.
|
|
|
|
var xOrY = config.isEW ? 'pageX' : 'pageY';
|
|
|
|
var widthOrHeight = config.isEW ? 'width' : 'height';
|
|
|
|
var lastOffset = eventMousedown[ xOrY ];
|
|
|
|
// Handle the actual dragging.
|
2024-11-15 10:55:47 +00:00
|
|
|
$( document ).on( 'mousemove.' + classNameDir, ( eventMousemove ) => {
|
2021-11-18 10:50:17 +00:00
|
|
|
// Initial width or height of the pane.
|
|
|
|
var startSize = resizingDragBar.getResizedPane()[ widthOrHeight ]();
|
|
|
|
// Current position of the mouse (relative to page, not viewport).
|
|
|
|
var newOffset = eventMousemove[ xOrY ];
|
|
|
|
// Distance the mouse has moved.
|
2022-03-25 02:51:08 +00:00
|
|
|
var change = rtlFactor * ( lastOffset - newOffset );
|
2021-11-18 10:50:17 +00:00
|
|
|
// Set the new size of the pane, and tell others about it.
|
|
|
|
var newSize = Math.max( startSize - change, ResizingDragBar.static.MIN_PANE_SIZE );
|
|
|
|
resizingDragBar.getResizedPane().css( widthOrHeight, newSize );
|
|
|
|
// Save the new starting point of the mouse, from which to calculate the next move.
|
|
|
|
lastOffset = newOffset;
|
|
|
|
// Let other scripts do things after the resize.
|
|
|
|
mw.hook( 'ext.WikiEditor.realtimepreview.resize' ).fire( resizingDragBar );
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
// Add a UI affordance within the handle area (CSS gives it its appearance).
|
|
|
|
this.$element.append( $( '<span>' ) );
|
|
|
|
// Remove the resize event handler when the mouse is released.
|
2024-11-15 10:55:47 +00:00
|
|
|
$( document ).on( 'mouseup', () => {
|
2021-11-18 10:50:17 +00:00
|
|
|
$( document ).off( 'mousemove.' + classNameDir );
|
|
|
|
$( document ).off( 'selectstart.' + classNameDir, false );
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
OO.inheritClass( ResizingDragBar, OO.ui.Element );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @static
|
|
|
|
* @property {number} See https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/button
|
|
|
|
*/
|
|
|
|
ResizingDragBar.static.MAIN_MOUSE_BUTTON = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @static
|
|
|
|
* @property {number} The minimum pane size, in pixels.
|
|
|
|
* Should be slightly more than the affordance length.
|
2022-07-21 16:31:57 +00:00
|
|
|
* This should match the max-width of the .ext-WikiEditor-twopanes-pane2 element.
|
2021-11-18 10:50:17 +00:00
|
|
|
*/
|
2022-07-21 16:31:57 +00:00
|
|
|
ResizingDragBar.static.MIN_PANE_SIZE = 248;
|
2021-11-18 10:50:17 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the pane that is resized by this bar (always the immediate prior sibling).
|
|
|
|
*
|
|
|
|
* @public
|
|
|
|
* @return {jQuery}
|
|
|
|
*/
|
|
|
|
ResizingDragBar.prototype.getResizedPane = function () {
|
|
|
|
return this.$element.prev();
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = ResizingDragBar;
|