Merge "Give instructions on removing email address from account"
[mediawiki.git] / resources / src / mediawiki.widgets / mw.widgets.NamespaceInputWidget.js
blob4f1b874991fe905378a411e1fb75ed7809e05d7d
1 /*!
2  * MediaWiki Widgets - NamespaceInputWidget 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 ( $, mw ) {
9         /**
10          * Namespace input widget. Displays a dropdown box with the choice of available namespaces.
11          *
12          * @class
13          * @extends OO.ui.DropdownInputWidget
14          *
15          * @constructor
16          * @param {Object} [config] Configuration options
17          * @cfg {string|null} [includeAllValue] Value for "all namespaces" option, if any
18          * @cfg {number[]} [exclude] List of namespace numbers to exclude from the selector
19          */
20         mw.widgets.NamespaceInputWidget = function MwWidgetsNamespaceInputWidget( config ) {
21                 // Configuration initialization
22                 config = $.extend( {}, config, { options: this.getNamespaceDropdownOptions( config ) } );
24                 // Parent constructor
25                 mw.widgets.NamespaceInputWidget.parent.call( this, config );
27                 // Initialization
28                 this.$element.addClass( 'mw-widget-namespaceInputWidget' );
29         };
31         /* Setup */
33         OO.inheritClass( mw.widgets.NamespaceInputWidget, OO.ui.DropdownInputWidget );
35         /* Methods */
37         /**
38          * @private
39          */
40         mw.widgets.NamespaceInputWidget.prototype.getNamespaceDropdownOptions = function ( config ) {
41                 var options,
42                         exclude = config.exclude || [],
43                         NS_MAIN = 0;
45                 options = $.map( mw.config.get( 'wgFormattedNamespaces' ), function ( name, ns ) {
46                         if ( ns < NS_MAIN || exclude.indexOf( Number( ns ) ) !== -1 ) {
47                                 return null; // skip
48                         }
49                         ns = String( ns );
50                         if ( ns === String( NS_MAIN ) ) {
51                                 name = mw.message( 'blanknamespace' ).text();
52                         }
53                         return { data: ns, label: name };
54                 } ).sort( function ( a, b ) {
55                         // wgFormattedNamespaces is an object, and so technically doesn't have to be ordered
56                         return a.data - b.data;
57                 } );
59                 if ( config.includeAllValue !== null && config.includeAllValue !== undefined ) {
60                         options.unshift( {
61                                 data: config.includeAllValue,
62                                 label: mw.message( 'namespacesall' ).text()
63                         } );
64                 }
66                 return options;
67         };
69 }( jQuery, mediaWiki ) );