Merge "auth: Follow up on e907d4328dc3e"
[lhc/web/wiklou.git] / resources / src / mediawiki.widgets / mw.widgets.SelectWithInputWidget.js
index 1960351..f61255a 100644 (file)
@@ -4,7 +4,7 @@
  * @copyright 2011-2017 MediaWiki Widgets Team and others; see AUTHORS.txt
  * @license The MIT License (MIT); see LICENSE.txt
  */
-( function ( $, mw ) {
+( function () {
 
        /**
         * Select with input widget. Displays an OO.ui.TextInputWidget along with
@@ -50,6 +50,9 @@
 
                // Events
                this.dropdowninput.on( 'change', this.onChange.bind( this ) );
+               this.textinput.on( 'change', function () {
+                       this.emit( 'change', this.getValue() );
+               }.bind( this ) );
 
                // Parent constructor
                mw.widgets.SelectWithInputWidget.parent.call( this, config );
                this.textinput.setDisabled( textinputIsHidden || disabled );
        };
 
+       /**
+        * Set the value from outside.
+        *
+        * @param {string|undefined} value
+        */
+       mw.widgets.SelectWithInputWidget.prototype.setValue = function ( value ) {
+               var selectable = false;
+
+               if ( this.or ) {
+                       if ( value !== 'other' ) {
+                               selectable = !!this.dropdowninput.dropdownWidget.getMenu().findItemFromData( value );
+                       }
+
+                       if ( selectable ) {
+                               this.dropdowninput.setValue( value );
+                               this.textinput.setValue( undefined );
+                       } else {
+                               this.dropdowninput.setValue( 'other' );
+                               this.textinput.setValue( value );
+                       }
+
+                       this.emit( 'change', value );
+               }
+       };
+
+       /**
+        * Get the value from outside.
+        *
+        * @return {string}
+        */
+       mw.widgets.SelectWithInputWidget.prototype.getValue = function () {
+               if ( this.or ) {
+                       if ( this.dropdowninput.getValue() !== 'other' ) {
+                               return this.dropdowninput.getValue();
+                       }
+
+                       return this.textinput.getValue();
+               } else {
+                       return '';
+               }
+       };
+
        /**
         * Handle change events on the DropdownInput
         *
                        // submitted with the form. So we should also disable fields when hiding them.
                        this.textinput.setDisabled( value !== 'other' || this.isDisabled() );
                }
+
+               this.emit( 'change', this.getValue() );
        };
 
-}( jQuery, mediaWiki ) );
+}() );