feat: add a column table layout
CI / test (push) Successful in 2m9s

This commit is contained in:
strsv committed 2026-11-06 19:03:59 +03:00
1 parent 1bd4ca8c53
commit a6db306847
7 files changed
+193 -30

No files matched your search

+103 -13
View File
@@ -285,6 +285,73 @@ function cardHtml(g, mode = "library") {
</article>`;
}
function rowHtml(g) {
const select = state.selectionMode
? `<input type="checkbox" data-select="${g.appid}"${state.selected.has(g.appid) ? " checked" : ""} aria-label="Выбрать игру">`
: "";
const genres = (g.genres || []).slice(0, 3).map(esc).join(", ") || "—";
const review = g.review != null ? `${g.review}%` : "—";
return `
<tr data-appid="${g.appid}" tabindex="0" role="button" aria-label="Открыть ${esc(g.name)}">
<td class="t-select">${select}</td>
<td class="t-name">
<div class="cover-inline">
<img data-appid="${g.appid}" loading="lazy" src="${coverUrl(g.appid)}" alt="">
<div class="fallback">${esc(initials(g.name))}</div>
</div>
<span class="t-title">${esc(g.name)}</span>
${statusChip(g.status)}
</td>
<td class="t-year">${g.year ? esc(g.year) : "—"}</td>
<td>${review}</td>
<td>${modeHours(g)}</td>
<td class="t-duration">${g.duration ? fmtDuration(g.duration) : "—"}</td>
<td class="t-genres">${genres}</td>
<td class="t-fav">${g.favorite ? "★" : ""}</td>
</tr>`;
}
function modeHours(g) {
return g.hours ? fmtHours(g.hours) : "—";
}
function tableHtml(items) {
return `
<table class="game-table">
<thead><tr>
<th class="t-select"></th>
<th>Игра</th>
<th class="t-year">Год</th>
<th>Отзывы</th>
<th>Часы</th>
<th class="t-duration">Длительность</th>
<th class="t-genres">Жанры</th>
<th class="t-fav">★</th>
</tr></thead>
<tbody>${items.map(rowHtml).join("")}</tbody>
</table>`;
}
function bindRows(root) {
root.querySelectorAll("tr[data-appid]").forEach((row) => {
if (row.dataset.bound) return;
row.dataset.bound = "1";
const activate = (event) => {
if (event && event.target instanceof Element && event.target.closest("input, label")) return;
if (state.selectionMode) toggleSelected(Number(row.dataset.appid));
else openGame(row.dataset.appid);
};
row.addEventListener("click", activate);
row.addEventListener("keydown", (event) => {
if (event.key === "Enter" || event.key === " ") {
event.preventDefault();
activate();
}
});
});
bindImages(root);
}
function skeletons(n = 12) {
return Array.from({ length: n }, () => `
<article class="card skeleton">
@@ -308,7 +375,8 @@ function attachCoverFallback(img) {
img.src = `${CDN}/${img.dataset.appid}/header.jpg`;
} else {
img.style.display = "none";
img.closest(".cover").classList.add("noimg");
const box = img.closest(".cover, .cover-inline");
if (box) box.classList.add("noimg");
}
};
img.addEventListener("error", handler);
@@ -316,7 +384,7 @@ function attachCoverFallback(img) {
}
function bindImages(root) {
root.querySelectorAll(".cover img").forEach(attachCoverFallback);
root.querySelectorAll(".cover img, .cover-inline img").forEach(attachCoverFallback);
}
function bindCards(root) {
@@ -548,6 +616,8 @@ function setSelected(appid, selected) {
if (input) input.checked = selected;
const card = document.querySelector(`#grid .card[data-appid="${appid}"]`);
if (card) card.classList.toggle("selected", selected);
const row = document.querySelector(`#grid tr[data-appid="${appid}"]`);
if (row) row.classList.toggle("selected", selected);
updateBulkBar();
}
@@ -558,7 +628,9 @@ function toggleSelected(appid) {
function clearSelection() {
state.selected.clear();
document.querySelectorAll("#grid input[data-select]").forEach((input) => { input.checked = false; });
document.querySelectorAll("#grid .card.selected").forEach((card) => card.classList.remove("selected"));
document.querySelectorAll("#grid .card.selected, #grid tr.selected").forEach((item) => {
item.classList.remove("selected");
});
updateBulkBar();
}
@@ -624,12 +696,15 @@ function filterParams() {
async function loadGames(reset = true) {
if (state.loading) return;
if (reset && state.selectionMode && state.selected.size) clearSelection();
const tableMode = grid.classList.contains("table-view");
state.loading = true;
$("#more").disabled = true;
if (reset) {
state.page = 1;
state.total = 0;
grid.innerHTML = skeletons(12);
grid.innerHTML = tableMode
? '<div class="empty">загрузка…</div>'
: skeletons(12);
}
try {
const params = filterParams();
@@ -638,9 +713,17 @@ async function loadGames(reset = true) {
if (reset) grid.innerHTML = "";
if (!data.items.length && reset) {
grid.innerHTML = '<div class="empty">ничего не нашлось — ослабь фильтры</div>';
} else if (tableMode) {
if (reset) grid.innerHTML = tableHtml(data.items);
else {
const tbody = grid.querySelector("tbody");
if (tbody) tbody.insertAdjacentHTML("beforeend", data.items.map((g) => rowHtml(g)).join(""));
}
bindRows(grid);
} else {
grid.insertAdjacentHTML("beforeend", data.items.map((g) => cardHtml(g)).join(""));
bindCards(grid);
}
grid.insertAdjacentHTML("beforeend", data.items.map((g) => cardHtml(g)).join(""));
bindCards(grid);
state.total = data.total;
$("#count").textContent = `${data.total.toLocaleString("ru-RU")} игр`;
state.page += 1;
@@ -1013,22 +1096,29 @@ grid.addEventListener("change", (event) => {
});
function applyLayout(mode) {
const list = mode === "list";
grid.classList.toggle("list-view", list);
if (!["grid", "list", "table"].includes(mode)) mode = "grid";
grid.dataset.layout = mode;
grid.classList.toggle("list-view", mode === "list");
grid.classList.toggle("table-view", mode === "table");
const button = $("#layoutToggle");
button.textContent = list ? "сетка" : "список";
button.title = list ? "Сетка карточек" : "Компактный список";
button.setAttribute("aria-pressed", String(list));
button.textContent = mode === "grid" ? "список" : mode === "list" ? "таблица" : "сетка";
button.title = mode === "grid"
? "Компактный список"
: mode === "list" ? "Таблица с колонками" : "Сетка карточек";
button.setAttribute("aria-pressed", String(mode !== "grid"));
}
$("#layoutToggle").addEventListener("click", () => {
const mode = grid.classList.contains("list-view") ? "grid" : "list";
const current = grid.dataset.layout || "grid";
const mode = current === "grid" ? "list" : current === "list" ? "table" : "grid";
applyLayout(mode);
try { localStorage.setItem(LAYOUT_KEY, mode); } catch (_) {}
loadGames(true);
});
try {
applyLayout(localStorage.getItem(LAYOUT_KEY) === "list" ? "list" : "grid");
const saved = localStorage.getItem(LAYOUT_KEY);
applyLayout(["list", "table"].includes(saved) ? saved : "grid");
} catch (_) {
applyLayout("grid");
}
+67
View File
@@ -477,6 +477,66 @@ html[data-theme="light"] .select-box {
.card-status { margin-top: 6px; }
.grid.list-view .select-box { right: 6px; bottom: 6px; width: 24px; height: 24px; }
.grid.table-view { display: block; }
.game-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.game-table th {
text-align: left;
padding: 8px 10px;
border-bottom: 1px solid var(--border);
color: var(--muted);
font-size: 11px;
text-transform: uppercase;
letter-spacing: .06em;
font-weight: 600;
white-space: nowrap;
}
.game-table td {
padding: 6px 10px;
border-bottom: 1px solid var(--border);
vertical-align: middle;
}
.game-table tbody tr { cursor: pointer; }
.game-table tbody tr:hover { background: rgba(255, 255, 255, .03); }
.game-table tbody tr.selected { background: rgba(243, 180, 92, .08); }
.game-table tbody tr:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
.game-table .t-select { width: 30px; padding-right: 0; }
.game-table .t-select input { width: 15px; height: 15px; accent-color: var(--accent); cursor: pointer; }
.game-table .t-name { min-width: 210px; }
.game-table .t-title { font-weight: 600; vertical-align: middle; }
.game-table .t-name .status-chip { margin-left: 8px; vertical-align: middle; }
.game-table .t-genres { color: var(--muted); font-size: 11.5px; }
.game-table .t-fav { color: var(--accent); width: 26px; text-align: center; }
.game-table .t-year, .game-table .t-duration { white-space: nowrap; }
.game-table td:nth-child(5) { white-space: nowrap; }
.cover-inline {
display: inline-block;
position: relative;
width: 34px;
aspect-ratio: 2 / 3;
margin-right: 10px;
vertical-align: middle;
border-radius: 5px;
overflow: hidden;
background: linear-gradient(145deg, #1b2436, #0d121b);
}
.cover-inline img { width: 100%; height: 100%; object-fit: cover; display: block; }
.cover-inline .fallback {
display: none;
position: absolute;
inset: 0;
place-items: center;
padding: 2px;
text-align: center;
font-size: 9px;
font-weight: 700;
color: rgba(255, 255, 255, .82);
background: linear-gradient(145deg, rgba(243, 180, 92, .25), rgba(94, 234, 212, .12));
}
.cover-inline.noimg .fallback { display: grid; }
html[data-theme="light"] .cover-inline { background: #e9e2d5; }
html[data-theme="light"] .cover-inline .fallback { color: rgba(23, 32, 51, .72); }
html[data-theme="light"] .game-table tbody tr:hover { background: rgba(23, 32, 51, .035); }
.card {
display: flex;
flex-direction: column;
@@ -969,4 +1029,11 @@ html[data-theme="light"] ::-webkit-scrollbar-thumb { background: #c9c0ad; }
.toolbar input { flex: 1 1 150px; }
.toolbar select { flex: 1 1 140px; min-width: 140px; }
.stat { padding: 4px 9px; font-size: 12px; }
.game-table { font-size: 12px; }
.game-table th, .game-table td { padding: 6px 7px; }
.game-table .t-year,
.game-table .t-genres,
.game-table .t-duration { display: none; }
.game-table .t-name { min-width: 0; }
.cover-inline { width: 28px; margin-right: 7px; }
}