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 * @cfg {boolean} [pushPending=true] Visually mark the input field as "pending", while
17 * requesting suggestions.
18 * @cfg {boolean} [performSearchOnClick=true] If true, the script will start a search when-
19 * ever a user hits a suggestion. If false, the text of the suggestion is inserted into the
21 * @cfg {string} [dataLocation='header'] Where the search input field will be
22 * used (header or content).
24 mw
.widgets
.SearchInputWidget
= function MwWidgetsSearchInputWidget( config
) {
25 // The parent constructors will detach this from the DOM, and won't
26 // be reattached until after this function is completed. As such
27 // grab a handle here. If no config.$input is passed tracking of
28 // form submissions won't work.
29 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(
72 OO
.inheritClass( mw
.widgets
.SearchInputWidget
, mw
.widgets
.TitleInputWidget
);
77 * @inheritdoc mw.widgets.TitleWidget
79 mw
.widgets
.SearchInputWidget
.prototype.getSuggestionsPromise = function () {
80 var api
= new mw
.Api(),
84 // reuse the searchSuggest function from mw.searchSuggest
85 promise
= mw
.searchSuggest
.request( api
, this.getQueryValue(), $.noop
, this.limit
);
88 promise
.done( function ( data
, jqXHR
) {
89 self
.requestType
= jqXHR
.getResponseHeader( 'X-OpenSearch-Type' );
96 * @inheritdoc mw.widgets.TitleInputWidget
98 mw
.widgets
.SearchInputWidget
.prototype.getLookupCacheDataFromResponse = function ( response
) {
101 // mw.widgets.TitleInputWidget uses response.query, which doesn't exist for opensearch,
102 // so return the whole response (titles only, and links)
104 data
: response
|| {},
106 type
: this.requestType
|| 'unknown',
107 query
: this.getQueryValue()
110 this.requestType
= undefined;
116 * @inheritdoc mw.widgets.TitleWidget
118 mw
.widgets
.SearchInputWidget
.prototype.getOptionsFromData = function ( data
) {
122 // mw.widgets.TitleWidget does a lot more work here, because the TitleOptionWidgets can
123 // differ a lot, depending on the returned data from the request. With the request used here
124 // we get only the search results.
125 $.each( data
.data
[ 1 ], function ( i
, result
) {
126 items
.push( new mw
.widgets
.TitleOptionWidget(
127 // data[ 3 ][ i ] is the link for this result
128 self
.getOptionWidgetData( result
, null, data
.data
[ 3 ][ i
] )
132 mw
.track( 'mw.widgets.SearchInputWidget', {
133 action
: 'impression-results',
134 numberOfResults
: items
.length
,
135 resultSetType
: data
.metadata
.type
,
136 query
: data
.metadata
.query
,
137 inputLocation
: this.dataLocation
|| 'header'
144 * @inheritdoc mw.widgets.TitleWidget
146 * @param {string} title
147 * @param {Object} data
148 * @param {string} url The Url to the result
150 mw
.widgets
.SearchInputWidget
.prototype.getOptionWidgetData = function ( title
, data
, url
) {
151 // the values used in mw.widgets-TitleWidget doesn't exist here, that's why
152 // the values are hard-coded here
160 disambiguation
: false,
161 query
: this.getQueryValue()
168 mw
.widgets
.SearchInputWidget
.prototype.onLookupMenuItemChoose = function () {
169 mw
.widgets
.SearchInputWidget
.parent
.prototype.onLookupMenuItemChoose
.apply( this, arguments
);
171 if ( this.performSearchOnClick
) {
172 this.$element
.closest( 'form' ).submit();
179 mw
.widgets
.SearchInputWidget
.prototype.getLookupMenuOptionsFromData = function () {
180 var items
= mw
.widgets
.SearchInputWidget
.parent
.prototype.getLookupMenuOptionsFromData
.apply(
184 this.lastLookupItems
= items
.map( function ( item
) {
191 }( jQuery
, mediaWiki
) );