Skip to content

Commit e372646

Browse files
bazilinskyyclaude
andcommitted
Add-video form: filter the locality list in the page, at most 100 shown, the per-query address as fallback
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent d8c38a4 commit e372646

1 file changed

Lines changed: 83 additions & 59 deletions

File tree

‎templates/add_video.html‎

Lines changed: 83 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -814,74 +814,98 @@ <h3>{{ message | safe }}</h3>
814814
const input = document.getElementById("locality");
815815
const box = document.getElementById("locality-suggestions");
816816

817-
let reqId = 0;
817+
// every locality at once (cached by the browser until the mapping changes), filtered here as one types: a request
818+
// per key through the tunnel was slow (user, 2026-11-28); the old per-query address is the fallback
819+
const SHOWN = 100;
820+
let ALL = null, loading = null, reqId = 0;
821+
822+
function loadAll() {
823+
if (!loading) {
824+
loading = fetch("/autocomplete/all?v={{ locality_v }}")
825+
.then(r => r.ok ? r.json() : Promise.reject(new Error(`HTTP ${r.status}`)))
826+
.then(rows => {
827+
ALL = rows.map(([locality, state, country, iso3]) => ({
828+
locality, state, country, iso3, name: locality.toLowerCase(),
829+
label: state ? `${locality}, ${state}, ${country} (${iso3})` : `${locality}, ${country} (${iso3})`
830+
}));
831+
})
832+
.catch(err => { console.error("Locality list:", err); loading = null; });
833+
}
834+
return loading;
835+
}
836+
837+
function matching(q) {
838+
const hits = ALL.filter(e => e.name.includes(q));
839+
hits.sort((a, b) => (!a.name.startsWith(q) - !b.name.startsWith(q)) || (a.name < b.name ? -1 : a.name > b.name));
840+
return hits;
841+
}
818842

819-
input.addEventListener("input", async () => {
820-
const q = input.value.trim();
821-
// invalidates any in-flight request, so only the newest one renders
822-
const myReq = ++reqId;
843+
function hideSuggestions() {
823844
box.innerHTML = "";
824845
box.classList.remove("has-items");
846+
}
825847

826-
if (q.length < 2) return;
827-
828-
try {
829-
const res = await fetch(`/autocomplete/localities?q=${encodeURIComponent(q)}`);
830-
const localities = await res.json();
831-
832-
if (myReq !== reqId) return;
833-
834-
if (localities.length === 0) return;
835-
836-
const seen = new Set();
837-
838-
localities.forEach(item => {
839-
const key = [
840-
item.locality?.toLowerCase(),
841-
item.state?.toLowerCase() || "",
842-
item.country?.toLowerCase()
843-
].join("|");
844-
845-
if (seen.has(key)) return;
846-
seen.add(key);
847-
848-
const div = document.createElement("div");
849-
div.textContent = item.label;
850-
851-
div.onclick = () => {
852-
// Always put only the locality name in the locality field
853-
input.value = item.locality;
854-
855-
// Put state (if any) into the separate state field
856-
const stateInput = document.getElementById("state");
857-
if (stateInput) {
858-
stateInput.value = item.state || ""; // use || instead of ternary for clarity
859-
}
848+
function showSuggestions(localities) {
849+
hideSuggestions();
850+
const seen = new Set();
851+
const frag = document.createDocumentFragment();
852+
for (const item of localities) {
853+
const key = [item.locality?.toLowerCase(), item.state?.toLowerCase() || "", item.country?.toLowerCase()].join("|");
854+
if (seen.has(key)) continue;
855+
seen.add(key);
856+
if (seen.size > SHOWN) break;
857+
858+
const div = document.createElement("div");
859+
div.textContent = item.label;
860+
div.onclick = () => {
861+
// Always put only the locality name in the locality field
862+
input.value = item.locality;
863+
864+
// Put state (if any) into the separate state field
865+
const stateInput = document.getElementById("state");
866+
if (stateInput) {
867+
stateInput.value = item.state || "";
868+
}
860869

861-
// Set country dropdown
862-
const countrySelect = document.getElementById("country");
863-
if (countrySelect) {
864-
for (const option of countrySelect.options) {
865-
if (option.value === item.country) {
866-
option.selected = true;
867-
break;
868-
}
870+
// Set country dropdown
871+
const countrySelect = document.getElementById("country");
872+
if (countrySelect) {
873+
for (const option of countrySelect.options) {
874+
if (option.value === item.country) {
875+
option.selected = true;
876+
break;
869877
}
870878
}
871-
872-
// Hide suggestions
873-
box.innerHTML = "";
874-
box.classList.remove("has-items");
875-
};
876-
877-
box.appendChild(div);
878-
});
879-
879+
}
880+
hideSuggestions();
881+
};
882+
frag.appendChild(div);
883+
}
884+
if (seen.size) {
885+
box.appendChild(frag);
880886
box.classList.add("has-items");
881-
} catch (err) {
882-
console.error("Autocomplete error:", err);
883887
}
884-
});
888+
}
889+
890+
if (input) {
891+
loadAll(); // at page load, so the list is here before the first key
892+
input.addEventListener("focus", loadAll);
893+
input.addEventListener("input", async () => {
894+
const q = input.value.trim().toLowerCase();
895+
const myReq = ++reqId; // only the newest key's suggestions are shown
896+
if (q.length < 2) return hideSuggestions();
897+
if (!ALL) await loadAll();
898+
if (myReq !== reqId) return;
899+
if (ALL) return showSuggestions(matching(q));
900+
try { // the list could not be loaded: ask per query as before
901+
const res = await fetch(`/autocomplete/localities?q=${encodeURIComponent(q)}`);
902+
const localities = await res.json();
903+
if (myReq === reqId) showSuggestions(localities);
904+
} catch (err) {
905+
console.error("Autocomplete error:", err);
906+
}
907+
});
908+
}
885909

886910
// Close when clicking outside
887911
document.addEventListener("click", e => {

0 commit comments

Comments
 (0)