1 /* eslint-disable no-restricted-properties */
3 * Add search suggestions to the search form.
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' ),
32 var api
, searchboxesSelectors
,
33 // Region where the suggestions box will appear directly below
34 // (using the same width). Can be a container element or the input
35 // itself, depending on what suits best in the environment.
36 // For Vector the suggestion box should align with the simpleSearch
37 // container's borders, in other skins it should align with the input
38 // element (not the search form, as that would leave the buttons
39 // vertically between the input and the suggestions).
40 $searchRegion
= $( '#simpleSearch, #searchInput' ).first(),
41 $searchInput
= $( '#searchInput' ),
42 previousSearchText
= $searchInput
.val();
44 // Compute form data for search suggestions functionality.
45 function getFormData( context
) {
46 var $form
, baseHref
, linkParams
;
48 if ( !context
.formData
) {
49 // Compute common parameters for links' hrefs
50 $form
= context
.config
.$region
.closest( 'form' );
52 baseHref
= $form
.attr( 'action' );
53 baseHref
+= baseHref
.indexOf( '?' ) > -1 ? '&' : '?';
55 linkParams
= $form
.serializeObject();
58 textParam
: context
.data
.$textbox
.attr( 'name' ),
59 linkParams
: linkParams
,
64 return context
.formData
;
68 * Callback that's run when the user changes the search input text
69 * 'this' is the search input box (jQuery object)
73 function onBeforeUpdate() {
74 var searchText
= this.val();
76 if ( searchText
&& searchText
!== previousSearchText
) {
77 mw
.track( 'mediawiki.searchSuggest', {
78 action
: 'session-start'
81 previousSearchText
= searchText
;
85 * Defines the location of autocomplete. Typically either
86 * header, which is in the top right of vector (for example)
87 * and content which identifies the main search bar on
88 * Special:Search. Defaults to header for skins that don't set
92 * @param {Object} context
95 function getInputLocation( context
) {
96 return context
.config
.$region
98 .find( '[data-search-loc]' )
99 .data( 'search-loc' ) || 'header';
103 * Callback that's run when suggestions have been updated either from the cache or the API
104 * 'this' is the search input box (jQuery object)
107 * @param {Object} metadata
109 function onAfterUpdate( metadata
) {
110 var context
= this.data( 'suggestionsContext' );
112 mw
.track( 'mediawiki.searchSuggest', {
113 action
: 'impression-results',
114 numberOfResults
: context
.config
.suggestions
.length
,
115 resultSetType
: metadata
.type
|| 'unknown',
116 query
: metadata
.query
,
117 inputLocation
: getInputLocation( context
)
121 // The function used to render the suggestions.
122 function renderFunction( text
, context
) {
123 var formData
= getFormData( context
),
124 textboxConfig
= context
.data
.$textbox
.data( 'mw-searchsuggest' ) || {};
126 // linkParams object is modified and reused
127 formData
.linkParams
[ formData
.textParam
] = text
;
129 // Allow trackers to attach tracking information, such
130 // as wprov, to clicked links.
131 mw
.track( 'mediawiki.searchSuggest', {
132 action
: 'render-one',
134 index
: context
.config
.suggestions
.indexOf( text
)
137 // this is the container <div>, jQueryfied
140 // wrap only as link, if the config doesn't disallow it
141 if ( textboxConfig
.wrapAsLink
!== false ) {
144 .attr( 'href', formData
.baseHref
+ $.param( formData
.linkParams
) )
145 .attr( 'title', text
)
146 .addClass( 'mw-searchSuggest-link' )
151 // The function used when the user makes a selection
152 function selectFunction( $input
, source
) {
153 var context
= $input
.data( 'suggestionsContext' ),
156 // Selecting via keyboard triggers a form submission. That will fire
157 // the submit-form event in addition to this click-result event.
158 if ( source
!== 'keyboard' ) {
159 mw
.track( 'mediawiki.searchSuggest', {
160 action
: 'click-result',
161 numberOfResults
: context
.config
.suggestions
.length
,
162 index
: context
.config
.suggestions
.indexOf( text
)
166 // allow the form to be submitted
170 function specialRenderFunction( query
, context
) {
172 formData
= getFormData( context
);
174 // linkParams object is modified and reused
175 formData
.linkParams
[ formData
.textParam
] = query
;
177 mw
.track( 'mediawiki.searchSuggest', {
178 action
: 'render-one',
180 index
: context
.config
.suggestions
.indexOf( query
)
183 if ( $el
.children().length
=== 0 ) {
187 .addClass( 'special-label' )
188 .text( mw
.msg( 'searchsuggest-containing' ) ),
190 .addClass( 'special-query' )
195 $el
.find( '.special-query' )
199 if ( $el
.parent().hasClass( 'mw-searchSuggest-link' ) ) {
200 $el
.parent().attr( 'href', formData
.baseHref
+ $.param( formData
.linkParams
) + '&fulltext=1' );
204 .attr( 'href', formData
.baseHref
+ $.param( formData
.linkParams
) + '&fulltext=1' )
205 .addClass( 'mw-searchSuggest-link' )
210 // Generic suggestions functionality for all search boxes
211 searchboxesSelectors
= [
212 // Primary searchbox on every page in standard skins
214 // Generic selector for skins with multiple searchboxes (used by CologneBlue)
215 // and for MediaWiki itself (special pages with page title inputs)
218 $( searchboxesSelectors
.join( ', ' ) )
220 fetch: function ( query
, response
, maxRows
) {
221 var node
= this[ 0 ];
223 api
= api
|| new mw
.Api();
225 $.data( node
, 'request', mw
.searchSuggest
.request( api
, query
, response
, maxRows
) );
227 cancel: function () {
228 var node
= this[ 0 ],
229 request
= $.data( node
, 'request' );
233 $.removeData( node
, 'request' );
237 render
: renderFunction
,
238 select: function () {
239 // allow the form to be submitted
244 before
: onBeforeUpdate
,
250 .on( 'paste cut drop', function () {
251 // make sure paste and cut events from the mouse and drag&drop events
252 // trigger the keypress handler and cause the suggestions to update
253 $( this ).trigger( 'keypress' );
255 // In most skins (at least Monobook and Vector), the font-size is messed up in <body>.
256 // (they use 2 elements to get a sane font-height). So, instead of making exceptions for
257 // each skin or adding more stylesheets, just copy it from the active element so auto-fit.
259 var $this = $( this );
261 .data( 'suggestions-context' )
262 .data
.$container
.css( 'fontSize', $this.css( 'fontSize' ) );
265 // Ensure that the thing is actually present!
266 if ( $searchRegion
.length
=== 0 ) {
267 // Don't try to set anything up if simpleSearch is disabled sitewide.
268 // The loader code loads us if the option is present, even if we're
269 // not actually enabled (anymore).
273 // Special suggestions functionality and tracking for skin-provided search box
274 $searchInput
.suggestions( {
276 before
: onBeforeUpdate
,
280 render
: renderFunction
,
281 select
: selectFunction
284 render
: specialRenderFunction
,
285 select: function ( $input
, source
) {
286 var context
= $input
.data( 'suggestionsContext' ),
288 if ( source
=== 'mouse' ) {
289 // mouse click won't trigger form submission, so we need to send a click event
290 mw
.track( 'mediawiki.searchSuggest', {
291 action
: 'click-result',
292 numberOfResults
: context
.config
.suggestions
.length
,
293 index
: context
.config
.suggestions
.indexOf( text
)
296 $input
.closest( 'form' )
297 .append( $( '<input type="hidden" name="fulltext" value="1"/>' ) );
299 return true; // allow the form to be submitted
302 $region
: $searchRegion
305 $searchInput
.closest( 'form' )
306 // track the form submit event
307 .on( 'submit', function () {
308 var context
= $searchInput
.data( 'suggestionsContext' );
309 mw
.track( 'mediawiki.searchSuggest', {
310 action
: 'submit-form',
311 numberOfResults
: context
.config
.suggestions
.length
,
312 $form
: context
.config
.$region
.closest( 'form' ),
313 inputLocation
: getInputLocation( context
),
314 index
: context
.config
.suggestions
.indexOf(
315 context
.data
.$textbox
.val()
319 // If the form includes any fallback fulltext search buttons, remove them
320 .find( '.mw-fallbackSearchButton' ).remove();