mediawiki-skins-Vector/resources/skins.vector.search/skins.vector.search.js
jdlrobson caed16e26f Allow multiple search components on the same page
Styling should not depend on IDs to allow us to have multiple
searches in the page.

Precursor for wiring up search in the sticky header.

This also tweaks performance metrics to track separate metrics
for the sticky header search

Change-Id: I5b4192a8f5a9f95af26c1faf904f7cc994323518
2021-09-23 23:23:58 +00:00

70 lines
1.9 KiB
JavaScript

/** @module search */
var
Vue = require( 'vue' ).default || require( 'vue' ),
App = require( './App.vue' ),
config = require( './config.json' );
/**
* @param {Function} createElement
* @param {Element} searchForm
* @return {Vue.VNode}
* @throws {Error} if the searchForm does not
* contain input[name=title] and input[name="search"] elements.
*/
function renderFn( createElement, searchForm ) {
var
titleInput = /** @type {HTMLInputElement|null} */ (
searchForm.querySelector( 'input[name=title]' )
),
search = /** @type {HTMLInputElement|null} */ ( searchForm.querySelector( 'input[name="search"]' ) ),
searchPageTitle = titleInput && titleInput.value;
if ( !search || !titleInput ) {
throw new Error( 'Attempted to create Vue search element from an incompatible element.' );
}
return createElement( App, {
props: $.extend( {
id: searchForm.id,
autofocusInput: search === document.activeElement,
action: searchForm.getAttribute( 'action' ),
searchAccessKey: search.getAttribute( 'accessKey' ),
searchPageTitle: searchPageTitle,
searchTitle: search.getAttribute( 'title' ),
searchPlaceholder: search.getAttribute( 'placeholder' ),
searchQuery: search.value
},
// Pass additional config from server.
config
)
} );
}
/**
* @param {NodeList} searchForms
* @return {void}
*/
function initApp( searchForms ) {
searchForms.forEach( function ( searchForm ) {
// eslint-disable-next-line no-new
new Vue( {
el: /** @type {Element} */ ( searchForm ),
render: function ( createElement ) {
return renderFn( createElement, /** @type {Element} */ ( searchForm ) );
}
} );
} );
}
/**
* @param {Document} document
* @return {void}
*/
function main( document ) {
var
// FIXME: Use .vector-search-box-form instead when cache allows.
searchForms = document.querySelectorAll( '.vector-search-box form' );
initApp( searchForms );
}
main( document );