| gio | 1bf0080 | 2024-08-17 12:31:41 +0400 | [diff] [blame] | 1 | function delaySearch(func, wait) { |
| 2 | let timeout; |
| 3 | return function (...args) { |
| 4 | clearTimeout(timeout); |
| 5 | timeout = setTimeout(() => func.apply(this, args), wait); |
| 6 | }; |
| 7 | } |
| 8 | |
| 9 | document.addEventListener("DOMContentLoaded", function () { |
| 10 | let searchRequestCount = 0; |
| 11 | const page = document.documentElement; |
| Davit Tabidze | e38c40f | 2024-08-19 13:58:36 +0400 | [diff] [blame] | 12 | const headerHeight = parseFloat(getComputedStyle(page).getPropertyValue("--pico-header-height").replace("px", "")); |
| gio | 1bf0080 | 2024-08-17 12:31:41 +0400 | [diff] [blame] | 13 | const nav = document.getElementById("menu"); |
| 14 | const windowHeight = window.innerHeight - headerHeight; |
| 15 | nav.style.setProperty("--max-height", `${windowHeight}px`); |
| 16 | const menu = document.getElementById("menu-nav"); |
| 17 | const menuHeight = parseFloat(getComputedStyle(document.getElementById('menu-nav')).height.replace("px", "")) + 15; |
| 18 | menu.style.setProperty("height", `${menuHeight}px`); |
| Davit Tabidze | e38c40f | 2024-08-19 13:58:36 +0400 | [diff] [blame] | 19 | const searchForm = document.getElementById("search-form"); |
| 20 | const searchInput = document.getElementById("search-input"); |
| 21 | function startSearchAnimation() { |
| 22 | searchInput.classList.remove("search-icon"); |
| 23 | searchInput.classList.add("search-progress-icon"); |
| 24 | } |
| 25 | function stopSearchAnimation() { |
| 26 | searchInput.classList.remove("search-progress-icon"); |
| 27 | searchInput.classList.add("search-icon"); |
| 28 | } |
| gio | 1bf0080 | 2024-08-17 12:31:41 +0400 | [diff] [blame] | 29 | function fetchAndUpdateAppList() { |
| 30 | searchRequestCount++; |
| 31 | const currentRequest = searchRequestCount; |
| 32 | const formData = new FormData(searchForm); |
| Davit Tabidze | e38c40f | 2024-08-19 13:58:36 +0400 | [diff] [blame] | 33 | const query = formData.get("query"); |
| 34 | const pageType = document.getElementById("page-type").value; |
| gio | 1bf0080 | 2024-08-17 12:31:41 +0400 | [diff] [blame] | 35 | const url = `/${pageType}?query=${encodeURIComponent(query)}`; |
| Davit Tabidze | e38c40f | 2024-08-19 13:58:36 +0400 | [diff] [blame] | 36 | startSearchAnimation(); |
| gio | 1bf0080 | 2024-08-17 12:31:41 +0400 | [diff] [blame] | 37 | fetch(url, { |
| Davit Tabidze | e38c40f | 2024-08-19 13:58:36 +0400 | [diff] [blame] | 38 | method: "GET" |
| gio | 1bf0080 | 2024-08-17 12:31:41 +0400 | [diff] [blame] | 39 | }) |
| 40 | .then(response => response.text()) |
| 41 | .then(html => { |
| 42 | if (currentRequest !== searchRequestCount) { |
| 43 | return; |
| 44 | } |
| Davit Tabidze | e38c40f | 2024-08-19 13:58:36 +0400 | [diff] [blame] | 45 | const tempDiv = document.createElement("div"); |
| gio | 1bf0080 | 2024-08-17 12:31:41 +0400 | [diff] [blame] | 46 | tempDiv.innerHTML = html; |
| Davit Tabidze | e38c40f | 2024-08-19 13:58:36 +0400 | [diff] [blame] | 47 | const newAppListHTML = tempDiv.querySelector("#app-list").innerHTML; |
| gio | 1bf0080 | 2024-08-17 12:31:41 +0400 | [diff] [blame] | 48 | const appListContainer = document.getElementById("app-list"); |
| 49 | appListContainer.innerHTML = newAppListHTML; |
| 50 | }) |
| Davit Tabidze | e38c40f | 2024-08-19 13:58:36 +0400 | [diff] [blame] | 51 | .catch(error => { |
| 52 | console.error("Error fetching app list:", error); |
| 53 | }) |
| 54 | .finally(() => { |
| 55 | stopSearchAnimation(); |
| 56 | }); |
| gio | 1bf0080 | 2024-08-17 12:31:41 +0400 | [diff] [blame] | 57 | } |
| 58 | const delayedFetchAndUpdateAppList = delaySearch(fetchAndUpdateAppList, 300); |
| Davit Tabidze | e38c40f | 2024-08-19 13:58:36 +0400 | [diff] [blame] | 59 | if (searchForm) { |
| 60 | searchForm.addEventListener("submit", (event) => { |
| 61 | event.preventDefault(); |
| 62 | fetchAndUpdateAppList(); |
| 63 | }); |
| 64 | } |
| 65 | if (searchInput) { |
| 66 | searchInput.addEventListener("input", () => { |
| 67 | delayedFetchAndUpdateAppList(); |
| 68 | }); |
| 69 | } |
| gio | 1bf0080 | 2024-08-17 12:31:41 +0400 | [diff] [blame] | 70 | }); |
| 71 | |
| 72 | let prevWindowHeight = window.innerHeight; |
| 73 | |
| 74 | window.addEventListener("resize", function () { |
| 75 | const nav = document.getElementById("menu"); |
| 76 | const windowHeight = window.innerHeight; |
| 77 | const heightDiff = prevWindowHeight - windowHeight; |
| 78 | const currentMaxHeight = parseFloat(nav.style.getPropertyValue("--max-height").replace("px", "")); |
| 79 | if (!isNaN(currentMaxHeight)) { |
| 80 | const newMaxHeight = currentMaxHeight - heightDiff; |
| 81 | nav.style.setProperty("--max-height", `${newMaxHeight}px`); |
| 82 | } |
| 83 | prevWindowHeight = windowHeight; |
| 84 | }); |