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',
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(
74 this.$element
.addClass( 'oo-ui-textInputWidget-type-search' );
75 this.updateSearchIndicator();
83 OO
.inheritClass( mw
.widgets
.SearchInputWidget
, mw
.widgets
.TitleInputWidget
);
91 mw
.widgets
.SearchInputWidget
.prototype.getInputElement = function () {
92 return $( '<input>' ).attr( 'type', 'search' );
98 mw
.widgets
.SearchInputWidget
.prototype.onIndicatorMouseDown = function ( e
) {
99 if ( e
.which
=== OO
.ui
.MouseButtons
.LEFT
) {
100 // Clear the text field
102 this.$input
[ 0 ].focus();
108 * Update the 'clear' indicator displayed on type: 'search' text
109 * fields, hiding it when the field is already empty or when it's not
112 mw
.widgets
.SearchInputWidget
.prototype.updateSearchIndicator = function () {
113 if ( this.getValue() === '' || this.isDisabled() || this.isReadOnly() ) {
114 this.setIndicator( null );
116 this.setIndicator( 'clear' );
121 * @see OO.ui.SearchInputWidget#onChange
123 mw
.widgets
.SearchInputWidget
.prototype.onChange = function () {
124 this.updateSearchIndicator();
128 * Handle disable events.
130 * @param {boolean} disabled Element is disabled
133 mw
.widgets
.SearchInputWidget
.prototype.onDisable = function () {
134 this.updateSearchIndicator();
140 mw
.widgets
.SearchInputWidget
.prototype.setReadOnly = function ( state
) {
141 mw
.widgets
.SearchInputWidget
.parent
.prototype.setReadOnly
.call( this, state
);
142 this.updateSearchIndicator();
147 * @inheritdoc mw.widgets.TitleWidget
149 mw
.widgets
.SearchInputWidget
.prototype.getSuggestionsPromise = function () {
150 var api
= this.getApi(),
154 // reuse the searchSuggest function from mw.searchSuggest
155 promise
= mw
.searchSuggest
.request( api
, this.getQueryValue(), $.noop
, this.limit
, this.getNamespace() );
158 promise
.done( function ( data
, jqXHR
) {
159 self
.requestType
= jqXHR
.getResponseHeader( 'X-OpenSearch-Type' );
166 * @inheritdoc mw.widgets.TitleInputWidget
168 mw
.widgets
.SearchInputWidget
.prototype.getLookupCacheDataFromResponse = function ( response
) {
171 // mw.widgets.TitleInputWidget uses response.query, which doesn't exist for opensearch,
172 // so return the whole response (titles only, and links)
174 data
: response
|| {},
176 type
: this.requestType
|| 'unknown',
177 query
: this.getQueryValue()
180 this.requestType
= undefined;
186 * @inheritdoc mw.widgets.TitleWidget
188 mw
.widgets
.SearchInputWidget
.prototype.getOptionsFromData = function ( data
) {
192 $.each( data
.data
[ 1 ], function ( i
, result
) {
193 items
.push( new mw
.widgets
.TitleOptionWidget(
194 self
.getOptionWidgetData(
196 // Create a result object that looks like the one from
197 // the parent's API query.
200 // data[ 3 ][ i ] is the link for this result
201 url
: data
.data
[ 3 ][ i
],
206 disambiguation
: false
212 mw
.track( 'mw.widgets.SearchInputWidget', {
213 action
: 'impression-results',
214 numberOfResults
: items
.length
,
215 resultSetType
: data
.metadata
.type
,
216 query
: data
.metadata
.query
,
217 inputLocation
: this.dataLocation
|| 'header'
226 mw
.widgets
.SearchInputWidget
.prototype.onLookupMenuItemChoose = function () {
227 mw
.widgets
.SearchInputWidget
.parent
.prototype.onLookupMenuItemChoose
.apply( this, arguments
);
229 if ( this.performSearchOnClick
) {
230 this.$element
.closest( 'form' ).submit();
237 mw
.widgets
.SearchInputWidget
.prototype.getLookupMenuOptionsFromData = function () {
238 var items
= mw
.widgets
.SearchInputWidget
.parent
.prototype.getLookupMenuOptionsFromData
.apply(
242 this.lastLookupItems
= items
.map( function ( item
) {
249 }( jQuery
, mediaWiki
) );