Merge "Expose $wgLocalInterwikis via the API"
[lhc/web/wiklou.git] / resources / src / mediawiki / mediawiki.searchSuggest.js
1 /*!
2 * Add search suggestions to the search form.
3 */
4 ( function ( mw, $ ) {
5 $( function () {
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
17 // Compatibility map
18 map = {
19 // SimpleSearch is broken in Opera < 9.6
20 opera: [['>=', 9.6]],
21 // Older Konquerors are unable to position the suggestions correctly (bug 50805)
22 konqueror: [['>=', '4.11']],
23 docomo: false,
24 blackberry: false,
25 ipod: false,
26 iphone: false
27 };
28
29 if ( !$.client.test( map ) ) {
30 return;
31 }
32
33 // Compute form data for search suggestions functionality.
34 function computeResultRenderCache( context ) {
35 var $form, baseHref, linkParams;
36
37 // Compute common parameters for links' hrefs
38 $form = context.config.$region.closest( 'form' );
39
40 baseHref = $form.attr( 'action' );
41 baseHref += baseHref.indexOf( '?' ) > -1 ? '&' : '?';
42
43 linkParams = {};
44 $.each( $form.serializeArray(), function ( idx, obj ) {
45 linkParams[ obj.name ] = obj.value;
46 } );
47
48 return {
49 textParam: context.data.$textbox.attr( 'name' ),
50 linkParams: linkParams,
51 baseHref: baseHref
52 };
53 }
54
55 // The function used to render the suggestions.
56 function renderFunction( text, context ) {
57 if ( !resultRenderCache ) {
58 resultRenderCache = computeResultRenderCache( context );
59 }
60
61 // linkParams object is modified and reused
62 resultRenderCache.linkParams[ resultRenderCache.textParam ] = text;
63
64 // this is the container <div>, jQueryfied
65 this.text( text )
66 .wrap(
67 $( '<a>' )
68 .attr( 'href', resultRenderCache.baseHref + $.param( resultRenderCache.linkParams ) )
69 .attr( 'title', text )
70 .addClass( 'mw-searchSuggest-link' )
71 );
72 }
73
74 function specialRenderFunction( query, context ) {
75 var $el = this;
76
77 if ( !resultRenderCache ) {
78 resultRenderCache = computeResultRenderCache( context );
79 }
80
81 // linkParams object is modified and reused
82 resultRenderCache.linkParams[ resultRenderCache.textParam ] = query;
83
84 if ( $el.children().length === 0 ) {
85 $el
86 .append(
87 $( '<div>' )
88 .addClass( 'special-label' )
89 .text( mw.msg( 'searchsuggest-containing' ) ),
90 $( '<div>' )
91 .addClass( 'special-query' )
92 .text( query )
93 )
94 .show();
95 } else {
96 $el.find( '.special-query' )
97 .text( query );
98 }
99
100 if ( $el.parent().hasClass( 'mw-searchSuggest-link' ) ) {
101 $el.parent().attr( 'href', resultRenderCache.baseHref + $.param( resultRenderCache.linkParams ) + '&fulltext=1' );
102 } else {
103 $el.wrap(
104 $( '<a>' )
105 .attr( 'href', resultRenderCache.baseHref + $.param( resultRenderCache.linkParams ) + '&fulltext=1' )
106 .addClass( 'mw-searchSuggest-link' )
107 );
108 }
109 }
110
111 // Generic suggestions functionality for all search boxes
112 searchboxesSelectors = [
113 // Primary searchbox on every page in standard skins
114 '#searchInput',
115 // Special:Search
116 '#powerSearchText',
117 '#searchText',
118 // Generic selector for skins with multiple searchboxes (used by CologneBlue)
119 // and for MediaWiki itself (special pages with page title inputs)
120 '.mw-searchInput'
121 ];
122 $( searchboxesSelectors.join( ', ' ) )
123 .suggestions( {
124 fetch: function ( query, response ) {
125 var node = this[0];
126
127 api = api || new mw.Api();
128
129 $.data( node, 'request', api.get( {
130 action: 'opensearch',
131 search: query,
132 namespace: 0,
133 suggest: ''
134 } ).done( function ( data ) {
135 response( data[ 1 ] );
136 } ) );
137 },
138 cancel: function () {
139 var node = this[0],
140 request = $.data( node, 'request' );
141
142 if ( request ) {
143 request.abort();
144 $.removeData( node, 'request' );
145 }
146 },
147 result: {
148 render: renderFunction,
149 select: function () {
150 // allow the form to be submitted
151 return true;
152 }
153 },
154 cache: true,
155 highlightInput: true
156 } )
157 .bind( 'paste cut drop', function () {
158 // make sure paste and cut events from the mouse and drag&drop events
159 // trigger the keypress handler and cause the suggestions to update
160 $( this ).trigger( 'keypress' );
161 } )
162 // In most skins (at least Monobook and Vector), the font-size is messed up in <body>.
163 // (they use 2 elements to get a sane font-height). So, instead of making exceptions for
164 // each skin or adding more stylesheets, just copy it from the active element so auto-fit.
165 .each( function () {
166 var $this = $( this );
167 $this
168 .data( 'suggestions-context' )
169 .data.$container
170 .css( 'fontSize', $this.css( 'fontSize' ) );
171 } );
172
173 // Ensure that the thing is actually present!
174 if ( $searchRegion.length === 0 ) {
175 // Don't try to set anything up if simpleSearch is disabled sitewide.
176 // The loader code loads us if the option is present, even if we're
177 // not actually enabled (anymore).
178 return;
179 }
180
181 // Special suggestions functionality for skin-provided search box
182 $searchInput.suggestions( {
183 special: {
184 render: specialRenderFunction,
185 select: function ( $input ) {
186 $input.closest( 'form' )
187 .append( $( '<input type="hidden" name="fulltext" value="1"/>' ) );
188 return true; // allow the form to be submitted
189 }
190 },
191 $region: $searchRegion
192 } );
193
194 // If the form includes any fallback fulltext search buttons, remove them
195 $searchInput.closest( 'form' ).find( '.mw-fallbackSearchButton' ).remove();
196 } );
197
198 }( mediaWiki, jQuery ) );