2 * MediaWiki Widgets - SearchInputWidget class.
4 * @copyright 2011-2015 MediaWiki Widgets Team and others; see AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
10 * Creates a mw.widgets.SearchInputWidget object.
13 * @extends mw.widgets.TitleInputWidget
16 * @param {Object} [config] Configuration options
17 * @cfg {boolean} [pushPending=false] Visually mark the input field as "pending", while
18 * requesting suggestions.
19 * @cfg {boolean} [performSearchOnClick=true] If true, the script will start a search when-
20 * ever a user hits a suggestion. If false, the text of the suggestion is inserted into the
22 * @cfg {string} [dataLocation='header'] Where the search input field will be
23 * used (header or content).
25 mw
.widgets
.SearchInputWidget
= function MwWidgetsSearchInputWidget( config
) {
26 // The parent constructors will detach this from the DOM, and won't
27 // be reattached until after this function is completed. As such
28 // grab a handle here. If no config.$input is passed tracking of
29 // form submissions won't work.
30 var $form
= config
.$input
? config
.$input
.closest( 'form' ) : $();
35 performSearchOnClick
: true,
36 dataLocation
: 'header'
40 mw
.widgets
.SearchInputWidget
.parent
.call( this, config
);
43 this.$element
.addClass( 'mw-widget-searchInputWidget' );
44 this.lookupMenu
.$element
.addClass( 'mw-widget-searchWidget-menu' );
45 this.lastLookupItems
= [];
46 if ( !config
.pushPending
) {
47 this.pushPending
= false;
49 if ( config
.dataLocation
) {
50 this.dataLocation
= config
.dataLocation
;
52 if ( config
.performSearchOnClick
) {
53 this.performSearchOnClick
= config
.performSearchOnClick
;
55 this.setLookupsDisabled( !this.suggestions
);
57 $form
.on( 'submit', function () {
58 mw
.track( 'mw.widgets.SearchInputWidget', {
59 action
: 'submit-form',
60 numberOfResults
: this.lastLookupItems
.length
,
62 inputLocation
: this.dataLocation
|| 'header',
63 index
: this.lastLookupItems
.indexOf(
69 this.$element
.addClass( 'oo-ui-textInputWidget-type-search' );
70 this.updateSearchIndicator();
78 OO
.inheritClass( mw
.widgets
.SearchInputWidget
, mw
.widgets
.TitleInputWidget
);
86 mw
.widgets
.SearchInputWidget
.prototype.getInputElement = function () {
87 return $( '<input>' ).attr( 'type', 'search' );
93 mw
.widgets
.SearchInputWidget
.prototype.onIndicatorMouseDown = function ( e
) {
94 if ( e
.which
=== OO
.ui
.MouseButtons
.LEFT
) {
95 // Clear the text field
97 this.$input
[ 0 ].focus();
103 * Update the 'clear' indicator displayed on type: 'search' text
104 * fields, hiding it when the field is already empty or when it's not
107 mw
.widgets
.SearchInputWidget
.prototype.updateSearchIndicator = function () {
108 if ( this.getValue() === '' || this.isDisabled() || this.isReadOnly() ) {
109 this.setIndicator( null );
111 this.setIndicator( 'clear' );
116 * @see OO.ui.SearchInputWidget#onChange
118 mw
.widgets
.SearchInputWidget
.prototype.onChange = function () {
119 mw
.widgets
.SearchInputWidget
.parent
.prototype.onChange
.call( this );
120 this.updateSearchIndicator();
124 * Handle disable events.
126 * @param {boolean} disabled Element is disabled
129 mw
.widgets
.SearchInputWidget
.prototype.onDisable = function () {
130 this.updateSearchIndicator();
136 mw
.widgets
.SearchInputWidget
.prototype.setReadOnly = function ( state
) {
137 mw
.widgets
.SearchInputWidget
.parent
.prototype.setReadOnly
.call( this, state
);
138 this.updateSearchIndicator();
143 * @inheritdoc mw.widgets.TitleWidget
145 mw
.widgets
.SearchInputWidget
.prototype.getSuggestionsPromise = function () {
146 var api
= this.getApi(),
150 // reuse the searchSuggest function from mw.searchSuggest
151 promise
= mw
.searchSuggest
.request( api
, this.getQueryValue(), $.noop
, this.limit
, this.getNamespace() );
154 promise
.done( function ( data
, jqXHR
) {
155 self
.requestType
= jqXHR
.getResponseHeader( 'X-OpenSearch-Type' );
162 * @inheritdoc mw.widgets.TitleInputWidget
164 mw
.widgets
.SearchInputWidget
.prototype.getLookupCacheDataFromResponse = function ( response
) {
167 // mw.widgets.TitleInputWidget uses response.query, which doesn't exist for opensearch,
168 // so return the whole response (titles only, and links)
170 data
: response
|| {},
172 type
: this.requestType
|| 'unknown',
173 query
: this.getQueryValue()
176 this.requestType
= undefined;
182 * @inheritdoc mw.widgets.TitleWidget
184 mw
.widgets
.SearchInputWidget
.prototype.getOptionsFromData = function ( data
) {
188 $.each( data
.data
[ 1 ], function ( i
, result
) {
189 items
.push( new mw
.widgets
.TitleOptionWidget(
190 self
.getOptionWidgetData(
192 // Create a result object that looks like the one from
193 // the parent's API query.
196 // data[ 3 ][ i ] is the link for this result
197 url
: data
.data
[ 3 ][ i
],
202 disambiguation
: false
208 mw
.track( 'mw.widgets.SearchInputWidget', {
209 action
: 'impression-results',
210 numberOfResults
: items
.length
,
211 resultSetType
: data
.metadata
.type
,
212 query
: data
.metadata
.query
,
213 inputLocation
: this.dataLocation
|| 'header'
222 mw
.widgets
.SearchInputWidget
.prototype.onLookupMenuItemChoose = function () {
223 mw
.widgets
.SearchInputWidget
.parent
.prototype.onLookupMenuItemChoose
.apply( this, arguments
);
225 if ( this.performSearchOnClick
) {
226 this.$element
.closest( 'form' ).submit();
233 mw
.widgets
.SearchInputWidget
.prototype.getLookupMenuOptionsFromData = function () {
234 var items
= mw
.widgets
.SearchInputWidget
.parent
.prototype.getLookupMenuOptionsFromData
.apply(
238 this.lastLookupItems
= items
.map( function ( item
) {
245 }( jQuery
, mediaWiki
) );