This commit is contained in:
1 parent
17e5805b2e
commit
c811107776
11 files changed
+253
-12
No files matched your search
+92
-4
@@ -28,6 +28,8 @@ const state = {
|
||||
loading: false,
|
||||
total: 0,
|
||||
meta: null,
|
||||
selectionMode: false,
|
||||
selected: new Set(),
|
||||
};
|
||||
|
||||
const $ = (sel) => document.querySelector(sel);
|
||||
@@ -230,6 +232,9 @@ function cardHtml(g, mode = "library") {
|
||||
? `<span class="badge score">${g.score}</span>`
|
||||
: (g.review != null ? `<span class="badge review ${g.review < 85 ? "warn" : ""}">${g.review}%</span>` : "");
|
||||
const installed = g.installed ? `<span class="badge installed" title="установлена">●</span>` : "";
|
||||
const selectBox = mode === "library" && state.selectionMode
|
||||
? `<label class="select-box" title="Выбрать игру"><input type="checkbox" data-select="${g.appid}"${state.selected.has(g.appid) ? " checked" : ""} aria-label="Выбрать игру"></label>`
|
||||
: "";
|
||||
const genres = (g.genres || []).slice(0, 3).map((x) => `<span class="genre">${esc(x)}</span>`).join("");
|
||||
const meta = [
|
||||
g.year ? esc(g.year) : null,
|
||||
@@ -248,7 +253,7 @@ function cardHtml(g, mode = "library") {
|
||||
<div class="cover">
|
||||
<img data-appid="${g.appid}" loading="lazy" src="${coverUrl(g.appid)}" alt="">
|
||||
<div class="fallback">${esc(initials(g.name))}</div>
|
||||
${review}${installed}
|
||||
${review}${installed}${selectBox}
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<h3 class="card-title">${esc(g.name)}</h3>
|
||||
@@ -299,11 +304,18 @@ function bindCards(root) {
|
||||
root.querySelectorAll(".card[data-appid], .mini[data-appid]").forEach((card) => {
|
||||
if (card.dataset.bound) return;
|
||||
card.dataset.bound = "1";
|
||||
const open = () => openGame(card.dataset.appid);
|
||||
card.addEventListener("click", open);
|
||||
const activate = (event) => {
|
||||
if (state.selectionMode && card.closest("#grid")) {
|
||||
if (event && event.target instanceof Element && event.target.closest(".select-box")) return;
|
||||
toggleSelected(Number(card.dataset.appid));
|
||||
return;
|
||||
}
|
||||
openGame(card.dataset.appid);
|
||||
};
|
||||
card.addEventListener("click", activate);
|
||||
if (card.tagName !== "BUTTON") {
|
||||
card.addEventListener("keydown", (e) => {
|
||||
if (e.key === "Enter" || e.key === " ") { e.preventDefault(); open(); }
|
||||
if (e.key === "Enter" || e.key === " ") { e.preventDefault(); activate(); }
|
||||
});
|
||||
}
|
||||
});
|
||||
@@ -484,6 +496,54 @@ function applySavedView(view) {
|
||||
loadGames(true);
|
||||
}
|
||||
|
||||
/* ---------- bulk selection ---------- */
|
||||
|
||||
const BULK_CONTROLS = ["#bulkStatus", "#bulkFavorite", "#bulkUnfavorite", "#bulkHide", "#bulkClear"];
|
||||
|
||||
function updateBulkBar() {
|
||||
const count = state.selected.size;
|
||||
$("#bulkCount").textContent = `${count} выбрано`;
|
||||
BULK_CONTROLS.forEach((selector) => { $(selector).disabled = count === 0; });
|
||||
}
|
||||
|
||||
function setSelected(appid, selected) {
|
||||
if (selected) state.selected.add(appid);
|
||||
else state.selected.delete(appid);
|
||||
const input = document.querySelector(`input[data-select="${appid}"]`);
|
||||
if (input) input.checked = selected;
|
||||
const card = document.querySelector(`#grid .card[data-appid="${appid}"]`);
|
||||
if (card) card.classList.toggle("selected", selected);
|
||||
updateBulkBar();
|
||||
}
|
||||
|
||||
function toggleSelected(appid) {
|
||||
setSelected(appid, !state.selected.has(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"));
|
||||
updateBulkBar();
|
||||
}
|
||||
|
||||
async function bulkUpdate(fields) {
|
||||
const appids = [...state.selected];
|
||||
if (!appids.length) return;
|
||||
try {
|
||||
const result = await api("/api/personal/bulk", {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ appids, ...fields }),
|
||||
});
|
||||
toast(`Обновлено игр: ${result.updated}`);
|
||||
clearSelection();
|
||||
await loadGames(true);
|
||||
await loadMeta();
|
||||
} catch (err) {
|
||||
toast(err.message);
|
||||
}
|
||||
}
|
||||
|
||||
/* ---------- library ---------- */
|
||||
|
||||
function filterParams() {
|
||||
@@ -509,6 +569,7 @@ function filterParams() {
|
||||
|
||||
async function loadGames(reset = true) {
|
||||
if (state.loading) return;
|
||||
if (reset && state.selectionMode && state.selected.size) clearSelection();
|
||||
state.loading = true;
|
||||
$("#more").disabled = true;
|
||||
if (reset) {
|
||||
@@ -858,6 +919,33 @@ $("#resetFilters").addEventListener("click", () => {
|
||||
loadGames(true);
|
||||
});
|
||||
|
||||
grid.addEventListener("change", (event) => {
|
||||
const input = event.target instanceof Element && event.target.closest("input[data-select]");
|
||||
if (input) setSelected(Number(input.value), input.checked);
|
||||
});
|
||||
|
||||
$("#selectMode").addEventListener("click", () => {
|
||||
state.selectionMode = !state.selectionMode;
|
||||
state.selected.clear();
|
||||
const button = $("#selectMode");
|
||||
button.classList.toggle("active", state.selectionMode);
|
||||
button.textContent = state.selectionMode ? "отмена" : "выбрать";
|
||||
$("#bulkBar").hidden = !state.selectionMode;
|
||||
updateBulkBar();
|
||||
loadGames(true);
|
||||
});
|
||||
|
||||
$("#bulkStatus").addEventListener("change", (event) => {
|
||||
const status = event.target.value;
|
||||
event.target.value = "";
|
||||
if (status) bulkUpdate({ status });
|
||||
});
|
||||
$("#bulkFavorite").addEventListener("click", () => bulkUpdate({ favorite: true }));
|
||||
$("#bulkUnfavorite").addEventListener("click", () => bulkUpdate({ favorite: false }));
|
||||
$("#bulkHide").addEventListener("click", () => bulkUpdate({ status: "ignore" }));
|
||||
$("#bulkClear").addEventListener("click", clearSelection);
|
||||
updateBulkBar();
|
||||
|
||||
$("#more").addEventListener("click", () => loadGames(false));
|
||||
|
||||
$("#recommendBtn").addEventListener("click", runRecommend);
|
||||
|
||||
Reference in new issue
Block a user