Debian Codegraph @

resources/src/mediawiki.widgets/mw.widgets.NamespacesMultiselectWidget.js

raw symbols SCIP
1 /*!
2 * MediaWiki Widgets - NamespacesMultiselectWidget class.
3 *
4 * @copyright 2011-2015 MediaWiki Widgets Team and others; see AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
6 */
7 ( function () {
8
9 /**
10 * @classdesc Namespaces multiselect widget.
11 *
12 * TODO: A lot of this is duplicated in mw.widgets.UsersMultiselectWidget
13 * and mw.widgets.TitlesMultiselectWidget. These classes should be
14 * refactored.
15 *
16 * @class
17 * @extends OO.ui.MenuTagMultiselectWidget
18 *
19 * @constructor
20 * @description Create an mw.widgets.NamespacesMultiselectWidget object.
21 * @param {Object} [config] Configuration options
22 */
23 mw.widgets.NamespacesMultiselectWidget = function MwWidgetsNamespacesMultiselectWidget( config ) {
24 const namespaces = {},
25 options = mw.widgets.NamespaceInputWidget.static.getNamespaceDropdownOptions( {} );
26
27 for ( let i = 0, ilen = options.length; i < ilen; i++ ) {
28 const option = options[ i ];
29 namespaces[ option.data ] = option.label;
30 }
31
32 config = $.extend( true, {
33 options: mw.widgets.NamespaceInputWidget.static.getNamespaceDropdownOptions( {} )
34 }, config );
35
36 // Parent constructor
37 mw.widgets.NamespacesMultiselectWidget.super.call( this, $.extend( true,
38 {
39 menu: {
40 filterMode: 'substring'
41 }
42 },
43 config,
44 {
45 selected: config && config.selected ? config.selected.map( ( id ) => ( {
46 data: id,
47 label: namespaces[ id ]
48 } ) ) : undefined
49 }
50 ) );
51
52 // Initialization
53 this.$element
54 .addClass( 'mw-widgets-namespacesMultiselectWidget' );
55
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' );
72 } );
73 }
74 };
75
76 /* Setup */
77
78 OO.inheritClass( mw.widgets.NamespacesMultiselectWidget, OO.ui.MenuTagMultiselectWidget );
79 OO.mixinClass( mw.widgets.NamespacesMultiselectWidget, OO.ui.mixin.PendingElement );
80
81 /* Methods */
82
83 /**
84 * @inheritdoc
85 */
86 mw.widgets.NamespacesMultiselectWidget.prototype.createMenuOptionWidget = function ( data, label, icon ) {
87 return new mw.widgets.NamespacesMenuOptionWidget( {
88 data: data,
89 label: label || data,
90 icon: icon
91 } );
92 };
93
94 }() );