2 * Add search suggestions to the search form.
6 var 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' );
19 // SimpleSearch is broken in Opera < 9.6
21 // Older Konquerors are unable to position the suggestions correctly (bug 50805)
22 konqueror
: [['>=', '4.11']],
29 if ( !$.client
.test( map
) ) {
33 // Compute form data for search suggestions functionality.
34 function computeResultRenderCache( context
) {
35 var $form
, formAction
, baseHref
, linkParams
;
37 // Compute common parameters for links' hrefs
38 $form
= context
.config
.$region
.closest( 'form' );
40 formAction
= $form
.attr( 'action' );
41 baseHref
= formAction
+ ( formAction
.match( /\?/ ) ? '&' : '?' );
44 $.each( $form
.serializeArray(), function ( idx
, obj
) {
45 linkParams
[ obj
.name
] = obj
.value
;
49 textParam
: context
.data
.$textbox
.attr( 'name' ),
50 linkParams
: linkParams
,
55 // The function used to render the suggestions.
56 function renderFunction( text
, context
) {
57 if ( !resultRenderCache
) {
58 resultRenderCache
= computeResultRenderCache( context
);
61 // linkParams object is modified and reused
62 resultRenderCache
.linkParams
[ resultRenderCache
.textParam
] = text
;
64 // this is the container <div>, jQueryfied
67 // the <span> is needed for $.autoEllipsis to work
69 .css( 'whiteSpace', 'nowrap' )
74 .attr( 'href', resultRenderCache
.baseHref
+ $.param( resultRenderCache
.linkParams
) )
75 .addClass( 'mw-searchSuggest-link' )
79 function specialRenderFunction( query
, context
) {
82 if ( !resultRenderCache
) {
83 resultRenderCache
= computeResultRenderCache( context
);
86 // linkParams object is modified and reused
87 resultRenderCache
.linkParams
[ resultRenderCache
.textParam
] = query
;
89 if ( $el
.children().length
=== 0 ) {
93 .addClass( 'special-label' )
94 .text( mw
.msg( 'searchsuggest-containing' ) ),
96 .addClass( 'special-query' )
102 $el
.find( '.special-query' )
107 if ( $el
.parent().hasClass( 'mw-searchSuggest-link' ) ) {
108 $el
.parent().attr( 'href', resultRenderCache
.baseHref
+ $.param( resultRenderCache
.linkParams
) + '&fulltext=1' );
112 .attr( 'href', resultRenderCache
.baseHref
+ $.param( resultRenderCache
.linkParams
) + '&fulltext=1' )
113 .addClass( 'mw-searchSuggest-link' )
118 // General suggestions functionality for all search boxes
119 searchboxesSelectors
= [
120 // Primary searchbox on every page in standard skins
125 // Generic selector for skins with multiple searchboxes (used by CologneBlue)
128 $( searchboxesSelectors
.join( ', ' ) )
130 fetch: function ( query
) {
133 if ( query
.length
!== 0 ) {
135 $el
.data( 'request', ( new mw
.Api() ).get( {
136 action
: 'opensearch',
140 } ).done( function ( data
) {
141 $el
.suggestions( 'suggestions', data
[1] );
145 cancel: function () {
146 var apiPromise
= $( this ).data( 'request' );
147 // If the delay setting has caused the fetch to have not even happened
148 // yet, the apiPromise object will have never been set.
149 if ( apiPromise
&& $.isFunction( apiPromise
.abort
) ) {
151 $( this ).removeData( 'request' );
155 render
: renderFunction
,
156 select: function ( $input
) {
157 $input
.closest( 'form' ).submit();
163 .bind( 'paste cut drop', function () {
164 // make sure paste and cut events from the mouse and drag&drop events
165 // trigger the keypress handler and cause the suggestions to update
166 $( this ).trigger( 'keypress' );
169 // Ensure that the thing is actually present!
170 if ( $searchRegion
.length
=== 0 ) {
171 // Don't try to set anything up if simpleSearch is disabled sitewide.
172 // The loader code loads us if the option is present, even if we're
173 // not actually enabled (anymore).
177 // Special suggestions functionality for skin-provided search box
178 $searchInput
.suggestions( {
180 render
: renderFunction
,
181 select: function ( $input
) {
182 $input
.closest( 'form' ).submit();
186 render
: specialRenderFunction
,
187 select: function ( $input
) {
188 $input
.closest( 'form' ).append(
189 $( '<input type="hidden" name="fulltext" value="1"/>' )
191 $input
.closest( 'form' ).submit();
194 $region
: $searchRegion
197 // If the form includes any fallback fulltext search buttons, remove them
198 $searchInput
.closest( 'form' ).find( '.mw-fallbackSearchButton' ).remove();
200 // In most skins (at least Monobook and Vector), the font-size is messed up in <body>.
201 // (they use 2 elements to get a sane font-height). So, instead of making exceptions for
202 // each skin or adding more stylesheets, just copy it from the active element so auto-fit.
204 .data( 'suggestions-context' )
206 .css( 'fontSize', $searchInput
.css( 'fontSize' ) );
210 }( mediaWiki
, jQuery
) );