2 * MediaWiki Widgets - NamespacesMultiselectWidget class.
4 * @copyright 2011-2015 MediaWiki Widgets Team and others; see AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
10 * @classdesc Namespaces multiselect widget.
12 * TODO: A lot of this is duplicated in mw.widgets.UsersMultiselectWidget
13 * and mw.widgets.TitlesMultiselectWidget. These classes should be
17 * @extends OO.ui.MenuTagMultiselectWidget
20 * @description Create an mw.widgets.NamespacesMultiselectWidget object.
21 * @param {Object} [config] Configuration options
23 mw.widgets.NamespacesMultiselectWidget = function MwWidgetsNamespacesMultiselectWidget( config ) {
24 const namespaces = {},
25 options = mw.widgets.NamespaceInputWidget.static.getNamespaceDropdownOptions( {} );
27 for ( let i = 0, ilen = options.length; i < ilen; i++ ) {
28 const option = options[ i ];
29 namespaces[ option.data ] = option.label;
32 config = $.extend( true, {
33 options: mw.widgets.NamespaceInputWidget.static.getNamespaceDropdownOptions( {} )
37 mw.widgets.NamespacesMultiselectWidget.super.call( this, $.extend( true,
40 filterMode: 'substring'
45 selected: config && config.selected ? config.selected.map( ( id ) => ( {
47 label: namespaces[ id ]
54 .addClass( 'mw-widgets-namespacesMultiselectWidget' );
56 if ( 'name' in config ) {
57 // Use this instead of <input type="hidden">, because hidden inputs do not have separate
58 // 'value' and 'defaultValue' properties. The script on Special:Preferences
59 // (mw.special.preferences.confirmClose) checks this property to see if a field was changed.
60 this.$hiddenInput = $( '<textarea>' )
61 .addClass( 'oo-ui-element-hidden' )
62 .attr( 'name', config.name )
63 .appendTo( this.$element );
64 // Update with preset values
65 // Set the default value (it might be different from just being empty)
66 this.$hiddenInput.prop( 'defaultValue', this.getItems().map( ( item ) => item.getData() ).join( '\n' ) );
67 this.on( 'change', ( items ) => {
68 this.$hiddenInput.val( items.map( ( item ) => item.getData() ).join( '\n' ) );
69 // Trigger a 'change' event as if a user edited the text
70 // (it is not triggered when changing the value from JS code).
71 this.$hiddenInput.trigger( 'change' );
78 OO.inheritClass( mw.widgets.NamespacesMultiselectWidget, OO.ui.MenuTagMultiselectWidget );
79 OO.mixinClass( mw.widgets.NamespacesMultiselectWidget, OO.ui.mixin.PendingElement );
86 mw.widgets.NamespacesMultiselectWidget.prototype.createMenuOptionWidget = function ( data, label, icon ) {
87 return new mw.widgets.NamespacesMenuOptionWidget( {