SearchInputWidget: Support searching a namespaces besides 0
[lhc/web/wiklou.git] / resources / src / mediawiki / mediawiki.searchSuggest.js
index 2d603bf..ce5f66a 100644 (file)
@@ -4,12 +4,12 @@
 ( function ( mw, $ ) {
        mw.searchSuggest = {
                // queries the wiki and calls response with the result
-               request: function ( api, query, response, maxRows ) {
+               request: function ( api, query, response, maxRows, namespace ) {
                        return api.get( {
                                formatversion: 2,
                                action: 'opensearch',
                                search: query,
-                               namespace: 0,
+                               namespace: namespace || 0,
                                limit: maxRows,
                                suggest: true
                        } ).done( function ( data, jqXHR ) {
                }
 
                /**
-                * defines the location of autocomplete. Typically either
+                * Defines the location of autocomplete. Typically either
                 * header, which is in the top right of vector (for example)
                 * and content which identifies the main search bar on
-                * Special:Search.  Defaults to header for skins that don't set
+                * Special:Search. Defaults to header for skins that don't set
                 * explicitly.
                 *
                 * @ignore
+                * @param {Object} context
+                * @return {string}
                 */
                function getInputLocation( context ) {
                        return context.config.$region
                 * 'this' is the search input box (jQuery object)
                 *
                 * @ignore
+                * @param {Object} metadata
                 */
                function onAfterUpdate( metadata ) {
                        var context = this.data( 'suggestionsContext' );
                        mw.track( 'mediawiki.searchSuggest', {
                                action: 'render-one',
                                formData: formData,
-                               index: context.config.suggestions.indexOf( text ) + 1
+                               index: context.config.suggestions.indexOf( text )
                        } );
 
                        // this is the container <div>, jQueryfied
                }
 
                // The function used when the user makes a selection
-               function selectFunction( $input ) {
+               function selectFunction( $input, source ) {
                        var context = $input.data( 'suggestionsContext' ),
                                text = $input.val();
 
-                       mw.track( 'mediawiki.searchSuggest', {
-                               action: 'click-result',
-                               numberOfResults: context.config.suggestions.length,
-                               clickIndex: context.config.suggestions.indexOf( text ) + 1
-                       } );
+                       // Selecting via keyboard triggers a form submission. That will fire
+                       // the submit-form event in addition to this click-result event.
+                       if ( source !== 'keyboard' ) {
+                               mw.track( 'mediawiki.searchSuggest', {
+                                       action: 'click-result',
+                                       numberOfResults: context.config.suggestions.length,
+                                       index: context.config.suggestions.indexOf( text )
+                               } );
+                       }
 
                        // allow the form to be submitted
                        return true;
                        // linkParams object is modified and reused
                        formData.linkParams[ formData.textParam ] = query;
 
+                       mw.track( 'mediawiki.searchSuggest', {
+                               action: 'render-one',
+                               formData: formData,
+                               index: context.config.suggestions.indexOf( query )
+                       } );
+
                        if ( $el.children().length === 0 ) {
                                $el
                                        .append(
                                cache: true,
                                highlightInput: true
                        } )
-                       .bind( 'paste cut drop', function () {
+                       .on( 'paste cut drop', function () {
                                // make sure paste and cut events from the mouse and drag&drop events
                                // trigger the keypress handler and cause the suggestions to update
                                $( this ).trigger( 'keypress' );
                        },
                        special: {
                                render: specialRenderFunction,
-                               select: function ( $input ) {
-                                       $input.closest( 'form' )
-                                               .append( $( '<input type="hidden" name="fulltext" value="1"/>' ) );
+                               select: function ( $input, source ) {
+                                       var context = $input.data( 'suggestionsContext' ),
+                                               text = $input.val();
+                                       if ( source === 'mouse' ) {
+                                               // mouse click won't trigger form submission, so we need to send a click event
+                                               mw.track( 'mediawiki.searchSuggest', {
+                                                       action: 'click-result',
+                                                       numberOfResults: context.config.suggestions.length,
+                                                       index: context.config.suggestions.indexOf( text )
+                                               } );
+                                       } else {
+                                               $input.closest( 'form' )
+                                                       .append( $( '<input type="hidden" name="fulltext" value="1"/>' ) );
+                                       }
                                        return true; // allow the form to be submitted
                                }
                        },
                                        action: 'submit-form',
                                        numberOfResults: context.config.suggestions.length,
                                        $form: context.config.$region.closest( 'form' ),
-                                       inputLocation: getInputLocation( context )
+                                       inputLocation: getInputLocation( context ),
+                                       index: context.config.suggestions.indexOf(
+                                               context.data.$textbox.val()
+                                       )
                                } );
                        } )
                        // If the form includes any fallback fulltext search buttons, remove them