70 lines
3.3 KiB
JavaScript
70 lines
3.3 KiB
JavaScript
let t = {}, pos = null;
|
|
const $ = (id) => document.getElementById(id);
|
|
const fmt = (s, o) => s.replace(/\{(\w+)\}/g, (_, k) => o[k]);
|
|
const dist = (m) => (m < 1000 ? m + " " + t.m : (m / 1000).toFixed(1) + " " + t.km);
|
|
|
|
async function setLang(l) {
|
|
t = await (await fetch(`i18n/${l}.json`)).json();
|
|
document.documentElement.lang = l;
|
|
document.querySelectorAll("[data-i]").forEach((e) => (e.textContent = t[e.dataset.i]));
|
|
document.querySelectorAll("[data-ph]").forEach((e) => (e.placeholder = t[e.dataset.ph]));
|
|
document.title = t.title;
|
|
document.querySelectorAll("[data-l]").forEach((b) => b.classList.toggle("on", b.dataset.l === l));
|
|
if (pos) load();
|
|
}
|
|
|
|
function msg(key) { $("out").innerHTML = `<div class="msg">${t[key]}</div>`; }
|
|
|
|
async function load() {
|
|
const r = await fetch(`/api/venues?lat=${pos.lat}&lng=${pos.lng}&only=${$("only").checked ? 1 : 0}`);
|
|
if (!r.ok) return msg("error");
|
|
const list = await r.json();
|
|
if (!list.length) return msg($("only").checked ? "noOffers" : "noneNearby");
|
|
const out = $("out"); out.textContent = "";
|
|
for (const v of list) {
|
|
const c = document.createElement("div"); c.className = "card";
|
|
const top = document.createElement("div"); top.className = "top";
|
|
const h = document.createElement("h2"); h.textContent = v.name;
|
|
const d = document.createElement("span"); d.className = "dist"; d.textContent = dist(v.distance);
|
|
top.append(h, d);
|
|
const m = document.createElement("div"); m.className = "meta"; m.textContent = v.address;
|
|
c.append(top, m);
|
|
if (v.lunch) c.append(tag(fmt(t.lunch, { price: v.lunch.price, from: v.lunch.from, to: v.lunch.to }), "lunch"));
|
|
if (v.promo) c.append(tag(fmt(t.promo, { text: v.promo.text, until: v.promo.until }), "promo"));
|
|
out.append(c);
|
|
}
|
|
}
|
|
function tag(s, cls) { const d = document.createElement("span"); d.className = "badge " + cls; d.textContent = s; return d; }
|
|
|
|
function showManual() { $("manual").hidden = false; }
|
|
|
|
$("geo").onclick = () => {
|
|
if (!navigator.geolocation) { showManual(); return msg("geoDenied"); }
|
|
navigator.geolocation.getCurrentPosition(
|
|
(p) => { pos = { lat: p.coords.latitude, lng: p.coords.longitude }; load(); },
|
|
() => { showManual(); msg("geoDenied"); });
|
|
};
|
|
$("district").onchange = (e) => { const d = districts.find((x) => x.id === e.target.value); if (d) { pos = d; load(); } };
|
|
$("addrBtn").onclick = async () => {
|
|
const r = await fetch("/api/locate?q=" + encodeURIComponent($("addr").value));
|
|
if (!r.ok) return msg("addrNotFound");
|
|
pos = await r.json(); load();
|
|
};
|
|
$("only").onchange = () => pos && load();
|
|
const menu = $("menu"), veil = $("veil");
|
|
const toggle = (open) => { menu.classList.toggle("open", open); veil.hidden = !open; };
|
|
$("burger").onclick = () => toggle(!menu.classList.contains("open"));
|
|
veil.onclick = () => toggle(false);
|
|
function route() {
|
|
const id = location.hash === "#about" ? "about" : "nearby";
|
|
document.querySelectorAll("section").forEach((e) => e.classList.toggle("on", e.id === id));
|
|
toggle(false);
|
|
}
|
|
window.onhashchange = route;
|
|
document.querySelectorAll("[data-l]").forEach((b) => (b.onclick = () => { setLang(b.dataset.l); toggle(false); }));
|
|
|
|
const districts = await (await fetch("/api/districts")).json();
|
|
$("district").innerHTML = "<option value=''>—</option>" + districts.map((d) => `<option value="${d.id}">${d.name}</option>`).join("");
|
|
await setLang("ru");
|
|
route();
|