# all files / src/widgets/ MenuTagMultiselectWidget.js

55.7% Statements 44/79
38.46% Branches 20/52
47.37% Functions 9/19
55.7% Lines 44/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
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295

    1×
    4×

    4×

    4×
    4×
    4×

    4×

    4×

    4×
    4×

    4×

    4×

    4×

    4×
    2×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    4×

    4×

    1×

    1×

    1×

    1×
    4×

    1×
    4×

    9×

    4×

    1×
    9×

    1×

    1×
    8×
    8×

    6×
    18×

    8×

     /**
     * MenuTagMultiselectWidget is a {@link OO.ui.TagMultiselectWidget OO.ui.TagMultiselectWidget} intended
     * to use a menu of selectable options.
     *
     *     @example
     *     // Example: A basic MenuTagMultiselectWidget.
     *     var widget = new OO.ui.MenuTagMultiselectWidget( {
     *         inputPosition: 'outline',
     *         options: [
     *            { data: 'option1', label: 'Option 1', icon: 'tag' },
     *            { data: 'option2', label: 'Option 2' },
     *            { data: 'option3', label: 'Option 3' },
     *         ],
     *         selected: [ 'option1', 'option2' ]
     *     } );
     *     $( 'body' ).append( widget.$element );
     *
     * @class
     * @extends OO.ui.TagMultiselectWidget
     *
     * @constructor
     * @param {Object} [config] Configuration object
     * @cfg {boolean} [clearInputOnChoose=true] Clear the text input value when a menu option is chosen
     * @cfg {Object} [menu] Configuration object for the menu widget
     * @cfg {jQuery} [$overlay] An overlay for the menu.
     *  See <https://www.mediawiki.org/wiki/OOUI/Concepts#Overlays>.
     * @cfg {Object[]} [options=[]] Array of menu options in the format `{ data: …, label: … }`
     */
    OO.ui.MenuTagMultiselectWidget = function OoUiMenuTagMultiselectWidget( config ) {
    	config = config || {};

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

    	this.$overlay = ( config.$overlay === true ? OO.ui.getDefaultOverlay() : config.$overlay ) || this.$element;
    	this.clearInputOnChoose = config.clearInputOnChoose === undefined || !!config.clearInputOnChoose;
    	this.menu = this.createMenuWidget( $.extend( {
    		widget: this,
    		input: this.hasInput ? this.input : null,
    		$input: this.hasInput ? this.input.$input : null,
    		filterFromInput: !!this.hasInput,
    		$autoCloseIgnore: this.hasInput ?
    			this.input.$element : $( [] ),
    		$floatableContainer: this.hasInput && this.inputPosition === 'outline' ?
    			this.input.$element : this.$element,
    		$overlay: this.$overlay,
    		disabled: this.isDisabled()
    	}, config.menu ) );
    	this.addOptions( config.options || [] );

    	// Events
    	this.menu.connect( this, {
    		choose: 'onMenuChoose',
    		toggle: 'onMenuToggle'
    	} );
    	Eif ( this.hasInput ) {
    		this.input.connect( this, { change: 'onInputChange' } );
    	}
    	this.connect( this, { resize: 'onResize' } );

    	// Initialization
    	this.$overlay
    		.append( this.menu.$element );
    	this.$element
    		.addClass( 'oo-ui-menuTagMultiselectWidget' );
    	// TagMultiselectWidget already does this, but it doesn't work right because this.menu is not yet
    	// set up while the parent constructor runs, and #getAllowedValues rejects everything.
    	if ( config.selected ) {
    		this.setValue( config.selected );
    	}
    };

    /* Initialization */

    OO.inheritClass( OO.ui.MenuTagMultiselectWidget, OO.ui.TagMultiselectWidget );

    /* Methods */

    /**
     * Respond to resize event
     */
    OO.ui.MenuTagMultiselectWidget.prototype.onResize = function () {
    	// Reposition the menu
    	this.menu.position();
    };

    /**
     * @inheritdoc
     */
    OO.ui.MenuTagMultiselectWidget.prototype.onInputFocus = function () {
    	// Parent method
    	OO.ui.MenuTagMultiselectWidget.parent.prototype.onInputFocus.call( this );

    	this.menu.toggle( true );
    };

    /**
     * Respond to input change event
     */
    OO.ui.MenuTagMultiselectWidget.prototype.onInputChange = function () {
    	this.menu.toggle( true );
    	this.initializeMenuSelection();
    };

    /**
     * Respond to menu choose event
     *
     * @param {OO.ui.OptionWidget} menuItem Chosen menu item
     */
    OO.ui.MenuTagMultiselectWidget.prototype.onMenuChoose = function ( menuItem ) {
    	// Add tag
    	this.addTag( menuItem.getData(), menuItem.getLabel() );
    	if ( this.hasInput && this.clearInputOnChoose ) {
    		this.input.setValue( '' );
    	}
    };

    /**
     * Respond to menu toggle event. Reset item highlights on hide.
     *
     * @param {boolean} isVisible The menu is visible
     */
    OO.ui.MenuTagMultiselectWidget.prototype.onMenuToggle = function ( isVisible ) {
    	if ( !isVisible ) {
    		this.menu.selectItem( null );
    		this.menu.highlightItem( null );
    	} else {
    		this.initializeMenuSelection();
    	}
    };

    /**
     * @inheritdoc
     */
    OO.ui.MenuTagMultiselectWidget.prototype.onTagSelect = function ( tagItem ) {
    	var menuItem = this.menu.findItemFromData( tagItem.getData() );
    	if ( !this.allowArbitrary ) {
    		// Override the base behavior from TagMultiselectWidget; the base behavior
    		// in TagMultiselectWidget is to remove the tag to edit it in the input,
    		// but in our case, we want to utilize the menu selection behavior, and
    		// definitely not remove the item.

    		// If there is an input that is used for filtering, erase the value so we don't filter
    		if ( this.hasInput && this.menu.filterFromInput ) {
    			this.input.setValue( '' );
    		}

    		// Select the menu item
    		this.menu.selectItem( menuItem );

    		this.focus();
    	} else {
    		// Use the default
    		OO.ui.MenuTagMultiselectWidget.parent.prototype.onTagSelect.call( this, tagItem );
    	}
    };

    /**
     * @inheritdoc
     */
    OO.ui.MenuTagMultiselectWidget.prototype.setDisabled = function ( isDisabled ) {
    	// Parent method
    	OO.ui.MenuTagMultiselectWidget.parent.prototype.setDisabled.call( this, isDisabled );

    	Iif ( this.menu ) {
    		// Protect against calling setDisabled() before the menu was initialized
    		this.menu.setDisabled( isDisabled );
    	}
    };

    /**
     * Highlight the first selectable item in the menu, if configured.
     *
     * @private
     * @chainable
     */
    OO.ui.MenuTagMultiselectWidget.prototype.initializeMenuSelection = function () {
    	if ( !this.menu.findSelectedItem() ) {
    		this.menu.highlightItem( this.menu.findFirstSelectableItem() );
    	}
    };

    /**
     * @inheritdoc
     */
    OO.ui.MenuTagMultiselectWidget.prototype.addTagFromInput = function () {
    	var inputValue = this.input.getValue(),
    		validated = false,
    		highlightedItem = this.menu.findHighlightedItem(),
    		item = this.menu.findItemFromData( inputValue );

    	if ( !inputValue ) {
    		return;
    	}

    	// Override the parent method so we add from the menu
    	// rather than directly from the input

    	// Look for a highlighted item first
    	if ( highlightedItem ) {
    		validated = this.addTag( highlightedItem.getData(), highlightedItem.getLabel() );
    	} else if ( item ) {
    		// Look for the element that fits the data
    		validated = this.addTag( item.getData(), item.getLabel() );
    	} else {
    		// Otherwise, add the tag - the method will only add if the
    		// tag is valid or if invalid tags are allowed
    		validated = this.addTag( inputValue );
    	}

    	if ( validated ) {
    		this.clearInput();
    		this.focus();
    	}
    };

    /**
     * Return the visible items in the menu. This is mainly used for when
     * the menu is filtering results.
     *
     * @return {OO.ui.MenuOptionWidget[]} Visible results
     */
    OO.ui.MenuTagMultiselectWidget.prototype.getMenuVisibleItems = function () {
    	return this.menu.getItems().filter( function ( menuItem ) {
    		return menuItem.isVisible();
    	} );
    };

    /**
     * Create the menu for this widget. This is in a separate method so that
     * child classes can override this without polluting the constructor with
     * unnecessary extra objects that will be overidden.
     *
     * @param {Object} menuConfig Configuration options
     * @return {OO.ui.MenuSelectWidget} Menu widget
     */
    OO.ui.MenuTagMultiselectWidget.prototype.createMenuWidget = function ( menuConfig ) {
    	return new OO.ui.MenuSelectWidget( menuConfig );
    };

    /**
     * Add options to the menu
     *
     * @param {Object[]} menuOptions Object defining options
     */
    OO.ui.MenuTagMultiselectWidget.prototype.addOptions = function ( menuOptions ) {
    	var widget = this,
    		items = menuOptions.map( function ( obj ) {
    			return widget.createMenuOptionWidget( obj.data, obj.label, obj.icon );
    		} );

    	this.menu.addItems( items );
    };

    /**
     * Create a menu option widget.
     *
     * @param {string} data Item data
     * @param {string} [label] Item label
     * @param {string} [icon] Symbolic icon name
     * @return {OO.ui.OptionWidget} Option widget
     */
    OO.ui.MenuTagMultiselectWidget.prototype.createMenuOptionWidget = function ( data, label, icon ) {
    	return new OO.ui.MenuOptionWidget( {
    		data: data,
    		label: label || data,
    		icon: icon
    	} );
    };

    /**
     * Get the menu
     *
     * @return {OO.ui.MenuSelectWidget} Menu
     */
    OO.ui.MenuTagMultiselectWidget.prototype.getMenu = function () {
    	return this.menu;
    };

    /**
     * Get the allowed values list
     *
     * @return {string[]} Allowed data values
     */
    OO.ui.MenuTagMultiselectWidget.prototype.getAllowedValues = function () {
    	var menuDatas = [];
    	if ( this.menu ) {
    		// If the parent constructor is calling us, we're not ready yet, this.menu is not set up.
    		menuDatas = this.menu.getItems().map( function ( menuItem ) {
    			return menuItem.getData();
    		} );
    	}
    	return this.allowedValues.concat( menuDatas );
    };