/*! * Add search suggestions to the search form. */ ( function () { // eslint-disable-next-line no-jquery/no-map-util const searchNS = $.map( mw.config.get( 'wgFormattedNamespaces' ), ( nsName, nsID ) => { if ( nsID >= 0 && mw.user.options.get( 'searchNs' + nsID ) ) { // Cast string key to number return Number( nsID ); } } ); /** * Convenience library for making searches for titles that match a string. * Loaded via the `mediawiki.searchSuggest` ResourceLoader library. * * @example * mw.loader.using('mediawiki.searchSuggest').then(() => { * var api = new mw.Api(); * mw.searchSuggest.request(api, 'Dogs that', ( results ) => { * alert( `Results that match: ${results.join( '\n' );}` ); * }); * }); * @namespace mw.searchSuggest */ mw.searchSuggest = { /** * @typedef {Object} mw.searchSuggest~ResponseMetaData * @property {string} type the contents of the X-OpenSearch-Type response header. * @property {string} searchId the contents of the X-Search-ID response header. * @property {string} query */ /** * @callback mw.searchSuggest~ResponseFunction * @param {string[]} titles titles of pages that match search * @param {ResponseMetaData} meta meta data relating to search. */ /** * Queries the wiki and calls response with the result. * * @param {mw.Api} api * @param {string} query * @param {ResponseFunction} response * @param {string|number} [limit] * @param {string|number|string[]|number[]} [namespace] * @return {jQuery.Deferred} */ request: function ( api, query, response, limit, namespace ) { return api.get( { formatversion: 2, action: 'opensearch', search: query, namespace: namespace || searchNS, limit } ).done( ( data, jqXHR ) => { response( data[ 1 ], { type: jqXHR.getResponseHeader( 'X-OpenSearch-Type' ), searchId: jqXHR.getResponseHeader( 'X-Search-ID' ), query } ); } ); } }; $( () => { let api; // 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). const $searchRegion = $( '#simpleSearch, #searchInput' ).first(), $searchInput = $( '#searchInput' ); let previousSearchText = $searchInput.val(); function serializeObject( fields ) { const obj = {}; for ( let i = 0; i < fields.length; i++ ) { obj[ fields[ i ].name ] = fields[ i ].value; } return obj; } // Compute form data for search suggestions functionality. function getFormData( context ) { if ( !context.formData ) { // Compute common parameters for links' hrefs const $form = context.config.$region.closest( 'form' ); let baseHref = $form.attr( 'action' ) || ''; baseHref += baseHref.includes( '?' ) ? '&' : '?'; const linkParams = serializeObject( $form.serializeArray() ); 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() { const searchText = this.val(); if ( searchText && searchText !== previousSearchText ) { mw.track( 'mediawiki.searchSuggest', { action: 'session-start' } ); } previousSearchText = searchText; } /** * 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 * explicitly. * * @ignore * @param {Object} context * @return {string} */ function getInputLocation( context ) { return context.config.$region .closest( 'form' ) .find( '[data-search-loc]' ) .data( 'search-loc' ) || 'header'; } /** * 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 * @param {Object} metadata */ function onAfterUpdate( metadata ) { const context = this.data( 'suggestionsContext' ); mw.track( 'mediawiki.searchSuggest', { action: 'impression-results', numberOfResults: context.config.suggestions.length, resultSetType: metadata.type || 'unknown', searchId: metadata.searchId || null, query: metadata.query, inputLocation: getInputLocation( context ) } ); } // The function used to render the suggestions. function renderFunction( text, context ) { const formData = getFormData( context ), textboxConfig = context.data.$textbox.data( 'mw-searchsuggest' ) || {}; // linkParams object is modified and reused formData.linkParams[ formData.textParam ] = text; // Allow trackers to attach tracking information, such // as wprov, to clicked links. mw.track( 'mediawiki.searchSuggest', { action: 'render-one', formData: formData, index: context.config.suggestions.indexOf( text ) } ); // this is the container