2 * Add search suggestions to the search form.
6 request: function ( api
, query
, response
, maxRows
) {
13 } ).done( function ( data
) {
14 response( data
[ 1 ] );
20 var api
, map
, searchboxesSelectors
,
21 // Region where the suggestions box will appear directly below
22 // (using the same width). Can be a container element or the input
23 // itself, depending on what suits best in the environment.
24 // For Vector the suggestion box should align with the simpleSearch
25 // container's borders, in other skins it should align with the input
26 // element (not the search form, as that would leave the buttons
27 // vertically between the input and the suggestions).
28 $searchRegion
= $( '#simpleSearch, #searchInput' ).first(),
29 $searchInput
= $( '#searchInput' ),
30 previousSearchText
= $searchInput
.val();
34 // SimpleSearch is broken in Opera < 9.6
35 opera
: [ [ '>=', 9.6 ] ],
36 // Older Konquerors are unable to position the suggestions correctly (bug 50805)
37 konqueror
: [ [ '>=', '4.11' ] ],
40 // Support for iOS 6 or higher. It has not been tested on iOS 5 or lower
41 ipod
: [ [ '>=', 6 ] ],
42 iphone
: [ [ '>=', 6 ] ]
45 if ( !$.client
.test( map
) ) {
49 // Compute form data for search suggestions functionality.
50 function getFormData( context
) {
51 var $form
, baseHref
, linkParams
;
53 if ( !context
.formData
) {
54 // Compute common parameters for links' hrefs
55 $form
= context
.config
.$region
.closest( 'form' );
57 baseHref
= $form
.attr( 'action' );
58 baseHref
+= baseHref
.indexOf( '?' ) > -1 ? '&' : '?';
60 linkParams
= $form
.serializeObject();
63 textParam
: context
.data
.$textbox
.attr( 'name' ),
64 linkParams
: linkParams
,
69 return context
.formData
;
73 * Callback that's run when the user changes the search input text
74 * 'this' is the search input box (jQuery object)
78 function onBeforeUpdate() {
79 var searchText
= this.val();
81 if ( searchText
&& searchText
!== previousSearchText
) {
82 mw
.track( 'mediawiki.searchSuggest', {
83 action
: 'session-start'
86 previousSearchText
= searchText
;
90 * Callback that's run when suggestions have been updated either from the cache or the API
91 * 'this' is the search input box (jQuery object)
95 function onAfterUpdate() {
96 var context
= this.data( 'suggestionsContext' );
98 mw
.track( 'mediawiki.searchSuggest', {
99 action
: 'impression-results',
100 numberOfResults
: context
.config
.suggestions
.length
,
101 // FIXME: when other types of search become available change this value accordingly
102 // See the API call below (opensearch = prefix)
103 resultSetType
: 'prefix'
107 // The function used to render the suggestions.
108 function renderFunction( text
, context
) {
109 var formData
= getFormData( context
);
111 // linkParams object is modified and reused
112 formData
.linkParams
[ formData
.textParam
] = text
;
114 // this is the container <div>, jQueryfied
118 .attr( 'href', formData
.baseHref
+ $.param( formData
.linkParams
) )
119 .attr( 'title', text
)
120 .addClass( 'mw-searchSuggest-link' )
124 // The function used when the user makes a selection
125 function selectFunction( $input
) {
126 var context
= $input
.data( 'suggestionsContext' ),
129 mw
.track( 'mediawiki.searchSuggest', {
130 action
: 'click-result',
131 numberOfResults
: context
.config
.suggestions
.length
,
132 clickIndex
: context
.config
.suggestions
.indexOf( text
) + 1
135 // allow the form to be submitted
139 function specialRenderFunction( query
, context
) {
141 formData
= getFormData( context
);
143 // linkParams object is modified and reused
144 formData
.linkParams
[ formData
.textParam
] = query
;
146 if ( $el
.children().length
=== 0 ) {
150 .addClass( 'special-label' )
151 .text( mw
.msg( 'searchsuggest-containing' ) ),
153 .addClass( 'special-query' )
158 $el
.find( '.special-query' )
162 if ( $el
.parent().hasClass( 'mw-searchSuggest-link' ) ) {
163 $el
.parent().attr( 'href', formData
.baseHref
+ $.param( formData
.linkParams
) + '&fulltext=1' );
167 .attr( 'href', formData
.baseHref
+ $.param( formData
.linkParams
) + '&fulltext=1' )
168 .addClass( 'mw-searchSuggest-link' )
173 // Generic suggestions functionality for all search boxes
174 searchboxesSelectors
= [
175 // Primary searchbox on every page in standard skins
180 // Generic selector for skins with multiple searchboxes (used by CologneBlue)
181 // and for MediaWiki itself (special pages with page title inputs)
184 $( searchboxesSelectors
.join( ', ' ) )
186 fetch: function ( query
, response
, maxRows
) {
187 var node
= this[ 0 ];
189 api
= api
|| new mw
.Api();
191 $.data( node
, 'request', mw
.searchSuggest
.request( api
, query
, response
, maxRows
) );
193 cancel: function () {
194 var node
= this[ 0 ],
195 request
= $.data( node
, 'request' );
199 $.removeData( node
, 'request' );
203 render
: renderFunction
,
204 select: function () {
205 // allow the form to be submitted
212 .bind( 'paste cut drop', function () {
213 // make sure paste and cut events from the mouse and drag&drop events
214 // trigger the keypress handler and cause the suggestions to update
215 $( this ).trigger( 'keypress' );
217 // In most skins (at least Monobook and Vector), the font-size is messed up in <body>.
218 // (they use 2 elements to get a sane font-height). So, instead of making exceptions for
219 // each skin or adding more stylesheets, just copy it from the active element so auto-fit.
221 var $this = $( this );
223 .data( 'suggestions-context' )
225 .css( 'fontSize', $this.css( 'fontSize' ) );
228 // Ensure that the thing is actually present!
229 if ( $searchRegion
.length
=== 0 ) {
230 // Don't try to set anything up if simpleSearch is disabled sitewide.
231 // The loader code loads us if the option is present, even if we're
232 // not actually enabled (anymore).
236 // Special suggestions functionality and tracking for skin-provided search box
237 $searchInput
.suggestions( {
239 before
: onBeforeUpdate
,
243 render
: renderFunction
,
244 select
: selectFunction
247 render
: specialRenderFunction
,
248 select: function ( $input
) {
249 $input
.closest( 'form' )
250 .append( $( '<input type="hidden" name="fulltext" value="1"/>' ) );
251 return true; // allow the form to be submitted
254 $region
: $searchRegion
257 $searchInput
.closest( 'form' )
258 // track the form submit event
259 .on( 'submit', function () {
260 var context
= $searchInput
.data( 'suggestionsContext' );
261 mw
.track( 'mediawiki.searchSuggest', {
262 action
: 'submit-form',
263 numberOfResults
: context
.config
.suggestions
.length
266 // If the form includes any fallback fulltext search buttons, remove them
267 .find( '.mw-fallbackSearchButton' ).remove();
270 }( mediaWiki
, jQuery
) );