2 * Add search suggestions to the search form.
5 $( document
).ready( function ( $ ) {
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' );
20 // Left-to-right languages
22 // SimpleSearch is broken in Opera < 9.6
29 // Right-to-left languages
40 if ( !$.client
.test( map
) ) {
44 // Compute form data for search suggestions functionality.
45 function computeResultRenderCache( context
) {
46 var $form
, formAction
, baseHref
, linkParams
;
48 // Compute common parameters for links' hrefs
49 $form
= context
.config
.$region
.closest( 'form' );
51 formAction
= $form
.attr( 'action' );
52 baseHref
= formAction
+ ( formAction
.match(/\?/) ? '&' : '?' );
55 $.each( $form
.serializeArray(), function ( idx
, obj
) {
56 linkParams
[ obj
.name
] = obj
.value
;
60 textParam
: context
.data
.$textbox
.attr( 'name' ),
61 linkParams
: linkParams
,
66 // The function used to render the suggestions.
67 function renderFunction( text
, context
) {
68 if ( !resultRenderCache
) {
69 resultRenderCache
= computeResultRenderCache( context
);
72 // linkParams object is modified and reused
73 resultRenderCache
.linkParams
[ resultRenderCache
.textParam
] = text
;
75 // this is the container <div>, jQueryfied
78 // the <span> is needed for $.autoEllipsis to work
80 .css( 'whiteSpace', 'nowrap' )
85 .attr( 'href', resultRenderCache
.baseHref
+ $.param( resultRenderCache
.linkParams
) )
86 .addClass( 'mw-searchSuggest-link' )
90 function specialRenderFunction( query
, context
) {
93 if ( !resultRenderCache
) {
94 resultRenderCache
= computeResultRenderCache( context
);
97 // linkParams object is modified and reused
98 resultRenderCache
.linkParams
[ resultRenderCache
.textParam
] = query
;
100 if ( $el
.children().length
=== 0 ) {
104 .addClass( 'special-label' )
105 .text( mw
.msg( 'searchsuggest-containing' ) ),
107 .addClass( 'special-query' )
113 $el
.find( '.special-query' )
118 if ( $el
.parent().hasClass( 'mw-searchSuggest-link' ) ) {
119 $el
.parent().attr( 'href', resultRenderCache
.baseHref
+ $.param( resultRenderCache
.linkParams
) + '&fulltext=1' );
123 .attr( 'href', resultRenderCache
.baseHref
+ $.param( resultRenderCache
.linkParams
) + '&fulltext=1' )
124 .addClass( 'mw-searchSuggest-link' )
129 // General suggestions functionality for all search boxes
130 searchboxesSelectors
= [
131 // Primary searchbox on every page in standard skins
133 // Secondary searchbox in legacy skins (LegacyTemplate::searchForm uses id "searchInput + unique id")
138 // Generic selector for skins with multiple searchboxes (used by CologneBlue)
141 $( searchboxesSelectors
.join(', ') )
143 fetch: function ( query
) {
146 if ( query
.length
!== 0 ) {
149 url
: mw
.util
.wikiScript( 'api' ),
152 action
: 'opensearch',
158 success: function ( data
) {
159 if ( $.isArray( data
) && data
.length
) {
160 $el
.suggestions( 'suggestions', data
[1] );
164 $el
.data( 'request', jqXhr
);
167 cancel: function () {
168 var jqXhr
= $(this).data( 'request' );
169 // If the delay setting has caused the fetch to have not even happened
170 // yet, the jqXHR object will have never been set.
171 if ( jqXhr
&& $.isFunction( jqXhr
.abort
) ) {
173 $(this).removeData( 'request' );
177 render
: renderFunction
,
178 select: function ( $input
) {
179 $input
.closest( 'form' ).submit();
185 .bind( 'paste cut drop', function () {
186 // make sure paste and cut events from the mouse and drag&drop events
187 // trigger the keypress handler and cause the suggestions to update
188 $( this ).trigger( 'keypress' );
191 // Ensure that the thing is actually present!
192 if ( $searchRegion
.length
=== 0 ) {
193 // Don't try to set anything up if simpleSearch is disabled sitewide.
194 // The loader code loads us if the option is present, even if we're
195 // not actually enabled (anymore).
199 // Placeholder text for search box
201 .attr( 'placeholder', mw
.msg( 'searchsuggest-search' ) )
204 // Special suggestions functionality for skin-provided search box
205 $searchInput
.suggestions( {
207 render
: renderFunction
,
208 select: function ( $input
) {
209 $input
.closest( 'form' ).submit();
213 render
: specialRenderFunction
,
214 select: function ( $input
) {
215 $input
.closest( 'form' ).append(
216 $( '<input type="hidden" name="fulltext" value="1"/>' )
218 $input
.closest( 'form' ).submit();
221 $region
: $searchRegion
224 // In most skins (at least Monobook and Vector), the font-size is messed up in <body>.
225 // (they use 2 elements to get a sane font-height). So, instead of making exceptions for
226 // each skin or adding more stylesheets, just copy it from the active element so auto-fit.
228 .data( 'suggestions-context' )
230 .css( 'fontSize', $searchInput
.css( 'fontSize' ) );
234 }( mediaWiki
, jQuery
) );