2 * Add search suggestions to the search form.
6 // queries the wiki and calls response with the result
7 request: function ( api
, query
, response
, maxRows
, namespace ) {
12 namespace: namespace || 0,
15 } ).done( function ( data
, jqXHR
) {
16 response( data
[ 1 ], {
17 type
: jqXHR
.getResponseHeader( 'X-OpenSearch-Type' ),
25 var api
, searchboxesSelectors
,
26 // Region where the suggestions box will appear directly below
27 // (using the same width). Can be a container element or the input
28 // itself, depending on what suits best in the environment.
29 // For Vector the suggestion box should align with the simpleSearch
30 // container's borders, in other skins it should align with the input
31 // element (not the search form, as that would leave the buttons
32 // vertically between the input and the suggestions).
33 $searchRegion
= $( '#simpleSearch, #searchInput' ).first(),
34 $searchInput
= $( '#searchInput' ),
35 previousSearchText
= $searchInput
.val();
37 // Compute form data for search suggestions functionality.
38 function getFormData( context
) {
39 var $form
, baseHref
, linkParams
;
41 if ( !context
.formData
) {
42 // Compute common parameters for links' hrefs
43 $form
= context
.config
.$region
.closest( 'form' );
45 baseHref
= $form
.attr( 'action' );
46 baseHref
+= baseHref
.indexOf( '?' ) > -1 ? '&' : '?';
48 linkParams
= $form
.serializeObject();
51 textParam
: context
.data
.$textbox
.attr( 'name' ),
52 linkParams
: linkParams
,
57 return context
.formData
;
61 * Callback that's run when the user changes the search input text
62 * 'this' is the search input box (jQuery object)
66 function onBeforeUpdate() {
67 var searchText
= this.val();
69 if ( searchText
&& searchText
!== previousSearchText
) {
70 mw
.track( 'mediawiki.searchSuggest', {
71 action
: 'session-start'
74 previousSearchText
= searchText
;
78 * Defines the location of autocomplete. Typically either
79 * header, which is in the top right of vector (for example)
80 * and content which identifies the main search bar on
81 * Special:Search. Defaults to header for skins that don't set
85 * @param {Object} context
88 function getInputLocation( context
) {
89 return context
.config
.$region
91 .find( '[data-search-loc]' )
92 .data( 'search-loc' ) || 'header';
96 * Callback that's run when suggestions have been updated either from the cache or the API
97 * 'this' is the search input box (jQuery object)
100 * @param {Object} metadata
102 function onAfterUpdate( metadata
) {
103 var context
= this.data( 'suggestionsContext' );
105 mw
.track( 'mediawiki.searchSuggest', {
106 action
: 'impression-results',
107 numberOfResults
: context
.config
.suggestions
.length
,
108 resultSetType
: metadata
.type
|| 'unknown',
109 query
: metadata
.query
,
110 inputLocation
: getInputLocation( context
)
114 // The function used to render the suggestions.
115 function renderFunction( text
, context
) {
116 var formData
= getFormData( context
),
117 textboxConfig
= context
.data
.$textbox
.data( 'mw-searchsuggest' ) || {};
119 // linkParams object is modified and reused
120 formData
.linkParams
[ formData
.textParam
] = text
;
122 // Allow trackers to attach tracking information, such
123 // as wprov, to clicked links.
124 mw
.track( 'mediawiki.searchSuggest', {
125 action
: 'render-one',
127 index
: context
.config
.suggestions
.indexOf( text
)
130 // this is the container <div>, jQueryfied
133 // wrap only as link, if the config doesn't disallow it
134 if ( textboxConfig
.wrapAsLink
!== false ) {
137 .attr( 'href', formData
.baseHref
+ $.param( formData
.linkParams
) )
138 .attr( 'title', text
)
139 .addClass( 'mw-searchSuggest-link' )
144 // The function used when the user makes a selection
145 function selectFunction( $input
, source
) {
146 var context
= $input
.data( 'suggestionsContext' ),
149 // Selecting via keyboard triggers a form submission. That will fire
150 // the submit-form event in addition to this click-result event.
151 if ( source
!== 'keyboard' ) {
152 mw
.track( 'mediawiki.searchSuggest', {
153 action
: 'click-result',
154 numberOfResults
: context
.config
.suggestions
.length
,
155 index
: context
.config
.suggestions
.indexOf( text
)
159 // allow the form to be submitted
163 function specialRenderFunction( query
, context
) {
165 formData
= getFormData( context
);
167 // linkParams object is modified and reused
168 formData
.linkParams
[ formData
.textParam
] = query
;
170 mw
.track( 'mediawiki.searchSuggest', {
171 action
: 'render-one',
173 index
: context
.config
.suggestions
.indexOf( query
)
176 if ( $el
.children().length
=== 0 ) {
180 .addClass( 'special-label' )
181 .text( mw
.msg( 'searchsuggest-containing' ) ),
183 .addClass( 'special-query' )
188 $el
.find( '.special-query' )
192 if ( $el
.parent().hasClass( 'mw-searchSuggest-link' ) ) {
193 $el
.parent().attr( 'href', formData
.baseHref
+ $.param( formData
.linkParams
) + '&fulltext=1' );
197 .attr( 'href', formData
.baseHref
+ $.param( formData
.linkParams
) + '&fulltext=1' )
198 .addClass( 'mw-searchSuggest-link' )
203 // Generic suggestions functionality for all search boxes
204 searchboxesSelectors
= [
205 // Primary searchbox on every page in standard skins
207 // Generic selector for skins with multiple searchboxes (used by CologneBlue)
208 // and for MediaWiki itself (special pages with page title inputs)
211 $( searchboxesSelectors
.join( ', ' ) )
213 fetch: function ( query
, response
, maxRows
) {
214 var node
= this[ 0 ];
216 api
= api
|| new mw
.Api();
218 $.data( node
, 'request', mw
.searchSuggest
.request( api
, query
, response
, maxRows
) );
220 cancel: function () {
221 var node
= this[ 0 ],
222 request
= $.data( node
, 'request' );
226 $.removeData( node
, 'request' );
230 render
: renderFunction
,
231 select: function () {
232 // allow the form to be submitted
237 before
: onBeforeUpdate
,
243 .on( 'paste cut drop', function () {
244 // make sure paste and cut events from the mouse and drag&drop events
245 // trigger the keypress handler and cause the suggestions to update
246 $( this ).trigger( 'keypress' );
248 // In most skins (at least Monobook and Vector), the font-size is messed up in <body>.
249 // (they use 2 elements to get a sane font-height). So, instead of making exceptions for
250 // each skin or adding more stylesheets, just copy it from the active element so auto-fit.
252 var $this = $( this );
254 .data( 'suggestions-context' )
256 .css( 'fontSize', $this.css( 'fontSize' ) );
259 // Ensure that the thing is actually present!
260 if ( $searchRegion
.length
=== 0 ) {
261 // Don't try to set anything up if simpleSearch is disabled sitewide.
262 // The loader code loads us if the option is present, even if we're
263 // not actually enabled (anymore).
267 // Special suggestions functionality and tracking for skin-provided search box
268 $searchInput
.suggestions( {
270 before
: onBeforeUpdate
,
274 render
: renderFunction
,
275 select
: selectFunction
278 render
: specialRenderFunction
,
279 select: function ( $input
, source
) {
280 var context
= $input
.data( 'suggestionsContext' ),
282 if ( source
=== 'mouse' ) {
283 // mouse click won't trigger form submission, so we need to send a click event
284 mw
.track( 'mediawiki.searchSuggest', {
285 action
: 'click-result',
286 numberOfResults
: context
.config
.suggestions
.length
,
287 index
: context
.config
.suggestions
.indexOf( text
)
290 $input
.closest( 'form' )
291 .append( $( '<input type="hidden" name="fulltext" value="1"/>' ) );
293 return true; // allow the form to be submitted
296 $region
: $searchRegion
299 $searchInput
.closest( 'form' )
300 // track the form submit event
301 .on( 'submit', function () {
302 var context
= $searchInput
.data( 'suggestionsContext' );
303 mw
.track( 'mediawiki.searchSuggest', {
304 action
: 'submit-form',
305 numberOfResults
: context
.config
.suggestions
.length
,
306 $form
: context
.config
.$region
.closest( 'form' ),
307 inputLocation
: getInputLocation( context
),
308 index
: context
.config
.suggestions
.indexOf(
309 context
.data
.$textbox
.val()
313 // If the form includes any fallback fulltext search buttons, remove them
314 .find( '.mw-fallbackSearchButton' ).remove();
317 }( mediaWiki
, jQuery
) );