2024-10-10 19:03:24 +08:00
|
|
|
import {escapeHtml} from '../services/util.ts';
|
2023-04-19 05:20:02 +08:00
|
|
|
import {onChildEvent} from '../services/dom';
|
|
|
|
import {Component} from './component';
|
|
|
|
import {KeyboardNavigationHandler} from '../services/keyboard-navigation';
|
2020-06-29 06:15:05 +08:00
|
|
|
|
|
|
|
const ajaxCache = {};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* AutoSuggest
|
|
|
|
*/
|
2022-11-15 07:19:02 +08:00
|
|
|
export class AutoSuggest extends Component {
|
2023-04-19 05:20:02 +08:00
|
|
|
|
2020-06-29 06:15:05 +08:00
|
|
|
setup() {
|
|
|
|
this.parent = this.$el.parentElement;
|
|
|
|
this.container = this.$el;
|
|
|
|
this.type = this.$opts.type;
|
|
|
|
this.url = this.$opts.url;
|
|
|
|
this.input = this.$refs.input;
|
|
|
|
this.list = this.$refs.list;
|
|
|
|
|
2020-06-30 05:11:03 +08:00
|
|
|
this.lastPopulated = 0;
|
2020-06-29 06:15:05 +08:00
|
|
|
this.setupListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
setupListeners() {
|
2023-04-08 00:47:46 +08:00
|
|
|
const navHandler = new KeyboardNavigationHandler(
|
|
|
|
this.list,
|
2023-04-19 22:20:04 +08:00
|
|
|
() => {
|
2023-04-08 00:47:46 +08:00
|
|
|
this.input.focus();
|
|
|
|
setTimeout(() => this.hideSuggestions(), 1);
|
|
|
|
},
|
|
|
|
event => {
|
|
|
|
event.preventDefault();
|
2023-10-04 16:08:10 +08:00
|
|
|
const selectionValue = event.target.textContent;
|
|
|
|
if (selectionValue) {
|
|
|
|
this.selectSuggestion(selectionValue);
|
|
|
|
}
|
2023-04-08 00:47:46 +08:00
|
|
|
},
|
|
|
|
);
|
|
|
|
navHandler.shareHandlingToEl(this.input);
|
|
|
|
|
|
|
|
onChildEvent(this.list, '.text-item', 'click', (event, el) => {
|
|
|
|
this.selectSuggestion(el.textContent);
|
|
|
|
});
|
|
|
|
|
2020-06-29 06:15:05 +08:00
|
|
|
this.input.addEventListener('input', this.requestSuggestions.bind(this));
|
|
|
|
this.input.addEventListener('focus', this.requestSuggestions.bind(this));
|
2023-04-08 00:47:46 +08:00
|
|
|
this.input.addEventListener('blur', this.hideSuggestionsIfFocusedLost.bind(this));
|
2020-06-29 06:15:05 +08:00
|
|
|
this.input.addEventListener('keydown', event => {
|
|
|
|
if (event.key === 'Tab') {
|
|
|
|
this.hideSuggestions();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
selectSuggestion(value) {
|
|
|
|
this.input.value = value;
|
2020-06-30 05:11:03 +08:00
|
|
|
this.lastPopulated = Date.now();
|
2020-06-29 06:15:05 +08:00
|
|
|
this.input.focus();
|
2020-06-30 05:11:03 +08:00
|
|
|
this.input.dispatchEvent(new Event('input', {bubbles: true}));
|
|
|
|
this.input.dispatchEvent(new Event('change', {bubbles: true}));
|
2020-06-29 06:15:05 +08:00
|
|
|
this.hideSuggestions();
|
|
|
|
}
|
|
|
|
|
|
|
|
async requestSuggestions() {
|
2020-06-30 05:11:03 +08:00
|
|
|
if (Date.now() - this.lastPopulated < 50) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-29 06:15:05 +08:00
|
|
|
const nameFilter = this.getNameFilterIfNeeded();
|
2022-09-28 20:50:40 +08:00
|
|
|
const search = this.input.value.toLowerCase();
|
2020-06-29 06:15:05 +08:00
|
|
|
const suggestions = await this.loadSuggestions(search, nameFilter);
|
2022-09-28 20:50:40 +08:00
|
|
|
|
2023-04-19 05:20:02 +08:00
|
|
|
const toShow = suggestions.filter(val => search === '' || val.toLowerCase().startsWith(search)).slice(0, 10);
|
2020-06-29 06:15:05 +08:00
|
|
|
|
|
|
|
this.displaySuggestions(toShow);
|
|
|
|
}
|
|
|
|
|
|
|
|
getNameFilterIfNeeded() {
|
|
|
|
if (this.type !== 'value') return null;
|
|
|
|
return this.parent.querySelector('input').value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {String} search
|
|
|
|
* @param {String|null} nameFilter
|
|
|
|
* @returns {Promise<Object|String|*>}
|
|
|
|
*/
|
|
|
|
async loadSuggestions(search, nameFilter = null) {
|
2022-09-28 20:50:40 +08:00
|
|
|
// Truncate search to prevent over numerous lookups
|
|
|
|
search = search.slice(0, 4);
|
|
|
|
|
2020-06-29 06:15:05 +08:00
|
|
|
const params = {search, name: nameFilter};
|
|
|
|
const cacheKey = `${this.url}:${JSON.stringify(params)}`;
|
|
|
|
|
|
|
|
if (ajaxCache[cacheKey]) {
|
|
|
|
return ajaxCache[cacheKey];
|
|
|
|
}
|
|
|
|
|
|
|
|
const resp = await window.$http.get(this.url, params);
|
|
|
|
ajaxCache[cacheKey] = resp.data;
|
|
|
|
return resp.data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {String[]} suggestions
|
|
|
|
*/
|
|
|
|
displaySuggestions(suggestions) {
|
|
|
|
if (suggestions.length === 0) {
|
2023-04-19 22:20:04 +08:00
|
|
|
this.hideSuggestions();
|
|
|
|
return;
|
2020-06-29 06:15:05 +08:00
|
|
|
}
|
|
|
|
|
2023-04-08 00:47:46 +08:00
|
|
|
// This used to use <button>s but was changed to div elements since Safari would not focus on buttons
|
|
|
|
// on which causes a range of other complexities related to focus handling.
|
2023-04-08 00:54:17 +08:00
|
|
|
this.list.innerHTML = suggestions.map(value => `<li><div tabindex="0" class="text-item">${escapeHtml(value)}</div></li>`).join('');
|
2020-06-29 06:15:05 +08:00
|
|
|
this.list.style.display = 'block';
|
2023-04-08 00:47:46 +08:00
|
|
|
for (const button of this.list.querySelectorAll('.text-item')) {
|
2020-06-29 06:15:05 +08:00
|
|
|
button.addEventListener('blur', this.hideSuggestionsIfFocusedLost.bind(this));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
hideSuggestions() {
|
|
|
|
this.list.style.display = 'none';
|
|
|
|
}
|
|
|
|
|
|
|
|
hideSuggestionsIfFocusedLost(event) {
|
|
|
|
if (!this.container.contains(event.relatedTarget)) {
|
|
|
|
this.hideSuggestions();
|
|
|
|
}
|
|
|
|
}
|
2023-04-19 05:20:02 +08:00
|
|
|
|
|
|
|
}
|