Merge "Convert Special:Search to OOUI"
[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 // Generic selector for skins with multiple searchboxes (used by CologneBlue)
162 // and for MediaWiki itself (special pages with page title inputs)
163 '.mw-searchInput'
164 ];
165 $( searchboxesSelectors.join( ', ' ) )
166 .suggestions( {
167 fetch: function ( query, response, maxRows ) {
168 var node = this[0];
169
170 api = api || new mw.Api();
171
172 $.data( node, 'request', api.get( {
173 action: 'opensearch',
174 search: query,
175 namespace: 0,
176 limit: maxRows,
177 suggest: ''
178 } ).done( function ( data ) {
179 response( data[ 1 ] );
180 } ) );
181 },
182 cancel: function () {
183 var node = this[0],
184 request = $.data( node, 'request' );
185
186 if ( request ) {
187 request.abort();
188 $.removeData( node, 'request' );
189 }
190 },
191 result: {
192 render: renderFunction,
193 select: function () {
194 // allow the form to be submitted
195 return true;
196 }
197 },
198 cache: true,
199 highlightInput: true
200 } )
201 .bind( 'paste cut drop', function () {
202 // make sure paste and cut events from the mouse and drag&drop events
203 // trigger the keypress handler and cause the suggestions to update
204 $( this ).trigger( 'keypress' );
205 } )
206 // In most skins (at least Monobook and Vector), the font-size is messed up in <body>.
207 // (they use 2 elements to get a sane font-height). So, instead of making exceptions for
208 // each skin or adding more stylesheets, just copy it from the active element so auto-fit.
209 .each( function () {
210 var $this = $( this );
211 $this
212 .data( 'suggestions-context' )
213 .data.$container
214 .css( 'fontSize', $this.css( 'fontSize' ) );
215 } );
216
217 // Ensure that the thing is actually present!
218 if ( $searchRegion.length === 0 ) {
219 // Don't try to set anything up if simpleSearch is disabled sitewide.
220 // The loader code loads us if the option is present, even if we're
221 // not actually enabled (anymore).
222 return;
223 }
224
225 // Special suggestions functionality and tracking for skin-provided search box
226 $searchInput.suggestions( {
227 update: {
228 before: onBeforeUpdate,
229 after: onAfterUpdate
230 },
231 result: {
232 render: renderFunction,
233 select: selectFunction
234 },
235 special: {
236 render: specialRenderFunction,
237 select: function ( $input ) {
238 $input.closest( 'form' )
239 .append( $( '<input type="hidden" name="fulltext" value="1"/>' ) );
240 return true; // allow the form to be submitted
241 }
242 },
243 $region: $searchRegion
244 } );
245
246 $searchInput.closest( 'form' )
247 // track the form submit event
248 .on( 'submit', function () {
249 var context = $searchInput.data( 'suggestionsContext' );
250 mw.track( 'mediawiki.searchSuggest', {
251 action: 'submit-form',
252 numberOfResults: context.config.suggestions.length
253 } );
254 } )
255 // If the form includes any fallback fulltext search buttons, remove them
256 .find( '.mw-fallbackSearchButton' ).remove();
257 } );
258
259 }( mediaWiki, jQuery ) );