Implement HTMLTitleTextField with suggestions for non-OOUI forms, too
authorFlorian <florian.schmidt.stargatewissen@gmail.com>
Thu, 29 Oct 2015 17:51:02 +0000 (18:51 +0100)
committerFlorian <florian.schmidt.stargatewissen@gmail.com>
Tue, 23 Feb 2016 18:01:17 +0000 (19:01 +0100)
Also implement a way to use mediawiki.searchSuggests without wrapping
a link around the suggestions.

Bug: T117033
Change-Id: I2c7fc71d19cefaa16a6cc4526af05be9cd32366e

includes/htmlform/HTMLTextField.php
includes/htmlform/HTMLTitleTextField.php
resources/src/mediawiki/mediawiki.searchSuggest.js

index c2606c2..fb7584b 100644 (file)
@@ -22,7 +22,7 @@ class HTMLTextField extends HTMLFormField {
                                'value' => $value,
                                'dir' => $this->mDir,
                                'spellcheck' => $this->getSpellCheck(),
-                       ] + $this->getTooltipAndAccessKey();
+                       ] + $this->getTooltipAndAccessKey() + $this->getDataAttribs();
 
                if ( $this->mClass !== '' ) {
                        $attribs['class'] = $this->mClass;
@@ -126,4 +126,13 @@ class HTMLTextField extends HTMLFormField {
        protected function getInputWidget( $params ) {
                return new OOUI\TextInputWidget( $params );
        }
+
+       /**
+        * Returns an array of data-* attributes to add to the field.
+        *
+        * @return array
+        */
+       protected function getDataAttribs() {
+               return [];
+       }
 }
index 09fbaa7..410d15d 100644 (file)
@@ -80,4 +80,20 @@ class HTMLTitleTextField extends HTMLTextField {
                $params['relative'] = $this->mParams['relative'];
                return new TitleInputWidget( $params );
        }
+
+       public function getInputHtml( $value ) {
+               // add mw-searchInput class to enable search suggestions for non-OOUI, too
+               $this->mClass .= 'mw-searchInput';
+
+               // return the HTMLTextField html
+               return parent::getInputHtml( $value );
+       }
+
+       protected function getDataAttribs() {
+               return [
+                       'data-mw-searchsuggest' => FormatJson::encode( [
+                               'wrapAsLink' => false,
+                       ] ),
+               ];
+       }
 }
index 0fcd22c..782501a 100644 (file)
 
                // The function used to render the suggestions.
                function renderFunction( text, context ) {
-                       var formData = getFormData( context );
+                       var formData = getFormData( context ),
+                               textboxConfig = context.data.$textbox.data( 'mw-searchsuggest' ) || {};
 
                        // linkParams object is modified and reused
                        formData.linkParams[ formData.textParam ] = text;
 
-                       // this is the container <div>, jQueryfied
-                       this.text( text )
-                               .wrap(
+                       // this is the container <div>, jQueryfield
+                       this.text( text );
+
+                       // wrap only as link, if the config doesn't disallow it
+                       if ( textboxConfig.wrapAsLink !== false ) {
+                               this.wrap(
                                        $( '<a>' )
                                                .attr( 'href', formData.baseHref + $.param( formData.linkParams ) )
                                                .attr( 'title', text )
                                                .addClass( 'mw-searchSuggest-link' )
                                );
+                       }
                }
 
                // The function used when the user makes a selection