1 import {escapeHtml} from '../services/util';
2 import {onChildEvent} from '../services/dom';
3 import {Component} from './component';
4 import {KeyboardNavigationHandler} from '../services/keyboard-navigation';
11 export class AutoSuggest extends Component {
14 this.parent = this.$el.parentElement;
15 this.container = this.$el;
16 this.type = this.$opts.type;
17 this.url = this.$opts.url;
18 this.input = this.$refs.input;
19 this.list = this.$refs.list;
21 this.lastPopulated = 0;
22 this.setupListeners();
26 const navHandler = new KeyboardNavigationHandler(
30 setTimeout(() => this.hideSuggestions(), 1);
33 event.preventDefault();
34 this.selectSuggestion(event.target.textContent);
37 navHandler.shareHandlingToEl(this.input);
39 onChildEvent(this.list, '.text-item', 'click', (event, el) => {
40 this.selectSuggestion(el.textContent);
43 this.input.addEventListener('input', this.requestSuggestions.bind(this));
44 this.input.addEventListener('focus', this.requestSuggestions.bind(this));
45 this.input.addEventListener('blur', this.hideSuggestionsIfFocusedLost.bind(this));
46 this.input.addEventListener('keydown', event => {
47 if (event.key === 'Tab') {
48 this.hideSuggestions();
53 selectSuggestion(value) {
54 this.input.value = value;
55 this.lastPopulated = Date.now();
57 this.input.dispatchEvent(new Event('input', {bubbles: true}));
58 this.input.dispatchEvent(new Event('change', {bubbles: true}));
59 this.hideSuggestions();
62 async requestSuggestions() {
63 if (Date.now() - this.lastPopulated < 50) {
67 const nameFilter = this.getNameFilterIfNeeded();
68 const search = this.input.value.toLowerCase();
69 const suggestions = await this.loadSuggestions(search, nameFilter);
71 const toShow = suggestions.filter(val => search === '' || val.toLowerCase().startsWith(search)).slice(0, 10);
73 this.displaySuggestions(toShow);
76 getNameFilterIfNeeded() {
77 if (this.type !== 'value') return null;
78 return this.parent.querySelector('input').value;
82 * @param {String} search
83 * @param {String|null} nameFilter
84 * @returns {Promise<Object|String|*>}
86 async loadSuggestions(search, nameFilter = null) {
87 // Truncate search to prevent over numerous lookups
88 search = search.slice(0, 4);
90 const params = {search, name: nameFilter};
91 const cacheKey = `${this.url}:${JSON.stringify(params)}`;
93 if (ajaxCache[cacheKey]) {
94 return ajaxCache[cacheKey];
97 const resp = await window.$http.get(this.url, params);
98 ajaxCache[cacheKey] = resp.data;
103 * @param {String[]} suggestions
105 displaySuggestions(suggestions) {
106 if (suggestions.length === 0) {
107 return this.hideSuggestions();
110 // This used to use <button>s but was changed to div elements since Safari would not focus on buttons
111 // on which causes a range of other complexities related to focus handling.
112 this.list.innerHTML = suggestions.map(value => `<li><div tabindex="0" class="text-item">${escapeHtml(value)}</div></li>`).join('');
113 this.list.style.display = 'block';
114 for (const button of this.list.querySelectorAll('.text-item')) {
115 button.addEventListener('blur', this.hideSuggestionsIfFocusedLost.bind(this));
120 this.list.style.display = 'none';
123 hideSuggestionsIfFocusedLost(event) {
124 if (!this.container.contains(event.relatedTarget)) {
125 this.hideSuggestions();