Merge "Added Id to the input box"
[lhc/web/wiklou.git] / resources / src / mediawiki.widgets / mw.widgets.NamespacesMultiselectWidget.js
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 * Creates an mw.widgets.NamespacesMultiselectWidget object
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 * @param {Object} [config] Configuration options
21 */
22 mw.widgets.NamespacesMultiselectWidget = function MwWidgetsNamespacesMultiselectWidget( config ) {
23 var i, ilen, option,
24 namespaces = {},
25 options = mw.widgets.NamespaceInputWidget.static.getNamespaceDropdownOptions( {} );
26
27 for ( i = 0, ilen = options.length; i < ilen; i++ ) {
28 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.parent.call( this, $.extend( true,
38 {
39 clearInputOnChoose: true,
40 inputPosition: 'inline',
41 allowEditTags: false
42 },
43 config,
44 {
45 selected: config && config.selected ? config.selected.map( function ( id ) {
46 return {
47 data: id,
48 label: namespaces[ id ]
49 };
50 } ) : undefined
51 }
52 ) );
53
54 // Initialization
55 this.$element
56 .addClass( 'mw-widgets-namespacesMultiselectWidget' );
57
58 if ( 'name' in config ) {
59 // Use this instead of <input type="hidden">, because hidden inputs do not have separate
60 // 'value' and 'defaultValue' properties. The script on Special:Preferences
61 // (mw.special.preferences.confirmClose) checks this property to see if a field was changed.
62 this.hiddenInput = $( '<textarea>' )
63 .addClass( 'oo-ui-element-hidden' )
64 .attr( 'name', config.name )
65 .appendTo( this.$element );
66 // Update with preset values
67 // Set the default value (it might be different from just being empty)
68 this.hiddenInput.prop( 'defaultValue', this.getItems().map( function ( item ) {
69 return item.getData();
70 } ).join( '\n' ) );
71 this.on( 'change', function ( items ) {
72 this.hiddenInput.val( items.map( function ( item ) {
73 return item.getData();
74 } ).join( '\n' ) );
75 // Trigger a 'change' event as if a user edited the text
76 // (it is not triggered when changing the value from JS code).
77 this.hiddenInput.trigger( 'change' );
78 }.bind( this ) );
79 }
80 };
81
82 /* Setup */
83
84 OO.inheritClass( mw.widgets.NamespacesMultiselectWidget, OO.ui.MenuTagMultiselectWidget );
85 OO.mixinClass( mw.widgets.NamespacesMultiselectWidget, OO.ui.mixin.PendingElement );
86
87 }() );