Merge "resourceloader: Don't call wfExpandUrl() on load.php urls"
[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 ( $, mw ) {
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.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 /* Methods */
36
37 /**
38 * @private
39 */
40 mw.widgets.NamespaceInputWidget.prototype.getNamespaceDropdownOptions = function ( config ) {
41 var options,
42 exclude = config.exclude || [],
43 NS_MAIN = 0;
44
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 } );
58
59 if ( config.includeAllValue !== null && config.includeAllValue !== undefined ) {
60 options.unshift( {
61 data: config.includeAllValue,
62 label: mw.message( 'namespacesall' ).text()
63 } );
64 }
65
66 return options;
67 };
68
69 }( jQuery, mediaWiki ) );