1 import {htmlToDom} from "../services/dom";
2 import {debounce} from "../services/util";
10 this.container = this.$el;
11 this.input = this.$refs.input;
12 this.suggestions = this.$refs.suggestions;
13 this.suggestionResultsWrap = this.$refs.suggestionResults;
14 this.loadingWrap = this.$refs.loading;
16 this.setupListeners();
20 const updateSuggestionsDebounced = debounce(this.updateSuggestions.bind(this), 200, false);
22 // Handle search input changes
23 this.input.addEventListener('input', () => {
24 const value = this.input.value;
25 if (value.length > 0) {
26 this.loadingWrap.style.display = 'block';
27 this.suggestionResultsWrap.style.opacity = '0.5';
28 updateSuggestionsDebounced(value);
30 this.hideSuggestions();
34 // Allow double click to show auto-click suggestions
35 this.input.addEventListener('dblclick', () => {
36 this.input.setAttribute('autocomplete', 'on');
43 * @param {String} search
45 async updateSuggestions(search) {
46 const {data: results} = await window.$http.get('/ajax/search/entities', {term: search, count: 5});
47 if (!this.input.value) {
51 const resultDom = htmlToDom(results);
53 const childrenToTrim = Array.from(resultDom.children).slice(9);
54 for (const child of childrenToTrim) {
58 this.suggestionResultsWrap.innerHTML = '';
59 this.suggestionResultsWrap.style.opacity = '1';
60 this.loadingWrap.style.display = 'none';
61 this.suggestionResultsWrap.append(resultDom);
62 if (!this.container.classList.contains('search-active')) {
63 this.showSuggestions();
68 this.container.classList.add('search-active');
69 window.requestAnimationFrame(() => {
70 this.suggestions.classList.add('search-suggestions-animation');
75 this.container.classList.remove('search-active');
76 this.suggestions.classList.remove('search-suggestions-animation');
77 this.suggestionResultsWrap.innerHTML = '';
81 export default GlobalSearch;