Debian Codegraph @

resources/src/mediawiki.widgets/mw.widgets.NamespaceInputWidget.js

raw symbols SCIP
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 * @classdesc Displays a dropdown box with the choice of available namespaces.
11 *
12 * @class
13 * @extends OO.ui.DropdownInputWidget
14 *
15 * @constructor
16 * @description Create an instance of `mw.widgets.NamespaceInputWidget`.
17 * @param {Object} [config] Configuration options
18 * @param {string|null} [config.includeAllValue] Value for "all namespaces" option, if any
19 * @param {boolean} [config.userLang=false] Display namespaces in user language
20 * @param {number[]} [config.exclude] List of namespace numbers to exclude from the selector
21 */
22 mw.widgets.NamespaceInputWidget = function MwWidgetsNamespaceInputWidget( config ) {
23 // Configuration initialization
24 config = Object.assign( {}, config, { options: this.constructor.static.getNamespaceDropdownOptions( config ) } );
25
26 // Parent constructor
27 mw.widgets.NamespaceInputWidget.super.call( this, config );
28
29 // Initialization
30 this.$element.addClass( 'mw-widget-namespaceInputWidget' );
31 };
32
33 /* Setup */
34
35 OO.inheritClass( mw.widgets.NamespaceInputWidget, OO.ui.DropdownInputWidget );
36
37 /* Static methods */
38
39 /**
40 * @typedef {Object} mw.widgets.NamespaceInputWidget~DropdownOptions
41 * @property {any} data
42 * @property {string} label
43 */
44
45 /**
46 * Get a list of namespace options, sorted by ID.
47 *
48 * @method getNamespaceDropdownOptions
49 * @param {Object} [config] Configuration options
50 * @return {DropdownOptions[]} Dropdown options
51 * @memberof mw.widgets.NamespaceInputWidget
52 */
53 mw.widgets.NamespaceInputWidget.static.getNamespaceDropdownOptions = function ( config ) {
54 const exclude = config.exclude || [],
55 mainNamespace = mw.config.get( 'wgNamespaceIds' )[ '' ];
56
57 const namespaces = config.userLang ?
58 require( './data.json' ).formattedNamespaces :
59 mw.config.get( 'wgFormattedNamespaces' );
60
61 // eslint-disable-next-line no-jquery/no-map-util
62 const options = $.map( namespaces, ( name, ns ) => {
63 if ( ns < mainNamespace || exclude.indexOf( Number( ns ) ) !== -1 ) {
64 return null; // skip
65 }
66 ns = String( ns );
67 if ( ns === String( mainNamespace ) ) {
68 name = mw.msg( 'blanknamespace' );
69 }
70 return { data: ns, label: name };
71 } ).sort(
72 // wgFormattedNamespaces is an object, and so technically doesn't have to be ordered
73 ( a, b ) => a.data - b.data
74 );
75
76 if ( config.includeAllValue !== null && config.includeAllValue !== undefined ) {
77 options.unshift( {
78 data: config.includeAllValue,
79 label: mw.msg( 'namespacesall' )
80 } );
81 }
82
83 return options;
84 };
85
86 }() );