# all files / src/widgets/ MultilineTextInputWidget.js

38.57% Statements 27/70
40% Branches 12/30
30% Functions 3/10
38.57% Lines 27/70

    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

    1×
    4748×

    4748×

    4748×

    4748×
    4748×
    4748×
    4748×

    4748×

    4748×

    4748×
    472×

    4748×

    1×

    1×
    2374×
    2374×
    2374×

    1×

    1×

    1×

    1×

    1×

    1×
    2374×

    1×

    1×

     /**
     * @class
     * @extends OO.ui.TextInputWidget
     *
     * @constructor
     * @param {Object} [config] Configuration options
     * @cfg {number} [rows] Number of visible lines in textarea. If used with `autosize`,
     *  specifies minimum number of rows to display.
     * @cfg {string} [labelPosition='after'] The position of the inline label relative to that of
     * @cfg {boolean} [autosize=false] Automatically resize the text input to fit its content.
     *  Use the #maxRows config to specify a maximum number of displayed rows.
     * @cfg {number} [maxRows] Maximum number of rows to display when #autosize is set to true.
     *  Defaults to the maximum of `10` and `2 * rows`, or `10` if `rows` isn't provided.
     */
    OO.ui.MultilineTextInputWidget = function OoUiMultilineTextInputWidget( config ) {
    	config = $.extend( {
    		type: 'text'
    	}, config );
    	config.multiline = false;
    	// Parent constructor
    	OO.ui.MultilineTextInputWidget.parent.call( this, config );

    	// Properties
    	this.multiline = true;
    	this.autosize = !!config.autosize;
    	this.minRows = config.rows !== undefined ? config.rows : '';
    	this.maxRows = config.maxRows || Math.max( 2 * ( this.minRows || 0 ), 10 );

    	// Clone for resizing
    	Iif ( this.autosize ) {
    		this.$clone = this.$input
    			.clone()
    			.insertAfter( this.$input )
    			.attr( 'aria-hidden', 'true' )
    			.addClass( 'oo-ui-element-hidden' );
    	}

    	// Events
    	this.connect( this, {
    		change: 'onChange'
    	} );

    	// Initialization
    	if ( this.multiline && config.rows ) {
    		this.$input.attr( 'rows', config.rows );
    	}
    	Iif ( this.autosize ) {
    		this.isWaitingToBeAttached = true;
    		this.installParentChangeDetector();
    	}
    };

    /* Setup */

    OO.inheritClass( OO.ui.MultilineTextInputWidget, OO.ui.TextInputWidget );

    /* Static Methods */

    /**
     * @inheritdoc
     */
    OO.ui.MultilineTextInputWidget.static.gatherPreInfuseState = function ( node, config ) {
    	var state = OO.ui.MultilineTextInputWidget.parent.static.gatherPreInfuseState( node, config );
    	state.scrollTop = config.$input.scrollTop();
    	return state;
    };

    /* Methods */

    /**
     * @inheritdoc
     */
    OO.ui.MultilineTextInputWidget.prototype.onElementAttach = function () {
    	OO.ui.MultilineTextInputWidget.parent.prototype.onElementAttach.call( this );
    	this.adjustSize();
    };

    /**
     * Handle change events.
     *
     * @private
     */
    OO.ui.MultilineTextInputWidget.prototype.onChange = function () {
    	this.adjustSize();
    };

    /**
     * @inheritdoc
     */
    OO.ui.MultilineTextInputWidget.prototype.updatePosition = function () {
    	OO.ui.MultilineTextInputWidget.parent.prototype.updatePosition.call( this );
    	this.adjustSize();
    };

    /**
     * Override TextInputWidget so it doesn't emit the 'enter' event.
     *
     * @private
     * @param {jQuery.Event} e Key press event
     */
    OO.ui.MultilineTextInputWidget.prototype.onKeyPress = function () {
    	return;
    };

    /**
     * Automatically adjust the size of the text input.
     *
     * This only affects multiline inputs that are {@link #autosize autosized}.
     *
     * @chainable
     * @fires resize
     */
    OO.ui.MultilineTextInputWidget.prototype.adjustSize = function () {
    	var scrollHeight, innerHeight, outerHeight, maxInnerHeight, measurementError,
    		idealHeight, newHeight, scrollWidth, property;

    	if ( this.$input.val() !== this.valCache ) {
    		if ( this.autosize ) {
    			this.$clone
    				.val( this.$input.val() )
    				.attr( 'rows', this.minRows )
    				// Set inline height property to 0 to measure scroll height
    				.css( 'height', 0 );

    			this.$clone.removeClass( 'oo-ui-element-hidden' );

    			this.valCache = this.$input.val();

    			scrollHeight = this.$clone[ 0 ].scrollHeight;

    			// Remove inline height property to measure natural heights
    			this.$clone.css( 'height', '' );
    			innerHeight = this.$clone.innerHeight();
    			outerHeight = this.$clone.outerHeight();

    			// Measure max rows height
    			this.$clone
    				.attr( 'rows', this.maxRows )
    				.css( 'height', 'auto' )
    				.val( '' );
    			maxInnerHeight = this.$clone.innerHeight();

    			// Difference between reported innerHeight and scrollHeight with no scrollbars present.
    			// This is sometimes non-zero on Blink-based browsers, depending on zoom level.
    			measurementError = maxInnerHeight - this.$clone[ 0 ].scrollHeight;
    			idealHeight = Math.min( maxInnerHeight, scrollHeight + measurementError );

    			this.$clone.addClass( 'oo-ui-element-hidden' );

    			// Only apply inline height when expansion beyond natural height is needed
    			// Use the difference between the inner and outer height as a buffer
    			newHeight = idealHeight > innerHeight ? idealHeight + ( outerHeight - innerHeight ) : '';
    			if ( newHeight !== this.styleHeight ) {
    				this.$input.css( 'height', newHeight );
    				this.styleHeight = newHeight;
    				this.emit( 'resize' );
    			}
    		}
    		scrollWidth = this.$input[ 0 ].offsetWidth - this.$input[ 0 ].clientWidth;
    		if ( scrollWidth !== this.scrollWidth ) {
    			property = this.$element.css( 'direction' ) === 'rtl' ? 'left' : 'right';
    			// Reset
    			this.$label.css( { right: '', left: '' } );
    			this.$indicator.css( { right: '', left: '' } );

    			if ( scrollWidth ) {
    				this.$indicator.css( property, scrollWidth );
    				if ( this.labelPosition === 'after' ) {
    					this.$label.css( property, scrollWidth );
    				}
    			}

    			this.scrollWidth = scrollWidth;
    			this.positionLabel();
    		}
    	}
    	return this;
    };

    /**
     * @inheritdoc
     * @protected
     */
    OO.ui.MultilineTextInputWidget.prototype.getInputElement = function () {
    	return $( '<textarea>' );
    };

    /**
     * Check if the input supports multiple lines.
     *
     * @return {boolean}
     */
    OO.ui.MultilineTextInputWidget.prototype.isMultiline = function () {
    	return !!this.multiline;
    };

    /**
     * Check if the input automatically adjusts its size.
     *
     * @return {boolean}
     */
    OO.ui.MultilineTextInputWidget.prototype.isAutosizing = function () {
    	return !!this.autosize;
    };