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