2 * Add search suggestions to the search form.
6 var api
, map
, resultRenderCache
, searchboxesSelectors
,
7 // Region where the suggestions box will appear directly below
8 // (using the same width). Can be a container element or the input
9 // itself, depending on what suits best in the environment.
10 // For Vector the suggestion box should align with the simpleSearch
11 // container's borders, in other skins it should align with the input
12 // element (not the search form, as that would leave the buttons
13 // vertically between the input and the suggestions).
14 $searchRegion
= $( '#simpleSearch, #searchInput' ).first(),
15 $searchInput
= $( '#searchInput' ),
16 previousSearchText
= $searchInput
.val();
20 // SimpleSearch is broken in Opera < 9.6
22 // Older Konquerors are unable to position the suggestions correctly (bug 50805)
23 konqueror
: [['>=', '4.11']],
26 // Support for iOS 6 or higher. It has not been tested on iOS 5 or lower
31 if ( !$.client
.test( map
) ) {
35 // Compute form data for search suggestions functionality.
36 function computeResultRenderCache( context
) {
37 var $form
, baseHref
, linkParams
;
39 // Compute common parameters for links' hrefs
40 $form
= context
.config
.$region
.closest( 'form' );
42 baseHref
= $form
.attr( 'action' );
43 baseHref
+= baseHref
.indexOf( '?' ) > -1 ? '&' : '?';
45 linkParams
= $form
.serializeObject();
48 textParam
: context
.data
.$textbox
.attr( 'name' ),
49 linkParams
: linkParams
,
55 * Callback that's run when the user changes the search input text
56 * 'this' is the search input box (jQuery object)
59 function onBeforeUpdate() {
60 var searchText
= this.val();
62 if ( searchText
&& searchText
!== previousSearchText
) {
63 mw
.track( 'mediawiki.searchSuggest', {
64 action
: 'session-start'
67 previousSearchText
= searchText
;
71 * Callback that's run when suggestions have been updated either from the cache or the API
72 * 'this' is the search input box (jQuery object)
75 function onAfterUpdate() {
76 var context
= this.data( 'suggestionsContext' );
78 mw
.track( 'mediawiki.searchSuggest', {
79 action
: 'impression-results',
80 numberOfResults
: context
.config
.suggestions
.length
,
81 // FIXME: when other types of search become available change this value accordingly
82 // See the API call below (opensearch = prefix)
83 resultSetType
: 'prefix'
87 // The function used to render the suggestions.
88 function renderFunction( text
, context
) {
89 if ( !resultRenderCache
) {
90 resultRenderCache
= computeResultRenderCache( context
);
93 // linkParams object is modified and reused
94 resultRenderCache
.linkParams
[ resultRenderCache
.textParam
] = text
;
96 // this is the container <div>, jQueryfied
100 .attr( 'href', resultRenderCache
.baseHref
+ $.param( resultRenderCache
.linkParams
) )
101 .attr( 'title', text
)
102 .addClass( 'mw-searchSuggest-link' )
106 // The function used when the user makes a selection
107 function selectFunction( $input
) {
108 var context
= $input
.data( 'suggestionsContext' ),
111 mw
.track( 'mediawiki.searchSuggest', {
112 action
: 'click-result',
113 numberOfResults
: context
.config
.suggestions
.length
,
114 clickIndex
: context
.config
.suggestions
.indexOf( text
) + 1
117 // allow the form to be submitted
121 function specialRenderFunction( query
, context
) {
124 if ( !resultRenderCache
) {
125 resultRenderCache
= computeResultRenderCache( context
);
128 // linkParams object is modified and reused
129 resultRenderCache
.linkParams
[ resultRenderCache
.textParam
] = query
;
131 if ( $el
.children().length
=== 0 ) {
135 .addClass( 'special-label' )
136 .text( mw
.msg( 'searchsuggest-containing' ) ),
138 .addClass( 'special-query' )
143 $el
.find( '.special-query' )
147 if ( $el
.parent().hasClass( 'mw-searchSuggest-link' ) ) {
148 $el
.parent().attr( 'href', resultRenderCache
.baseHref
+ $.param( resultRenderCache
.linkParams
) + '&fulltext=1' );
152 .attr( 'href', resultRenderCache
.baseHref
+ $.param( resultRenderCache
.linkParams
) + '&fulltext=1' )
153 .addClass( 'mw-searchSuggest-link' )
158 // Generic suggestions functionality for all search boxes
159 searchboxesSelectors
= [
160 // Primary searchbox on every page in standard skins
165 // Generic selector for skins with multiple searchboxes (used by CologneBlue)
166 // and for MediaWiki itself (special pages with page title inputs)
169 $( searchboxesSelectors
.join( ', ' ) )
171 fetch: function ( query
, response
, maxRows
) {
174 api
= api
|| new mw
.Api();
176 $.data( node
, 'request', api
.get( {
177 action
: 'opensearch',
182 } ).done( function ( data
) {
183 response( data
[ 1 ] );
186 cancel: function () {
188 request
= $.data( node
, 'request' );
192 $.removeData( node
, 'request' );
196 render
: renderFunction
,
197 select: function () {
198 // allow the form to be submitted
205 .bind( 'paste cut drop', function () {
206 // make sure paste and cut events from the mouse and drag&drop events
207 // trigger the keypress handler and cause the suggestions to update
208 $( this ).trigger( 'keypress' );
210 // In most skins (at least Monobook and Vector), the font-size is messed up in <body>.
211 // (they use 2 elements to get a sane font-height). So, instead of making exceptions for
212 // each skin or adding more stylesheets, just copy it from the active element so auto-fit.
214 var $this = $( this );
216 .data( 'suggestions-context' )
218 .css( 'fontSize', $this.css( 'fontSize' ) );
221 // Ensure that the thing is actually present!
222 if ( $searchRegion
.length
=== 0 ) {
223 // Don't try to set anything up if simpleSearch is disabled sitewide.
224 // The loader code loads us if the option is present, even if we're
225 // not actually enabled (anymore).
229 // Special suggestions functionality and tracking for skin-provided search box
230 $searchInput
.suggestions( {
232 before
: onBeforeUpdate
,
236 render
: renderFunction
,
237 select
: selectFunction
240 render
: specialRenderFunction
,
241 select: function ( $input
) {
242 $input
.closest( 'form' )
243 .append( $( '<input type="hidden" name="fulltext" value="1"/>' ) );
244 return true; // allow the form to be submitted
247 $region
: $searchRegion
250 // If the form includes any fallback fulltext search buttons, remove them
251 $searchInput
.closest( 'form' ).find( '.mw-fallbackSearchButton' ).remove();
254 }( mediaWiki
, jQuery
) );