# all files / src/mixins/ ClippableElement.js

26.43% Statements 37/140
11.11% Branches 7/63
30.77% Functions 4/13
26.43% Lines 37/140

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393

    1×

    7648×

    7648×
    7648×
    7648×
    7648×
    7648×
    7648×
    7648×
    7648×
    7648×
    7648×
    7648×
    7648×

    7648×
    1920×

    7648×

    1×
    7648×

    7648×
    7648×

    1×
    1920×
    1920×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×
    15656×

    15656×

    15656×

    1×

     /**
     * Element that can be automatically clipped to visible boundaries.
     *
     * Whenever the element's natural height changes, you have to call
     * {@link OO.ui.mixin.ClippableElement#clip} to make sure it's still
     * clipping correctly.
     *
     * The dimensions of #$clippableContainer will be compared to the boundaries of the
     * nearest scrollable container. If #$clippableContainer is too tall and/or too wide,
     * then #$clippable will be given a fixed reduced height and/or width and will be made
     * scrollable. By default, #$clippable and #$clippableContainer are the same element,
     * but you can build a static footer by setting #$clippableContainer to an element that contains
     * #$clippable and the footer.
     *
     * @abstract
     * @class
     *
     * @constructor
     * @param {Object} [config] Configuration options
     * @cfg {jQuery} [$clippable] Node to clip, assigned to #$clippable, omit to use #$element
     * @cfg {jQuery} [$clippableContainer] Node to keep visible, assigned to #$clippableContainer,
     *   omit to use #$clippable
     */
    OO.ui.mixin.ClippableElement = function OoUiMixinClippableElement( config ) {
    	// Configuration initialization
    	config = config || {};

    	// Properties
    	this.$clippable = null;
    	this.$clippableContainer = null;
    	this.clipping = false;
    	this.clippedHorizontally = false;
    	this.clippedVertically = false;
    	this.$clippableScrollableContainer = null;
    	this.$clippableScroller = null;
    	this.$clippableWindow = null;
    	this.idealWidth = null;
    	this.idealHeight = null;
    	this.onClippableScrollHandler = this.clip.bind( this );
    	this.onClippableWindowResizeHandler = this.clip.bind( this );

    	// Initialization
    	if ( config.$clippableContainer ) {
    		this.setClippableContainer( config.$clippableContainer );
    	}
    	this.setClippableElement( config.$clippable || this.$element );
    };

    /* Methods */

    /**
     * Set clippable element.
     *
     * If an element is already set, it will be cleaned up before setting up the new element.
     *
     * @param {jQuery} $clippable Element to make clippable
     */
    OO.ui.mixin.ClippableElement.prototype.setClippableElement = function ( $clippable ) {
    	Iif ( this.$clippable ) {
    		this.$clippable.removeClass( 'oo-ui-clippableElement-clippable' );
    		this.$clippable.css( { width: '', height: '', overflowX: '', overflowY: '' } );
    		OO.ui.Element.static.reconsiderScrollbars( this.$clippable[ 0 ] );
    	}

    	this.$clippable = $clippable.addClass( 'oo-ui-clippableElement-clippable' );
    	this.clip();
    };

    /**
     * Set clippable container.
     *
     * This is the container that will be measured when deciding whether to clip. When clipping,
     * #$clippable will be resized in order to keep the clippable container fully visible.
     *
     * If the clippable container is unset, #$clippable will be used.
     *
     * @param {jQuery|null} $clippableContainer Container to keep visible, or null to unset
     */
    OO.ui.mixin.ClippableElement.prototype.setClippableContainer = function ( $clippableContainer ) {
    	this.$clippableContainer = $clippableContainer;
    	Iif ( this.$clippable ) {
    		this.clip();
    	}
    };

    /**
     * Toggle clipping.
     *
     * Do not turn clipping on until after the element is attached to the DOM and visible.
     *
     * @param {boolean} [clipping] Enable clipping, omit to toggle
     * @chainable
     */
    OO.ui.mixin.ClippableElement.prototype.toggleClipping = function ( clipping ) {
    	clipping = clipping === undefined ? !this.clipping : !!clipping;

    	if ( clipping && !this.warnedUnattached && !this.isElementAttached() ) {
    		OO.ui.warnDeprecation( 'ClippableElement#toggleClipping: Before calling this method, the element must be attached to the DOM.' );
    		this.warnedUnattached = true;
    	}

    	if ( this.clipping !== clipping ) {
    		this.clipping = clipping;
    		if ( clipping ) {
    			this.$clippableScrollableContainer = $( this.getClosestScrollableElementContainer() );
    			// If the clippable container is the root, we have to listen to scroll events and check
    			// jQuery.scrollTop on the window because of browser inconsistencies
    			this.$clippableScroller = this.$clippableScrollableContainer.is( 'html, body' ) ?
    				$( OO.ui.Element.static.getWindow( this.$clippableScrollableContainer ) ) :
    				this.$clippableScrollableContainer;
    			this.$clippableScroller.on( 'scroll', this.onClippableScrollHandler );
    			this.$clippableWindow = $( this.getElementWindow() )
    				.on( 'resize', this.onClippableWindowResizeHandler );
    			// Initial clip after visible
    			this.clip();
    		} else {
    			this.$clippable.css( {
    				width: '',
    				height: '',
    				maxWidth: '',
    				maxHeight: '',
    				overflowX: '',
    				overflowY: ''
    			} );
    			OO.ui.Element.static.reconsiderScrollbars( this.$clippable[ 0 ] );

    			this.$clippableScrollableContainer = null;
    			this.$clippableScroller.off( 'scroll', this.onClippableScrollHandler );
    			this.$clippableScroller = null;
    			this.$clippableWindow.off( 'resize', this.onClippableWindowResizeHandler );
    			this.$clippableWindow = null;
    		}
    	}

    	return this;
    };

    /**
     * Check if the element will be clipped to fit the visible area of the nearest scrollable container.
     *
     * @return {boolean} Element will be clipped to the visible area
     */
    OO.ui.mixin.ClippableElement.prototype.isClipping = function () {
    	return this.clipping;
    };

    /**
     * Check if the bottom or right of the element is being clipped by the nearest scrollable container.
     *
     * @return {boolean} Part of the element is being clipped
     */
    OO.ui.mixin.ClippableElement.prototype.isClipped = function () {
    	return this.clippedHorizontally || this.clippedVertically;
    };

    /**
     * Check if the right of the element is being clipped by the nearest scrollable container.
     *
     * @return {boolean} Part of the element is being clipped
     */
    OO.ui.mixin.ClippableElement.prototype.isClippedHorizontally = function () {
    	return this.clippedHorizontally;
    };

    /**
     * Check if the bottom of the element is being clipped by the nearest scrollable container.
     *
     * @return {boolean} Part of the element is being clipped
     */
    OO.ui.mixin.ClippableElement.prototype.isClippedVertically = function () {
    	return this.clippedVertically;
    };

    /**
     * Set the ideal size. These are the dimensions #$clippable will have when it's not being clipped.
     *
     * @param {number|string} [width] Width as a number of pixels or CSS string with unit suffix
     * @param {number|string} [height] Height as a number of pixels or CSS string with unit suffix
     */
    OO.ui.mixin.ClippableElement.prototype.setIdealSize = function ( width, height ) {
    	this.idealWidth = width;
    	this.idealHeight = height;

    	if ( !this.clipping ) {
    		// Update dimensions
    		this.$clippable.css( { width: width, height: height } );
    	}
    	// While clipping, idealWidth and idealHeight are not considered
    };

    /**
     * Return the side of the clippable on which it is "anchored" (aligned to something else).
     * ClippableElement will clip the opposite side when reducing element's width.
     *
     * Classes that mix in ClippableElement should override this to return 'right' if their
     * clippable is absolutely positioned and using 'right: Npx' (and not using 'left').
     * If your class also mixes in FloatableElement, this is handled automatically.
     *
     * (This can't be guessed from the actual CSS because the computed values for 'left'/'right' are
     * always in pixels, even if they were unset or set to 'auto'.)
     *
     * When in doubt, 'left' (or 'right' in RTL) is a sane fallback.
     *
     * @return {string} 'left' or 'right'
     */
    OO.ui.mixin.ClippableElement.prototype.getHorizontalAnchorEdge = function () {
    	if ( this.computePosition && this.positioning && this.computePosition().right !== '' ) {
    		return 'right';
    	}
    	return 'left';
    };

    /**
     * Return the side of the clippable on which it is "anchored" (aligned to something else).
     * ClippableElement will clip the opposite side when reducing element's width.
     *
     * Classes that mix in ClippableElement should override this to return 'bottom' if their
     * clippable is absolutely positioned and using 'bottom: Npx' (and not using 'top').
     * If your class also mixes in FloatableElement, this is handled automatically.
     *
     * (This can't be guessed from the actual CSS because the computed values for 'left'/'right' are
     * always in pixels, even if they were unset or set to 'auto'.)
     *
     * When in doubt, 'top' is a sane fallback.
     *
     * @return {string} 'top' or 'bottom'
     */
    OO.ui.mixin.ClippableElement.prototype.getVerticalAnchorEdge = function () {
    	if ( this.computePosition && this.positioning && this.computePosition().bottom !== '' ) {
    		return 'bottom';
    	}
    	return 'top';
    };

    /**
     * Clip element to visible boundaries and allow scrolling when needed. You should call this method
     * when the element's natural height changes.
     *
     * Element will be clipped the bottom or right of the element is within 10px of the edge of, or
     * overlapped by, the visible area of the nearest scrollable container.
     *
     * Because calling clip() when the natural height changes isn't always possible, we also set
     * max-height when the element isn't being clipped. This means that if the element tries to grow
     * beyond the edge, something reasonable will happen before clip() is called.
     *
     * @chainable
     */
    OO.ui.mixin.ClippableElement.prototype.clip = function () {
    	var extraHeight, extraWidth, viewportSpacing,
    		desiredWidth, desiredHeight, allotedWidth, allotedHeight,
    		naturalWidth, naturalHeight, clipWidth, clipHeight,
    		$item, itemRect, $viewport, viewportRect, availableRect,
    		direction, vertScrollbarWidth, horizScrollbarHeight,
    		// Extra tolerance so that the sloppy code below doesn't result in results that are off
    		// by one or two pixels. (And also so that we have space to display drop shadows.)
    		// Chosen by fair dice roll.
    		buffer = 7;

    	Eif ( !this.clipping ) {
    		// this.$clippableScrollableContainer and this.$clippableWindow are null, so the below will fail
    		return this;
    	}

    	function rectIntersection( a, b ) {
    		var out = {};
    		out.top = Math.max( a.top, b.top );
    		out.left = Math.max( a.left, b.left );
    		out.bottom = Math.min( a.bottom, b.bottom );
    		out.right = Math.min( a.right, b.right );
    		return out;
    	}

    	viewportSpacing = OO.ui.getViewportSpacing();

    	if ( this.$clippableScrollableContainer.is( 'html, body' ) ) {
    		$viewport = $( this.$clippableScrollableContainer[ 0 ].ownerDocument.body );
    		// Dimensions of the browser window, rather than the element!
    		viewportRect = {
    			top: 0,
    			left: 0,
    			right: document.documentElement.clientWidth,
    			bottom: document.documentElement.clientHeight
    		};
    		viewportRect.top += viewportSpacing.top;
    		viewportRect.left += viewportSpacing.left;
    		viewportRect.right -= viewportSpacing.right;
    		viewportRect.bottom -= viewportSpacing.bottom;
    	} else {
    		$viewport = this.$clippableScrollableContainer;
    		viewportRect = $viewport[ 0 ].getBoundingClientRect();
    		// Convert into a plain object
    		viewportRect = $.extend( {}, viewportRect );
    	}

    	// Account for scrollbar gutter
    	direction = $viewport.css( 'direction' );
    	vertScrollbarWidth = $viewport.innerWidth() - $viewport.prop( 'clientWidth' );
    	horizScrollbarHeight = $viewport.innerHeight() - $viewport.prop( 'clientHeight' );
    	viewportRect.bottom -= horizScrollbarHeight;
    	if ( direction === 'rtl' ) {
    		viewportRect.left += vertScrollbarWidth;
    	} else {
    		viewportRect.right -= vertScrollbarWidth;
    	}

    	// Add arbitrary tolerance
    	viewportRect.top += buffer;
    	viewportRect.left += buffer;
    	viewportRect.right -= buffer;
    	viewportRect.bottom -= buffer;

    	$item = this.$clippableContainer || this.$clippable;

    	extraHeight = $item.outerHeight() - this.$clippable.outerHeight();
    	extraWidth = $item.outerWidth() - this.$clippable.outerWidth();

    	itemRect = $item[ 0 ].getBoundingClientRect();
    	// Convert into a plain object
    	itemRect = $.extend( {}, itemRect );

    	// Item might already be clipped, so we can't just use its dimensions (in case we might need to
    	// make it larger than before). Extend the rectangle to the maximum size we are allowed to take.
    	if ( this.getHorizontalAnchorEdge() === 'right' ) {
    		itemRect.left = viewportRect.left;
    	} else {
    		itemRect.right = viewportRect.right;
    	}
    	if ( this.getVerticalAnchorEdge() === 'bottom' ) {
    		itemRect.top = viewportRect.top;
    	} else {
    		itemRect.bottom = viewportRect.bottom;
    	}

    	availableRect = rectIntersection( viewportRect, itemRect );

    	desiredWidth = Math.max( 0, availableRect.right - availableRect.left );
    	desiredHeight = Math.max( 0, availableRect.bottom - availableRect.top );
    	// It should never be desirable to exceed the dimensions of the browser viewport... right?
    	desiredWidth = Math.min( desiredWidth,
    		document.documentElement.clientWidth - viewportSpacing.left - viewportSpacing.right );
    	desiredHeight = Math.min( desiredHeight,
    		document.documentElement.clientHeight - viewportSpacing.top - viewportSpacing.right );
    	allotedWidth = Math.ceil( desiredWidth - extraWidth );
    	allotedHeight = Math.ceil( desiredHeight - extraHeight );
    	naturalWidth = this.$clippable.prop( 'scrollWidth' );
    	naturalHeight = this.$clippable.prop( 'scrollHeight' );
    	clipWidth = allotedWidth < naturalWidth;
    	clipHeight = allotedHeight < naturalHeight;

    	if ( clipWidth ) {
    		// The order matters here. If overflow is not set first, Chrome displays bogus scrollbars. See T157672.
    		// Forcing a reflow is a smaller workaround than calling reconsiderScrollbars() for this case.
    		this.$clippable.css( 'overflowX', 'scroll' );
    		void this.$clippable[ 0 ].offsetHeight; // Force reflow
    		this.$clippable.css( {
    			width: Math.max( 0, allotedWidth ),
    			maxWidth: ''
    		} );
    	} else {
    		this.$clippable.css( {
    			overflowX: '',
    			width: this.idealWidth || '',
    			maxWidth: Math.max( 0, allotedWidth )
    		} );
    	}
    	if ( clipHeight ) {
    		// The order matters here. If overflow is not set first, Chrome displays bogus scrollbars. See T157672.
    		// Forcing a reflow is a smaller workaround than calling reconsiderScrollbars() for this case.
    		this.$clippable.css( 'overflowY', 'scroll' );
    		void this.$clippable[ 0 ].offsetHeight; // Force reflow
    		this.$clippable.css( {
    			height: Math.max( 0, allotedHeight ),
    			maxHeight: ''
    		} );
    	} else {
    		this.$clippable.css( {
    			overflowY: '',
    			height: this.idealHeight || '',
    			maxHeight: Math.max( 0, allotedHeight )
    		} );
    	}

    	// If we stopped clipping in at least one of the dimensions
    	if ( ( this.clippedHorizontally && !clipWidth ) || ( this.clippedVertically && !clipHeight ) ) {
    		OO.ui.Element.static.reconsiderScrollbars( this.$clippable[ 0 ] );
    	}

    	this.clippedHorizontally = clipWidth;
    	this.clippedVertically = clipHeight;

    	return this;
    };