Debian Codegraph @

extensions/WikiEditor/modules/realtimepreview/ResizingDragBar.js

raw symbols SCIP
1 /**
2 * @class
3 * @constructor
4 * @extends OO.ui.Element
5 * @param {Object} [config] Configuration options
6 * @param {boolean} [config.isEW] Orientation of the drag bar, East-West (true) or North-South (false).
7 */
8 function ResizingDragBar( config ) {
9 config = Object.assign( {}, {
10 isEW: true,
11 classes: [ 'ext-WikiEditor-ResizingDragBar' ]
12 }, config );
13 ResizingDragBar.super.call( this, config );
14
15 var classNameDir = 'ext-WikiEditor-ResizingDragBar-' + ( config.isEW ? 'ew' : 'ns' );
16 // Possible class names:
17 // * ext-WikiEditor-ResizingDragBar-ew
18 // * ext-WikiEditor-ResizingDragBar-ns
19 this.$element.addClass( classNameDir );
20
21 var resizingDragBar = this;
22 // Determine the horizontal direction to move (flexbox automatically reverses but the offset direction doesn't).
23 var rtlFactor = config.isEW && OO.ui.Element.static.getDir( document ) === 'rtl' ? -1 : 1;
24 this.$element.on( 'mousedown', function ( eventMousedown ) {
25 if ( eventMousedown.button !== ResizingDragBar.static.MAIN_MOUSE_BUTTON ) {
26 // If not the main mouse (e.g. left) button, ignore.
27 return;
28 }
29 // Prevent selecting (or anything else) when dragging over other parts of the page.
30 $( document ).on( 'selectstart.' + classNameDir, false );
31 // Set up parameter names.
32 var xOrY = config.isEW ? 'pageX' : 'pageY';
33 var widthOrHeight = config.isEW ? 'width' : 'height';
34 var lastOffset = eventMousedown[ xOrY ];
35 // Handle the actual dragging.
36 $( document ).on( 'mousemove.' + classNameDir, function ( eventMousemove ) {
37 // Initial width or height of the pane.
38 var startSize = resizingDragBar.getResizedPane()[ widthOrHeight ]();
39 // Current position of the mouse (relative to page, not viewport).
40 var newOffset = eventMousemove[ xOrY ];
41 // Distance the mouse has moved.
42 var change = rtlFactor * ( lastOffset - newOffset );
43 // Set the new size of the pane, and tell others about it.
44 var newSize = Math.max( startSize - change, ResizingDragBar.static.MIN_PANE_SIZE );
45 resizingDragBar.getResizedPane().css( widthOrHeight, newSize );
46 // Save the new starting point of the mouse, from which to calculate the next move.
47 lastOffset = newOffset;
48 // Let other scripts do things after the resize.
49 mw.hook( 'ext.WikiEditor.realtimepreview.resize' ).fire( resizingDragBar );
50 } );
51 } );
52 // Add a UI affordance within the handle area (CSS gives it its appearance).
53 this.$element.append( $( '<span>' ) );
54 // Remove the resize event handler when the mouse is released.
55 $( document ).on( 'mouseup', function () {
56 $( document ).off( 'mousemove.' + classNameDir );
57 $( document ).off( 'selectstart.' + classNameDir, false );
58 } );
59 }
60
61 OO.inheritClass( ResizingDragBar, OO.ui.Element );
62
63 /**
64 * @static
65 * @property {number} See https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/button
66 */
67 ResizingDragBar.static.MAIN_MOUSE_BUTTON = 0;
68
69 /**
70 * @static
71 * @property {number} The minimum pane size, in pixels.
72 * Should be slightly more than the affordance length.
73 * This should match the max-width of the .ext-WikiEditor-twopanes-pane2 element.
74 */
75 ResizingDragBar.static.MIN_PANE_SIZE = 248;
76
77 /**
78 * Get the pane that is resized by this bar (always the immediate prior sibling).
79 *
80 * @public
81 * @return {jQuery}
82 */
83 ResizingDragBar.prototype.getResizedPane = function () {
84 return this.$element.prev();
85 };
86
87 module.exports = ResizingDragBar;