blob: 9cafd83beeac2d058ee9713b024a72531a325e8a [file] [log] [blame]
gio1bf00802024-08-17 12:31:41 +04001function delaySearch(func, wait) {
2 let timeout;
3 return function (...args) {
4 clearTimeout(timeout);
5 timeout = setTimeout(() => func.apply(this, args), wait);
6 };
7}
8
9document.addEventListener("DOMContentLoaded", function () {
10 let searchRequestCount = 0;
11 const page = document.documentElement;
Davit Tabidzee38c40f2024-08-19 13:58:36 +040012 const headerHeight = parseFloat(getComputedStyle(page).getPropertyValue("--pico-header-height").replace("px", ""));
gio1bf00802024-08-17 12:31:41 +040013 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 Tabidzee38c40f2024-08-19 13:58:36 +040019 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 }
gio1bf00802024-08-17 12:31:41 +040029 function fetchAndUpdateAppList() {
30 searchRequestCount++;
31 const currentRequest = searchRequestCount;
32 const formData = new FormData(searchForm);
Davit Tabidzee38c40f2024-08-19 13:58:36 +040033 const query = formData.get("query");
34 const pageType = document.getElementById("page-type").value;
gio1bf00802024-08-17 12:31:41 +040035 const url = `/${pageType}?query=${encodeURIComponent(query)}`;
Davit Tabidzee38c40f2024-08-19 13:58:36 +040036 startSearchAnimation();
gio1bf00802024-08-17 12:31:41 +040037 fetch(url, {
Davit Tabidzee38c40f2024-08-19 13:58:36 +040038 method: "GET"
gio1bf00802024-08-17 12:31:41 +040039 })
40 .then(response => response.text())
41 .then(html => {
42 if (currentRequest !== searchRequestCount) {
43 return;
44 }
Davit Tabidzee38c40f2024-08-19 13:58:36 +040045 const tempDiv = document.createElement("div");
gio1bf00802024-08-17 12:31:41 +040046 tempDiv.innerHTML = html;
Davit Tabidzee38c40f2024-08-19 13:58:36 +040047 const newAppListHTML = tempDiv.querySelector("#app-list").innerHTML;
gio1bf00802024-08-17 12:31:41 +040048 const appListContainer = document.getElementById("app-list");
49 appListContainer.innerHTML = newAppListHTML;
50 })
Davit Tabidzee38c40f2024-08-19 13:58:36 +040051 .catch(error => {
52 console.error("Error fetching app list:", error);
53 })
54 .finally(() => {
55 stopSearchAnimation();
56 });
gio1bf00802024-08-17 12:31:41 +040057 }
58 const delayedFetchAndUpdateAppList = delaySearch(fetchAndUpdateAppList, 300);
Davit Tabidzee38c40f2024-08-19 13:58:36 +040059 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 }
gio1bf00802024-08-17 12:31:41 +040070});
71
72let prevWindowHeight = window.innerHeight;
73
74window.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});