build: Enable jscs rules 'requireSpacesInside*Brackets' and make pass
[lhc/web/wiklou.git] / resources / src / mediawiki / mediawiki.searchSuggest.js
index f981b90..3c4f32e 100644 (file)
@@ -2,8 +2,22 @@
  * Add search suggestions to the search form.
  */
 ( function ( mw, $ ) {
+       mw.searchSuggest = {
+               request: function ( api, query, response, maxRows ) {
+                       return api.get( {
+                               action: 'opensearch',
+                               search: query,
+                               namespace: 0,
+                               limit: maxRows,
+                               suggest: ''
+                       } ).done( function ( data ) {
+                               response( data[ 1 ] );
+                       } );
+               }
+       };
+
        $( function () {
-               var api, map, resultRenderCache, searchboxesSelectors,
+               var api, map, searchboxesSelectors,
                        // Region where the suggestions box will appear directly below
                        // (using the same width). Can be a container element or the input
                        // itself, depending on what suits best in the environment.
                // Compatibility map
                map = {
                        // SimpleSearch is broken in Opera < 9.6
-                       opera: [['>=', 9.6]],
+                       opera: [ [ '>=', 9.6 ] ],
                        // Older Konquerors are unable to position the suggestions correctly (bug 50805)
-                       konqueror: [['>=', '4.11']],
+                       konqueror: [ [ '>=', '4.11' ] ],
                        docomo: false,
                        blackberry: false,
                        // Support for iOS 6 or higher. It has not been tested on iOS 5 or lower
-                       ipod: [['>=', 6]],
-                       iphone: [['>=', 6]]
+                       ipod: [ [ '>=', 6 ] ],
+                       iphone: [ [ '>=', 6 ] ]
                };
 
                if ( !$.client.test( map ) ) {
                }
 
                // Compute form data for search suggestions functionality.
-               function computeResultRenderCache( context ) {
+               function getFormData( context ) {
                        var $form, baseHref, linkParams;
 
-                       // Compute common parameters for links' hrefs
-                       $form = context.config.$region.closest( 'form' );
+                       if ( !context.formData ) {
+                               // Compute common parameters for links' hrefs
+                               $form = context.config.$region.closest( 'form' );
 
-                       baseHref = $form.attr( 'action' );
-                       baseHref += baseHref.indexOf( '?' ) > -1 ? '&' : '?';
+                               baseHref = $form.attr( 'action' );
+                               baseHref += baseHref.indexOf( '?' ) > -1 ? '&' : '?';
 
-                       linkParams = $form.serializeObject();
+                               linkParams = $form.serializeObject();
 
-                       return {
-                               textParam: context.data.$textbox.attr( 'name' ),
-                               linkParams: linkParams,
-                               baseHref: baseHref
-                       };
+                               context.formData = {
+                                       textParam: context.data.$textbox.attr( 'name' ),
+                                       linkParams: linkParams,
+                                       baseHref: baseHref
+                               };
+                       }
+
+                       return context.formData;
                }
 
                /**
 
                // The function used to render the suggestions.
                function renderFunction( text, context ) {
-                       if ( !resultRenderCache ) {
-                               resultRenderCache = computeResultRenderCache( context );
-                       }
+                       var formData = getFormData( context );
 
                        // linkParams object is modified and reused
-                       resultRenderCache.linkParams[ resultRenderCache.textParam ] = text;
+                       formData.linkParams[ formData.textParam ] = text;
 
                        // this is the container <div>, jQueryfied
                        this.text( text )
                                .wrap(
                                        $( '<a>' )
-                                               .attr( 'href', resultRenderCache.baseHref + $.param( resultRenderCache.linkParams ) )
+                                               .attr( 'href', formData.baseHref + $.param( formData.linkParams ) )
                                                .attr( 'title', text )
                                                .addClass( 'mw-searchSuggest-link' )
                                );
                }
 
                function specialRenderFunction( query, context ) {
-                       var $el = this;
-
-                       if ( !resultRenderCache ) {
-                               resultRenderCache = computeResultRenderCache( context );
-                       }
+                       var $el = this,
+                               formData = getFormData( context );
 
                        // linkParams object is modified and reused
-                       resultRenderCache.linkParams[ resultRenderCache.textParam ] = query;
+                       formData.linkParams[ formData.textParam ] = query;
 
                        if ( $el.children().length === 0 ) {
                                $el
                        }
 
                        if ( $el.parent().hasClass( 'mw-searchSuggest-link' ) ) {
-                               $el.parent().attr( 'href', resultRenderCache.baseHref + $.param( resultRenderCache.linkParams ) + '&fulltext=1' );
+                               $el.parent().attr( 'href', formData.baseHref + $.param( formData.linkParams ) + '&fulltext=1' );
                        } else {
                                $el.wrap(
                                        $( '<a>' )
-                                               .attr( 'href', resultRenderCache.baseHref + $.param( resultRenderCache.linkParams ) + '&fulltext=1' )
+                                               .attr( 'href', formData.baseHref + $.param( formData.linkParams ) + '&fulltext=1' )
                                                .addClass( 'mw-searchSuggest-link' )
                                );
                        }
                $( searchboxesSelectors.join( ', ' ) )
                        .suggestions( {
                                fetch: function ( query, response, maxRows ) {
-                                       var node = this[0];
+                                       var node = this[ 0 ];
 
                                        api = api || new mw.Api();
 
-                                       $.data( node, 'request', api.get( {
-                                               action: 'opensearch',
-                                               search: query,
-                                               namespace: 0,
-                                               limit: maxRows,
-                                               suggest: ''
-                                       } ).done( function ( data ) {
-                                               response( data[ 1 ] );
-                                       } ) );
+                                       $.data( node, 'request', mw.searchSuggest.request( api, query, response, maxRows ) );
                                },
                                cancel: function () {
-                                       var node = this[0],
+                                       var node = this[ 0 ],
                                                request = $.data( node, 'request' );
 
                                        if ( request ) {
                        $region: $searchRegion
                } );
 
-               // If the form includes any fallback fulltext search buttons, remove them
-               $searchInput.closest( 'form' ).find( '.mw-fallbackSearchButton' ).remove();
+               $searchInput.closest( 'form' )
+                       // track the form submit event
+                       .on( 'submit', function () {
+                               var context = $searchInput.data( 'suggestionsContext' );
+                               mw.track( 'mediawiki.searchSuggest', {
+                                       action: 'submit-form',
+                                       numberOfResults: context.config.suggestions.length
+                               } );
+                       } )
+                       // If the form includes any fallback fulltext search buttons, remove them
+                       .find( '.mw-fallbackSearchButton' ).remove();
        } );
 
 }( mediaWiki, jQuery ) );