4 import {htmlToDom} from "../services/dom";
9 this.container = this.$el;
10 this.input = this.$refs.input;
11 this.suggestions = this.$refs.suggestions;
12 this.suggestionResultsWrap = this.$refs.suggestionResults;
14 this.setupListeners();
18 this.hideOnOuterEventListener = this.hideOnOuterEventListener.bind(this);
20 this.input.addEventListener('input', () => {
21 const value = this.input.value;
22 if (value.length > 0) {
23 this.updateSuggestions(value);
25 this.hideSuggestions();
30 async updateSuggestions(search) {
31 const {data: results} = await window.$http.get('/ajax/search/entities', {term: search, count: 5});
32 const resultDom = htmlToDom(results);
34 const childrenToTrim = Array.from(resultDom.children).slice(9);
35 for (const child of childrenToTrim) {
39 this.suggestionResultsWrap.innerHTML = '';
40 this.suggestionResultsWrap.append(resultDom);
41 if (!this.container.classList.contains('search-active')) {
42 this.showSuggestions();
47 this.container.classList.add('search-active');
48 document.addEventListener('click', this.hideOnOuterEventListener);
49 document.addEventListener('focusin', this.hideOnOuterEventListener);
50 window.requestAnimationFrame(() => {
51 this.suggestions.classList.add('search-suggestions-animation');
56 this.container.classList.remove('search-active');
57 this.suggestions.classList.remove('search-suggestions-animation');
58 this.suggestionResultsWrap.innerHTML = '';
59 document.removeEventListener('click', this.hideOnOuterEventListener);
60 document.removeEventListener('focusin', this.hideOnOuterEventListener);
63 hideOnOuterEventListener(event) {
64 if (!this.container.contains(event.target)) {
65 this.hideSuggestions();
70 export default GlobalSearch;