about summary refs log tree commit diff stats
path: root/frontend/static/search.js
diff options
context:
space:
mode:
authorAlan Pearce2024-05-12 14:55:02 +0200
committerAlan Pearce2024-05-12 15:16:34 +0200
commit42bcd10f010f884bddb0142c31b526faace24186 (patch)
treedc78a09af2e34f84ded4cfbfbd1b0f1ba5b15266 /frontend/static/search.js
parentf769decb676f26210bd6884d33e84ada5898b17c (diff)
downloadsearchix-42bcd10f010f884bddb0142c31b526faace24186.tar.lz
searchix-42bcd10f010f884bddb0142c31b526faace24186.tar.zst
searchix-42bcd10f010f884bddb0142c31b526faace24186.zip
feat: clicking another source re-uses query
Diffstat (limited to 'frontend/static/search.js')
-rw-r--r--frontend/static/search.js18
1 files changed, 17 insertions, 1 deletions
diff --git a/frontend/static/search.js b/frontend/static/search.js
index cadae32..358de13 100644
--- a/frontend/static/search.js
+++ b/frontend/static/search.js
@@ -1,4 +1,6 @@
 const search = document.getElementById("search");
+const nav = document.querySelectorAll("body > header > nav")[0];
+const queryInput = document.getElementById("query");
 let results = document.getElementById("results");
 let pagination = document.getElementById("pagination");
 
@@ -91,6 +93,20 @@ async function getResults(url) {
   }
 }
 
+queryInput.addEventListener("input", function (ev) {
+  for (const el of nav.children) {
+    if (el.nodeName === "A") {
+      const url = new URL(el.href);
+      if (ev.target.value) {
+        url.searchParams.set("query", ev.target.value);
+      } else {
+        url.searchParams.delete("query");
+      }
+      el.href = url.toString();
+    }
+  }
+});
+
 search.addEventListener("submit", function (ev) {
   const url = new URL(this.action);
   const formData = new FormData(this);
@@ -119,7 +135,7 @@ addEventListener("popstate", function (ev) {
   if (ev.state != null) {
     url = new URL(ev.state.url);
     if (!url.pathname.endsWith("/search") && ev.state.fragment !== null) {
-      document.getElementById("query").value = ev.state.input;
+      queryInput.value = ev.state.input;
       renderFragmentHTML(ev.state.fragment);
       return;
     }