| 1 | /* search.js — the search box updates the list as you type. |
| 2 | * |
| 3 | * Progressive enhancement over an ordinary GET form: with scripts off the |
| 4 | * form submits and the server renders the same page. With them on, typing |
| 5 | * fetches that same URL and swaps every [data-live-region] — the results, |
| 6 | * the view tabs and the filter menus — by id. The search box itself is |
| 7 | * never replaced, so it keeps focus and the cursor stays put. |
| 8 | * |
| 9 | * All three are swapped, not just the results, because every link in the |
| 10 | * tabs and filter menus carries the current search. Swapping only the |
| 11 | * results (the first version) left those links carrying the search the |
| 12 | * page was LOADED with, so picking a filter after typing quietly threw the |
| 13 | * typed search away. |
| 14 | * |
| 15 | * Fast enough to feel instant because the server answers from an in-memory |
| 16 | * index (index.go) — a search over 5,000 cards is a fraction of a |
| 17 | * millisecond — so the cost is one round trip and a render. Keystrokes are |
| 18 | * debounced, and a request a newer keystroke overtakes is aborted, so a slow |
| 19 | * answer can never overwrite a fresher one. |
| 20 | * |
| 21 | * The URL follows along (history.replaceState), so a live search is still a |
| 22 | * link, and Back is not littered with an entry per keystroke. |
| 23 | * |
| 24 | * When the answer is not a list page — a redirect to sign-in because the |
| 25 | * session expired, or an error — the browser simply navigates to the URL, |
| 26 | * so the person sees what the server has to say. The first version |
| 27 | * swallowed that silently and the box went dead. |
| 28 | */ |
| 29 | (function () { |
| 30 | "use strict"; |
| 31 | |
| 32 | var DEBOUNCE_MS = 120; |
| 33 | |
| 34 | function init() { |
| 35 | var bar = document.querySelector("[data-live-search]"); |
| 36 | if (!bar || !window.fetch || !window.DOMParser) return; |
| 37 | var form = bar.querySelector("form"); |
| 38 | var input = form && form.querySelector('input[name="q"]'); |
| 39 | if (!input) return; |
| 40 | |
| 41 | var timer = 0; |
| 42 | var inflight = null; |
| 43 | |
| 44 | function urlFor() { |
| 45 | var params = new URLSearchParams(new FormData(form)); |
| 46 | if (!params.get("q")) params.delete("q"); |
| 47 | var qs = params.toString(); |
| 48 | return (form.getAttribute("action") || location.pathname) + (qs ? "?" + qs : ""); |
| 49 | } |
| 50 | |
| 51 | function refresh() { |
| 52 | var url = urlFor(); |
| 53 | if (inflight) inflight.abort(); |
| 54 | inflight = new AbortController(); |
| 55 | var results = document.querySelector("[data-live-results]"); |
| 56 | if (results) results.setAttribute("aria-busy", "true"); |
| 57 | fetch(url, { signal: inflight.signal, headers: { Accept: "text/html" }, credentials: "same-origin" }) |
| 58 | .then(function (r) { |
| 59 | if (!r.ok || r.redirected) { |
| 60 | location.assign(url); |
| 61 | return null; |
| 62 | } |
| 63 | return r.text(); |
| 64 | }) |
| 65 | .then(function (text) { |
| 66 | if (text === null) return; |
| 67 | var doc = new DOMParser().parseFromString(text, "text/html"); |
| 68 | var regions = document.querySelectorAll("[data-live-region][id]"); |
| 69 | var swapped = 0; |
| 70 | regions.forEach(function (el) { |
| 71 | var fresh = doc.getElementById(el.id); |
| 72 | if (fresh && fresh.hasAttribute("data-live-region")) { |
| 73 | el.innerHTML = fresh.innerHTML; |
| 74 | swapped++; |
| 75 | } |
| 76 | }); |
| 77 | if (swapped === 0) { |
| 78 | location.assign(url); // not a list page: let the browser show it |
| 79 | return; |
| 80 | } |
| 81 | history.replaceState(null, "", url); |
| 82 | }) |
| 83 | .catch(function (err) { |
| 84 | // An abort is a newer keystroke winning. Anything else: navigate, |
| 85 | // and let the server's page say what went wrong. |
| 86 | if (err && err.name === "AbortError") return; |
| 87 | location.assign(url); |
| 88 | }) |
| 89 | .finally(function () { |
| 90 | var r = document.querySelector("[data-live-results]"); |
| 91 | if (r) r.removeAttribute("aria-busy"); |
| 92 | }); |
| 93 | } |
| 94 | |
| 95 | input.addEventListener("input", function () { |
| 96 | clearTimeout(timer); |
| 97 | timer = setTimeout(refresh, DEBOUNCE_MS); |
| 98 | }); |
| 99 | |
| 100 | // Enter runs the same refresh at once instead of reloading the page; |
| 101 | // refresh itself falls back to navigating when a swap is not possible. |
| 102 | form.addEventListener("submit", function (e) { |
| 103 | e.preventDefault(); |
| 104 | clearTimeout(timer); |
| 105 | refresh(); |
| 106 | }); |
| 107 | } |
| 108 | |
| 109 | if (document.readyState === "loading") { |
| 110 | document.addEventListener("DOMContentLoaded", init); |
| 111 | } else { |
| 112 | init(); |
| 113 | } |
| 114 | })(); |
| 115 | |