dika/public/base.js
2026-11-16 16:43:17 +05:00

59 lines
3.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const LANGS = ["ru", "kk", "en"];
const saved = () => { try { return localStorage.getItem("lang"); } catch { return null; } };
const lang = LANGS.includes(saved()) ? saved() : "ru";
const t = await (await fetch(`i18n/${lang}.json`)).json();
const fmt = (s, o) => s.replace(/\{(\w+)\}/g, (_, k) => o[k] ?? ""); // без undefined на экране
const date = (iso) => (iso ? iso.split("-").reverse().join(".") : "");
const today = new Date(Date.now() + 5 * 3600000).toISOString().slice(0, 10); // Астана UTC+5
document.documentElement.lang = lang;
document.title = t.baseTitle;
document.querySelectorAll("[data-i]").forEach((e) => (e.textContent = t[e.dataset.i]));
const COLS = ["colName", "colAddress", "colCoords", "colLunch", "colPromo", "colChecked", "colDescription", "colPhoto", "colSource"];
document.getElementById("head").replaceChildren(...COLS.map((k) => { const th = document.createElement("th"); th.textContent = t[k]; return th; }));
// Ячейка с текстом и «Источник: …»; адрес в источнике («2ГИС — https://…») становится ссылкой
const safe = (u) => /^https?:\/\//.test(u);
function cell(text, source, photoUrl) {
const td = document.createElement("td");
if (photoUrl && safe(photoUrl)) { const a = document.createElement("a"); a.href = photoUrl; a.textContent = text; a.target = "_blank"; a.rel = "noopener noreferrer"; td.append(a); }
else td.append(text);
if (source) {
const d = document.createElement("div"); d.className = "srcnote";
const m = /^(.*?)\s*(https?:\/\/\S+)$/.exec(source);
const name = (m ? m[1] : source).replace(/[\s—-]+$/, "");
d.append(t.srcLabel + " ");
if (m) { const a = document.createElement("a"); a.href = m[2]; a.textContent = name === "владелец" ? t.srcOwner : name || m[2]; a.target = "_blank"; a.rel = "noopener noreferrer"; d.append(a); }
else d.append(name === "владелец" ? t.srcOwner : name);
td.append(d);
}
return td;
}
try {
const r = await fetch("/api/base");
if (!r.ok) throw new Error();
const list = await r.json();
document.getElementById("count").textContent = fmt(t.baseCount, { n: list.length });
const body = document.getElementById("rows");
if (!list.length) { const e = document.getElementById("err"); e.textContent = t.listPreparing; e.hidden = false; }
for (const v of list) {
const ptext = v.promo && (t.promoTexts?.[v.promo.text] || v.promo.text);
// акция с прошедшим сроком не выглядит действующей; пустых ячеек нет (DIK-26)
const promo = !v.promo ? t.noPromo : v.promo.until && v.promo.until < today ? fmt(t.basePromoEnded, { text: ptext, until: date(v.promo.until) })
: ptext + (v.promo.until ? ` (${date(v.promo.until)})` : "");
const lunch = v.lunch ? fmt(t.baseLunch, v.lunch) : t.noLunch;
const tr = document.createElement("tr");
for (const s of [v.name, v.address, `${v.lat}, ${v.lng}`, lunch, promo, v.checked ? date(v.checked) : t.baseNotChecked]) {
const td = document.createElement("td"); td.textContent = s; tr.append(td);
}
tr.append(cell(v.description || t.noDescriptionBase, v.description && v.descriptionSource), cell(v.photo ? t.photoOf : t.noPhotoBase, v.photo && v.photoSource, v.photo));
const src = document.createElement("td");
if (/^https?:\/\//.test(v.link || "")) { const a = document.createElement("a"); a.href = v.link; a.target = "_blank"; a.rel = "noopener noreferrer"; a.textContent = v.source || t.baseNoSource; src.append(a); }
else src.textContent = v.source || t.baseNoSource;
const web = document.createElement("div"); web.className = "web"; web.textContent = t[v.web === "обновлено" ? "webUpdated" : v.web === "нет данных" ? "webNone" : "webNot"];
src.append(web);
tr.append(src);
body.append(tr);
}
} catch {
const e = document.getElementById("err"); e.textContent = t.baseError; e.hidden = false;
}