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
) {
16 response( data
[ 1 ] );
19 // The name of the request api for event logging purposes
24 var api
, map
, searchboxesSelectors
,
25 // Region where the suggestions box will appear directly below
26 // (using the same width). Can be a container element or the input
27 // itself, depending on what suits best in the environment.
28 // For Vector the suggestion box should align with the simpleSearch
29 // container's borders, in other skins it should align with the input
30 // element (not the search form, as that would leave the buttons
31 // vertically between the input and the suggestions).
32 $searchRegion
= $( '#simpleSearch, #searchInput' ).first(),
33 $searchInput
= $( '#searchInput' ),
34 previousSearchText
= $searchInput
.val();
38 // SimpleSearch is broken in Opera < 9.6
39 opera
: [ [ '>=', 9.6 ] ],
40 // Older Konquerors are unable to position the suggestions correctly (bug 50805)
41 konqueror
: [ [ '>=', '4.11' ] ],
44 // Support for iOS 6 or higher. It has not been tested on iOS 5 or lower
45 ipod
: [ [ '>=', 6 ] ],
46 iphone
: [ [ '>=', 6 ] ]
49 if ( !$.client
.test( map
) ) {
53 // Compute form data for search suggestions functionality.
54 function getFormData( context
) {
55 var $form
, baseHref
, linkParams
;
57 if ( !context
.formData
) {
58 // Compute common parameters for links' hrefs
59 $form
= context
.config
.$region
.closest( 'form' );
61 baseHref
= $form
.attr( 'action' );
62 baseHref
+= baseHref
.indexOf( '?' ) > -1 ? '&' : '?';
64 linkParams
= $form
.serializeObject();
67 textParam
: context
.data
.$textbox
.attr( 'name' ),
68 linkParams
: linkParams
,
73 return context
.formData
;
77 * Callback that's run when the user changes the search input text
78 * 'this' is the search input box (jQuery object)
82 function onBeforeUpdate() {
83 var searchText
= this.val();
85 if ( searchText
&& searchText
!== previousSearchText
) {
86 mw
.track( 'mediawiki.searchSuggest', {
87 action
: 'session-start'
90 previousSearchText
= searchText
;
94 * Callback that's run when suggestions have been updated either from the cache or the API
95 * 'this' is the search input box (jQuery object)
99 function onAfterUpdate() {
100 var context
= this.data( 'suggestionsContext' );
102 mw
.track( 'mediawiki.searchSuggest', {
103 action
: 'impression-results',
104 numberOfResults
: context
.config
.suggestions
.length
,
105 resultSetType
: mw
.searchSuggest
.type
109 // The function used to render the suggestions.
110 function renderFunction( text
, context
) {
111 var formData
= getFormData( context
);
113 // linkParams object is modified and reused
114 formData
.linkParams
[ formData
.textParam
] = text
;
116 // this is the container <div>, jQueryfied
120 .attr( 'href', formData
.baseHref
+ $.param( formData
.linkParams
) )
121 .attr( 'title', text
)
122 .addClass( 'mw-searchSuggest-link' )
126 // The function used when the user makes a selection
127 function selectFunction( $input
) {
128 var context
= $input
.data( 'suggestionsContext' ),
131 mw
.track( 'mediawiki.searchSuggest', {
132 action
: 'click-result',
133 numberOfResults
: context
.config
.suggestions
.length
,
134 clickIndex
: context
.config
.suggestions
.indexOf( text
) + 1
137 // allow the form to be submitted
141 function specialRenderFunction( query
, context
) {
143 formData
= getFormData( context
);
145 // linkParams object is modified and reused
146 formData
.linkParams
[ formData
.textParam
] = query
;
148 if ( $el
.children().length
=== 0 ) {
152 .addClass( 'special-label' )
153 .text( mw
.msg( 'searchsuggest-containing' ) ),
155 .addClass( 'special-query' )
160 $el
.find( '.special-query' )
164 if ( $el
.parent().hasClass( 'mw-searchSuggest-link' ) ) {
165 $el
.parent().attr( 'href', formData
.baseHref
+ $.param( formData
.linkParams
) + '&fulltext=1' );
169 .attr( 'href', formData
.baseHref
+ $.param( formData
.linkParams
) + '&fulltext=1' )
170 .addClass( 'mw-searchSuggest-link' )
175 // Generic suggestions functionality for all search boxes
176 searchboxesSelectors
= [
177 // Primary searchbox on every page in standard skins
182 // Generic selector for skins with multiple searchboxes (used by CologneBlue)
183 // and for MediaWiki itself (special pages with page title inputs)
186 $( searchboxesSelectors
.join( ', ' ) )
188 fetch: function ( query
, response
, maxRows
) {
189 var node
= this[ 0 ];
191 api
= api
|| new mw
.Api();
193 $.data( node
, 'request', mw
.searchSuggest
.request( api
, query
, response
, maxRows
) );
195 cancel: function () {
196 var node
= this[ 0 ],
197 request
= $.data( node
, 'request' );
201 $.removeData( node
, 'request' );
205 render
: renderFunction
,
206 select: function () {
207 // allow the form to be submitted
214 .bind( 'paste cut drop', function () {
215 // make sure paste and cut events from the mouse and drag&drop events
216 // trigger the keypress handler and cause the suggestions to update
217 $( this ).trigger( 'keypress' );
219 // In most skins (at least Monobook and Vector), the font-size is messed up in <body>.
220 // (they use 2 elements to get a sane font-height). So, instead of making exceptions for
221 // each skin or adding more stylesheets, just copy it from the active element so auto-fit.
223 var $this = $( this );
225 .data( 'suggestions-context' )
227 .css( 'fontSize', $this.css( 'fontSize' ) );
230 // Ensure that the thing is actually present!
231 if ( $searchRegion
.length
=== 0 ) {
232 // Don't try to set anything up if simpleSearch is disabled sitewide.
233 // The loader code loads us if the option is present, even if we're
234 // not actually enabled (anymore).
238 // Special suggestions functionality and tracking for skin-provided search box
239 $searchInput
.suggestions( {
241 before
: onBeforeUpdate
,
245 render
: renderFunction
,
246 select
: selectFunction
249 render
: specialRenderFunction
,
250 select: function ( $input
) {
251 $input
.closest( 'form' )
252 .append( $( '<input type="hidden" name="fulltext" value="1"/>' ) );
253 return true; // allow the form to be submitted
256 $region
: $searchRegion
259 $searchInput
.closest( 'form' )
260 // track the form submit event
261 .on( 'submit', function () {
262 var context
= $searchInput
.data( 'suggestionsContext' );
263 mw
.track( 'mediawiki.searchSuggest', {
264 action
: 'submit-form',
265 numberOfResults
: context
.config
.suggestions
.length
268 // If the form includes any fallback fulltext search buttons, remove them
269 .find( '.mw-fallbackSearchButton' ).remove();
272 }( mediaWiki
, jQuery
) );