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;
15 this.button = this.$refs.button;
17 this.setupListeners();
21 const updateSuggestionsDebounced = debounce(this.updateSuggestions.bind(this), 200, false);
23 // Handle search input changes
24 this.input.addEventListener('input', () => {
25 const value = this.input.value;
26 if (value.length > 0) {
27 this.loadingWrap.style.display = 'block';
28 this.suggestionResultsWrap.style.opacity = '0.5';
29 updateSuggestionsDebounced(value);
31 this.hideSuggestions();
35 // Allow double click to show auto-click suggestions
36 this.input.addEventListener('dblclick', () => {
37 this.input.setAttribute('autocomplete', 'on');
44 * @param {String} search
46 async updateSuggestions(search) {
47 const {data: results} = await window.$http.get('/search/suggest', {term: search});
48 if (!this.input.value) {
52 const resultDom = htmlToDom(results);
54 this.suggestionResultsWrap.innerHTML = '';
55 this.suggestionResultsWrap.style.opacity = '1';
56 this.loadingWrap.style.display = 'none';
57 this.suggestionResultsWrap.append(resultDom);
58 if (!this.container.classList.contains('search-active')) {
59 this.showSuggestions();
64 this.container.classList.add('search-active');
65 window.requestAnimationFrame(() => {
66 this.suggestions.classList.add('search-suggestions-animation');
71 this.container.classList.remove('search-active');
72 this.suggestions.classList.remove('search-suggestions-animation');
73 this.suggestionResultsWrap.innerHTML = '';
77 export default GlobalSearch;