# all files / src/widgets/ MultilineTextInputWidget.js

40.79% Statements 31/76
34.21% Branches 13/38
36.36% Functions 4/11
40.79% Lines 31/76

    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

    1×
    3960×

    3960×

    3960×

    3960×
    3960×
    3960×
    3960×

    3960×

    3960×

    3960×
    344×

    3960×

    1×

    1×
    1980×
    1980×
    1980×

    1×

    1×

    1×

    1×

    1×

    1×
    1980×

    1×

    1×

    1×
    1980×
    1980×
    1980×

     /**
     * @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 {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.$input.addClass( 'oo-ui-textInputWidget-autosized' );
    		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();
    };

    /**
     * @inheritdoc
     *
     * Modify to emit 'enter' on Ctrl/Meta+Enter, instead of plain Enter
     */
    OO.ui.MultilineTextInputWidget.prototype.onKeyPress = function ( e ) {
    	if (
    		( e.which === OO.ui.Keys.ENTER && ( e.ctrlKey || e.metaKey ) ) ||
    		// Some platforms emit keycode 10 for ctrl+enter in a textarea
    		e.which === 10
    	) {
    		this.emit( 'enter', e );
    	}
    };

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

    /**
     * @inheritdoc
     */
    OO.ui.MultilineTextInputWidget.prototype.restorePreInfuseState = function ( state ) {
    	OO.ui.MultilineTextInputWidget.parent.prototype.restorePreInfuseState.call( this, state );
    	Eif ( state.scrollTop !== undefined ) {
    		this.$input.scrollTop( state.scrollTop );
    	}
    };