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