# all files / src/widgets/ MultiselectWidget.js

79.31% Statements 23/29
75% Branches 3/4
44.44% Functions 4/9
79.31% Lines 23/29

    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

    1×

    1592×

    1592×

    1592×

    1592×

    1592×

    1592×

    1592×
    1592×

    1×
    1×

    1×

    1×

    1×
    3184×
    1040×
    1040×

    3184×

    1×
    3184×

    3184×

    3184×
    3184×

     /**
     * MultiselectWidget allows selecting multiple options from a list.
     *
     * For more information about menus and options, please see the [OOUI documentation on MediaWiki][1].
     *
     * [1]: https://www.mediawiki.org/wiki/OOUI/Widgets/Selects_and_Options#Menu_selects_and_options
     *
     * @class
     * @abstract
     * @extends OO.ui.Widget
     * @mixins OO.ui.mixin.GroupWidget
     *
     * @constructor
     * @param {Object} [config] Configuration options
     * @cfg {OO.ui.MultioptionWidget[]} [items] An array of options to add to the multiselect.
     */
    OO.ui.MultiselectWidget = function OoUiMultiselectWidget( config ) {
    	// Parent constructor
    	OO.ui.MultiselectWidget.parent.call( this, config );

    	// Configuration initialization
    	config = config || {};

    	// Mixin constructors
    	OO.ui.mixin.GroupWidget.call( this, config );

    	// Events
    	this.aggregate( { change: 'select' } );
    	// This is mostly for compatibility with TagMultiselectWidget... normally, 'change' is emitted
    	// by GroupElement only when items are added/removed
    	this.connect( this, { select: [ 'emit', 'change' ] } );

    	// Initialization
    	Iif ( config.items ) {
    		this.addItems( config.items );
    	}
    	this.$group.addClass( 'oo-ui-multiselectWidget-group' );
    	this.$element.addClass( 'oo-ui-multiselectWidget' )
    		.append( this.$group );
    };

    /* Setup */

    OO.inheritClass( OO.ui.MultiselectWidget, OO.ui.Widget );
    OO.mixinClass( OO.ui.MultiselectWidget, OO.ui.mixin.GroupWidget );

    /* Events */

    /**
     * @event change
     *
     * A change event is emitted when the set of items changes, or an item is selected or deselected.
     */

    /**
     * @event select
     *
     * A select event is emitted when an item is selected or deselected.
     */

    /* Methods */

    /**
     * Find options that are selected.
     *
     * @return {OO.ui.MultioptionWidget[]} Selected options
     */
    OO.ui.MultiselectWidget.prototype.findSelectedItems = function () {
    	return this.items.filter( function ( item ) {
    		return item.isSelected();
    	} );
    };

    /**
     * Find the data of options that are selected.
     *
     * @return {Object[]|string[]} Values of selected options
     */
    OO.ui.MultiselectWidget.prototype.findSelectedItemsData = function () {
    	return this.findSelectedItems().map( function ( item ) {
    		return item.data;
    	} );
    };

    /**
     * Select options by reference. Options not mentioned in the `items` array will be deselected.
     *
     * @param {OO.ui.MultioptionWidget[]} items Items to select
     * @chainable
     */
    OO.ui.MultiselectWidget.prototype.selectItems = function ( items ) {
    	this.items.forEach( function ( item ) {
    		var selected = items.indexOf( item ) !== -1;
    		item.setSelected( selected );
    	} );
    	return this;
    };

    /**
     * Select items by their data. Options not mentioned in the `datas` array will be deselected.
     *
     * @param {Object[]|string[]} datas Values of items to select
     * @chainable
     */
    OO.ui.MultiselectWidget.prototype.selectItemsByData = function ( datas ) {
    	var items,
    		widget = this;
    	items = datas.map( function ( data ) {
    		return widget.findItemFromData( data );
    	} );
    	this.selectItems( items );
    	return this;
    };