From 13498fabdb406773ae73abd2e5c6e38748c53a0f Mon Sep 17 00:00:00 2001 From: Florian Date: Thu, 29 Oct 2015 18:51:02 +0100 Subject: [PATCH] Implement HTMLTitleTextField with suggestions for non-OOUI forms, too Also implement a way to use mediawiki.searchSuggests without wrapping a link around the suggestions. Bug: T117033 Change-Id: I2c7fc71d19cefaa16a6cc4526af05be9cd32366e --- includes/htmlform/HTMLTextField.php | 11 ++++++++++- includes/htmlform/HTMLTitleTextField.php | 16 ++++++++++++++++ .../src/mediawiki/mediawiki.searchSuggest.js | 13 +++++++++---- 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/includes/htmlform/HTMLTextField.php b/includes/htmlform/HTMLTextField.php index c2606c2329..fb7584b7d1 100644 --- a/includes/htmlform/HTMLTextField.php +++ b/includes/htmlform/HTMLTextField.php @@ -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 []; + } } diff --git a/includes/htmlform/HTMLTitleTextField.php b/includes/htmlform/HTMLTitleTextField.php index 09fbaa74c8..410d15d080 100644 --- a/includes/htmlform/HTMLTitleTextField.php +++ b/includes/htmlform/HTMLTitleTextField.php @@ -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, + ] ), + ]; + } } diff --git a/resources/src/mediawiki/mediawiki.searchSuggest.js b/resources/src/mediawiki/mediawiki.searchSuggest.js index 0fcd22c46f..782501aa98 100644 --- a/resources/src/mediawiki/mediawiki.searchSuggest.js +++ b/resources/src/mediawiki/mediawiki.searchSuggest.js @@ -108,19 +108,24 @@ // 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
, jQueryfied - this.text( text ) - .wrap( + // this is the container
, jQueryfield + this.text( text ); + + // wrap only as link, if the config doesn't disallow it + if ( textboxConfig.wrapAsLink !== false ) { + this.wrap( $( '' ) .attr( 'href', formData.baseHref + $.param( formData.linkParams ) ) .attr( 'title', text ) .addClass( 'mw-searchSuggest-link' ) ); + } } // The function used when the user makes a selection -- 2.20.1