# all files / src/widgets/ CheckboxMultiselectInputWidget.js

85.71% Statements 54/63
71.43% Branches 10/14
71.43% Functions 10/14
85.71% Lines 54/63

    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

    1×

    1592×

    1592×

    1592×

    1592×

    1592×

    1592×
    1592×

    1592×

    1592×

    1×

    1×
    796×
    796×

    796×

    1×
    796×

    796×
    796×

    1×

    1592×

    1×
    2388×

    2388×
    2388×

    2388×

    1×
    7960×
    7960×
    7960×
    7960×

    1×
    15920×

    15920×
    1592×

    14328×

    14328×

    1×
    1592×
    1592×
    1592×

    1×
    1592×

    1592×

    520×
    520×

    520×
    520×

    520×
    520×
    520×

    1592×

    1592×

    1×

     /**
     * CheckboxMultiselectInputWidget is a
     * {@link OO.ui.CheckboxMultiselectWidget CheckboxMultiselectWidget} intended to be used within a
     * HTML form, such as a OO.ui.FormLayout. The selected values are synchronized with the value of
     * HTML `<input type=checkbox>` tags. Please see the [OOjs UI documentation on MediaWiki][1] for
     * more information about input widgets.
     *
     *     @example
     *     // Example: A CheckboxMultiselectInputWidget with three options
     *     var multiselectInput = new OO.ui.CheckboxMultiselectInputWidget( {
     *         options: [
     *             { data: 'a', label: 'First' },
     *             { data: 'b', label: 'Second'},
     *             { data: 'c', label: 'Third' }
     *         ]
     *     } );
     *     $( 'body' ).append( multiselectInput.$element );
     *
     * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Widgets/Inputs
     *
     * @class
     * @extends OO.ui.InputWidget
     *
     * @constructor
     * @param {Object} [config] Configuration options
     * @cfg {Object[]} [options=[]] Array of menu options in the format `{ data: …, label: …, disabled: … }`
     */
    OO.ui.CheckboxMultiselectInputWidget = function OoUiCheckboxMultiselectInputWidget( config ) {
    	// Configuration initialization
    	config = config || {};

    	// Properties (must be done before parent constructor which calls #setDisabled)
    	this.checkboxMultiselectWidget = new OO.ui.CheckboxMultiselectWidget();

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

    	// Properties
    	this.inputName = config.name;

    	// Initialization
    	this.$element
    		.addClass( 'oo-ui-checkboxMultiselectInputWidget' )
    		.append( this.checkboxMultiselectWidget.$element );
    	// We don't use this.$input, but rather the CheckboxInputWidgets inside each option
    	this.$input.detach();
    	this.setOptions( config.options || [] );
    	// Have to repeat this from parent, as we need options to be set up for this to make sense
    	this.setValue( config.value );

    	// setValue when checkboxMultiselectWidget changes
    	this.checkboxMultiselectWidget.on( 'change', function () {
    		this.setValue( this.checkboxMultiselectWidget.getSelectedItemsData() );
    	}.bind( this ) );
    };

    /* Setup */

    OO.inheritClass( OO.ui.CheckboxMultiselectInputWidget, OO.ui.InputWidget );

    /* Static Methods */

    /**
     * @inheritdoc
     */
    OO.ui.CheckboxMultiselectInputWidget.static.gatherPreInfuseState = function ( node, config ) {
    	var state = OO.ui.CheckboxMultiselectInputWidget.parent.static.gatherPreInfuseState( node, config );
    	state.value = $( node ).find( '.oo-ui-checkboxInputWidget .oo-ui-inputWidget-input:checked' )
    		.toArray().map( function ( el ) { return el.value; } );
    	return state;
    };

    /**
     * @inheritdoc
     */
    OO.ui.CheckboxMultiselectInputWidget.static.reusePreInfuseDOM = function ( node, config ) {
    	config = OO.ui.CheckboxMultiselectInputWidget.parent.static.reusePreInfuseDOM( node, config );
    	// Cannot reuse the `<input type=checkbox>` set
    	delete config.$input;
    	return config;
    };

    /* Methods */

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

    /**
     * @inheritdoc
     */
    OO.ui.CheckboxMultiselectInputWidget.prototype.getValue = function () {
    	var value = this.$element.find( '.oo-ui-checkboxInputWidget .oo-ui-inputWidget-input:checked' )
    		.toArray().map( function ( el ) { return el.value; } );
    	Eif ( this.value !== value ) {
    		this.setValue( value );
    	}
    	return this.value;
    };

    /**
     * @inheritdoc
     */
    OO.ui.CheckboxMultiselectInputWidget.prototype.setValue = function ( value ) {
    	value = this.cleanUpValue( value );
    	this.checkboxMultiselectWidget.selectItemsByData( value );
    	OO.ui.CheckboxMultiselectInputWidget.parent.prototype.setValue.call( this, value );
    	return this;
    };

    /**
     * Clean up incoming value.
     *
     * @param {string[]} value Original value
     * @return {string[]} Cleaned up value
     */
    OO.ui.CheckboxMultiselectInputWidget.prototype.cleanUpValue = function ( value ) {
    	var i, singleValue,
    		cleanValue = [];
    	if ( !Array.isArray( value ) ) {
    		return cleanValue;
    	}
    	for ( i = 0; i < value.length; i++ ) {
    		singleValue =
    			OO.ui.CheckboxMultiselectInputWidget.parent.prototype.cleanUpValue.call( this, value[ i ] );
    		// Remove options that we don't have here
    		if ( !this.checkboxMultiselectWidget.getItemFromData( singleValue ) ) {
    			continue;
    		}
    		cleanValue.push( singleValue );
    	}
    	return cleanValue;
    };

    /**
     * @inheritdoc
     */
    OO.ui.CheckboxMultiselectInputWidget.prototype.setDisabled = function ( state ) {
    	this.checkboxMultiselectWidget.setDisabled( state );
    	OO.ui.CheckboxMultiselectInputWidget.parent.prototype.setDisabled.call( this, state );
    	return this;
    };

    /**
     * Set the options available for this input.
     *
     * @param {Object[]} options Array of menu options in the format `{ data: …, label: …, disabled: … }`
     * @chainable
     */
    OO.ui.CheckboxMultiselectInputWidget.prototype.setOptions = function ( options ) {
    	var widget = this;

    	// Rebuild the checkboxMultiselectWidget menu
    	this.checkboxMultiselectWidget
    		.clearItems()
    		.addItems( options.map( function ( opt ) {
    			var optValue, item, optDisabled;
    			optValue =
    				OO.ui.CheckboxMultiselectInputWidget.parent.prototype.cleanUpValue.call( widget, opt.data );
    			optDisabled = opt.disabled !== undefined ? opt.disabled : false;
    			item = new OO.ui.CheckboxMultioptionWidget( {
    				data: optValue,
    				label: opt.label !== undefined ? opt.label : optValue,
    				disabled: optDisabled
    			} );
    			// Set the 'name' and 'value' for form submission
    			item.checkbox.$input.attr( 'name', widget.inputName );
    			item.checkbox.setValue( optValue );
    			return item;
    		} ) );

    	// Re-set the value, checking the checkboxes as needed.
    	// This will also get rid of any stale options that we just removed.
    	this.setValue( this.getValue() );

    	return this;
    };

    /**
     * @inheritdoc
     */
    OO.ui.CheckboxMultiselectInputWidget.prototype.focus = function () {
    	this.checkboxMultiselectWidget.focus();
    	return this;
    };