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
) {
15 } ).done( function ( data
, jqXHR
) {
16 response( data
[ 1 ], {
17 type
: jqXHR
.getResponseHeader( 'X-OpenSearch-Type' ),
25 var api
, map
, 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();
39 // SimpleSearch is broken in Opera < 9.6
40 opera
: [ [ '>=', 9.6 ] ],
41 // Older Konquerors are unable to position the suggestions correctly (bug 50805)
42 konqueror
: [ [ '>=', '4.11' ] ],
45 // Support for iOS 6 or higher. It has not been tested on iOS 5 or lower
46 ipod
: [ [ '>=', 6 ] ],
47 iphone
: [ [ '>=', 6 ] ]
50 if ( !$.client
.test( map
) ) {
54 // Compute form data for search suggestions functionality.
55 function getFormData( context
) {
56 var $form
, baseHref
, linkParams
;
58 if ( !context
.formData
) {
59 // Compute common parameters for links' hrefs
60 $form
= context
.config
.$region
.closest( 'form' );
62 baseHref
= $form
.attr( 'action' );
63 baseHref
+= baseHref
.indexOf( '?' ) > -1 ? '&' : '?';
65 linkParams
= $form
.serializeObject();
68 textParam
: context
.data
.$textbox
.attr( 'name' ),
69 linkParams
: linkParams
,
74 return context
.formData
;
78 * Callback that's run when the user changes the search input text
79 * 'this' is the search input box (jQuery object)
83 function onBeforeUpdate() {
84 var searchText
= this.val();
86 if ( searchText
&& searchText
!== previousSearchText
) {
87 mw
.track( 'mediawiki.searchSuggest', {
88 action
: 'session-start'
91 previousSearchText
= searchText
;
95 * defines the location of autocomplete. Typically either
96 * header, which is in the top right of vector (for example)
97 * and content which identifies the main search bar on
98 * Special:Search. Defaults to header for skins that don't set
103 function getInputLocation( context
) {
104 return context
.config
.$region
106 .find( '[data-search-loc]' )
107 .data( 'search-loc' ) || 'header';
111 * Callback that's run when suggestions have been updated either from the cache or the API
112 * 'this' is the search input box (jQuery object)
116 function onAfterUpdate( metadata
) {
117 var context
= this.data( 'suggestionsContext' );
119 mw
.track( 'mediawiki.searchSuggest', {
120 action
: 'impression-results',
121 numberOfResults
: context
.config
.suggestions
.length
,
122 resultSetType
: metadata
.type
|| 'unknown',
123 query
: metadata
.query
,
124 inputLocation
: getInputLocation( context
)
128 // The function used to render the suggestions.
129 function renderFunction( text
, context
) {
130 var formData
= getFormData( context
);
132 // linkParams object is modified and reused
133 formData
.linkParams
[ formData
.textParam
] = text
;
135 // Allow trackers to attach tracking information, such
136 // as wprov, to clicked links.
137 mw
.track( 'mediawiki.searchSuggest', {
138 action
: 'render-one',
140 index
: context
.config
.suggestions
.indexOf( text
) + 1
143 // this is the container <div>, jQueryfied
147 .attr( 'href', formData
.baseHref
+ $.param( formData
.linkParams
) )
148 .attr( 'title', text
)
149 .addClass( 'mw-searchSuggest-link' )
153 // The function used when the user makes a selection
154 function selectFunction( $input
) {
155 var context
= $input
.data( 'suggestionsContext' ),
158 mw
.track( 'mediawiki.searchSuggest', {
159 action
: 'click-result',
160 numberOfResults
: context
.config
.suggestions
.length
,
161 clickIndex
: context
.config
.suggestions
.indexOf( text
) + 1
164 // allow the form to be submitted
168 function specialRenderFunction( query
, context
) {
170 formData
= getFormData( context
);
172 // linkParams object is modified and reused
173 formData
.linkParams
[ formData
.textParam
] = query
;
175 if ( $el
.children().length
=== 0 ) {
179 .addClass( 'special-label' )
180 .text( mw
.msg( 'searchsuggest-containing' ) ),
182 .addClass( 'special-query' )
187 $el
.find( '.special-query' )
191 if ( $el
.parent().hasClass( 'mw-searchSuggest-link' ) ) {
192 $el
.parent().attr( 'href', formData
.baseHref
+ $.param( formData
.linkParams
) + '&fulltext=1' );
196 .attr( 'href', formData
.baseHref
+ $.param( formData
.linkParams
) + '&fulltext=1' )
197 .addClass( 'mw-searchSuggest-link' )
202 // Generic suggestions functionality for all search boxes
203 searchboxesSelectors
= [
204 // Primary searchbox on every page in standard skins
209 // Generic selector for skins with multiple searchboxes (used by CologneBlue)
210 // and for MediaWiki itself (special pages with page title inputs)
213 $( searchboxesSelectors
.join( ', ' ) )
215 fetch: function ( query
, response
, maxRows
) {
216 var node
= this[ 0 ];
218 api
= api
|| new mw
.Api();
220 $.data( node
, 'request', mw
.searchSuggest
.request( api
, query
, response
, maxRows
) );
222 cancel: function () {
223 var node
= this[ 0 ],
224 request
= $.data( node
, 'request' );
228 $.removeData( node
, 'request' );
232 render
: renderFunction
,
233 select: function () {
234 // allow the form to be submitted
239 before
: onBeforeUpdate
,
245 .bind( 'paste cut drop', function () {
246 // make sure paste and cut events from the mouse and drag&drop events
247 // trigger the keypress handler and cause the suggestions to update
248 $( this ).trigger( 'keypress' );
250 // In most skins (at least Monobook and Vector), the font-size is messed up in <body>.
251 // (they use 2 elements to get a sane font-height). So, instead of making exceptions for
252 // each skin or adding more stylesheets, just copy it from the active element so auto-fit.
254 var $this = $( this );
256 .data( 'suggestions-context' )
258 .css( 'fontSize', $this.css( 'fontSize' ) );
261 // Ensure that the thing is actually present!
262 if ( $searchRegion
.length
=== 0 ) {
263 // Don't try to set anything up if simpleSearch is disabled sitewide.
264 // The loader code loads us if the option is present, even if we're
265 // not actually enabled (anymore).
269 // Special suggestions functionality and tracking for skin-provided search box
270 $searchInput
.suggestions( {
272 before
: onBeforeUpdate
,
276 render
: renderFunction
,
277 select
: selectFunction
280 render
: specialRenderFunction
,
281 select: function ( $input
) {
282 $input
.closest( 'form' )
283 .append( $( '<input type="hidden" name="fulltext" value="1"/>' ) );
284 return true; // allow the form to be submitted
287 $region
: $searchRegion
290 $searchInput
.closest( 'form' )
291 // track the form submit event
292 .on( 'submit', function () {
293 var context
= $searchInput
.data( 'suggestionsContext' );
294 mw
.track( 'mediawiki.searchSuggest', {
295 action
: 'submit-form',
296 numberOfResults
: context
.config
.suggestions
.length
,
297 $form
: context
.config
.$region
.closest( 'form' ),
298 inputLocation
: getInputLocation( context
)
301 // If the form includes any fallback fulltext search buttons, remove them
302 .find( '.mw-fallbackSearchButton' ).remove();
305 }( mediaWiki
, jQuery
) );