Merge "Add support for 'hu-formal'"
[lhc/web/wiklou.git] / resources / src / mediawiki.special / mediawiki.special.apisandbox.js
index 435dec2..df87c9c 100644 (file)
@@ -1,4 +1,3 @@
-/* eslint-disable no-use-before-define */
 ( function ( $, mw, OO ) {
        'use strict';
        var ApiSandbox, Util, WidgetMethods, Validators,
                moduleInfoCache = {},
                baseRequestParams;
 
+       /**
+        * A wrapper for a widget that provides an enable/disable button
+        *
+        * @class
+        * @private
+        * @constructor
+        * @param {OO.ui.Widget} widget
+        * @param {Object} [config] Configuration options
+        */
+       function OptionalWidget( widget, config ) {
+               var k;
+
+               config = config || {};
+
+               this.widget = widget;
+               this.$cover = config.$cover ||
+                       $( '<div>' ).addClass( 'mw-apisandbox-optionalWidget-cover' );
+               this.checkbox = new OO.ui.CheckboxInputWidget( config.checkbox )
+                       .on( 'change', this.onCheckboxChange, [], this );
+
+               OptionalWidget[ 'super' ].call( this, config );
+
+               // Forward most methods for convenience
+               for ( k in this.widget ) {
+                       if ( $.isFunction( this.widget[ k ] ) && !this[ k ] ) {
+                               this[ k ] = this.widget[ k ].bind( this.widget );
+                       }
+               }
+
+               this.$cover.on( 'click', this.onOverlayClick.bind( this ) );
+
+               this.$element
+                       .addClass( 'mw-apisandbox-optionalWidget' )
+                       .append(
+                               this.$cover,
+                               $( '<div>' ).addClass( 'mw-apisandbox-optionalWidget-fields' ).append(
+                                       $( '<div>' ).addClass( 'mw-apisandbox-optionalWidget-widget' ).append(
+                                               widget.$element
+                                       ),
+                                       $( '<div>' ).addClass( 'mw-apisandbox-optionalWidget-checkbox' ).append(
+                                               this.checkbox.$element
+                                       )
+                               )
+                       );
+
+               this.setDisabled( widget.isDisabled() );
+       }
+       OO.inheritClass( OptionalWidget, OO.ui.Widget );
+       OptionalWidget.prototype.onCheckboxChange = function ( checked ) {
+               this.setDisabled( !checked );
+       };
+       OptionalWidget.prototype.onOverlayClick = function () {
+               this.setDisabled( false );
+               if ( $.isFunction( this.widget.focus ) ) {
+                       this.widget.focus();
+               }
+       };
+       OptionalWidget.prototype.setDisabled = function ( disabled ) {
+               OptionalWidget[ 'super' ].prototype.setDisabled.call( this, disabled );
+               this.widget.setDisabled( this.isDisabled() );
+               this.checkbox.setSelected( !this.isDisabled() );
+               this.$cover.toggle( this.isDisabled() );
+               return this;
+       };
+
        WidgetMethods = {
                textInputWidget: {
                        getApiValue: function () {
 
                dropdownWidget: {
                        getApiValue: function () {
-                               var item = this.getMenu().getSelectedItem();
+                               var item = this.getMenu().findSelectedItem();
                                return item === null ? undefined : item.getData();
                        },
                        setApiValue: function ( v ) {
                        }
                },
 
-               capsuleWidget: {
+               tagWidget: {
                        getApiValue: function () {
-                               var items = this.getItemsData();
+                               var items = this.getValue();
                                if ( items.join( '' ).indexOf( '|' ) === -1 ) {
                                        return items.join( '|' );
                                } else {
                        },
                        setApiValue: function ( v ) {
                                if ( v === undefined || v === '' || v === '\x1f' ) {
-                                       this.setItemsFromData( [] );
+                                       this.setValue( [] );
                                } else {
                                        v = String( v );
                                        if ( v.indexOf( '\x1f' ) !== 0 ) {
-                                               this.setItemsFromData( v.split( '|' ) );
+                                               this.setValue( v.split( '|' ) );
                                        } else {
-                                               this.setItemsFromData( v.substr( 1 ).split( '\x1f' ) );
+                                               this.setValue( v.substr( 1 ).split( '\x1f' ) );
                                        }
                                }
                        },
                                if ( !suppressErrors ) {
                                        ok = this.getApiValue() !== undefined && !(
                                                pi.allspecifier !== undefined &&
-                                               this.getItemsData().length > 1 &&
-                                               this.getItemsData().indexOf( pi.allspecifier ) !== -1
+                                               this.getValue().length > 1 &&
+                                               this.getValue().indexOf( pi.allspecifier ) !== -1
                                        );
                                }
 
                                this.setIconTitle( ok ? '' : mw.message( 'apisandbox-alert-field' ).plain() );
                                return $.Deferred().resolve( ok ).promise();
                        },
-                       createItemWidget: function ( data, label ) {
-                               var item = OO.ui.CapsuleMultiselectWidget.prototype.createItemWidget.call( this, data, label );
+                       createTagItemWidget: function ( data, label ) {
+                               var item = OO.ui.TagMultiselectWidget.prototype.createTagItemWidget.call( this, data, label );
                                if ( this.paramInfo.deprecatedvalues &&
                                        this.paramInfo.deprecatedvalues.indexOf( data ) >= 0
                                ) {
                                case 'string':
                                case 'user':
                                        if ( Util.apiBool( pi.multi ) ) {
-                                               widget = new OO.ui.CapsuleMultiselectWidget( {
+                                               widget = new OO.ui.TagMultiselectWidget( {
                                                        allowArbitrary: true,
                                                        allowDuplicates: Util.apiBool( pi.allowsduplicates ),
                                                        $overlay: true
                                                } );
                                                widget.paramInfo = pi;
-                                               $.extend( widget, WidgetMethods.capsuleWidget );
+                                               $.extend( widget, WidgetMethods.tagWidget );
                                        } else {
                                                widget = new OO.ui.TextInputWidget( {
                                                        required: Util.apiBool( pi.required )
                                                        } ) );
                                                }
 
-                                               widget = new OO.ui.CapsuleMultiselectWidget( {
+                                               widget = new OO.ui.MenuTagMultiselectWidget( {
                                                        menu: { items: items },
                                                        $overlay: true
                                                } );
                                                widget.paramInfo = pi;
-                                               $.extend( widget, WidgetMethods.capsuleWidget );
+                                               $.extend( widget, WidgetMethods.tagWidget );
                                        } else {
                                                widget = new OO.ui.DropdownWidget( {
                                                        menu: { items: items },
                                                        } ) );
                                                }
 
-                                               widget = new OO.ui.CapsuleMultiselectWidget( {
+                                               widget = new OO.ui.MenuTagMultiselectWidget( {
                                                        menu: { items: items },
                                                        $overlay: true
                                                } );
                                                widget.paramInfo = pi;
-                                               $.extend( widget, WidgetMethods.capsuleWidget );
+                                               $.extend( widget, WidgetMethods.tagWidget );
                                                if ( Util.apiBool( pi.submodules ) ) {
                                                        widget.getSubmodules = WidgetMethods.submoduleWidget.multi;
                                                        widget.on( 'change', ApiSandbox.updateUI );
                                                throw new Error( 'Unknown multiMode "' + multiMode + '"' );
                                }
 
-                               widget = new OO.ui.CapsuleMultiselectWidget( {
+                               widget = new OO.ui.PopupTagMultiselectWidget( {
                                        allowArbitrary: true,
                                        allowDuplicates: Util.apiBool( pi.allowsduplicates ),
                                        $overlay: true,
                                        }
                                } );
                                widget.paramInfo = pi;
-                               $.extend( widget, WidgetMethods.capsuleWidget );
+                               $.extend( widget, WidgetMethods.tagWidget );
 
                                func = function () {
                                        if ( !innerWidget.isDisabled() ) {
                                                innerWidget.apiCheckValid().done( function ( ok ) {
                                                        if ( ok ) {
-                                                               widget.addItemsFromData( [ innerWidget.getApiValue() ] );
+                                                               widget.addTag( innerWidget.getApiValue() );
                                                                innerWidget.setApiValue( undefined );
                                                        }
                                                } );
                        var i,
                                menu = formatDropdown.getMenu(),
                                items = menu.getItems(),
-                               selectedField = menu.getSelectedItem() ? menu.getSelectedItem().getData() : null;
+                               selectedField = menu.findSelectedItem() ? menu.findSelectedItem().getData() : null;
 
                        for ( i = 0; i < items.length; i++ ) {
                                items[ i ].getData().toggle( items[ i ].getData() === selectedField );
                                }
 
                                menu = formatDropdown.getMenu();
-                               selectedLabel = menu.getSelectedItem() ? menu.getSelectedItem().getLabel() : '';
+                               selectedLabel = menu.findSelectedItem() ? menu.findSelectedItem().getLabel() : '';
                                if ( typeof selectedLabel !== 'string' ) {
                                        selectedLabel = selectedLabel.text();
                                }
                                                }
                                                if ( Util.apiBool( pi.parameters[ i ].multi ) ) {
                                                        tmp = [];
-                                                       if ( flag && !( widget instanceof OO.ui.CapsuleMultiselectWidget ) &&
+                                                       if ( flag && !( widget instanceof OO.ui.TagMultiselectWidget ) &&
                                                                !(
                                                                        widget instanceof OptionalWidget &&
-                                                                       widget.widget instanceof OO.ui.CapsuleMultiselectWidget
+                                                                       widget.widget instanceof OO.ui.TagMultiselectWidget
                                                                )
                                                        ) {
                                                                tmp.push( mw.message( 'api-help-param-multi-separate' ).parse() );
                return ret;
        };
 
-       /**
-        * A wrapper for a widget that provides an enable/disable button
-        *
-        * @class
-        * @private
-        * @constructor
-        * @param {OO.ui.Widget} widget
-        * @param {Object} [config] Configuration options
-        */
-       function OptionalWidget( widget, config ) {
-               var k;
-
-               config = config || {};
-
-               this.widget = widget;
-               this.$cover = config.$cover ||
-                       $( '<div>' ).addClass( 'mw-apisandbox-optionalWidget-cover' );
-               this.checkbox = new OO.ui.CheckboxInputWidget( config.checkbox )
-                       .on( 'change', this.onCheckboxChange, [], this );
-
-               OptionalWidget[ 'super' ].call( this, config );
-
-               // Forward most methods for convenience
-               for ( k in this.widget ) {
-                       if ( $.isFunction( this.widget[ k ] ) && !this[ k ] ) {
-                               this[ k ] = this.widget[ k ].bind( this.widget );
-                       }
-               }
-
-               this.$cover.on( 'click', this.onOverlayClick.bind( this ) );
-
-               this.$element
-                       .addClass( 'mw-apisandbox-optionalWidget' )
-                       .append(
-                               this.$cover,
-                               $( '<div>' ).addClass( 'mw-apisandbox-optionalWidget-fields' ).append(
-                                       $( '<div>' ).addClass( 'mw-apisandbox-optionalWidget-widget' ).append(
-                                               widget.$element
-                                       ),
-                                       $( '<div>' ).addClass( 'mw-apisandbox-optionalWidget-checkbox' ).append(
-                                               this.checkbox.$element
-                                       )
-                               )
-                       );
-
-               this.setDisabled( widget.isDisabled() );
-       }
-       OO.inheritClass( OptionalWidget, OO.ui.Widget );
-       OptionalWidget.prototype.onCheckboxChange = function ( checked ) {
-               this.setDisabled( !checked );
-       };
-       OptionalWidget.prototype.onOverlayClick = function () {
-               this.setDisabled( false );
-               if ( $.isFunction( this.widget.focus ) ) {
-                       this.widget.focus();
-               }
-       };
-       OptionalWidget.prototype.setDisabled = function ( disabled ) {
-               OptionalWidget[ 'super' ].prototype.setDisabled.call( this, disabled );
-               this.widget.setDisabled( this.isDisabled() );
-               this.checkbox.setSelected( !this.isDisabled() );
-               this.$cover.toggle( this.isDisabled() );
-               return this;
-       };
-
        $( ApiSandbox.init );
 
        module.exports = ApiSandbox;