/*! * 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, 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. // For Vector the suggestion box should align with the simpleSearch // container's borders, in other skins it should align with the input // element (not the search form, as that would leave the buttons // vertically between the input and the suggestions). $searchRegion = $( '#simpleSearch, #searchInput' ).first(), $searchInput = $( '#searchInput' ), previousSearchText = $searchInput.val(); // Compatibility map map = { // SimpleSearch is broken in Opera < 9.6 opera: [ [ '>=', 9.6 ] ], // Older Konquerors are unable to position the suggestions correctly (bug 50805) 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 ] ] }; if ( !$.client.test( map ) ) { return; } // Compute form data for search suggestions functionality. function getFormData( context ) { var $form, baseHref, linkParams; if ( !context.formData ) { // Compute common parameters for links' hrefs $form = context.config.$region.closest( 'form' ); baseHref = $form.attr( 'action' ); baseHref += baseHref.indexOf( '?' ) > -1 ? '&' : '?'; linkParams = $form.serializeObject(); context.formData = { textParam: context.data.$textbox.attr( 'name' ), linkParams: linkParams, baseHref: baseHref }; } return context.formData; } /** * Callback that's run when the user changes the search input text * 'this' is the search input box (jQuery object) * * @ignore */ function onBeforeUpdate() { var searchText = this.val(); if ( searchText && searchText !== previousSearchText ) { mw.track( 'mediawiki.searchSuggest', { action: 'session-start' } ); } previousSearchText = searchText; } /** * Callback that's run when suggestions have been updated either from the cache or the API * 'this' is the search input box (jQuery object) * * @ignore */ function onAfterUpdate() { var context = this.data( 'suggestionsContext' ); mw.track( 'mediawiki.searchSuggest', { action: 'impression-results', numberOfResults: context.config.suggestions.length, // FIXME: when other types of search become available change this value accordingly // See the API call below (opensearch = prefix) resultSetType: 'prefix' } ); } // The function used to render the suggestions. function renderFunction( text, context ) { var formData = getFormData( context ); // linkParams object is modified and reused formData.linkParams[ formData.textParam ] = text; // this is the container