Merge "resources: Strip '$' and 'mw' from file closures"
[lhc/web/wiklou.git] / resources / src / mediawiki.widgets / mw.widgets.NamespaceInputWidget.js
1 /* eslint-disable no-restricted-properties */
2 /*!
3 * MediaWiki Widgets - NamespaceInputWidget class.
4 *
5 * @copyright 2011-2015 MediaWiki Widgets Team and others; see AUTHORS.txt
6 * @license The MIT License (MIT); see LICENSE.txt
7 */
8 ( function () {
9
10 /**
11 * Namespace input widget. Displays a dropdown box with the choice of available namespaces.
12 *
13 * @class
14 * @extends OO.ui.DropdownInputWidget
15 *
16 * @constructor
17 * @param {Object} [config] Configuration options
18 * @cfg {string|null} [includeAllValue] Value for "all namespaces" option, if any
19 * @cfg {number[]} [exclude] List of namespace numbers to exclude from the selector
20 */
21 mw.widgets.NamespaceInputWidget = function MwWidgetsNamespaceInputWidget( config ) {
22 // Configuration initialization
23 config = $.extend( {}, config, { options: this.getNamespaceDropdownOptions( config ) } );
24
25 // Parent constructor
26 mw.widgets.NamespaceInputWidget.parent.call( this, config );
27
28 // Initialization
29 this.$element.addClass( 'mw-widget-namespaceInputWidget' );
30 };
31
32 /* Setup */
33
34 OO.inheritClass( mw.widgets.NamespaceInputWidget, OO.ui.DropdownInputWidget );
35
36 /* Methods */
37
38 /**
39 * @private
40 * @param {Object} [config] Configuration options
41 * @return {Object[]} Dropdown options
42 */
43 mw.widgets.NamespaceInputWidget.prototype.getNamespaceDropdownOptions = function ( config ) {
44 var options,
45 exclude = config.exclude || [],
46 mainNamespace = mw.config.get( 'wgNamespaceIds' )[ '' ];
47
48 options = $.map( mw.config.get( 'wgFormattedNamespaces' ), function ( name, ns ) {
49 if ( ns < mainNamespace || exclude.indexOf( Number( ns ) ) !== -1 ) {
50 return null; // skip
51 }
52 ns = String( ns );
53 if ( ns === String( mainNamespace ) ) {
54 name = mw.message( 'blanknamespace' ).text();
55 }
56 return { data: ns, label: name };
57 } ).sort( function ( a, b ) {
58 // wgFormattedNamespaces is an object, and so technically doesn't have to be ordered
59 return a.data - b.data;
60 } );
61
62 if ( config.includeAllValue !== null && config.includeAllValue !== undefined ) {
63 options.unshift( {
64 data: config.includeAllValue,
65 label: mw.message( 'namespacesall' ).text()
66 } );
67 }
68
69 return options;
70 };
71
72 }() );