Merge "Drop zh-tw message "saveprefs""
[lhc/web/wiklou.git] / resources / src / mediawiki / mediawiki.searchSuggest.js
1 /*!
2 * Add search suggestions to the search form.
3 */
4 ( function ( mw, $ ) {
5 mw.searchSuggest = {
6 request: function ( api, query, response, maxRows ) {
7 return api.get( {
8 action: 'opensearch',
9 search: query,
10 namespace: 0,
11 limit: maxRows,
12 suggest: ''
13 } ).done( function ( data ) {
14 response( data[ 1 ] );
15 } );
16 }
17 };
18
19 $( function () {
20 var api, map, searchboxesSelectors,
21 // Region where the suggestions box will appear directly below
22 // (using the same width). Can be a container element or the input
23 // itself, depending on what suits best in the environment.
24 // For Vector the suggestion box should align with the simpleSearch
25 // container's borders, in other skins it should align with the input
26 // element (not the search form, as that would leave the buttons
27 // vertically between the input and the suggestions).
28 $searchRegion = $( '#simpleSearch, #searchInput' ).first(),
29 $searchInput = $( '#searchInput' ),
30 previousSearchText = $searchInput.val();
31
32 // Compatibility map
33 map = {
34 // SimpleSearch is broken in Opera < 9.6
35 opera: [ [ '>=', 9.6 ] ],
36 // Older Konquerors are unable to position the suggestions correctly (bug 50805)
37 konqueror: [ [ '>=', '4.11' ] ],
38 docomo: false,
39 blackberry: false,
40 // Support for iOS 6 or higher. It has not been tested on iOS 5 or lower
41 ipod: [ [ '>=', 6 ] ],
42 iphone: [ [ '>=', 6 ] ]
43 };
44
45 if ( !$.client.test( map ) ) {
46 return;
47 }
48
49 // Compute form data for search suggestions functionality.
50 function getFormData( context ) {
51 var $form, baseHref, linkParams;
52
53 if ( !context.formData ) {
54 // Compute common parameters for links' hrefs
55 $form = context.config.$region.closest( 'form' );
56
57 baseHref = $form.attr( 'action' );
58 baseHref += baseHref.indexOf( '?' ) > -1 ? '&' : '?';
59
60 linkParams = $form.serializeObject();
61
62 context.formData = {
63 textParam: context.data.$textbox.attr( 'name' ),
64 linkParams: linkParams,
65 baseHref: baseHref
66 };
67 }
68
69 return context.formData;
70 }
71
72 /**
73 * Callback that's run when the user changes the search input text
74 * 'this' is the search input box (jQuery object)
75 *
76 * @ignore
77 */
78 function onBeforeUpdate() {
79 var searchText = this.val();
80
81 if ( searchText && searchText !== previousSearchText ) {
82 mw.track( 'mediawiki.searchSuggest', {
83 action: 'session-start'
84 } );
85 }
86 previousSearchText = searchText;
87 }
88
89 /**
90 * Callback that's run when suggestions have been updated either from the cache or the API
91 * 'this' is the search input box (jQuery object)
92 *
93 * @ignore
94 */
95 function onAfterUpdate() {
96 var context = this.data( 'suggestionsContext' );
97
98 mw.track( 'mediawiki.searchSuggest', {
99 action: 'impression-results',
100 numberOfResults: context.config.suggestions.length,
101 // FIXME: when other types of search become available change this value accordingly
102 // See the API call below (opensearch = prefix)
103 resultSetType: 'prefix'
104 } );
105 }
106
107 // The function used to render the suggestions.
108 function renderFunction( text, context ) {
109 var formData = getFormData( context );
110
111 // linkParams object is modified and reused
112 formData.linkParams[ formData.textParam ] = text;
113
114 // this is the container <div>, jQueryfied
115 this.text( text )
116 .wrap(
117 $( '<a>' )
118 .attr( 'href', formData.baseHref + $.param( formData.linkParams ) )
119 .attr( 'title', text )
120 .addClass( 'mw-searchSuggest-link' )
121 );
122 }
123
124 // The function used when the user makes a selection
125 function selectFunction( $input ) {
126 var context = $input.data( 'suggestionsContext' ),
127 text = $input.val();
128
129 mw.track( 'mediawiki.searchSuggest', {
130 action: 'click-result',
131 numberOfResults: context.config.suggestions.length,
132 clickIndex: context.config.suggestions.indexOf( text ) + 1
133 } );
134
135 // allow the form to be submitted
136 return true;
137 }
138
139 function specialRenderFunction( query, context ) {
140 var $el = this,
141 formData = getFormData( context );
142
143 // linkParams object is modified and reused
144 formData.linkParams[ formData.textParam ] = query;
145
146 if ( $el.children().length === 0 ) {
147 $el
148 .append(
149 $( '<div>' )
150 .addClass( 'special-label' )
151 .text( mw.msg( 'searchsuggest-containing' ) ),
152 $( '<div>' )
153 .addClass( 'special-query' )
154 .text( query )
155 )
156 .show();
157 } else {
158 $el.find( '.special-query' )
159 .text( query );
160 }
161
162 if ( $el.parent().hasClass( 'mw-searchSuggest-link' ) ) {
163 $el.parent().attr( 'href', formData.baseHref + $.param( formData.linkParams ) + '&fulltext=1' );
164 } else {
165 $el.wrap(
166 $( '<a>' )
167 .attr( 'href', formData.baseHref + $.param( formData.linkParams ) + '&fulltext=1' )
168 .addClass( 'mw-searchSuggest-link' )
169 );
170 }
171 }
172
173 // Generic suggestions functionality for all search boxes
174 searchboxesSelectors = [
175 // Primary searchbox on every page in standard skins
176 '#searchInput',
177 // Special:Search
178 '#powerSearchText',
179 '#searchText',
180 // Generic selector for skins with multiple searchboxes (used by CologneBlue)
181 // and for MediaWiki itself (special pages with page title inputs)
182 '.mw-searchInput'
183 ];
184 $( searchboxesSelectors.join( ', ' ) )
185 .suggestions( {
186 fetch: function ( query, response, maxRows ) {
187 var node = this[ 0 ];
188
189 api = api || new mw.Api();
190
191 $.data( node, 'request', mw.searchSuggest.request( api, query, response, maxRows ) );
192 },
193 cancel: function () {
194 var node = this[ 0 ],
195 request = $.data( node, 'request' );
196
197 if ( request ) {
198 request.abort();
199 $.removeData( node, 'request' );
200 }
201 },
202 result: {
203 render: renderFunction,
204 select: function () {
205 // allow the form to be submitted
206 return true;
207 }
208 },
209 cache: true,
210 highlightInput: true
211 } )
212 .bind( 'paste cut drop', function () {
213 // make sure paste and cut events from the mouse and drag&drop events
214 // trigger the keypress handler and cause the suggestions to update
215 $( this ).trigger( 'keypress' );
216 } )
217 // In most skins (at least Monobook and Vector), the font-size is messed up in <body>.
218 // (they use 2 elements to get a sane font-height). So, instead of making exceptions for
219 // each skin or adding more stylesheets, just copy it from the active element so auto-fit.
220 .each( function () {
221 var $this = $( this );
222 $this
223 .data( 'suggestions-context' )
224 .data.$container
225 .css( 'fontSize', $this.css( 'fontSize' ) );
226 } );
227
228 // Ensure that the thing is actually present!
229 if ( $searchRegion.length === 0 ) {
230 // Don't try to set anything up if simpleSearch is disabled sitewide.
231 // The loader code loads us if the option is present, even if we're
232 // not actually enabled (anymore).
233 return;
234 }
235
236 // Special suggestions functionality and tracking for skin-provided search box
237 $searchInput.suggestions( {
238 update: {
239 before: onBeforeUpdate,
240 after: onAfterUpdate
241 },
242 result: {
243 render: renderFunction,
244 select: selectFunction
245 },
246 special: {
247 render: specialRenderFunction,
248 select: function ( $input ) {
249 $input.closest( 'form' )
250 .append( $( '<input type="hidden" name="fulltext" value="1"/>' ) );
251 return true; // allow the form to be submitted
252 }
253 },
254 $region: $searchRegion
255 } );
256
257 $searchInput.closest( 'form' )
258 // track the form submit event
259 .on( 'submit', function () {
260 var context = $searchInput.data( 'suggestionsContext' );
261 mw.track( 'mediawiki.searchSuggest', {
262 action: 'submit-form',
263 numberOfResults: context.config.suggestions.length
264 } );
265 } )
266 // If the form includes any fallback fulltext search buttons, remove them
267 .find( '.mw-fallbackSearchButton' ).remove();
268 } );
269
270 }( mediaWiki, jQuery ) );