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-30 19:55:41 +02:00
|
|
|
import { getTermContexts } from './match_query';
|
2024-06-02 18:40:09 +02:00
|
|
|
import store from './utils/store';
|
2024-08-31 16:48:30 +02:00
|
|
|
import { TermContext } from './query/lex';
|
|
|
|
import { $$ } from './utils/dom';
|
|
|
|
import { fetchSuggestions, SuggestionsPopup, TermSuggestion } from './utils/suggestions';
|
2021-12-27 01:16:21 +01:00
|
|
|
|
2024-08-28 00:36:33 +02:00
|
|
|
let inputField: HTMLInputElement | null = null,
|
|
|
|
originalTerm: string | undefined,
|
|
|
|
originalQuery: string | undefined,
|
|
|
|
selectedTerm: TermContext | null = null;
|
2019-10-05 02:09:52 +02:00
|
|
|
|
2024-08-31 16:48:30 +02:00
|
|
|
const popup = new SuggestionsPopup();
|
2019-10-05 02:09:52 +02:00
|
|
|
|
2024-08-28 01:11:27 +02:00
|
|
|
function isSearchField(targetInput: HTMLElement): boolean {
|
2024-08-28 00:36:33 +02:00
|
|
|
return targetInput && targetInput.dataset.acMode === 'search';
|
2024-05-29 21:57:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function restoreOriginalValue() {
|
2024-08-31 16:48:30 +02:00
|
|
|
if (!inputField) return;
|
2024-08-28 00:36:33 +02:00
|
|
|
|
|
|
|
if (isSearchField(inputField) && originalQuery) {
|
|
|
|
inputField.value = originalQuery;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (originalTerm) {
|
|
|
|
inputField.value = originalTerm;
|
|
|
|
}
|
2024-05-29 21:57:36 +02:00
|
|
|
}
|
|
|
|
|
2024-08-28 00:36:33 +02:00
|
|
|
function applySelectedValue(selection: string) {
|
2024-08-31 16:48:30 +02:00
|
|
|
if (!inputField) return;
|
2024-05-29 21:57:36 +02:00
|
|
|
|
2024-08-28 00:36:33 +02:00
|
|
|
if (!isSearchField(inputField)) {
|
|
|
|
inputField.value = selection;
|
2024-05-29 21:57:36 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-08-28 00:36:33 +02:00
|
|
|
if (selectedTerm && originalQuery) {
|
|
|
|
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();
|
|
|
|
}
|
2024-05-29 21:57:36 +02:00
|
|
|
}
|
|
|
|
|
2024-08-28 01:11:27 +02:00
|
|
|
function isSelectionOutsideCurrentTerm(): boolean {
|
2024-08-28 00:36:33 +02:00
|
|
|
if (!inputField || !selectedTerm) return true;
|
|
|
|
if (inputField.selectionStart === null || inputField.selectionEnd === null) return true;
|
|
|
|
|
2024-05-29 23:37:35 +02:00
|
|
|
const selectionIndex = Math.min(inputField.selectionStart, inputField.selectionEnd);
|
|
|
|
const [startIndex, endIndex] = selectedTerm[0];
|
|
|
|
|
|
|
|
return startIndex > selectionIndex || endIndex < selectionIndex;
|
|
|
|
}
|
|
|
|
|
2024-08-28 00:36:33 +02:00
|
|
|
function keydownHandler(event: KeyboardEvent) {
|
2024-08-31 16:48:30 +02:00
|
|
|
if (inputField !== event.currentTarget) return;
|
2019-10-05 02:09:52 +02:00
|
|
|
|
2024-08-28 00:36:33 +02:00
|
|
|
if (inputField && isSearchField(inputField)) {
|
2024-05-29 22:32:03 +02:00
|
|
|
// Prevent submission of the search field when Enter was hit
|
2024-08-31 16:48:30 +02:00
|
|
|
if (popup.selectedTerm && event.keyCode === 13) event.preventDefault(); // Enter
|
2024-05-29 22:32:03 +02:00
|
|
|
|
|
|
|
// Close autocompletion popup when text cursor is outside current tag
|
2024-08-31 16:48:30 +02:00
|
|
|
if (selectedTerm && (event.keyCode === 37 || event.keyCode === 39)) {
|
2024-07-04 02:27:59 +02:00
|
|
|
// ArrowLeft || ArrowRight
|
2024-05-29 22:32:03 +02:00
|
|
|
requestAnimationFrame(() => {
|
2024-08-31 16:48:30 +02:00
|
|
|
if (isSelectionOutsideCurrentTerm()) popup.hide();
|
2024-05-30 19:55:41 +02:00
|
|
|
});
|
2024-05-29 22:32:03 +02:00
|
|
|
}
|
|
|
|
}
|
2024-05-29 21:57:36 +02:00
|
|
|
|
2024-08-31 16:48:30 +02:00
|
|
|
if (!popup.isActive) return;
|
|
|
|
|
|
|
|
if (event.keyCode === 38) popup.selectPrevious(); // ArrowUp
|
|
|
|
if (event.keyCode === 40) popup.selectNext(); // ArrowDown
|
|
|
|
if (event.keyCode === 13 || event.keyCode === 27 || event.keyCode === 188) popup.hide(); // Enter || Esc || Comma
|
2024-07-04 02:27:59 +02:00
|
|
|
if (event.keyCode === 38 || event.keyCode === 40) {
|
|
|
|
// ArrowUp || ArrowDown
|
2024-08-31 16:48:30 +02:00
|
|
|
if (popup.selectedTerm) {
|
|
|
|
applySelectedValue(popup.selectedTerm);
|
|
|
|
} else {
|
|
|
|
restoreOriginalValue();
|
|
|
|
}
|
2019-10-05 02:09:52 +02:00
|
|
|
|
2024-08-31 16:48:30 +02:00
|
|
|
event.preventDefault();
|
2019-10-05 02:09:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-31 16:48:30 +02:00
|
|
|
function findSelectedTerm(targetInput: HTMLInputElement, searchQuery: string): TermContext | null {
|
|
|
|
if (targetInput.selectionStart === null || targetInput.selectionEnd === null) return null;
|
2019-10-05 02:09:52 +02:00
|
|
|
|
2024-08-31 16:48:30 +02:00
|
|
|
const selectionIndex = Math.min(targetInput.selectionStart, targetInput.selectionEnd);
|
|
|
|
const terms = getTermContexts(searchQuery);
|
2024-05-29 21:57:36 +02:00
|
|
|
|
2024-08-28 00:36:33 +02:00
|
|
|
return terms.find(([range]) => range[0] < selectionIndex && range[1] >= selectionIndex) ?? null;
|
2024-05-29 21:57:36 +02:00
|
|
|
}
|
|
|
|
|
2024-06-02 18:40:09 +02:00
|
|
|
function toggleSearchAutocomplete() {
|
2024-06-03 16:59:49 +02:00
|
|
|
const enable = store.get('enable_search_ac');
|
2024-06-02 18:40:09 +02:00
|
|
|
|
2024-08-28 00:36:33 +02:00
|
|
|
for (const searchField of $$<HTMLInputElement>('input[data-ac-mode=search]')) {
|
2024-06-03 16:59:49 +02:00
|
|
|
if (enable) {
|
|
|
|
searchField.autocomplete = 'off';
|
2024-07-04 02:27:59 +02:00
|
|
|
} else {
|
2024-06-03 16:59:49 +02:00
|
|
|
searchField.removeAttribute('data-ac');
|
|
|
|
searchField.autocomplete = 'on';
|
|
|
|
}
|
2024-06-02 18:40:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-05 02:09:52 +02:00
|
|
|
function listenAutocomplete() {
|
2024-08-31 16:48:30 +02:00
|
|
|
let serverSideSuggestionsTimeout: number | undefined;
|
2019-10-05 02:09:52 +02:00
|
|
|
|
2024-08-28 00:36:33 +02:00
|
|
|
let localAc: LocalAutocompleter | null = null;
|
2021-12-27 01:16:21 +01:00
|
|
|
let localFetched = false;
|
|
|
|
|
|
|
|
document.addEventListener('focusin', fetchLocalAutocomplete);
|
|
|
|
|
2019-10-05 02:09:52 +02:00
|
|
|
document.addEventListener('input', event => {
|
2024-08-31 16:48:30 +02:00
|
|
|
popup.hide();
|
2021-12-27 01:16:21 +01:00
|
|
|
fetchLocalAutocomplete(event);
|
2024-08-31 16:48:30 +02:00
|
|
|
window.clearTimeout(serverSideSuggestionsTimeout);
|
2021-12-27 01:16:21 +01:00
|
|
|
|
2024-08-28 00:36:33 +02:00
|
|
|
if (!(event.target instanceof HTMLInputElement)) return;
|
|
|
|
|
|
|
|
const targetedInput = event.target;
|
|
|
|
|
2024-08-31 16:48:30 +02:00
|
|
|
if (!targetedInput.dataset.ac) return;
|
|
|
|
|
|
|
|
targetedInput.addEventListener('keydown', keydownHandler);
|
|
|
|
|
|
|
|
if (localAc !== null) {
|
2024-08-28 00:36:33 +02:00
|
|
|
inputField = targetedInput;
|
2024-06-02 17:59:06 +02:00
|
|
|
let suggestionsCount = 5;
|
2024-05-29 21:57:36 +02:00
|
|
|
|
2024-08-28 00:36:33 +02:00
|
|
|
if (isSearchField(inputField)) {
|
2024-05-29 21:57:36 +02:00
|
|
|
originalQuery = inputField.value;
|
2024-08-31 16:48:30 +02:00
|
|
|
selectedTerm = findSelectedTerm(inputField, originalQuery);
|
2024-06-02 17:59:06 +02:00
|
|
|
suggestionsCount = 10;
|
2024-05-29 21:57:36 +02:00
|
|
|
|
|
|
|
// We don't need to run auto-completion if user is not selecting tag at all
|
|
|
|
if (!selectedTerm) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-06-03 19:42:58 +02:00
|
|
|
originalTerm = selectedTerm[1].toLowerCase();
|
2024-07-04 02:27:59 +02:00
|
|
|
} else {
|
2024-05-29 21:57:36 +02:00
|
|
|
originalTerm = `${inputField.value}`.toLowerCase();
|
|
|
|
}
|
2021-12-27 01:16:21 +01:00
|
|
|
|
2024-07-04 02:27:59 +02:00
|
|
|
const suggestions = localAc
|
2024-08-13 23:16:47 +02:00
|
|
|
.matchPrefix(originalTerm)
|
|
|
|
.topK(suggestionsCount)
|
2024-07-04 02:27:59 +02:00
|
|
|
.map(({ name, imageCount }) => ({ label: `${name} (${imageCount})`, value: name }));
|
2024-03-24 16:38:23 +01:00
|
|
|
|
|
|
|
if (suggestions.length) {
|
2024-08-31 16:48:30 +02:00
|
|
|
popup.renderSuggestions(suggestions).showForField(targetedInput);
|
|
|
|
return;
|
2024-03-24 16:38:23 +01:00
|
|
|
}
|
2021-12-27 01:16:21 +01:00
|
|
|
}
|
2019-10-05 02:09:52 +02:00
|
|
|
|
2024-08-31 16:48:30 +02:00
|
|
|
const { acMinLength: minTermLength, acSource: endpointUrl } = targetedInput.dataset;
|
|
|
|
|
|
|
|
if (!endpointUrl) return;
|
|
|
|
|
2019-10-05 02:09:52 +02:00
|
|
|
// Use a timeout to delay requests until the user has stopped typing
|
2024-08-31 16:48:30 +02:00
|
|
|
serverSideSuggestionsTimeout = window.setTimeout(() => {
|
2024-08-28 00:36:33 +02:00
|
|
|
inputField = targetedInput;
|
2019-10-05 02:09:52 +02:00
|
|
|
originalTerm = inputField.value;
|
|
|
|
|
2021-10-26 03:17:57 +02:00
|
|
|
const fetchedTerm = inputField.value;
|
2019-10-05 02:09:52 +02:00
|
|
|
|
2024-08-31 16:48:30 +02:00
|
|
|
if (minTermLength && fetchedTerm.length < parseInt(minTermLength, 10)) return;
|
2024-08-28 00:36:33 +02:00
|
|
|
|
2024-08-31 16:48:30 +02:00
|
|
|
fetchSuggestions(endpointUrl, fetchedTerm).then(suggestions => {
|
|
|
|
// inputField could get overwritten while the suggestions are being fetched - use previously targeted input
|
|
|
|
if (fetchedTerm === targetedInput.value) {
|
|
|
|
popup.renderSuggestions(suggestions).showForField(targetedInput);
|
|
|
|
}
|
|
|
|
});
|
2019-10-05 02:09:52 +02:00
|
|
|
}, 300);
|
|
|
|
});
|
|
|
|
|
|
|
|
// If there's a click outside the inputField, remove autocomplete
|
|
|
|
document.addEventListener('click', event => {
|
2024-08-31 16:48:30 +02:00
|
|
|
if (event.target && event.target !== inputField) popup.hide();
|
2024-08-28 00:36:33 +02:00
|
|
|
if (inputField && event.target === inputField && isSearchField(inputField) && isSelectionOutsideCurrentTerm()) {
|
2024-08-31 16:48:30 +02:00
|
|
|
popup.hide();
|
2024-08-28 00:36:33 +02:00
|
|
|
}
|
2019-10-05 02:09:52 +02:00
|
|
|
});
|
2021-12-27 01:16:21 +01:00
|
|
|
|
2024-08-28 00:36:33 +02:00
|
|
|
function fetchLocalAutocomplete(event: Event) {
|
|
|
|
if (!(event.target instanceof HTMLInputElement)) return;
|
|
|
|
|
2021-12-27 01:16:21 +01:00
|
|
|
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())
|
2024-07-04 02:27:59 +02:00
|
|
|
.then(buf => {
|
|
|
|
localAc = new LocalAutocompleter(buf);
|
|
|
|
});
|
2021-12-27 01:16:21 +01:00
|
|
|
}
|
|
|
|
}
|
2024-06-02 18:40:09 +02:00
|
|
|
|
|
|
|
toggleSearchAutocomplete();
|
2024-08-31 16:48:30 +02:00
|
|
|
|
|
|
|
popup.onItemSelected((event: CustomEvent<TermSuggestion>) => {
|
|
|
|
if (!event.detail || !inputField) return;
|
|
|
|
|
|
|
|
const originalSuggestion = event.detail;
|
|
|
|
applySelectedValue(originalSuggestion.value);
|
|
|
|
|
|
|
|
inputField.dispatchEvent(
|
|
|
|
new CustomEvent('autocomplete', {
|
|
|
|
detail: Object.assign(
|
|
|
|
{
|
|
|
|
type: 'click',
|
|
|
|
},
|
|
|
|
originalSuggestion,
|
|
|
|
),
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
2019-10-05 02:09:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export { listenAutocomplete };
|