Files
sechskies/assets/js/totoga2.js
T

113 lines
3.9 KiB
JavaScript
Raw Normal View History

2026-05-14 09:20:10 +08:00
(() => {
const hubButtons = Array.from(document.querySelectorAll("[data-totoga-hub]"));
const hubPanels = Array.from(document.querySelectorAll("[data-totoga-panel]"));
function setHubPanel(name) {
2026-05-14 16:09:17 +08:00
if (!hubButtons.length || !hubPanels.length) return;
2026-05-14 09:20:10 +08:00
hubButtons.forEach((btn) => btn.classList.toggle("active", btn.dataset.totogaHub === name));
hubPanels.forEach((panel) => {
panel.hidden = panel.dataset.totogaPanel !== name;
});
}
hubButtons.forEach((button) => {
button.addEventListener("click", () => {
const name = button.dataset.totogaHub;
if (name) setHubPanel(name);
});
});
2026-05-14 09:20:10 +08:00
const reader = document.getElementById("totoga2-reader");
2026-05-14 16:09:17 +08:00
const chapters = reader ? Array.from(reader.querySelectorAll(".chronicle-chapter")) : [];
2026-05-14 09:20:10 +08:00
const modeButtons = Array.from(document.querySelectorAll("[data-totoga-reader-mode]"));
const pager = document.getElementById("totoga2-pager");
const prevBtn = document.getElementById("totoga2-prev");
const nextBtn = document.getElementById("totoga2-next");
const indicator = document.getElementById("totoga2-chapter-indicator");
2026-05-14 16:09:17 +08:00
const hasPager = Boolean(pager && prevBtn && nextBtn && indicator);
2026-05-14 09:20:10 +08:00
let currentIndex = 0;
function updatePagedView() {
2026-05-14 16:09:17 +08:00
if (!hasPager) return;
2026-05-14 09:20:10 +08:00
chapters.forEach((chapter, index) => {
chapter.style.display = index === currentIndex ? "" : "none";
chapter.classList.toggle("active", index === currentIndex);
});
indicator.textContent = `${currentIndex + 1} / ${chapters.length}`;
prevBtn.disabled = currentIndex === 0;
nextBtn.disabled = currentIndex === chapters.length - 1;
}
function setReaderMode(mode) {
2026-05-14 16:09:17 +08:00
if (!reader) return;
2026-05-14 09:20:10 +08:00
reader.dataset.mode = mode;
modeButtons.forEach((btn) => btn.classList.toggle("active", btn.dataset.totogaReaderMode === mode));
2026-05-14 16:09:17 +08:00
if (mode === "paged" && hasPager) {
2026-05-14 09:20:10 +08:00
pager.hidden = false;
updatePagedView();
} else {
2026-05-14 16:09:17 +08:00
if (pager) pager.hidden = true;
2026-05-14 09:20:10 +08:00
chapters.forEach((chapter) => {
chapter.style.display = "";
chapter.classList.remove("active");
});
}
}
modeButtons.forEach((button) => {
button.addEventListener("click", () => setReaderMode(button.dataset.totogaReaderMode));
});
2026-05-14 16:09:17 +08:00
if (hasPager) {
prevBtn.addEventListener("click", () => {
if (currentIndex > 0) {
currentIndex -= 1;
updatePagedView();
}
});
2026-05-14 09:20:10 +08:00
2026-05-14 16:09:17 +08:00
nextBtn.addEventListener("click", () => {
if (currentIndex < chapters.length - 1) {
currentIndex += 1;
updatePagedView();
}
});
}
2026-05-14 09:20:10 +08:00
2026-05-14 16:09:17 +08:00
if (hubButtons.length > 0) {
const initialHub =
hubButtons.find((b) => b.classList.contains("active"))?.dataset.totogaHub ||
hubButtons[0]?.dataset.totogaHub ||
"video";
setHubPanel(initialHub);
} else {
hubPanels.forEach((panel) => {
panel.hidden = false;
});
}
if (reader) {
setReaderMode("scroll");
}
2026-05-14 09:20:10 +08:00
function applyLocationHashNavigation() {
const hash = (location.hash || "").slice(1);
2026-05-14 16:09:17 +08:00
if (hash === "ebook" && hubButtons.length > 0) {
2026-05-14 09:20:10 +08:00
setHubPanel("ebook");
return;
}
if (hash && document.getElementById(hash)) {
2026-05-14 16:09:17 +08:00
if (hubButtons.length > 0) setHubPanel("reader");
2026-05-14 09:20:10 +08:00
setReaderMode("scroll");
2026-05-14 16:09:17 +08:00
if (pager) pager.hidden = true;
2026-05-14 09:20:10 +08:00
requestAnimationFrame(() => {
document.getElementById(hash)?.scrollIntoView({ behavior: "smooth", block: "start" });
});
}
}
applyLocationHashNavigation();
window.addEventListener("hashchange", applyLocationHashNavigation);
})();