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
22 mw
.widgets
.SearchInputWidget
= function MwWidgetsSearchInputWidget( config
) {
27 performSearchOnClick
: true
31 mw
.widgets
.SearchInputWidget
.parent
.call( this, config
);
34 this.$element
.addClass( 'mw-widget-searchInputWidget' );
35 this.lookupMenu
.$element
.addClass( 'mw-widget-searchWidget-menu' );
36 if ( !config
.pushPending
) {
37 this.pushPending
= false;
39 if ( config
.dataLocation
) {
40 this.dataLocation
= config
.dataLocation
;
42 if ( config
.performSearchOnClick
) {
43 this.performSearchOnClick
= config
.performSearchOnClick
;
45 this.setLookupsDisabled( !this.suggestions
);
50 OO
.inheritClass( mw
.widgets
.SearchInputWidget
, mw
.widgets
.TitleInputWidget
);
55 * @inheritdoc mw.widgets.TitleWidget
57 mw
.widgets
.SearchInputWidget
.prototype.getSuggestionsPromise = function () {
58 var api
= new mw
.Api(),
62 // reuse the searchSuggest function from mw.searchSuggest
63 promise
= mw
.searchSuggest
.request( api
, this.getQueryValue(), $.noop
, this.limit
);
66 promise
.done( function ( data
, jqXHR
) {
67 self
.requestType
= jqXHR
.getResponseHeader( 'X-OpenSearch-Type' );
74 * @inheritdoc mw.widgets.TitleInputWidget
76 mw
.widgets
.SearchInputWidget
.prototype.getLookupCacheDataFromResponse = function ( response
) {
79 // mw.widgets.TitleInputWidget uses response.query, which doesn't exist for opensearch,
80 // so return the whole response (titles only, and links)
84 type
: this.requestType
|| 'unknown',
85 query
: this.getQueryValue()
88 this.requestType
= undefined;
94 * @inheritdoc mw.widgets.TitleWidget
96 mw
.widgets
.SearchInputWidget
.prototype.getOptionsFromData = function ( data
) {
100 // mw.widgets.TitleWidget does a lot more work here, because the TitleOptionWidgets can
101 // differ a lot, depending on the returned data from the request. With the request used here
102 // we get only the search results.
103 $.each( data
.data
[ 1 ], function ( i
, result
) {
104 items
.push( new mw
.widgets
.TitleOptionWidget(
105 // data[ 3 ][ i ] is the link for this result
106 self
.getOptionWidgetData( result
, null, data
.data
[ 3 ][ i
] )
110 mw
.track( 'mw.widgets.SearchInputWidget', {
111 action
: 'impression-results',
112 numberOfResults
: items
.length
,
113 resultSetType
: data
.metadata
.type
,
114 query
: data
.metadata
.query
,
115 inputLocation
: this.dataLocation
|| 'header'
122 * @inheritdoc mw.widgets.TitleWidget
124 * @param {string} title
125 * @param {Object} data
126 * @param {string} url The Url to the result
128 mw
.widgets
.SearchInputWidget
.prototype.getOptionWidgetData = function ( title
, data
, url
) {
129 // the values used in mw.widgets-TitleWidget doesn't exist here, that's why
130 // the values are hard-coded here
138 disambiguation
: false,
139 query
: this.getQueryValue()
146 mw
.widgets
.SearchInputWidget
.prototype.onLookupMenuItemChoose = function ( item
) {
149 // get items which was suggested before the input changes
150 items
= this.lookupMenu
.items
;
152 mw
.widgets
.SearchInputWidget
.parent
.prototype.onLookupMenuItemChoose
.apply( this, arguments
);
154 mw
.track( 'mw.widgets.SearchInputWidget', {
155 action
: 'click-result',
156 numberOfResults
: items
.length
,
157 clickIndex
: items
.indexOf( item
) + 1
160 if ( this.performSearchOnClick
) {
161 this.$element
.closest( 'form' ).submit();
165 }( jQuery
, mediaWiki
) );