2019-10-05 02:09:52 +02:00
|
|
|
/**
|
|
|
|
* Autocomplete.
|
|
|
|
*/
|
|
|
|
|
2024-04-30 02:39:52 +02:00
|
|
|
import { LocalAutocompleter } from './utils/local-autocompleter';
|
|
|
|
import { handleError } from './utils/requests';
|
2024-05-29 21:57:36 +02:00
|
|
|
import { getTermContexts } from "./match_query";
|
2021-12-27 01:16:21 +01:00
|
|
|
|
2019-10-05 02:09:52 +02:00
|
|
|
const cache = {};
|
2024-05-29 21:57:36 +02:00
|
|
|
/** @type {HTMLInputElement} */
|
|
|
|
let inputField,
|
|
|
|
/** @type {string} */
|
|
|
|
originalTerm,
|
|
|
|
/** @type {string} */
|
|
|
|
originalQuery,
|
|
|
|
/** @type {TermContext[]} */
|
|
|
|
searchTokens,
|
|
|
|
/** @type {TermContext} */
|
|
|
|
selectedTerm;
|
2019-10-05 02:09:52 +02:00
|
|
|
|
|
|
|
function removeParent() {
|
|
|
|
const parent = document.querySelector('.autocomplete');
|
|
|
|
if (parent) parent.parentNode.removeChild(parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
function removeSelected() {
|
|
|
|
const selected = document.querySelector('.autocomplete__item--selected');
|
|
|
|
if (selected) selected.classList.remove('autocomplete__item--selected');
|
|
|
|
}
|
|
|
|
|
2024-05-29 21:57:36 +02:00
|
|
|
function isSearchField() {
|
|
|
|
return inputField && inputField.name === 'q';
|
|
|
|
}
|
|
|
|
|
|
|
|
function restoreOriginalValue() {
|
|
|
|
inputField.value = isSearchField() ? originalQuery : originalTerm;
|
|
|
|
}
|
|
|
|
|
|
|
|
function applySelectedValue(selection) {
|
|
|
|
if (!isSearchField()) {
|
|
|
|
inputField.value = selection;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!selectedTerm) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const [startIndex, endIndex] = selectedTerm[0];
|
|
|
|
inputField.value = originalQuery.slice(0, startIndex) + selection + originalQuery.slice(endIndex);
|
|
|
|
inputField.setSelectionRange(startIndex + selection.length, startIndex + selection.length);
|
|
|
|
inputField.focus();
|
|
|
|
}
|
|
|
|
|
2019-10-05 02:09:52 +02:00
|
|
|
function changeSelected(firstOrLast, current, sibling) {
|
|
|
|
if (current && sibling) { // if the currently selected item has a sibling, move selection to it
|
|
|
|
current.classList.remove('autocomplete__item--selected');
|
|
|
|
sibling.classList.add('autocomplete__item--selected');
|
|
|
|
}
|
|
|
|
else if (current) { // if the next keypress will take the user outside the list, restore the unautocompleted term
|
2024-05-29 21:57:36 +02:00
|
|
|
restoreOriginalValue();
|
2019-10-05 02:09:52 +02:00
|
|
|
removeSelected();
|
|
|
|
}
|
|
|
|
else if (firstOrLast) { // if no item in the list is selected, select the first or last
|
|
|
|
firstOrLast.classList.add('autocomplete__item--selected');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-29 23:37:35 +02:00
|
|
|
function isSelectionOutsideCurrentTerm() {
|
|
|
|
const selectionIndex = Math.min(inputField.selectionStart, inputField.selectionEnd);
|
|
|
|
const [startIndex, endIndex] = selectedTerm[0];
|
|
|
|
|
|
|
|
return startIndex > selectionIndex || endIndex < selectionIndex;
|
|
|
|
}
|
|
|
|
|
2019-10-05 02:09:52 +02:00
|
|
|
function keydownHandler(event) {
|
|
|
|
const selected = document.querySelector('.autocomplete__item--selected'),
|
|
|
|
firstItem = document.querySelector('.autocomplete__item:first-of-type'),
|
|
|
|
lastItem = document.querySelector('.autocomplete__item:last-of-type');
|
|
|
|
|
2024-05-29 22:32:03 +02:00
|
|
|
if (isSearchField()) {
|
|
|
|
// Prevent submission of the search field when Enter was hit
|
|
|
|
if (selected && event.keyCode === 13) event.preventDefault(); // Enter
|
|
|
|
|
|
|
|
// Close autocompletion popup when text cursor is outside current tag
|
|
|
|
if (selectedTerm && firstItem && (event.keyCode === 37 || event.keyCode === 39)) { // ArrowLeft || ArrowRight
|
|
|
|
requestAnimationFrame(() => {
|
2024-05-29 23:37:35 +02:00
|
|
|
if (isSelectionOutsideCurrentTerm()) removeParent();
|
2024-05-29 22:32:03 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2024-05-29 21:57:36 +02:00
|
|
|
|
2019-10-05 02:09:52 +02:00
|
|
|
if (event.keyCode === 38) changeSelected(lastItem, selected, selected && selected.previousSibling); // ArrowUp
|
|
|
|
if (event.keyCode === 40) changeSelected(firstItem, selected, selected && selected.nextSibling); // ArrowDown
|
|
|
|
if (event.keyCode === 13 || event.keyCode === 27 || event.keyCode === 188) removeParent(); // Enter || Esc || Comma
|
|
|
|
if (event.keyCode === 38 || event.keyCode === 40) { // ArrowUp || ArrowDown
|
|
|
|
const newSelected = document.querySelector('.autocomplete__item--selected');
|
2024-05-29 21:57:36 +02:00
|
|
|
if (newSelected) applySelectedValue(newSelected.dataset.value);
|
2019-10-05 02:09:52 +02:00
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function createItem(list, suggestion) {
|
|
|
|
const item = document.createElement('li');
|
|
|
|
item.className = 'autocomplete__item';
|
|
|
|
|
|
|
|
item.textContent = suggestion.label;
|
|
|
|
item.dataset.value = suggestion.value;
|
|
|
|
|
|
|
|
item.addEventListener('mouseover', () => {
|
|
|
|
removeSelected();
|
|
|
|
item.classList.add('autocomplete__item--selected');
|
|
|
|
});
|
|
|
|
|
|
|
|
item.addEventListener('mouseout', () => {
|
|
|
|
removeSelected();
|
|
|
|
});
|
|
|
|
|
|
|
|
item.addEventListener('click', () => {
|
2024-05-29 21:57:36 +02:00
|
|
|
applySelectedValue(item.dataset.value);
|
2019-10-05 02:09:52 +02:00
|
|
|
inputField.dispatchEvent(
|
|
|
|
new CustomEvent('autocomplete', {
|
|
|
|
detail: {
|
|
|
|
type: 'click',
|
|
|
|
label: suggestion.label,
|
|
|
|
value: suggestion.value,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
list.appendChild(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
function createList(suggestions) {
|
|
|
|
const parent = document.querySelector('.autocomplete'),
|
|
|
|
list = document.createElement('ul');
|
|
|
|
list.className = 'autocomplete__list';
|
|
|
|
|
|
|
|
suggestions.forEach(suggestion => createItem(list, suggestion));
|
|
|
|
|
|
|
|
parent.appendChild(list);
|
|
|
|
}
|
|
|
|
|
|
|
|
function createParent() {
|
|
|
|
const parent = document.createElement('div');
|
|
|
|
parent.className = 'autocomplete';
|
|
|
|
|
|
|
|
// Position the parent below the inputfield
|
|
|
|
parent.style.position = 'absolute';
|
|
|
|
parent.style.left = `${inputField.offsetLeft}px`;
|
|
|
|
// Take the inputfield offset, add its height and subtract the amount by which the parent element has scrolled
|
|
|
|
parent.style.top = `${inputField.offsetTop + inputField.offsetHeight - inputField.parentNode.scrollTop}px`;
|
|
|
|
|
|
|
|
// We append the parent at the end of body
|
|
|
|
document.body.appendChild(parent);
|
|
|
|
}
|
|
|
|
|
2021-10-26 03:17:57 +02:00
|
|
|
function showAutocomplete(suggestions, fetchedTerm, targetInput) {
|
2019-10-05 02:09:52 +02:00
|
|
|
// Remove old autocomplete suggestions
|
|
|
|
removeParent();
|
|
|
|
|
|
|
|
// Save suggestions in cache
|
2021-10-26 03:17:57 +02:00
|
|
|
cache[fetchedTerm] = suggestions;
|
2019-10-05 02:09:52 +02:00
|
|
|
|
|
|
|
// If the input target is not empty, still visible, and suggestions were found
|
|
|
|
if (targetInput.value && targetInput.style.display !== 'none' && suggestions.length) {
|
|
|
|
createParent();
|
|
|
|
createList(suggestions);
|
|
|
|
inputField.addEventListener('keydown', keydownHandler);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-26 03:17:57 +02:00
|
|
|
function getSuggestions(term) {
|
|
|
|
return fetch(`${inputField.dataset.acSource}${term}`).then(response => response.json());
|
2019-10-05 02:09:52 +02:00
|
|
|
}
|
|
|
|
|
2024-05-29 21:57:36 +02:00
|
|
|
function getSelectedTerm() {
|
|
|
|
if (!inputField || !originalQuery) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const selectionIndex = Math.min(inputField.selectionStart, inputField.selectionEnd);
|
|
|
|
const terms = getTermContexts(originalQuery);
|
|
|
|
|
|
|
|
return terms.find(([range]) => range[0] < selectionIndex && range[1] >= selectionIndex);
|
|
|
|
}
|
|
|
|
|
2019-10-05 02:09:52 +02:00
|
|
|
function listenAutocomplete() {
|
|
|
|
let timeout;
|
|
|
|
|
2021-12-27 01:16:21 +01:00
|
|
|
/** @type {LocalAutocompleter} */
|
|
|
|
let localAc = null;
|
|
|
|
let localFetched = false;
|
|
|
|
|
|
|
|
document.addEventListener('focusin', fetchLocalAutocomplete);
|
|
|
|
|
2019-10-05 02:09:52 +02:00
|
|
|
document.addEventListener('input', event => {
|
|
|
|
removeParent();
|
2021-12-27 01:16:21 +01:00
|
|
|
fetchLocalAutocomplete(event);
|
2024-03-24 16:38:23 +01:00
|
|
|
window.clearTimeout(timeout);
|
2021-12-27 01:16:21 +01:00
|
|
|
|
|
|
|
if (localAc !== null && 'ac' in event.target.dataset) {
|
|
|
|
inputField = event.target;
|
2024-05-29 21:57:36 +02:00
|
|
|
|
|
|
|
if (isSearchField()) {
|
|
|
|
originalQuery = inputField.value;
|
|
|
|
selectedTerm = getSelectedTerm();
|
|
|
|
|
|
|
|
// We don't need to run auto-completion if user is not selecting tag at all
|
|
|
|
if (!selectedTerm) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
originalTerm = selectedTerm[1];
|
|
|
|
} else {
|
|
|
|
originalTerm = `${inputField.value}`.toLowerCase();
|
|
|
|
}
|
2021-12-27 01:16:21 +01:00
|
|
|
|
2021-12-30 01:52:15 +01:00
|
|
|
const suggestions = localAc.topK(originalTerm, 5).map(({ name, imageCount }) => ({ label: `${name} (${imageCount})`, value: name }));
|
2024-03-24 16:38:23 +01:00
|
|
|
|
|
|
|
if (suggestions.length) {
|
|
|
|
return showAutocomplete(suggestions, originalTerm, event.target);
|
|
|
|
}
|
2021-12-27 01:16:21 +01:00
|
|
|
}
|
2019-10-05 02:09:52 +02:00
|
|
|
|
|
|
|
// Use a timeout to delay requests until the user has stopped typing
|
|
|
|
timeout = window.setTimeout(() => {
|
|
|
|
inputField = event.target;
|
|
|
|
originalTerm = inputField.value;
|
|
|
|
|
2021-10-26 03:17:57 +02:00
|
|
|
const fetchedTerm = inputField.value;
|
|
|
|
const {ac, acMinLength} = inputField.dataset;
|
2019-10-05 02:09:52 +02:00
|
|
|
|
2021-10-26 03:17:57 +02:00
|
|
|
if (ac && (fetchedTerm.length >= acMinLength)) {
|
|
|
|
if (cache[fetchedTerm]) {
|
2021-11-17 02:20:55 +01:00
|
|
|
showAutocomplete(cache[fetchedTerm], fetchedTerm, event.target);
|
2019-10-05 02:09:52 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// inputField could get overwritten while the suggestions are being fetched - use event.target
|
2024-03-24 16:38:23 +01:00
|
|
|
getSuggestions(fetchedTerm).then(suggestions => {
|
|
|
|
if (fetchedTerm === event.target.value) {
|
|
|
|
showAutocomplete(suggestions, fetchedTerm, event.target);
|
|
|
|
}
|
|
|
|
});
|
2019-10-05 02:09:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}, 300);
|
|
|
|
});
|
|
|
|
|
|
|
|
// If there's a click outside the inputField, remove autocomplete
|
|
|
|
document.addEventListener('click', event => {
|
|
|
|
if (event.target && event.target !== inputField) removeParent();
|
2024-05-29 23:37:35 +02:00
|
|
|
if (event.target === inputField && isSearchField() && isSelectionOutsideCurrentTerm()) removeParent();
|
2019-10-05 02:09:52 +02:00
|
|
|
});
|
2021-12-27 01:16:21 +01:00
|
|
|
|
|
|
|
function fetchLocalAutocomplete(event) {
|
|
|
|
if (!localFetched && event.target.dataset && 'ac' in event.target.dataset) {
|
2023-03-30 15:45:14 +02:00
|
|
|
const now = new Date();
|
|
|
|
const cacheKey = `${now.getUTCFullYear()}-${now.getUTCMonth()}-${now.getUTCDate()}`;
|
|
|
|
|
2021-12-27 01:16:21 +01:00
|
|
|
localFetched = true;
|
2023-03-30 15:45:14 +02:00
|
|
|
|
|
|
|
fetch(`/autocomplete/compiled?vsn=2&key=${cacheKey}`, { credentials: 'omit', cache: 'force-cache' })
|
2021-12-27 01:16:21 +01:00
|
|
|
.then(handleError)
|
|
|
|
.then(resp => resp.arrayBuffer())
|
|
|
|
.then(buf => localAc = new LocalAutocompleter(buf));
|
|
|
|
}
|
|
|
|
}
|
2019-10-05 02:09:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export { listenAutocomplete };
|