Welcome to mirror list, hosted at ThFree Co, Russian Federation.

init.js « header_search « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4e9404007ec6b774089d9869e351aab0012cb3cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import * as Sentry from '@sentry/browser';
import { HEADER_INIT_EVENTS } from './constants';

async function eventHandler(callback = () => {}) {
  if (this.newHeaderSearchFeatureFlag) {
    const { initHeaderSearchApp } = await import(
      /* webpackChunkName: 'globalSearch' */ '~/header_search'
    ).catch((error) => Sentry.captureException(error));

    // 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();
    callback();
    return;
  }

  const { default: initSearchAutocomplete } = await import(
    /* webpackChunkName: 'globalSearch' */ '../search_autocomplete'
  ).catch((error) => Sentry.captureException(error));

  const searchDropdown = initSearchAutocomplete();
  searchDropdown.onSearchInputFocus();
  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,
      eventHandler.bind(
        { searchInputBox, newHeaderSearchFeatureFlag: gon?.features?.newHeaderSearch },
        cleanEventListeners,
      ),
      { once: true },
    );
  });
}

export default initHeaderSearch;
export { eventHandler, cleanEventListeners };