2022-06-07 19:57:26 +03:00
|
|
|
let searchInput;
|
|
|
|
let currentFocus;
|
|
|
|
let originalSearch;
|
|
|
|
let autocompleteResults;
|
|
|
|
|
|
|
|
const handleUserInput = () => {
|
2020-05-24 23:03:11 +03:00
|
|
|
let xhrRequest = new XMLHttpRequest();
|
2020-10-29 18:09:31 +03:00
|
|
|
xhrRequest.open("POST", "autocomplete");
|
2020-05-24 23:03:11 +03:00
|
|
|
xhrRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
|
2020-08-15 20:58:16 +03:00
|
|
|
xhrRequest.onload = function () {
|
2020-05-24 23:03:11 +03:00
|
|
|
if (xhrRequest.readyState === 4 && xhrRequest.status !== 200) {
|
2020-06-06 00:24:44 +03:00
|
|
|
// Do nothing if failed to fetch autocomplete results
|
2020-05-24 23:03:11 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fill autocomplete with fetched results
|
2022-06-07 19:57:26 +03:00
|
|
|
autocompleteResults = JSON.parse(xhrRequest.responseText)[1];
|
|
|
|
updateAutocompleteList();
|
2020-05-24 23:03:11 +03:00
|
|
|
};
|
|
|
|
|
2022-06-07 19:57:26 +03:00
|
|
|
xhrRequest.send('q=' + searchInput.value);
|
2020-05-24 23:03:11 +03:00
|
|
|
};
|
|
|
|
|
2022-06-07 19:57:26 +03:00
|
|
|
const removeActive = suggestion => {
|
|
|
|
// Remove "autocomplete-active" class from previously active suggestion
|
|
|
|
for (let i = 0; i < suggestion.length; i++) {
|
|
|
|
suggestion[i].classList.remove("autocomplete-active");
|
|
|
|
}
|
|
|
|
};
|
2020-05-24 23:03:11 +03:00
|
|
|
|
2022-06-07 19:57:26 +03:00
|
|
|
const addActive = (suggestion) => {
|
|
|
|
// Handle navigation outside of suggestion list
|
|
|
|
if (!suggestion || !suggestion[currentFocus]) {
|
|
|
|
if (currentFocus >= suggestion.length) {
|
|
|
|
// Move selection back to the beginning
|
|
|
|
currentFocus = 0;
|
|
|
|
} else if (currentFocus < 0) {
|
|
|
|
// Retrieve original search and remove active suggestion selection
|
|
|
|
currentFocus = -1;
|
|
|
|
searchInput.value = originalSearch;
|
|
|
|
removeActive(suggestion);
|
|
|
|
return;
|
2020-08-15 20:58:16 +03:00
|
|
|
} else {
|
2022-06-07 19:57:26 +03:00
|
|
|
return;
|
2020-08-15 20:58:16 +03:00
|
|
|
}
|
2022-06-07 19:57:26 +03:00
|
|
|
}
|
2020-05-24 23:03:11 +03:00
|
|
|
|
2022-06-07 19:57:26 +03:00
|
|
|
removeActive(suggestion);
|
|
|
|
suggestion[currentFocus].classList.add("autocomplete-active");
|
2020-08-15 20:58:16 +03:00
|
|
|
|
2022-06-07 19:57:26 +03:00
|
|
|
// Autofill search bar with suggestion content (minus the "bang name" if using a bang operator)
|
|
|
|
let searchContent = suggestion[currentFocus].textContent;
|
|
|
|
if (searchContent.indexOf('(') > 0) {
|
|
|
|
searchInput.value = searchContent.substring(0, searchContent.indexOf('('));
|
|
|
|
} else {
|
|
|
|
searchInput.value = searchContent;
|
|
|
|
}
|
2020-10-10 22:55:14 +03:00
|
|
|
|
2022-06-07 19:57:26 +03:00
|
|
|
searchInput.focus();
|
|
|
|
};
|
2020-05-24 23:03:11 +03:00
|
|
|
|
2022-06-07 19:57:26 +03:00
|
|
|
const autocompleteInput = (e) => {
|
|
|
|
// Handle navigation between autocomplete suggestions
|
2024-04-19 23:00:12 +03:00
|
|
|
let suggestion = document.getElementById("autocomplete-list");
|
2022-06-07 19:57:26 +03:00
|
|
|
if (suggestion) suggestion = suggestion.getElementsByTagName("div");
|
|
|
|
if (e.keyCode === 40) { // down
|
|
|
|
e.preventDefault();
|
|
|
|
currentFocus++;
|
|
|
|
addActive(suggestion);
|
|
|
|
} else if (e.keyCode === 38) { //up
|
|
|
|
e.preventDefault();
|
|
|
|
currentFocus--;
|
|
|
|
addActive(suggestion);
|
|
|
|
} else if (e.keyCode === 13) { // enter
|
|
|
|
e.preventDefault();
|
|
|
|
if (currentFocus > -1) {
|
|
|
|
if (suggestion) suggestion[currentFocus].click();
|
2020-05-24 23:03:11 +03:00
|
|
|
}
|
2022-06-07 19:57:26 +03:00
|
|
|
} else {
|
|
|
|
originalSearch = searchInput.value;
|
|
|
|
}
|
|
|
|
};
|
2020-05-24 23:03:11 +03:00
|
|
|
|
2022-06-07 19:57:26 +03:00
|
|
|
const updateAutocompleteList = () => {
|
2024-04-19 23:00:12 +03:00
|
|
|
let autocompleteItem, i;
|
2022-06-07 19:57:26 +03:00
|
|
|
let val = originalSearch;
|
2024-04-19 23:00:12 +03:00
|
|
|
|
|
|
|
let autocompleteList = document.getElementById("autocomplete-list");
|
|
|
|
autocompleteList.innerHTML = "";
|
2022-06-07 19:57:26 +03:00
|
|
|
|
|
|
|
if (!val || !autocompleteResults) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
currentFocus = -1;
|
|
|
|
|
|
|
|
for (i = 0; i < autocompleteResults.length; i++) {
|
|
|
|
if (autocompleteResults[i].substr(0, val.length).toUpperCase() === val.toUpperCase()) {
|
|
|
|
autocompleteItem = document.createElement("div");
|
2024-04-19 23:00:12 +03:00
|
|
|
autocompleteItem.setAttribute("class", "autocomplete-item");
|
2022-06-07 19:57:26 +03:00
|
|
|
autocompleteItem.innerHTML = "<strong>" + autocompleteResults[i].substr(0, val.length) + "</strong>";
|
|
|
|
autocompleteItem.innerHTML += autocompleteResults[i].substr(val.length);
|
|
|
|
autocompleteItem.innerHTML += "<input type=\"hidden\" value=\"" + autocompleteResults[i] + "\">";
|
|
|
|
autocompleteItem.addEventListener("click", function () {
|
|
|
|
searchInput.value = this.getElementsByTagName("input")[0].value;
|
2024-04-19 23:00:12 +03:00
|
|
|
autocompleteList.innerHTML = "";
|
2022-06-07 19:57:26 +03:00
|
|
|
document.getElementById("search-form").submit();
|
|
|
|
});
|
|
|
|
autocompleteList.appendChild(autocompleteItem);
|
2020-05-24 23:03:11 +03:00
|
|
|
}
|
2022-06-07 19:57:26 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
document.addEventListener("DOMContentLoaded", function() {
|
2024-04-19 23:00:12 +03:00
|
|
|
let autocompleteList = document.createElement("div");
|
|
|
|
autocompleteList.setAttribute("id", "autocomplete-list");
|
|
|
|
autocompleteList.setAttribute("class", "autocomplete-items");
|
|
|
|
|
2022-06-07 19:57:26 +03:00
|
|
|
searchInput = document.getElementById("search-bar");
|
2024-04-19 23:00:12 +03:00
|
|
|
searchInput.parentNode.appendChild(autocompleteList);
|
|
|
|
|
2022-06-07 19:57:26 +03:00
|
|
|
searchInput.addEventListener("keydown", (event) => autocompleteInput(event));
|
2020-05-24 23:03:11 +03:00
|
|
|
|
|
|
|
document.addEventListener("click", function (e) {
|
2024-04-19 23:00:12 +03:00
|
|
|
autocompleteList.innerHTML = "";
|
2020-05-24 23:03:11 +03:00
|
|
|
});
|
2024-04-19 23:00:12 +03:00
|
|
|
});
|