registration: Allow extensions to specify which MW core versions they require
[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 */
19 mw.widgets.NamespaceInputWidget = function MwWidgetsNamespaceInputWidget( config ) {
20 // Configuration initialization
21 config = $.extend( {}, config, { options: this.getNamespaceDropdownOptions( config ) } );
22
23 // Parent constructor
24 mw.widgets.NamespaceInputWidget.parent.call( this, config );
25
26 // Initialization
27 this.$element.addClass( 'mw-widget-namespaceInputWidget' );
28 };
29
30 /* Setup */
31
32 OO.inheritClass( mw.widgets.NamespaceInputWidget, OO.ui.DropdownInputWidget );
33
34 /* Methods */
35
36 /**
37 * @private
38 */
39 mw.widgets.NamespaceInputWidget.prototype.getNamespaceDropdownOptions = function ( config ) {
40 var options,
41 NS_MAIN = 0;
42
43 options = $.map( mw.config.get( 'wgFormattedNamespaces' ), function ( name, ns ) {
44 if ( ns < NS_MAIN ) {
45 return null; // skip
46 }
47 ns = String( ns );
48 if ( ns === String( NS_MAIN ) ) {
49 name = mw.message( 'blanknamespace' ).text();
50 }
51 return { data: ns, label: name };
52 } ).sort( function ( a, b ) {
53 // wgFormattedNamespaces is an object, and so technically doesn't have to be ordered
54 return a.data - b.data;
55 } );
56
57 if ( config.includeAllValue !== null && config.includeAllValue !== undefined ) {
58 options.unshift( {
59 data: config.includeAllValue,
60 label: mw.message( 'namespacesall' ).text()
61 } );
62 }
63
64 return options;
65 };
66
67 }( jQuery, mediaWiki ) );