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
) {
29 performSearchOnClick
: true,
30 dataLocation
: 'header'
34 mw
.widgets
.SearchInputWidget
.parent
.call( this, config
);
37 this.$element
.addClass( 'mw-widget-searchInputWidget' );
38 this.lookupMenu
.$element
.addClass( 'mw-widget-searchWidget-menu' );
39 if ( !config
.pushPending
) {
40 this.pushPending
= false;
42 if ( config
.dataLocation
) {
43 this.dataLocation
= config
.dataLocation
;
45 if ( config
.performSearchOnClick
) {
46 this.performSearchOnClick
= config
.performSearchOnClick
;
48 this.setLookupsDisabled( !this.suggestions
);
53 OO
.inheritClass( mw
.widgets
.SearchInputWidget
, mw
.widgets
.TitleInputWidget
);
58 * @inheritdoc mw.widgets.TitleWidget
60 mw
.widgets
.SearchInputWidget
.prototype.getSuggestionsPromise = function () {
61 var api
= new mw
.Api(),
65 // reuse the searchSuggest function from mw.searchSuggest
66 promise
= mw
.searchSuggest
.request( api
, this.getQueryValue(), $.noop
, this.limit
);
69 promise
.done( function ( data
, jqXHR
) {
70 self
.requestType
= jqXHR
.getResponseHeader( 'X-OpenSearch-Type' );
77 * @inheritdoc mw.widgets.TitleInputWidget
79 mw
.widgets
.SearchInputWidget
.prototype.getLookupCacheDataFromResponse = function ( response
) {
82 // mw.widgets.TitleInputWidget uses response.query, which doesn't exist for opensearch,
83 // so return the whole response (titles only, and links)
87 type
: this.requestType
|| 'unknown',
88 query
: this.getQueryValue()
91 this.requestType
= undefined;
97 * @inheritdoc mw.widgets.TitleWidget
99 mw
.widgets
.SearchInputWidget
.prototype.getOptionsFromData = function ( data
) {
103 // mw.widgets.TitleWidget does a lot more work here, because the TitleOptionWidgets can
104 // differ a lot, depending on the returned data from the request. With the request used here
105 // we get only the search results.
106 $.each( data
.data
[ 1 ], function ( i
, result
) {
107 items
.push( new mw
.widgets
.TitleOptionWidget(
108 // data[ 3 ][ i ] is the link for this result
109 self
.getOptionWidgetData( result
, null, data
.data
[ 3 ][ i
] )
113 mw
.track( 'mw.widgets.SearchInputWidget', {
114 action
: 'impression-results',
115 numberOfResults
: items
.length
,
116 resultSetType
: data
.metadata
.type
,
117 query
: data
.metadata
.query
,
118 inputLocation
: this.dataLocation
|| 'header'
125 * @inheritdoc mw.widgets.TitleWidget
127 * @param {string} title
128 * @param {Object} data
129 * @param {string} url The Url to the result
131 mw
.widgets
.SearchInputWidget
.prototype.getOptionWidgetData = function ( title
, data
, url
) {
132 // the values used in mw.widgets-TitleWidget doesn't exist here, that's why
133 // the values are hard-coded here
141 disambiguation
: false,
142 query
: this.getQueryValue()
149 mw
.widgets
.SearchInputWidget
.prototype.onLookupMenuItemChoose = function ( item
) {
152 // get items which was suggested before the input changes
153 items
= this.lookupMenu
.items
;
155 mw
.widgets
.SearchInputWidget
.parent
.prototype.onLookupMenuItemChoose
.apply( this, arguments
);
157 mw
.track( 'mw.widgets.SearchInputWidget', {
158 action
: 'click-result',
159 numberOfResults
: items
.length
,
160 clickIndex
: items
.indexOf( item
) + 1
163 if ( this.performSearchOnClick
) {
164 this.$element
.closest( 'form' ).submit();
168 }( jQuery
, mediaWiki
) );