2021-03-07 22:04:05 +03:00
|
|
|
document.addEventListener("DOMContentLoaded", () => {
|
2022-08-04 02:54:10 +03:00
|
|
|
const advSearchToggle = document.getElementById("adv-search-toggle");
|
|
|
|
const advSearchDiv = document.getElementById("adv-search-div");
|
2021-03-07 22:04:05 +03:00
|
|
|
const searchBar = document.getElementById("search-bar");
|
2022-08-01 23:32:24 +03:00
|
|
|
const countrySelect = document.getElementById("result-country");
|
2022-12-21 23:24:27 +03:00
|
|
|
const timePeriodSelect = document.getElementById("result-time-period");
|
2022-06-10 01:40:49 +03:00
|
|
|
const arrowKeys = [37, 38, 39, 40];
|
|
|
|
let searchValue = searchBar.value;
|
2021-03-07 22:04:05 +03:00
|
|
|
|
2022-08-01 23:32:24 +03:00
|
|
|
countrySelect.onchange = () => {
|
|
|
|
let str = window.location.href;
|
2022-08-01 23:35:43 +03:00
|
|
|
n = str.lastIndexOf("/search");
|
2022-08-01 23:32:24 +03:00
|
|
|
if (n > 0) {
|
2022-12-21 23:24:27 +03:00
|
|
|
str = str.substring(0, n) + `/search?q=${searchBar.value}`;
|
|
|
|
str = tackOnParams(str);
|
2022-08-01 23:32:24 +03:00
|
|
|
window.location.href = str;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-21 23:24:27 +03:00
|
|
|
timePeriodSelect.onchange = () => {
|
|
|
|
let str = window.location.href;
|
|
|
|
n = str.lastIndexOf("/search");
|
|
|
|
if (n > 0) {
|
|
|
|
str = str.substring(0, n) + `/search?q=${searchBar.value}`;
|
|
|
|
str = tackOnParams(str);
|
|
|
|
window.location.href = str;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function tackOnParams(str) {
|
|
|
|
if (timePeriodSelect.value != "") {
|
|
|
|
str = str + `&tbs=${timePeriodSelect.value}`;
|
|
|
|
}
|
|
|
|
if (countrySelect.value != "") {
|
|
|
|
str = str + `&country=${countrySelect.value}`;
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2022-08-04 02:54:10 +03:00
|
|
|
const toggleAdvancedSearch = on => {
|
|
|
|
if (on) {
|
|
|
|
advSearchDiv.style.maxHeight = "70px";
|
|
|
|
} else {
|
|
|
|
advSearchDiv.style.maxHeight = "0px";
|
|
|
|
}
|
|
|
|
localStorage.advSearchToggled = on;
|
|
|
|
}
|
|
|
|
|
2022-08-04 02:59:08 +03:00
|
|
|
try {
|
|
|
|
toggleAdvancedSearch(JSON.parse(localStorage.advSearchToggled));
|
|
|
|
} catch (error) {
|
|
|
|
console.warn("Did not recover advanced search toggle state");
|
|
|
|
}
|
|
|
|
|
2022-08-04 02:54:10 +03:00
|
|
|
advSearchToggle.onclick = () => {
|
|
|
|
toggleAdvancedSearch(advSearchToggle.checked);
|
|
|
|
}
|
|
|
|
|
2022-06-10 01:40:49 +03:00
|
|
|
searchBar.addEventListener("keyup", function(event) {
|
|
|
|
if (event.keyCode === 13) {
|
2021-03-07 22:04:05 +03:00
|
|
|
document.getElementById("search-form").submit();
|
2022-06-10 01:40:49 +03:00
|
|
|
} else if (searchBar.value !== searchValue && !arrowKeys.includes(event.keyCode)) {
|
|
|
|
searchValue = searchBar.value;
|
|
|
|
handleUserInput();
|
2021-03-07 22:04:05 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|