49 lines
2.3 KiB
JavaScript
49 lines
2.3 KiB
JavaScript
const LANGS = ["ru", "kk", "en"];
|
|
const saved = () => { try { return localStorage.getItem("lang"); } catch { return null; } };
|
|
const fmt = (s, o) => s.replace(/\{(\w+)\}/g, (_, k) => o[k]);
|
|
const date = (iso) => (iso ? iso.split("-").reverse().join(".") : "");
|
|
let t = {}, list = null, failed = false;
|
|
const $ = (id) => document.getElementById(id);
|
|
const COLS = ["colName", "colDistrict", "colAddress", "colCoords", "colLunch", "colPromo", "colChecked"];
|
|
|
|
function render() {
|
|
document.title = t.baseTitle;
|
|
document.querySelectorAll("[data-i]").forEach((e) => (e.textContent = t[e.dataset.i]));
|
|
document.querySelectorAll("[data-al]").forEach((e) => e.setAttribute("aria-label", t[e.dataset.al]));
|
|
$("head").replaceChildren(...COLS.map((k) => { const th = document.createElement("th"); th.textContent = t[k]; return th; }));
|
|
const body = $("rows"), e = $("err");
|
|
body.replaceChildren(); e.hidden = true; $("count").textContent = "";
|
|
if (failed) { e.textContent = t.baseError; e.hidden = false; return; }
|
|
if (!list) return;
|
|
$("count").textContent = fmt(t.baseCount, { n: list.length });
|
|
if (!list.length) { e.textContent = t.listPreparing; e.hidden = false; }
|
|
for (const v of list) {
|
|
const promo = v.promo ? (t.promoTexts?.[v.promo.text] || v.promo.text) + (v.promo.until ? ` (${date(v.promo.until)})` : "") : t.noPromo;
|
|
const lunch = v.lunch ? fmt(t.baseLunch, v.lunch) : t.noLunch;
|
|
const tr = document.createElement("tr");
|
|
for (const s of [v.name, t["district_" + v.district] || v.district, v.address, `${v.lat}, ${v.lng}`, lunch, promo, date(v.checked)]) {
|
|
const td = document.createElement("td"); td.textContent = s; tr.append(td);
|
|
}
|
|
body.append(tr);
|
|
}
|
|
}
|
|
|
|
async function setLang(l) {
|
|
try { localStorage.setItem("lang", l); } catch {}
|
|
t = await (await fetch(`i18n/${l}.json`)).json();
|
|
document.documentElement.lang = l;
|
|
document.querySelectorAll("[data-l]").forEach((b) => {
|
|
b.classList.toggle("on", b.dataset.l === l); b.setAttribute("aria-pressed", String(b.dataset.l === l));
|
|
});
|
|
render();
|
|
}
|
|
document.querySelectorAll("[data-l]").forEach((b) => (b.onclick = () => setLang(b.dataset.l)));
|
|
|
|
await setLang(LANGS.includes(saved()) ? saved() : "ru");
|
|
try {
|
|
const r = await fetch("/api/base");
|
|
if (!r.ok) throw new Error();
|
|
list = await r.json();
|
|
} catch { failed = true; }
|
|
render();
|