# all files / src/widgets/ InputWidget.js

91.14% Statements 72/79
86.11% Branches 31/36
76.92% Functions 10/13
91.14% Lines 72/79

    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

    1×

    27030×

    27030×

    27030×
    27030×
    27030×

    27030×
    27030×
    27030×
    27030×

    27030×

    27030×

    27030×

    27030×
    27030×
    1920×

    27030×
    5736×

    1×
    1×
    1×
    1×
    1×

    1×
    12983×

    12983×
    12983×

    1×
    12983×
    12983×
    11391×

    11391×

    12983×

    1×

    1×

    1×

    25600×
    25600×
    654×

    25600×

    1×
    1920×
    1920×

    1×
    30276×

    30276×
    10166×

    30276×
    7394×
    7394×

    30276×
    26870×
    26870×

    30276×

    1×
    33116×
    22080×
    11036×

    11036×

    1×
    30171×
    30171×
    3141×

    30171×

    1×
    5736×
    5736×

    1×
    12983×
    12983×
    1442×

    12983×

     /**
     * InputWidget is the base class for all input widgets, which
     * include {@link OO.ui.TextInputWidget text inputs}, {@link OO.ui.CheckboxInputWidget checkbox inputs},
     * {@link OO.ui.RadioInputWidget radio inputs}, and {@link OO.ui.ButtonInputWidget button inputs}.
     * See the [OOUI documentation on MediaWiki] [1] for more information and examples.
     *
     * [1]: https://www.mediawiki.org/wiki/OOUI/Widgets/Inputs
     *
     * @abstract
     * @class
     * @extends OO.ui.Widget
     * @mixins OO.ui.mixin.FlaggedElement
     * @mixins OO.ui.mixin.TabIndexedElement
     * @mixins OO.ui.mixin.TitledElement
     * @mixins OO.ui.mixin.AccessKeyedElement
     *
     * @constructor
     * @param {Object} [config] Configuration options
     * @cfg {string} [name=''] The value of the input’s HTML `name` attribute.
     * @cfg {string} [value=''] The value of the input.
     * @cfg {string} [dir] The directionality of the input (ltr/rtl).
     * @cfg {string} [inputId] The value of the input’s HTML `id` attribute.
     * @cfg {Function} [inputFilter] The name of an input filter function. Input filters modify the value of an input
     *  before it is accepted.
     */
    OO.ui.InputWidget = function OoUiInputWidget( config ) {
    	// Configuration initialization
    	config = config || {};

    	// Parent constructor
    	OO.ui.InputWidget.parent.call( this, config );

    	// Properties
    	// See #reusePreInfuseDOM about config.$input
    	this.$input = config.$input || this.getInputElement( config );
    	this.value = '';
    	this.inputFilter = config.inputFilter;

    	// Mixin constructors
    	OO.ui.mixin.FlaggedElement.call( this, config );
    	OO.ui.mixin.TabIndexedElement.call( this, $.extend( {}, config, { $tabIndexed: this.$input } ) );
    	OO.ui.mixin.TitledElement.call( this, $.extend( {}, config, { $titled: this.$input } ) );
    	OO.ui.mixin.AccessKeyedElement.call( this, $.extend( {}, config, { $accessKeyed: this.$input } ) );

    	// Events
    	this.$input.on( 'keydown mouseup cut paste change input select', this.onEdit.bind( this ) );

    	// Initialization
    	this.$input
    		.addClass( 'oo-ui-inputWidget-input' )
    		.attr( 'name', config.name )
    		.prop( 'disabled', this.isDisabled() );
    	this.$element
    		.addClass( 'oo-ui-inputWidget' )
    		.append( this.$input );
    	this.setValue( config.value );
    	if ( config.dir ) {
    		this.setDir( config.dir );
    	}
    	if ( config.inputId !== undefined ) {
    		this.setInputId( config.inputId );
    	}
    };

    /* Setup */

    OO.inheritClass( OO.ui.InputWidget, OO.ui.Widget );
    OO.mixinClass( OO.ui.InputWidget, OO.ui.mixin.FlaggedElement );
    OO.mixinClass( OO.ui.InputWidget, OO.ui.mixin.TabIndexedElement );
    OO.mixinClass( OO.ui.InputWidget, OO.ui.mixin.TitledElement );
    OO.mixinClass( OO.ui.InputWidget, OO.ui.mixin.AccessKeyedElement );

    /* Static Methods */

    /**
     * @inheritdoc
     */
    OO.ui.InputWidget.static.reusePreInfuseDOM = function ( node, config ) {
    	config = OO.ui.InputWidget.parent.static.reusePreInfuseDOM( node, config );
    	// Reusing `$input` lets browsers preserve inputted values across page reloads, see T114134.
    	config.$input = $( node ).find( '.oo-ui-inputWidget-input' );
    	return config;
    };

    /**
     * @inheritdoc
     */
    OO.ui.InputWidget.static.gatherPreInfuseState = function ( node, config ) {
    	var state = OO.ui.InputWidget.parent.static.gatherPreInfuseState( node, config );
    	if ( config.$input && config.$input.length ) {
    		state.value = config.$input.val();
    		// Might be better in TabIndexedElement, but it's awkward to do there because mixins are awkward
    		state.focus = config.$input.is( ':focus' );
    	}
    	return state;
    };

    /* Events */

    /**
     * @event change
     *
     * A change event is emitted when the value of the input changes.
     *
     * @param {string} value
     */

    /* Methods */

    /**
     * Get input element.
     *
     * Subclasses of OO.ui.InputWidget use the `config` parameter to produce different elements in
     * different circumstances. The element must have a `value` property (like form elements).
     *
     * @protected
     * @param {Object} config Configuration options
     * @return {jQuery} Input element
     */
    OO.ui.InputWidget.prototype.getInputElement = function () {
    	return $( '<input>' );
    };

    /**
     * Handle potentially value-changing events.
     *
     * @private
     * @param {jQuery.Event} e Key down, mouse up, cut, paste, change, input, or select event
     */
    OO.ui.InputWidget.prototype.onEdit = function () {
    	var widget = this;
    	if ( !this.isDisabled() ) {
    		// Allow the stack to clear so the value will be updated
    		setTimeout( function () {
    			widget.setValue( widget.$input.val() );
    		} );
    	}
    };

    /**
     * Get the value of the input.
     *
     * @return {string} Input value
     */
    OO.ui.InputWidget.prototype.getValue = function () {
    	// Resynchronize our internal data with DOM data. Other scripts executing on the page can modify
    	// it, and we won't know unless they're kind enough to trigger a 'change' event.
    	var value = this.$input.val();
    	if ( this.value !== value ) {
    		this.setValue( value );
    	}
    	return this.value;
    };

    /**
     * Set the directionality of the input.
     *
     * @param {string} dir Text directionality: 'ltr', 'rtl' or 'auto'
     * @chainable
     */
    OO.ui.InputWidget.prototype.setDir = function ( dir ) {
    	this.$input.prop( 'dir', dir );
    	return this;
    };

    /**
     * Set the value of the input.
     *
     * @param {string} value New value
     * @fires change
     * @chainable
     */
    OO.ui.InputWidget.prototype.setValue = function ( value ) {
    	value = this.cleanUpValue( value );
    	// Update the DOM if it has changed. Note that with cleanUpValue, it
    	// is possible for the DOM value to change without this.value changing.
    	if ( this.$input.val() !== value ) {
    		this.$input.val( value );
    	}
    	if ( this.value !== value ) {
    		this.value = value;
    		this.emit( 'change', this.value );
    	}
    	// The first time that the value is set (probably while constructing the widget),
    	// remember it in defaultValue. This property can be later used to check whether
    	// the value of the input has been changed since it was created.
    	if ( this.defaultValue === undefined ) {
    		this.defaultValue = this.value;
    		this.$input[ 0 ].defaultValue = this.defaultValue;
    	}
    	return this;
    };

    /**
     * Clean up incoming value.
     *
     * Ensures value is a string, and converts undefined and null to empty string.
     *
     * @private
     * @param {string} value Original value
     * @return {string} Cleaned up value
     */
    OO.ui.InputWidget.prototype.cleanUpValue = function ( value ) {
    	if ( value === undefined || value === null ) {
    		return '';
    	} else Iif ( this.inputFilter ) {
    		return this.inputFilter( String( value ) );
    	} else {
    		return String( value );
    	}
    };

    /**
     * @inheritdoc
     */
    OO.ui.InputWidget.prototype.setDisabled = function ( state ) {
    	OO.ui.InputWidget.parent.prototype.setDisabled.call( this, state );
    	if ( this.$input ) {
    		this.$input.prop( 'disabled', this.isDisabled() );
    	}
    	return this;
    };

    /**
     * Set the 'id' attribute of the `<input>` element.
     *
     * @param {string} id
     * @chainable
     */
    OO.ui.InputWidget.prototype.setInputId = function ( id ) {
    	this.$input.attr( 'id', id );
    	return this;
    };

    /**
     * @inheritdoc
     */
    OO.ui.InputWidget.prototype.restorePreInfuseState = function ( state ) {
    	OO.ui.InputWidget.parent.prototype.restorePreInfuseState.call( this, state );
    	if ( state.value !== undefined && state.value !== this.getValue() ) {
    		this.setValue( state.value );
    	}
    	Iif ( state.focus ) {
    		this.focus();
    	}
    };