debian-mirror-gitlab/app/assets/javascripts/header_search/init.js

40 lines
1.2 KiB
JavaScript
Raw Normal View History

2022-08-13 15:12:31 +05:30
import * as Sentry from '@sentry/browser';
import { HEADER_INIT_EVENTS } from './constants';
async function eventHandler(callback = () => {}) {
2023-06-20 00:43:36 +05:30
const { initHeaderSearchApp } = await import(
/* webpackChunkName: 'globalSearch' */ '~/header_search'
2022-08-13 15:12:31 +05:30
).catch((error) => Sentry.captureException(error));
2023-06-20 00:43:36 +05:30
// In case the user started searching before we bootstrapped,
// let's pass the search along.
const initialSearchValue = this.searchInputBox.value;
initHeaderSearchApp(initialSearchValue);
// this is new #search input element. We need to re-find it.
// And re-focus in it.
document.querySelector('#search').focus();
2022-08-13 15:12:31 +05:30
callback();
}
function cleanEventListeners() {
HEADER_INIT_EVENTS.forEach((eventType) => {
document.querySelector('#search').removeEventListener(eventType, eventHandler);
});
}
function initHeaderSearch() {
const searchInputBox = document.querySelector('#search');
HEADER_INIT_EVENTS.forEach((eventType) => {
searchInputBox?.addEventListener(
eventType,
2023-06-20 00:43:36 +05:30
eventHandler.bind({ searchInputBox }, cleanEventListeners),
2022-08-13 15:12:31 +05:30
{ once: true },
);
});
}
export default initHeaderSearch;
export { eventHandler, cleanEventListeners };