adventskalender/shared/js/save-progress.js hinzugefügt
This commit is contained in:
85
adventskalender/shared/js/save-progress.js
Normal file
85
adventskalender/shared/js/save-progress.js
Normal file
@@ -0,0 +1,85 @@
|
|||||||
|
// shared/js/save-progress.js
|
||||||
|
/**
|
||||||
|
* Bratonien Adventskalender – Fortschritt speichern (shared)
|
||||||
|
* ----------------------------------------------------------
|
||||||
|
* - speichert geöffnete Türchen (Einzeltüren & Flügeltüren)
|
||||||
|
* - stellt Zustand nach Neuladen wieder her
|
||||||
|
* - Dev-Modus (?dev): zeigt Reset-Button + deaktiviert Tagessperre
|
||||||
|
*/
|
||||||
|
|
||||||
|
(function () {
|
||||||
|
const yearMatch = document.title.match(/\d{4}/);
|
||||||
|
const year = yearMatch ? yearMatch[0] : new Date().getFullYear();
|
||||||
|
const STORAGE_KEY = `bratonien_opened_${year}`;
|
||||||
|
|
||||||
|
/** === DEV-FLAG === */
|
||||||
|
const isDev = window.location.search.includes("dev");
|
||||||
|
window.BRATONIEN_DEV = isDev;
|
||||||
|
|
||||||
|
/** === HELFER === */
|
||||||
|
function getOpenedDays() {
|
||||||
|
try {
|
||||||
|
return JSON.parse(localStorage.getItem(STORAGE_KEY)) || [];
|
||||||
|
} catch {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function saveOpenedDays(days) {
|
||||||
|
localStorage.setItem(STORAGE_KEY, JSON.stringify([...new Set(days)]));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** === STATUS LADEN === */
|
||||||
|
window.addEventListener("DOMContentLoaded", () => {
|
||||||
|
const openedDays = getOpenedDays();
|
||||||
|
|
||||||
|
openedDays.forEach(day => {
|
||||||
|
// normale Tür
|
||||||
|
document.querySelectorAll(`.door[data-day="${day}"]`).forEach(door => {
|
||||||
|
door.classList.add("open");
|
||||||
|
});
|
||||||
|
|
||||||
|
// Flügeltüren
|
||||||
|
const left = document.querySelector(`.fluegel.left[data-day="${day}"]`);
|
||||||
|
const right = document.querySelector(`.fluegel.right[data-day="${day}"]`);
|
||||||
|
if (left && right) {
|
||||||
|
left.classList.add("rota");
|
||||||
|
right.classList.add("rota");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// === DEV BUTTON ===
|
||||||
|
if (isDev) {
|
||||||
|
const btn = document.createElement("button");
|
||||||
|
btn.textContent = "Reset Kalender";
|
||||||
|
btn.style.position = "fixed";
|
||||||
|
btn.style.bottom = "15px";
|
||||||
|
btn.style.right = "15px";
|
||||||
|
btn.style.zIndex = "9999";
|
||||||
|
btn.style.padding = "8px 14px";
|
||||||
|
btn.style.border = "2px solid gold";
|
||||||
|
btn.style.borderRadius = "10px";
|
||||||
|
btn.style.background = "#1a1a1a";
|
||||||
|
btn.style.color = "gold";
|
||||||
|
btn.style.cursor = "pointer";
|
||||||
|
btn.style.fontFamily = "sans-serif";
|
||||||
|
btn.style.fontSize = "14px";
|
||||||
|
btn.addEventListener("click", () => {
|
||||||
|
localStorage.removeItem(STORAGE_KEY);
|
||||||
|
location.reload();
|
||||||
|
});
|
||||||
|
document.body.appendChild(btn);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
/** === STATUS SPEICHERN (auf Event hören) === */
|
||||||
|
document.addEventListener("doorOpened", e => {
|
||||||
|
const day = e.detail?.day;
|
||||||
|
if (!day) return;
|
||||||
|
const opened = getOpenedDays();
|
||||||
|
if (!opened.includes(day)) {
|
||||||
|
opened.push(day);
|
||||||
|
saveOpenedDays(opened);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
})();
|
||||||
Reference in New Issue
Block a user