2 * Add search suggestions to the search form.
5 // eslint-disable-next-line no-jquery/no-map-util
6 var searchNS
= $.map( mw
.config
.get( 'wgFormattedNamespaces' ), function ( nsName
, nsID
) {
7 if ( nsID
>= 0 && mw
.user
.options
.get( 'searchNs' + nsID
) ) {
8 // Cast string key to number
13 // queries the wiki and calls response with the result
14 request: function ( api
, query
, response
, maxRows
, namespace ) {
19 namespace: namespace || searchNS
,
22 } ).done( function ( data
, jqXHR
) {
23 response( data
[ 1 ], {
24 type
: jqXHR
.getResponseHeader( 'X-OpenSearch-Type' ),
25 searchId
: jqXHR
.getResponseHeader( 'X-Search-ID' ),
33 var api
, searchboxesSelectors
,
34 // Region where the suggestions box will appear directly below
35 // (using the same width). Can be a container element or the input
36 // itself, depending on what suits best in the environment.
37 // For Vector the suggestion box should align with the simpleSearch
38 // container's borders, in other skins it should align with the input
39 // element (not the search form, as that would leave the buttons
40 // vertically between the input and the suggestions).
41 $searchRegion
= $( '#simpleSearch, #searchInput' ).first(),
42 $searchInput
= $( '#searchInput' ),
43 previousSearchText
= $searchInput
.val();
45 // Compute form data for search suggestions functionality.
46 function getFormData( context
) {
47 var $form
, baseHref
, linkParams
;
49 if ( !context
.formData
) {
50 // Compute common parameters for links' hrefs
51 $form
= context
.config
.$region
.closest( 'form' );
53 baseHref
= $form
.attr( 'action' );
54 baseHref
+= baseHref
.indexOf( '?' ) > -1 ? '&' : '?';
56 linkParams
= $form
.serializeObject();
59 textParam
: context
.data
.$textbox
.attr( 'name' ),
60 linkParams
: linkParams
,
65 return context
.formData
;
69 * Callback that's run when the user changes the search input text
70 * 'this' is the search input box (jQuery object)
74 function onBeforeUpdate() {
75 var searchText
= this.val();
77 if ( searchText
&& searchText
!== previousSearchText
) {
78 mw
.track( 'mediawiki.searchSuggest', {
79 action
: 'session-start'
82 previousSearchText
= searchText
;
86 * Defines the location of autocomplete. Typically either
87 * header, which is in the top right of vector (for example)
88 * and content which identifies the main search bar on
89 * Special:Search. Defaults to header for skins that don't set
93 * @param {Object} context
96 function getInputLocation( context
) {
97 return context
.config
.$region
99 .find( '[data-search-loc]' )
100 .data( 'search-loc' ) || 'header';
104 * Callback that's run when suggestions have been updated either from the cache or the API
105 * 'this' is the search input box (jQuery object)
108 * @param {Object} metadata
110 function onAfterUpdate( metadata
) {
111 var context
= this.data( 'suggestionsContext' );
113 mw
.track( 'mediawiki.searchSuggest', {
114 action
: 'impression-results',
115 numberOfResults
: context
.config
.suggestions
.length
,
116 resultSetType
: metadata
.type
|| 'unknown',
117 searchId
: metadata
.searchId
|| null,
118 query
: metadata
.query
,
119 inputLocation
: getInputLocation( context
)
123 // The function used to render the suggestions.
124 function renderFunction( text
, context
) {
125 var formData
= getFormData( context
),
126 textboxConfig
= context
.data
.$textbox
.data( 'mw-searchsuggest' ) || {};
128 // linkParams object is modified and reused
129 formData
.linkParams
[ formData
.textParam
] = text
;
131 // Allow trackers to attach tracking information, such
132 // as wprov, to clicked links.
133 mw
.track( 'mediawiki.searchSuggest', {
134 action
: 'render-one',
136 index
: context
.config
.suggestions
.indexOf( text
)
139 // this is the container <div>, jQueryfied
142 // wrap only as link, if the config doesn't disallow it
143 if ( textboxConfig
.wrapAsLink
!== false ) {
146 .attr( 'href', formData
.baseHref
+ $.param( formData
.linkParams
) )
147 .attr( 'title', text
)
148 .addClass( 'mw-searchSuggest-link' )
153 // The function used when the user makes a selection
154 function selectFunction( $input
, source
) {
155 var context
= $input
.data( 'suggestionsContext' ),
158 // Selecting via keyboard triggers a form submission. That will fire
159 // the submit-form event in addition to this click-result event.
160 if ( source
!== 'keyboard' ) {
161 mw
.track( 'mediawiki.searchSuggest', {
162 action
: 'click-result',
163 numberOfResults
: context
.config
.suggestions
.length
,
164 index
: context
.config
.suggestions
.indexOf( text
)
168 // allow the form to be submitted
172 function specialRenderFunction( query
, context
) {
174 formData
= getFormData( context
);
176 // linkParams object is modified and reused
177 formData
.linkParams
[ formData
.textParam
] = query
;
179 mw
.track( 'mediawiki.searchSuggest', {
180 action
: 'render-one',
182 index
: context
.config
.suggestions
.indexOf( query
)
185 if ( $el
.children().length
=== 0 ) {
189 .addClass( 'special-label' )
190 .text( mw
.msg( 'searchsuggest-containing' ) ),
192 .addClass( 'special-query' )
197 $el
.find( '.special-query' )
201 // eslint-disable-next-line no-jquery/no-class-state
202 if ( $el
.parent().hasClass( 'mw-searchSuggest-link' ) ) {
203 $el
.parent().attr( 'href', formData
.baseHref
+ $.param( formData
.linkParams
) + '&fulltext=1' );
207 .attr( 'href', formData
.baseHref
+ $.param( formData
.linkParams
) + '&fulltext=1' )
208 .addClass( 'mw-searchSuggest-link' )
213 // Generic suggestions functionality for all search boxes
214 searchboxesSelectors
= [
215 // Primary searchbox on every page in standard skins
217 // Generic selector for skins with multiple searchboxes (used by CologneBlue)
218 // and for MediaWiki itself (special pages with page title inputs)
221 $( searchboxesSelectors
.join( ', ' ) )
223 fetch: function ( query
, response
, maxRows
) {
224 var node
= this[ 0 ];
226 api
= api
|| new mw
.Api();
228 $.data( node
, 'request', mw
.searchSuggest
.request( api
, query
, response
, maxRows
) );
230 cancel: function () {
231 var node
= this[ 0 ],
232 request
= $.data( node
, 'request' );
236 $.removeData( node
, 'request' );
240 render
: renderFunction
,
241 select: function () {
242 // allow the form to be submitted
247 before
: onBeforeUpdate
,
253 .on( 'paste cut drop', function () {
254 // make sure paste and cut events from the mouse and drag&drop events
255 // trigger the keypress handler and cause the suggestions to update
256 $( this ).trigger( 'keypress' );
258 // In most skins (at least Monobook and Vector), the font-size is messed up in <body>.
259 // (they use 2 elements to get a sane font-height). So, instead of making exceptions for
260 // each skin or adding more stylesheets, just copy it from the active element so auto-fit.
262 var $this = $( this );
264 .data( 'suggestions-context' )
265 .data
.$container
.css( 'fontSize', $this.css( 'fontSize' ) );
268 // Ensure that the thing is actually present!
269 if ( $searchRegion
.length
=== 0 ) {
270 // Don't try to set anything up if simpleSearch is disabled sitewide.
271 // The loader code loads us if the option is present, even if we're
272 // not actually enabled (anymore).
276 // Special suggestions functionality and tracking for skin-provided search box
277 $searchInput
.suggestions( {
279 before
: onBeforeUpdate
,
283 render
: renderFunction
,
284 select
: selectFunction
287 render
: specialRenderFunction
,
288 select: function ( $input
, source
) {
289 var context
= $input
.data( 'suggestionsContext' ),
291 if ( source
=== 'mouse' ) {
292 // mouse click won't trigger form submission, so we need to send a click event
293 mw
.track( 'mediawiki.searchSuggest', {
294 action
: 'click-result',
295 numberOfResults
: context
.config
.suggestions
.length
,
296 index
: context
.config
.suggestions
.indexOf( text
)
299 $input
.closest( 'form' )
306 .attr( 'name', 'fulltext' )
309 return true; // allow the form to be submitted
312 $region
: $searchRegion
315 $searchInput
.closest( 'form' )
316 // track the form submit event
317 .on( 'submit', function () {
318 var context
= $searchInput
.data( 'suggestionsContext' );
319 mw
.track( 'mediawiki.searchSuggest', {
320 action
: 'submit-form',
321 numberOfResults
: context
.config
.suggestions
.length
,
322 $form
: context
.config
.$region
.closest( 'form' ),
323 inputLocation
: getInputLocation( context
),
324 index
: context
.config
.suggestions
.indexOf(
325 context
.data
.$textbox
.val()
329 // If the form includes any fallback fulltext search buttons, remove them
330 .find( '.mw-fallbackSearchButton' ).remove();