345 lines
9.5 KiB
JavaScript
345 lines
9.5 KiB
JavaScript
const CARDS_PER_PAGE = 12;
|
|
|
|
let currentGroupId = null;
|
|
let currentPage = 1;
|
|
|
|
export async function loadCardDeck() {
|
|
const body = document.getElementById("qm-body-carddeck");
|
|
if (!body) return;
|
|
|
|
body.innerHTML = renderShell();
|
|
|
|
try {
|
|
const res = await fetch("/api/card-groups");
|
|
if (!res.ok) throw new Error("API Fehler");
|
|
const groups = await res.json();
|
|
|
|
renderTabs(groups);
|
|
|
|
if (groups.length) {
|
|
currentGroupId = groups[0].id;
|
|
await loadCards();
|
|
}
|
|
} catch (err) {
|
|
console.error("Gruppen Fehler:", err);
|
|
}
|
|
}
|
|
|
|
/* ── Grundstruktur ──────────────────────────── */
|
|
function renderShell() {
|
|
return `
|
|
<div class="kd-wrap">
|
|
<aside class="kd-tabs" id="kd-tabs">
|
|
<div class="kd-loading">Lade...</div>
|
|
</aside>
|
|
<main class="kd-main">
|
|
<div class="kd-grid" id="kd-grid"></div>
|
|
<div class="kd-pagination" id="kd-pagination"></div>
|
|
</main>
|
|
</div>
|
|
|
|
<style>
|
|
.kd-wrap {
|
|
display: flex;
|
|
width: 100%;
|
|
height: 100%;
|
|
gap: 0;
|
|
font-family: "Cinzel", serif;
|
|
}
|
|
|
|
.kd-tabs {
|
|
display: flex;
|
|
flex-direction: column;
|
|
gap: 6px;
|
|
padding: 20px 10px;
|
|
background: rgba(0,0,0,0.25);
|
|
border-right: 2px solid #6b4b2a;
|
|
min-width: 140px;
|
|
flex-shrink: 0;
|
|
overflow-y: auto;
|
|
}
|
|
|
|
.kd-tab {
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 8px;
|
|
padding: 10px 14px;
|
|
background: linear-gradient(135deg, #2a1a08, #1a0f04);
|
|
border: 2px solid #6b4b2a;
|
|
border-radius: 8px;
|
|
color: #c8a86a;
|
|
font-family: "Cinzel", serif;
|
|
font-size: 13px;
|
|
font-weight: 600;
|
|
cursor: pointer;
|
|
transition: all 0.2s;
|
|
text-align: left;
|
|
white-space: nowrap;
|
|
}
|
|
|
|
.kd-tab:hover {
|
|
color: #f0d9a6;
|
|
filter: brightness(1.2);
|
|
}
|
|
|
|
.kd-tab-active {
|
|
color: #fff !important;
|
|
box-shadow: inset 0 0 10px rgba(0,0,0,0.5), 0 0 14px rgba(200,160,60,0.3) !important;
|
|
}
|
|
|
|
.kd-tab-dot {
|
|
width: 12px;
|
|
height: 12px;
|
|
border-radius: 50%;
|
|
flex-shrink: 0;
|
|
border: 1px solid rgba(255,255,255,0.3);
|
|
}
|
|
|
|
.kd-main {
|
|
flex: 1;
|
|
display: flex;
|
|
flex-direction: column;
|
|
padding: 10px 20px 20px 20px;
|
|
overflow: hidden;
|
|
}
|
|
|
|
.kd-grid {
|
|
flex: 1;
|
|
display: grid;
|
|
grid-template-columns: repeat(6, 1fr);
|
|
gap: 14px;
|
|
overflow-y: auto;
|
|
overflow-x: visible;
|
|
align-content: start;
|
|
padding-top: 10px;
|
|
}
|
|
|
|
.kd-card {
|
|
position: relative;
|
|
border: 2px solid #6b4b2a;
|
|
border-radius: 8px;
|
|
overflow: hidden;
|
|
background: #1a0f04;
|
|
cursor: pointer;
|
|
transition: transform 0.2s, box-shadow 0.2s, border-color 0.2s;
|
|
aspect-ratio: 3/4;
|
|
}
|
|
|
|
.kd-card:hover {
|
|
transform: scale(1.05) translateY(-4px);
|
|
border-color: #f0d060;
|
|
box-shadow: 0 8px 24px rgba(0,0,0,0.7), 0 0 14px rgba(200,160,60,0.3);
|
|
z-index: 10;
|
|
}
|
|
|
|
.kd-card img {
|
|
width: 100%;
|
|
height: 100%;
|
|
object-fit: cover;
|
|
display: block;
|
|
}
|
|
|
|
.kd-card-name {
|
|
position: absolute;
|
|
bottom: 0;
|
|
left: 0;
|
|
right: 0;
|
|
padding: 4px 6px;
|
|
background: linear-gradient(transparent, rgba(0,0,0,0.85));
|
|
font-family: "Cinzel", serif;
|
|
font-size: 10px;
|
|
color: #f0d9a6;
|
|
text-align: center;
|
|
line-height: 1.3;
|
|
}
|
|
|
|
.kd-stat-atk {
|
|
position: absolute;
|
|
bottom: 28px;
|
|
left: 5px;
|
|
background: rgba(180,40,20,0.85);
|
|
border: 1px solid #ff6040;
|
|
border-radius: 4px;
|
|
color: #fff;
|
|
font-family: "Cinzel", serif;
|
|
font-size: 11px;
|
|
font-weight: bold;
|
|
padding: 1px 5px;
|
|
}
|
|
|
|
.kd-stat-def {
|
|
position: absolute;
|
|
bottom: 28px;
|
|
right: 5px;
|
|
background: rgba(20,80,180,0.85);
|
|
border: 1px solid #4090ff;
|
|
border-radius: 4px;
|
|
color: #fff;
|
|
font-family: "Cinzel", serif;
|
|
font-size: 11px;
|
|
font-weight: bold;
|
|
padding: 1px 5px;
|
|
}
|
|
|
|
.kd-empty {
|
|
grid-column: 1 / -1;
|
|
text-align: center;
|
|
color: #8b6a3c;
|
|
font-size: 16px;
|
|
padding: 60px 0;
|
|
}
|
|
|
|
.kd-pagination {
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
gap: 8px;
|
|
padding-top: 14px;
|
|
flex-shrink: 0;
|
|
}
|
|
|
|
.kd-page-btn {
|
|
background: linear-gradient(#3a2810, #1a0f04);
|
|
border: 1px solid #8b6a3c;
|
|
border-radius: 6px;
|
|
color: #f0d9a6;
|
|
font-family: "Cinzel", serif;
|
|
font-size: 12px;
|
|
padding: 5px 14px;
|
|
cursor: pointer;
|
|
transition: 0.15s;
|
|
}
|
|
|
|
.kd-page-btn:hover {
|
|
border-color: #f0d060;
|
|
}
|
|
|
|
.kd-page-btn:disabled {
|
|
opacity: 0.35;
|
|
cursor: not-allowed;
|
|
}
|
|
|
|
.kd-page-btn.kd-page-active {
|
|
background: linear-gradient(#6b4b2a, #3c2414);
|
|
border-color: #f0d060;
|
|
color: #fff5cc;
|
|
}
|
|
|
|
.kd-page-info {
|
|
color: #a08060;
|
|
font-size: 12px;
|
|
min-width: 80px;
|
|
text-align: center;
|
|
}
|
|
|
|
.kd-loading {
|
|
text-align: center;
|
|
color: #8b6a3c;
|
|
font-size: 15px;
|
|
padding: 20px 0;
|
|
animation: kd-pulse 1.2s infinite;
|
|
}
|
|
|
|
@keyframes kd-pulse {
|
|
0%, 100% { opacity: 1; }
|
|
50% { opacity: 0.4; }
|
|
}
|
|
</style>
|
|
`;
|
|
}
|
|
|
|
/* ── Tabs aus DB ────────────────────────────── */
|
|
function renderTabs(groups) {
|
|
const container = document.getElementById("kd-tabs");
|
|
if (!container) return;
|
|
|
|
container.innerHTML = groups.map((g, i) => {
|
|
const color = g.color || "#6b4b2a";
|
|
return `
|
|
<button class="kd-tab ${i === 0 ? "kd-tab-active" : ""}"
|
|
data-group="${g.id}"
|
|
style="border-color:${color};${i === 0 ? `background:${color}33;` : ""}">
|
|
<span class="kd-tab-dot" style="background:${color};"></span>
|
|
<span>${g.name}</span>
|
|
</button>
|
|
`;
|
|
}).join("");
|
|
|
|
container.querySelectorAll(".kd-tab").forEach((btn) => {
|
|
btn.addEventListener("click", async () => {
|
|
const color = btn.style.borderColor;
|
|
container.querySelectorAll(".kd-tab").forEach((b) => {
|
|
b.classList.remove("kd-tab-active");
|
|
b.style.background = "";
|
|
});
|
|
btn.classList.add("kd-tab-active");
|
|
btn.style.background = `${color}33`;
|
|
currentGroupId = parseInt(btn.dataset.group);
|
|
currentPage = 1;
|
|
await loadCards();
|
|
});
|
|
});
|
|
}
|
|
|
|
/* ── Karten laden ───────────────────────────── */
|
|
async function loadCards() {
|
|
const grid = document.getElementById("kd-grid");
|
|
const pagination = document.getElementById("kd-pagination");
|
|
if (!grid) return;
|
|
|
|
grid.innerHTML = `<div class="kd-loading">Lade Karten...</div>`;
|
|
pagination.innerHTML = "";
|
|
|
|
try {
|
|
const res = await fetch(`/api/cards?group_id=${currentGroupId}&page=${currentPage}&limit=${CARDS_PER_PAGE}`);
|
|
if (!res.ok) throw new Error("API Fehler");
|
|
const data = await res.json();
|
|
renderGrid(grid, data.cards);
|
|
renderPagination(pagination, data.totalPages, data.total);
|
|
} catch {
|
|
grid.innerHTML = `<div class="kd-empty">Noch keine Karten für diese Gruppe vorhanden.</div>`;
|
|
}
|
|
}
|
|
|
|
/* ── Grid ───────────────────────────────────── */
|
|
function renderGrid(grid, cards) {
|
|
if (!cards || !cards.length) {
|
|
grid.innerHTML = `<div class="kd-empty">Noch keine Karten für diese Gruppe vorhanden.</div>`;
|
|
return;
|
|
}
|
|
grid.innerHTML = cards.map((c) => `
|
|
<div class="kd-card" title="${c.name}">
|
|
<img src="/images/cards/${c.image}" alt="${c.name}"
|
|
onerror="this.src='/images/avatar_placeholder.svg'">
|
|
${c.attack != null ? `<span class="kd-stat-atk">${c.attack}</span>` : ""}
|
|
${c.defense != null ? `<span class="kd-stat-def">${c.defense}</span>` : ""}
|
|
<div class="kd-card-name">${c.name}</div>
|
|
</div>
|
|
`).join("");
|
|
}
|
|
|
|
/* ── Pagination ─────────────────────────────── */
|
|
function renderPagination(pagination, totalPages, total) {
|
|
if (totalPages <= 1) return;
|
|
pagination.innerHTML = `
|
|
<button class="kd-page-btn" id="kd-prev" ${currentPage === 1 ? "disabled" : ""}>◀</button>
|
|
${Array.from({ length: totalPages }, (_, i) => i + 1).map((p) => `
|
|
<button class="kd-page-btn ${p === currentPage ? "kd-page-active" : ""}" data-page="${p}">${p}</button>
|
|
`).join("")}
|
|
<button class="kd-page-btn" id="kd-next" ${currentPage === totalPages ? "disabled" : ""}>▶</button>
|
|
<span class="kd-page-info">${total} Karten</span>
|
|
`;
|
|
document.getElementById("kd-prev")?.addEventListener("click", async () => {
|
|
if (currentPage > 1) { currentPage--; await loadCards(); }
|
|
});
|
|
document.getElementById("kd-next")?.addEventListener("click", async () => {
|
|
if (currentPage < totalPages) { currentPage++; await loadCards(); }
|
|
});
|
|
pagination.querySelectorAll("[data-page]").forEach((btn) => {
|
|
btn.addEventListener("click", async () => {
|
|
currentPage = parseInt(btn.dataset.page);
|
|
await loadCards();
|
|
});
|
|
});
|
|
}
|