Merge "Don't process "all" option in Html::namespaceSelectorOptions"
[lhc/web/wiklou.git] / resources / src / mediawiki.widgets / mw.widgets.NamespaceInputWidget.js
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 () {
8
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.constructor.static.getNamespaceDropdownOptions( config ) } );
23
24 // Parent constructor
25 mw.widgets.NamespaceInputWidget.parent.call( this, config );
26
27 // Initialization
28 this.$element.addClass( 'mw-widget-namespaceInputWidget' );
29 };
30
31 /* Setup */
32
33 OO.inheritClass( mw.widgets.NamespaceInputWidget, OO.ui.DropdownInputWidget );
34
35 /* Static methods */
36
37 /**
38 * Get a list of namespace options, sorted by ID.
39 *
40 * @param {Object} [config] Configuration options
41 * @return {Object[]} Dropdown options
42 */
43 mw.widgets.NamespaceInputWidget.static.getNamespaceDropdownOptions = function ( config ) {
44 var options,
45 exclude = config.exclude || [],
46 mainNamespace = mw.config.get( 'wgNamespaceIds' )[ '' ];
47
48 // eslint-disable-next-line no-jquery/no-map-util
49 options = $.map( mw.config.get( 'wgFormattedNamespaces' ), function ( name, ns ) {
50 if ( ns < mainNamespace || exclude.indexOf( Number( ns ) ) !== -1 ) {
51 return null; // skip
52 }
53 ns = String( ns );
54 if ( ns === String( mainNamespace ) ) {
55 name = mw.message( 'blanknamespace' ).text();
56 }
57 return { data: ns, label: name };
58 } ).sort( function ( a, b ) {
59 // wgFormattedNamespaces is an object, and so technically doesn't have to be ordered
60 return a.data - b.data;
61 } );
62
63 if ( config.includeAllValue !== null && config.includeAllValue !== undefined ) {
64 options.unshift( {
65 data: config.includeAllValue,
66 label: mw.message( 'namespacesall' ).text()
67 } );
68 }
69
70 return options;
71 };
72
73 }() );