summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/header_search/index.js
blob: f6963263725b3945ba55156a92725bd803148066 (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
import Vue from 'vue';
import * as Sentry from '@sentry/browser';
import Translate from '~/vue_shared/translate';
import HeaderSearchApp from './components/app.vue';
import createStore from './store';
import { SEARCH_INPUT_FIELD_MAX_WIDTH } from './constants';

Vue.use(Translate);

export const initHeaderSearchApp = (search = '') => {
  const el = document.getElementById('js-header-search');
  const headerEl = document.querySelector('.header-content');

  if (!el && !headerEl) {
    return false;
  }

  const searchContainer = headerEl.querySelector('.global-search-container');
  const newHeader = headerEl.querySelector('.header-search-new');

  const { searchPath, issuesPath, mrPath, autocompletePath } = el.dataset;
  let { searchContext } = el.dataset;

  try {
    searchContext = JSON.parse(searchContext);
    newHeader.style.maxWidth = SEARCH_INPUT_FIELD_MAX_WIDTH;
  } catch (error) {
    Sentry.captureException(error);
  }

  return new Vue({
    el,
    store: createStore({ searchPath, issuesPath, mrPath, autocompletePath, searchContext, search }),
    render(createElement) {
      return createElement(HeaderSearchApp, {
        on: {
          expandSearchBar: () => {
            searchContainer.style.flexGrow = '1';
          },
          collapseSearchBar: () => {
            searchContainer.style.flexGrow = '0';
          },
        },
      });
    },
  });
};