Debian Codegraph @

skins/Vector/resources/skins.vector.js/searchLoader.js

raw symbols SCIP
1 /**
2 * Disabling this rule as it's only necessary for
3 * combining multiple class names and documenting the output.
4 * That doesn't happen in this file but the linter still throws an error.
5 * https://github.com/wikimedia/eslint-plugin-mediawiki/blob/master/docs/rules/class-doc.md
6 */
7
8 /** @interface VectorResourceLoaderVirtualConfig */
9 /** @interface MediaWikiPageReadyModule */
10
11 const /** @type {VectorResourceLoaderVirtualConfig} */
12 config = require( /** @type {string} */ ( './config.json' ) ),
13 // T251544: Collect search performance metrics to compare Vue search with
14 // mediawiki.searchSuggest performance.
15 CAN_TEST_SEARCH = !!(
16 window.performance &&
17 !!performance.mark &&
18 !!performance.measure &&
19 performance.getEntriesByName ),
20 LOAD_START_MARK = 'mwVectorVueSearchLoadStart',
21 LOAD_END_MARK = 'mwVectorVueSearchLoadEnd',
22 LOAD_MEASURE = 'mwVectorVueSearchLoadStartToLoadEnd';
23
24 /**
25 * Loads the search module via `mw.loader.using` on the element's
26 * focus event. Or, if the element is already focused, loads the
27 * search module immediately.
28 * After the search module is loaded, executes a function to remove
29 * the loading indicator.
30 *
31 * @param {Element} element search input.
32 * @param {string} moduleName resourceLoader module to load.
33 * @param {string|null} startMarker
34 * @param {null|function(): void} afterLoadFn function to execute after search module loads.
35 */
36 function loadSearchModule( element, moduleName, startMarker, afterLoadFn ) {
37 const SHOULD_TEST_SEARCH = CAN_TEST_SEARCH &&
38 moduleName === 'skins.vector.search';
39
40 function requestSearchModule() {
41 if ( SHOULD_TEST_SEARCH && startMarker !== null && afterLoadFn !== null ) {
42 performance.mark( startMarker );
43 mw.loader.using( moduleName, afterLoadFn );
44 } else {
45 mw.loader.load( moduleName );
46 }
47 element.removeEventListener( 'focus', requestSearchModule );
48 }
49
50 if ( document.activeElement === element ) {
51 requestSearchModule();
52 } else {
53 element.addEventListener( 'focus', requestSearchModule );
54 }
55 }
56
57 /**
58 * Marks when the lazy load has completed.
59 *
60 * @param {string} startMarker
61 * @param {string} endMarker
62 * @param {string} measureMarker
63 */
64 function markLoadEnd( startMarker, endMarker, measureMarker ) {
65 if ( performance.getEntriesByName( startMarker ).length ) {
66 performance.mark( endMarker );
67 performance.measure( measureMarker, startMarker, endMarker );
68 }
69 }
70
71 /**
72 * Initialize the loading of the search module as well as the loading indicator.
73 * Only initialize the loading indicator when not using the core search module.
74 *
75 * @param {Document} document
76 */
77 function initSearchLoader( document ) {
78 const searchBoxes = document.querySelectorAll( '.vector-search-box' );
79
80 // Allow developers to defined $wgVectorSearchApiUrl in LocalSettings to target different APIs
81 if ( config.VectorSearchApiUrl ) {
82 mw.config.set( 'wgVectorSearchApiUrl', config.VectorSearchApiUrl );
83 }
84
85 if ( !searchBoxes.length ) {
86 return;
87 }
88
89 /**
90 * If we are in a browser that doesn't support ES6 fall back to non-JS version.
91 */
92 if ( mw.loader.getState( 'skins.vector.search' ) === null ) {
93 document.body.classList.remove(
94 'skin-vector-search-vue'
95 );
96 return;
97 }
98
99 Array.prototype.forEach.call( searchBoxes, ( searchBox ) => {
100 const searchInner = searchBox.querySelector( 'form > div' ),
101 searchInput = searchBox.querySelector( 'input[name="search"]' ),
102 isPrimarySearch = searchInput && searchInput.getAttribute( 'id' ) === 'searchInput';
103
104 if ( !searchInput || !searchInner ) {
105 return;
106 }
107 // Remove tooltips while Vue search is still loading
108 searchInput.setAttribute( 'autocomplete', 'off' );
109 loadSearchModule(
110 searchInput,
111 'skins.vector.search',
112 isPrimarySearch ? LOAD_START_MARK : null,
113 // Note, loading Vue.js will remove the element from the DOM.
114 () => {
115 if ( isPrimarySearch ) {
116 markLoadEnd( LOAD_START_MARK, LOAD_END_MARK, LOAD_MEASURE );
117 }
118 }
119 );
120 } );
121 }
122
123 module.exports = {
124 initSearchLoader: initSearchLoader
125 };