2022-06-07 16:57:26 +00:00
|
|
|
let searchInput;
|
|
|
|
let currentFocus;
|
|
|
|
let originalSearch;
|
|
|
|
let autocompleteResults;
|
|
|
|
|
|
|
|
const handleUserInput = () => {
|
2020-05-24 20:03:11 +00:00
|
|
|
let xhrRequest = new XMLHttpRequest();
|
2020-10-29 15:09:31 +00:00
|
|
|
xhrRequest.open("POST", "autocomplete");
|
2020-05-24 20:03:11 +00:00
|
|
|
xhrRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
|
2020-08-15 17:58:16 +00:00
|
|
|
xhrRequest.onload = function () {
|
2020-05-24 20:03:11 +00:00
|
|
|
if (xhrRequest.readyState === 4 && xhrRequest.status !== 200) {
|
2020-06-05 21:24:44 +00:00
|
|
|
// Do nothing if failed to fetch autocomplete results
|
2020-05-24 20:03:11 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fill autocomplete with fetched results
|
2022-06-07 16:57:26 +00:00
|
|
|
autocompleteResults = JSON.parse(xhrRequest.responseText)[1];
|
|
|
|
updateAutocompleteList();
|
2020-05-24 20:03:11 +00:00
|
|
|
};
|
|
|
|
|
2022-06-07 16:57:26 +00:00
|
|
|
xhrRequest.send('q=' + searchInput.value);
|
2020-05-24 20:03:11 +00:00
|
|
|
};
|
|
|
|
|
2022-06-07 16:57:26 +00:00
|
|
|
const closeAllLists = el => {
|
|
|
|
// Close all autocomplete suggestions
|
|
|
|
let suggestions = document.getElementsByClassName("autocomplete-items");
|
|
|
|
for (let i = 0; i < suggestions.length; i++) {
|
|
|
|
if (el !== suggestions[i] && el !== searchInput) {
|
|
|
|
suggestions[i].parentNode.removeChild(suggestions[i]);
|
2020-05-24 20:03:11 +00:00
|
|
|
}
|
2022-06-07 16:57:26 +00:00
|
|
|
}
|
|
|
|
};
|
2020-05-24 20:03:11 +00:00
|
|
|
|
2022-06-07 16:57:26 +00: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 20:03:11 +00:00
|
|
|
|
2022-06-07 16:57:26 +00: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 17:58:16 +00:00
|
|
|
} else {
|
2022-06-07 16:57:26 +00:00
|
|
|
return;
|
2020-08-15 17:58:16 +00:00
|
|
|
}
|
2022-06-07 16:57:26 +00:00
|
|
|
}
|
2020-05-24 20:03:11 +00:00
|
|
|
|
2022-06-07 16:57:26 +00:00
|
|
|
removeActive(suggestion);
|
|
|
|
suggestion[currentFocus].classList.add("autocomplete-active");
|
2020-08-15 17:58:16 +00:00
|
|
|
|
2022-06-07 16:57:26 +00: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 19:55:14 +00:00
|
|
|
|
2022-06-07 16:57:26 +00:00
|
|
|
searchInput.focus();
|
|
|
|
};
|
2020-05-24 20:03:11 +00:00
|
|
|
|
2022-06-07 16:57:26 +00:00
|
|
|
const autocompleteInput = (e) => {
|
|
|
|
// Handle navigation between autocomplete suggestions
|
|
|
|
let suggestion = document.getElementById(this.id + "-autocomplete-list");
|
|
|
|
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 20:03:11 +00:00
|
|
|
}
|
2022-06-07 16:57:26 +00:00
|
|
|
} else {
|
|
|
|
originalSearch = searchInput.value;
|
|
|
|
}
|
|
|
|
};
|
2020-05-24 20:03:11 +00:00
|
|
|
|
2022-06-07 16:57:26 +00:00
|
|
|
const updateAutocompleteList = () => {
|
|
|
|
let autocompleteList, autocompleteItem, i;
|
|
|
|
let val = originalSearch;
|
|
|
|
closeAllLists();
|
|
|
|
|
|
|
|
if (!val || !autocompleteResults) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
currentFocus = -1;
|
|
|
|
autocompleteList = document.createElement("div");
|
|
|
|
autocompleteList.setAttribute("id", this.id + "-autocomplete-list");
|
|
|
|
autocompleteList.setAttribute("class", "autocomplete-items");
|
|
|
|
searchInput.parentNode.appendChild(autocompleteList);
|
|
|
|
|
|
|
|
for (i = 0; i < autocompleteResults.length; i++) {
|
|
|
|
if (autocompleteResults[i].substr(0, val.length).toUpperCase() === val.toUpperCase()) {
|
|
|
|
autocompleteItem = document.createElement("div");
|
|
|
|
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;
|
|
|
|
closeAllLists();
|
|
|
|
document.getElementById("search-form").submit();
|
|
|
|
});
|
|
|
|
autocompleteList.appendChild(autocompleteItem);
|
2020-05-24 20:03:11 +00:00
|
|
|
}
|
2022-06-07 16:57:26 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
document.addEventListener("DOMContentLoaded", function() {
|
|
|
|
searchInput = document.getElementById("search-bar");
|
|
|
|
searchInput.addEventListener("keydown", (event) => autocompleteInput(event));
|
2020-05-24 20:03:11 +00:00
|
|
|
|
|
|
|
document.addEventListener("click", function (e) {
|
|
|
|
closeAllLists(e.target);
|
|
|
|
});
|
2022-06-07 16:57:26 +00:00
|
|
|
});
|