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
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
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)
77 function onBeforeUpdate() {
78 var searchText
= this.val();
80 if ( searchText
&& searchText
!== previousSearchText
) {
81 mw
.track( 'mediawiki.searchSuggest', {
82 action
: 'session-start'
85 previousSearchText
= searchText
;
89 * Callback that's run when suggestions have been updated either from the cache or the API
90 * 'this' is the search input box (jQuery object)
93 function onAfterUpdate() {
94 var context
= this.data( 'suggestionsContext' );
96 mw
.track( 'mediawiki.searchSuggest', {
97 action
: 'impression-results',
98 numberOfResults
: context
.config
.suggestions
.length
,
99 // FIXME: when other types of search become available change this value accordingly
100 // See the API call below (opensearch = prefix)
101 resultSetType
: 'prefix'
105 // The function used to render the suggestions.
106 function renderFunction( text
, context
) {
107 var formData
= getFormData( context
);
109 // linkParams object is modified and reused
110 formData
.linkParams
[ formData
.textParam
] = text
;
112 // this is the container <div>, jQueryfied
116 .attr( 'href', formData
.baseHref
+ $.param( formData
.linkParams
) )
117 .attr( 'title', text
)
118 .addClass( 'mw-searchSuggest-link' )
122 // The function used when the user makes a selection
123 function selectFunction( $input
) {
124 var context
= $input
.data( 'suggestionsContext' ),
127 mw
.track( 'mediawiki.searchSuggest', {
128 action
: 'click-result',
129 numberOfResults
: context
.config
.suggestions
.length
,
130 clickIndex
: context
.config
.suggestions
.indexOf( text
) + 1
133 // allow the form to be submitted
137 function specialRenderFunction( query
, context
) {
139 formData
= getFormData( context
);
141 // linkParams object is modified and reused
142 formData
.linkParams
[ formData
.textParam
] = query
;
144 if ( $el
.children().length
=== 0 ) {
148 .addClass( 'special-label' )
149 .text( mw
.msg( 'searchsuggest-containing' ) ),
151 .addClass( 'special-query' )
156 $el
.find( '.special-query' )
160 if ( $el
.parent().hasClass( 'mw-searchSuggest-link' ) ) {
161 $el
.parent().attr( 'href', formData
.baseHref
+ $.param( formData
.linkParams
) + '&fulltext=1' );
165 .attr( 'href', formData
.baseHref
+ $.param( formData
.linkParams
) + '&fulltext=1' )
166 .addClass( 'mw-searchSuggest-link' )
171 // Generic suggestions functionality for all search boxes
172 searchboxesSelectors
= [
173 // Primary searchbox on every page in standard skins
178 // Generic selector for skins with multiple searchboxes (used by CologneBlue)
179 // and for MediaWiki itself (special pages with page title inputs)
182 $( searchboxesSelectors
.join( ', ' ) )
184 fetch: function ( query
, response
, maxRows
) {
187 api
= api
|| new mw
.Api();
189 $.data( node
, 'request', mw
.searchSuggest
.request( api
, query
, response
, maxRows
) );
191 cancel: function () {
193 request
= $.data( node
, 'request' );
197 $.removeData( node
, 'request' );
201 render
: renderFunction
,
202 select: function () {
203 // allow the form to be submitted
210 .bind( 'paste cut drop', function () {
211 // make sure paste and cut events from the mouse and drag&drop events
212 // trigger the keypress handler and cause the suggestions to update
213 $( this ).trigger( 'keypress' );
215 // In most skins (at least Monobook and Vector), the font-size is messed up in <body>.
216 // (they use 2 elements to get a sane font-height). So, instead of making exceptions for
217 // each skin or adding more stylesheets, just copy it from the active element so auto-fit.
219 var $this = $( this );
221 .data( 'suggestions-context' )
223 .css( 'fontSize', $this.css( 'fontSize' ) );
226 // Ensure that the thing is actually present!
227 if ( $searchRegion
.length
=== 0 ) {
228 // Don't try to set anything up if simpleSearch is disabled sitewide.
229 // The loader code loads us if the option is present, even if we're
230 // not actually enabled (anymore).
234 // Special suggestions functionality and tracking for skin-provided search box
235 $searchInput
.suggestions( {
237 before
: onBeforeUpdate
,
241 render
: renderFunction
,
242 select
: selectFunction
245 render
: specialRenderFunction
,
246 select: function ( $input
) {
247 $input
.closest( 'form' )
248 .append( $( '<input type="hidden" name="fulltext" value="1"/>' ) );
249 return true; // allow the form to be submitted
252 $region
: $searchRegion
255 $searchInput
.closest( 'form' )
256 // track the form submit event
257 .on( 'submit', function () {
258 var context
= $searchInput
.data( 'suggestionsContext' );
259 mw
.track( 'mediawiki.searchSuggest', {
260 action
: 'submit-form',
261 numberOfResults
: context
.config
.suggestions
.length
264 // If the form includes any fallback fulltext search buttons, remove them
265 .find( '.mw-fallbackSearchButton' ).remove();
268 }( mediaWiki
, jQuery
) );