mw.widgets: Make TitleSearchWidget read-only-able
authorEd Sanders <esanders@wikimedia.org>
Mon, 11 Feb 2019 13:52:39 +0000 (13:52 +0000)
committerEd Sanders <esanders@wikimedia.org>
Mon, 11 Feb 2019 13:52:39 +0000 (13:52 +0000)
Change-Id: I3eed52438c1e416d4dcdedb564bb9df3745d0f77

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

index b9bd1e0..f1f98a2 100644 (file)
                var widget = this;
 
                this.getRequestData().done( function ( data ) {
+                       if ( widget.query.isReadOnly() ) {
+                               // The request object is always abortable, so just
+                               // prevent the results from displaying
+                               return;
+                       }
                        // Parent method
                        mw.widgets.TitleSearchWidget.parent.prototype.onQueryChange.call( widget );
                        widget.results.addItems( widget.getOptionsFromData( data ) );
                return response.query || {};
        };
 
+       /**
+        * Check if the widget is read-only.
+        *
+        * @return {boolean}
+        */
+       mw.widgets.TitleSearchWidget.prototype.isReadOnly = function () {
+               return this.query.isReadOnly();
+       };
+
+       /**
+        * Set the read-only state of the widget.
+        *
+        * @param {boolean} readOnly Make input read-only
+        * @chainable
+        * @return {mw.widgets.TitleSearchWidget} The widget, for chaining
+        */
+       mw.widgets.TitleSearchWidget.prototype.setReadOnly = function ( readOnly ) {
+               this.query.setReadOnly( readOnly );
+               if ( readOnly ) {
+                       // Hide results
+                       this.results.clearItems();
+               }
+               return this;
+       };
+
 }() );