Merge "HTMLMultiSelectField: Add 'dropdown' option for 'mw-chosen' behavior and document"
[lhc/web/wiklou.git] / resources / src / mediawiki.widgets / mw.widgets.NamespaceInputWidget.js
index 05e6f27..c88395e 100644 (file)
@@ -7,62 +7,63 @@
 ( function ( $, mw ) {
 
        /**
-        * Creates a mw.widgets.NamespaceInputWidget object.
-        *
-        * This is not a complete implementation and is not intended for public usage. It only exists
-        * because of HTMLForm shenanigans.
+        * Namespace input widget. Displays a dropdown box with the choice of available namespaces.
         *
         * @class
-        * @private
-        * @extends OO.ui.Widget
+        * @extends OO.ui.DropdownInputWidget
         *
         * @constructor
         * @param {Object} [config] Configuration options
-        * @cfg {OO.ui.DropdownInputWidget} namespace Widget to include
-        * @cfg {OO.ui.CheckboxInputWidget|null} invert Widget to include
-        * @cfg {OO.ui.CheckboxInputWidget|null} associated Widget to include
-        * @cfg {string|null} allValue Value for "all namespaces" option, if any
+        * @cfg {string|null} [includeAllValue] Value for "all namespaces" option, if any
+        * @cfg {number[]} [exclude] List of namespace numbers to exclude from the selector
         */
        mw.widgets.NamespaceInputWidget = function MwWidgetsNamespaceInputWidget( config ) {
+               // Configuration initialization
+               config = $.extend( {}, config, { options: this.getNamespaceDropdownOptions( config ) } );
+
                // Parent constructor
                mw.widgets.NamespaceInputWidget.parent.call( this, config );
 
-               // Properties
-               this.namespace = config.namespace;
-               this.invert = config.invert;
-               this.associated = config.associated;
-               this.allValue = config.allValue;
-
-               // Events
-               this.namespace.connect( this, { change: 'updateCheckboxesState' } );
-
                // Initialization
-               this.$element
-                       .addClass( 'mw-widget-namespaceInputWidget' )
-                       .append(
-                               this.namespace.$element,
-                               this.invert ? this.invert.$element : '',
-                               this.associated ? this.associated.$element : ''
-                       );
-               this.updateCheckboxesState();
+               this.$element.addClass( 'mw-widget-namespaceInputWidget' );
        };
 
        /* Setup */
 
-       OO.inheritClass( mw.widgets.NamespaceInputWidget, OO.ui.Widget );
+       OO.inheritClass( mw.widgets.NamespaceInputWidget, OO.ui.DropdownInputWidget );
 
        /* Methods */
 
        /**
-        * Update the disabled state of checkboxes when the value of namespace dropdown changes.
+        * @private
         */
-       mw.widgets.NamespaceInputWidget.prototype.updateCheckboxesState = function () {
-               if ( this.invert ) {
-                       this.invert.getField().setDisabled( this.namespace.getValue() === this.allValue );
-               }
-               if ( this.associated ) {
-                       this.associated.getField().setDisabled( this.namespace.getValue() === this.allValue );
+       mw.widgets.NamespaceInputWidget.prototype.getNamespaceDropdownOptions = function ( config ) {
+               var options,
+                       exclude = config.exclude || [],
+                       mainNamespace = mw.config.get( 'wgNamespaceIds' )[ '' ];
+
+               options = $.map( mw.config.get( 'wgFormattedNamespaces' ), function ( name, ns ) {
+                       if ( ns < mainNamespace || exclude.indexOf( Number( ns ) ) !== -1 ) {
+                               return null; // skip
+                       }
+                       ns = String( ns );
+                       if ( ns === String( mainNamespace ) ) {
+                               name = mw.message( 'blanknamespace' ).text();
+                       }
+                       return { data: ns, label: name };
+               } ).sort( function ( a, b ) {
+                       // wgFormattedNamespaces is an object, and so technically doesn't have to be ordered
+                       return a.data - b.data;
+               } );
+
+               if ( config.includeAllValue !== null && config.includeAllValue !== undefined ) {
+                       options.unshift( {
+                               data: config.includeAllValue,
+                               label: mw.message( 'namespacesall' ).text()
+                       } );
                }
+
+               return options;
        };
 
 }( jQuery, mediaWiki ) );