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 * @classdesc Search input widget.
13 * @extends mw.widgets.TitleInputWidget
16 * @description Create a mw.widgets.SearchInputWidget object.
17 * @param {Object} [config] Configuration options
18 * @param {boolean} [config.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 * @param {string} [config.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 const $form = config.$input ? config.$input.closest( 'form' ) : $();
31 config = Object.assign( {
34 showPendingRequest: false,
35 performSearchOnClick: true,
36 dataLocation: 'header'
40 mw.widgets.SearchInputWidget.super.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.dataLocation ) {
47 this.dataLocation = config.dataLocation;
49 if ( config.performSearchOnClick ) {
50 this.performSearchOnClick = config.performSearchOnClick;
52 this.setLookupsDisabled( !this.suggestions );
54 $form.on( 'submit', () => {
55 mw.track( 'mw.widgets.SearchInputWidget', {
56 action: 'submit-form',
57 numberOfResults: this.lastLookupItems.length,
59 inputLocation: this.dataLocation || 'header',
60 index: this.lastLookupItems.indexOf(
70 this.$element.addClass( 'oo-ui-textInputWidget-type-search' );
71 this.updateSearchIndicator();
79 OO.inheritClass( mw.widgets.SearchInputWidget, mw.widgets.TitleInputWidget );
87 mw.widgets.SearchInputWidget.prototype.getInputElement = function () {
88 return $( '<input>' ).attr( 'type', 'search' );
94 mw.widgets.SearchInputWidget.prototype.onIndicatorMouseDown = function ( e ) {
95 if ( e.which === OO.ui.MouseButtons.LEFT ) {
96 // Clear the text field
98 this.$input[ 0 ].focus();
104 * Update the 'clear' indicator displayed on type: 'search' text
105 * fields, hiding it when the field is already empty or when it's not
108 mw.widgets.SearchInputWidget.prototype.updateSearchIndicator = function () {
109 if ( this.getValue() === '' || this.isDisabled() || this.isReadOnly() ) {
110 this.setIndicator( null );
112 this.setIndicator( 'clear' );
119 mw.widgets.SearchInputWidget.prototype.onChange = function () {
120 this.updateSearchIndicator();
124 * Handle disable events.
126 * @param {boolean} disabled Element is disabled
129 mw.widgets.SearchInputWidget.prototype.onDisable = function () {
130 this.updateSearchIndicator();
136 mw.widgets.SearchInputWidget.prototype.setReadOnly = function ( state ) {
137 mw.widgets.SearchInputWidget.super.prototype.setReadOnly.call( this, state );
138 this.updateSearchIndicator();
145 mw.widgets.SearchInputWidget.prototype.getSuggestionsPromise = function () {
146 const api = this.getApi();
148 // While the name is, for historical reasons, 'session-start', this indicates
149 // a new backend request is being performed.
150 mw.track( 'mw.widgets.SearchInputWidget', {
151 action: 'session-start'
154 // reuse the searchSuggest function from mw.searchSuggest
155 const promise = mw.searchSuggest.request( api, this.getQueryValue(), () => {}, this.limit, this.getNamespace() );
158 promise.done( ( data, jqXHR ) => {
159 this.requestType = jqXHR.getResponseHeader( 'X-OpenSearch-Type' );
160 this.searchId = jqXHR.getResponseHeader( 'X-Search-ID' );
169 mw.widgets.SearchInputWidget.prototype.getLookupCacheDataFromResponse = function ( response ) {
170 // mw.widgets.TitleInputWidget uses response.query, which doesn't exist for opensearch,
171 // so return the whole response (titles only, and links)
173 data: response || {},
175 type: this.requestType || 'unknown',
176 searchId: this.searchId || null,
177 query: this.getQueryValue()
180 this.requestType = undefined;
181 this.searchId = undefined;
189 mw.widgets.SearchInputWidget.prototype.getOptionsFromData = function ( data ) {
191 titles = data.data[ 1 ],
192 descriptions = data.data[ 2 ],
193 urls = data.data[ 3 ];
195 // eslint-disable-next-line no-jquery/no-each-util
196 $.each( titles, ( i, result ) => {
197 items.push( new mw.widgets.TitleOptionWidget(
198 this.getOptionWidgetData(
200 // Create a result object that looks like the one from
201 // the parent's API query.
205 imageUrl: null, // The JSON 'opensearch' API doesn't have images
206 description: descriptions[ i ],
209 disambiguation: false
215 mw.track( 'mw.widgets.SearchInputWidget', {
216 action: 'impression-results',
217 numberOfResults: items.length,
218 resultSetType: data.metadata.type,
219 searchId: data.metadata.searchId,
220 query: data.metadata.query,
221 inputLocation: this.dataLocation || 'header'
230 mw.widgets.SearchInputWidget.prototype.onLookupMenuChoose = function () {
231 mw.widgets.SearchInputWidget.super.prototype.onLookupMenuChoose.apply( this, arguments );
233 if ( this.performSearchOnClick ) {
234 this.$element.closest( 'form' ).trigger( 'submit' );
241 mw.widgets.SearchInputWidget.prototype.getLookupMenuOptionsFromData = function () {
242 const items = mw.widgets.SearchInputWidget.super.prototype.getLookupMenuOptionsFromData.apply(
246 this.lastLookupItems = items.map( ( item ) => item.data );