DIK-40: переключатель языка на странице «Все заведения» #29
@ -10,6 +10,9 @@ body{font:15px/1.4 system-ui,sans-serif;margin:0;background:var(--bg);color:var(
|
||||
header{display:flex;align-items:center;gap:12px;padding:12px 16px;background:var(--accent);color:#fff}
|
||||
header h1{font-size:19px;margin:0;flex:1}
|
||||
header a{color:#fff;font-weight:600}
|
||||
.langs{display:flex;gap:4px}
|
||||
.langs button{min-width:40px;padding:6px 8px;border:1px solid #fff8;border-radius:8px;background:none;color:#fff;font:inherit;font-size:14px;font-weight:700;cursor:pointer}
|
||||
.langs button.on{background:#fff;color:var(--accent);border-color:#fff}
|
||||
main{padding:16px}
|
||||
p{color:var(--mute);margin:0 0 8px}
|
||||
.wrap{overflow-x:auto;border:1px solid var(--line);border-radius:12px;background:var(--card)}
|
||||
@ -20,7 +23,7 @@ th{position:sticky;top:0;background:var(--card);font-size:13px;color:var(--mute)
|
||||
.srcnote{font-size:12px;color:var(--mute);margin-top:4px}td a{color:var(--accent)}.web{font-size:12px;color:var(--mute);margin-top:4px}
|
||||
.msg{padding:12px;background:#fff3bf;color:#4a3b00;border-radius:12px}
|
||||
</style></head><body>
|
||||
<header><h1 data-i="baseTitle"></h1><a href="/" data-i="backHome"></a></header>
|
||||
<header><h1 data-i="baseTitle"></h1><div class="langs"><button data-l="ru" class="on" aria-pressed="true" title="Русский" aria-label="Русский" lang="ru">RU</button><button data-l="kk" title="Қазақша" aria-label="Қазақша" lang="kk" aria-pressed="false">KK</button><button data-l="en" title="English" aria-label="English" lang="en" aria-pressed="false">EN</button></div><a href="/" data-i="backHome"></a></header>
|
||||
<main>
|
||||
<p data-i="baseIntro"></p><p id="count"></p>
|
||||
<section id="sources" class="src"><h2 data-i="srcTitle"></h2><p data-i="srcIntro"></p>
|
||||
|
||||
@ -1,15 +1,11 @@
|
||||
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]));
|
||||
let t = {}, list = null, failed = false;
|
||||
const $ = (id) => document.getElementById(id);
|
||||
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) {
|
||||
@ -51,13 +47,16 @@ function valueCell(text, src, web) {
|
||||
if (web === "нет данных" && !src.own) { const n = document.createElement("div"); n.className = "srcnote"; n.textContent = fmt(t.webNotFound, { source: src.name, date: src.date }); td.append(n); }
|
||||
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; }
|
||||
function render() {
|
||||
document.title = t.baseTitle;
|
||||
document.querySelectorAll("[data-i]").forEach((e) => (e.textContent = t[e.dataset.i]));
|
||||
$("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 ptext = v.promo && (t.promoTexts?.[v.promo.text] || v.promo.text);
|
||||
// акция с прошедшим сроком не выглядит действующей; пустых ячеек нет (DIK-26)
|
||||
@ -82,6 +81,23 @@ try {
|
||||
tr.append(srcTd);
|
||||
body.append(tr);
|
||||
}
|
||||
} catch {
|
||||
const e = document.getElementById("err"); e.textContent = t.baseError; e.hidden = false;
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
@ -135,3 +135,11 @@ test("Сценарий: главная без районов, подсказка
|
||||
}
|
||||
assert.match(app, /msg\("listPreparing"\)/);
|
||||
});
|
||||
|
||||
test("Сценарий: на странице «Все заведения» есть переключатель языка и текст меняется сразу (DIK-40)", () => {
|
||||
const html = fs.readFileSync("public/base.html", "utf8");
|
||||
const base = fs.readFileSync("public/base.js", "utf8");
|
||||
for (const l of ["ru", "kk", "en"]) assert.match(html, new RegExp(`data-l="${l}"`));
|
||||
assert.match(base, /async function setLang/);
|
||||
assert.match(base, /localStorage\.setItem\("lang"/);
|
||||
});
|
||||
|
||||
Loading…
Reference in New Issue
Block a user