# all files / src/mixins/ ClippableElement.js

29.25% Statements 31/106
9.43% Branches 5/53
30% Functions 3/10
29.25% Lines 31/106

    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

    1×

    5908×

    5908×
    5908×
    5908×
    5908×
    5908×
    5908×
    5908×
    5908×
    5908×
    5908×
    5908×
    5908×

    5908×

    5908×

    1×
    5908×

    5908×
    5908×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×
    14112×

    14112×

    14112×

     /**
     * 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
    	Iif ( 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;
    	if ( 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
    };

    /**
     * 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 $container, extraHeight, extraWidth, ccOffset,
    		$scrollableContainer, scOffset, scHeight, scWidth,
    		ccWidth, scrollerIsWindow, scrollTop, scrollLeft,
    		desiredWidth, desiredHeight, allotedWidth, allotedHeight,
    		naturalWidth, naturalHeight, clipWidth, clipHeight,
    		buffer = 7; // Chosen by fair dice roll

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

    	$container = this.$clippableContainer || this.$clippable;
    	extraHeight = $container.outerHeight() - this.$clippable.outerHeight();
    	extraWidth = $container.outerWidth() - this.$clippable.outerWidth();
    	ccOffset = $container.offset();
    	if ( this.$clippableScrollableContainer.is( 'html, body' ) ) {
    		$scrollableContainer = this.$clippableWindow;
    		scOffset = { top: 0, left: 0 };
    	} else {
    		$scrollableContainer = this.$clippableScrollableContainer;
    		scOffset = $scrollableContainer.offset();
    	}
    	scHeight = $scrollableContainer.innerHeight() - buffer;
    	scWidth = $scrollableContainer.innerWidth() - buffer;
    	ccWidth = $container.outerWidth() + buffer;
    	scrollerIsWindow = this.$clippableScroller[ 0 ] === this.$clippableWindow[ 0 ];
    	scrollTop = scrollerIsWindow ? this.$clippableScroller.scrollTop() : 0;
    	scrollLeft = scrollerIsWindow ? this.$clippableScroller.scrollLeft() : 0;
    	desiredWidth = ccOffset.left < 0 ?
    		ccWidth + ccOffset.left :
    		( scOffset.left + scrollLeft + scWidth ) - ccOffset.left;
    	desiredHeight = ( scOffset.top + scrollTop + scHeight ) - ccOffset.top;
    	// It should never be desirable to exceed the dimensions of the browser viewport... right?
    	desiredWidth = Math.min( desiredWidth, document.documentElement.clientWidth );
    	desiredHeight = Math.min( desiredHeight, document.documentElement.clientHeight );
    	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;
    };