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=true] 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' ) : $();
36 performSearchOnClick
: true,
37 dataLocation
: 'header'
41 mw
.widgets
.SearchInputWidget
.parent
.call( this, config
);
44 this.$element
.addClass( 'mw-widget-searchInputWidget' );
45 this.lookupMenu
.$element
.addClass( 'mw-widget-searchWidget-menu' );
46 this.lastLookupItems
= [];
47 if ( !config
.pushPending
) {
48 this.pushPending
= false;
50 if ( config
.dataLocation
) {
51 this.dataLocation
= config
.dataLocation
;
53 if ( config
.performSearchOnClick
) {
54 this.performSearchOnClick
= config
.performSearchOnClick
;
56 this.setLookupsDisabled( !this.suggestions
);
58 $form
.on( 'submit', function () {
59 mw
.track( 'mw.widgets.SearchInputWidget', {
60 action
: 'submit-form',
61 numberOfResults
: this.lastLookupItems
.length
,
63 inputLocation
: this.dataLocation
|| 'header',
64 index
: this.lastLookupItems
.indexOf(
73 OO
.inheritClass( mw
.widgets
.SearchInputWidget
, mw
.widgets
.TitleInputWidget
);
78 * @inheritdoc mw.widgets.TitleWidget
80 mw
.widgets
.SearchInputWidget
.prototype.getSuggestionsPromise = function () {
81 var api
= this.getApi(),
85 // reuse the searchSuggest function from mw.searchSuggest
86 promise
= mw
.searchSuggest
.request( api
, this.getQueryValue(), $.noop
, this.limit
);
89 promise
.done( function ( data
, jqXHR
) {
90 self
.requestType
= jqXHR
.getResponseHeader( 'X-OpenSearch-Type' );
97 * @inheritdoc mw.widgets.TitleInputWidget
99 mw
.widgets
.SearchInputWidget
.prototype.getLookupCacheDataFromResponse = function ( response
) {
102 // mw.widgets.TitleInputWidget uses response.query, which doesn't exist for opensearch,
103 // so return the whole response (titles only, and links)
105 data
: response
|| {},
107 type
: this.requestType
|| 'unknown',
108 query
: this.getQueryValue()
111 this.requestType
= undefined;
117 * @inheritdoc mw.widgets.TitleWidget
119 mw
.widgets
.SearchInputWidget
.prototype.getOptionsFromData = function ( data
) {
123 // mw.widgets.TitleWidget does a lot more work here, because the TitleOptionWidgets can
124 // differ a lot, depending on the returned data from the request. With the request used here
125 // we get only the search results.
126 $.each( data
.data
[ 1 ], function ( i
, result
) {
127 items
.push( new mw
.widgets
.TitleOptionWidget(
128 // data[ 3 ][ i ] is the link for this result
129 self
.getOptionWidgetData( result
, null, data
.data
[ 3 ][ i
] )
133 mw
.track( 'mw.widgets.SearchInputWidget', {
134 action
: 'impression-results',
135 numberOfResults
: items
.length
,
136 resultSetType
: data
.metadata
.type
,
137 query
: data
.metadata
.query
,
138 inputLocation
: this.dataLocation
|| 'header'
145 * @inheritdoc mw.widgets.TitleWidget
147 * @param {string} title
148 * @param {Object} data
149 * @param {string} url The Url to the result
151 mw
.widgets
.SearchInputWidget
.prototype.getOptionWidgetData = function ( title
, data
, url
) {
152 // the values used in mw.widgets-TitleWidget doesn't exist here, that's why
153 // the values are hard-coded here
161 disambiguation
: false,
162 query
: this.getQueryValue()
169 mw
.widgets
.SearchInputWidget
.prototype.onLookupMenuItemChoose = function () {
170 mw
.widgets
.SearchInputWidget
.parent
.prototype.onLookupMenuItemChoose
.apply( this, arguments
);
172 if ( this.performSearchOnClick
) {
173 this.$element
.closest( 'form' ).submit();
180 mw
.widgets
.SearchInputWidget
.prototype.getLookupMenuOptionsFromData = function () {
181 var items
= mw
.widgets
.SearchInputWidget
.parent
.prototype.getLookupMenuOptionsFromData
.apply(
185 this.lastLookupItems
= items
.map( function ( item
) {
192 }( jQuery
, mediaWiki
) );