Files
SenSu/static/web_panel/js/app.js
T
qinglong a1dd401efc Fix theme toggle: move init to app.js, remove duplicate script
- Theme initialized BEFORE page render (no flash)
- toggleTheme() now in app.js global scope
- Proxy page rewritten with btn-filled MD3 class
2026-06-11 12:46:33 +08:00

95 lines
3.7 KiB
JavaScript

// 主题初始化 (必须在渲染前执行, 防止闪白)
(function(){
var s = localStorage.getItem("sensu-theme") || "dark";
document.documentElement.setAttribute("data-theme", s);
})();
window.toggleTheme = function(){
var t = document.documentElement.getAttribute("data-theme") === "light" ? "dark" : "light";
document.documentElement.setAttribute("data-theme", t);
localStorage.setItem("sensu-theme", t);
var btn = document.querySelector(".theme-toggle");
if(btn) btn.textContent = t === "light" ? "☀️" : "🌙";
};
// 初始化检查
window.onload = async () => {
// 设置按钮初始图标
var btn = document.querySelector(".theme-toggle");
if(btn) btn.textContent = document.documentElement.getAttribute("data-theme") === "light" ? "☀️" : "🌙";
try {
const res = await fetch('./api/auth/status', { credentials: 'include' });
if(res.status === 401) { window.location.href = './index.html'; return; }
const data = await res.json();
if(!data.authenticated) { window.location.href = './index.html'; return; }
document.getElementById('uname').textContent = data.username || 'Admin';
loadPage('dashboard'); // 默认加载
} catch(e) { window.location.href = './index.html'; }
};
// 路由加载器
async function loadPage(pageName) {
const content = document.getElementById('page-content');
const bar = document.getElementById('progress');
// 侧边栏高亮
document.querySelectorAll('.nav-item').forEach(el => el.classList.remove('active'));
document.querySelector(`.nav-item[data-page="${pageName}"]`)?.classList.add('active');
// 进度条动画
bar.classList.add('active'); bar.style.width = '0%';
await new Promise(r => requestAnimationFrame(() => { bar.style.width = '80%'; setTimeout(r, 100); }));
try {
// 🟢 关键修改:fetch 路径必须包含 /static/
const html = await fetch(`./static/pages/${pageName}.html`).then(r => {
if(!r.ok) throw new Error('404');
return r.text();
});
content.innerHTML = html;
// 动态加载对应 JS 模块
// 🟢 关键修改:script src 路径必须包含 /static/
const script = document.createElement('script');
script.src = `./static/pages/${pageName}.js?t=${Date.now()}`;
script.onload = () => {
// 触发模块初始化
const moduleName = pageName.charAt(0).toUpperCase() + pageName.slice(1) + 'Module';
if(window[moduleName]?.init) {
window[moduleName].init();
}
bar.style.width = '100%';
setTimeout(() => bar.classList.remove('active'), 200);
};
script.onerror = () => {
throw new Error('JS Load Failed');
};
document.head.appendChild(script);
} catch(e) {
content.innerHTML = `<div style="color:var(--error); text-align:center; margin-top:20vh;">页面加载失败: ${e.message}</div>`;
bar.style.background = 'var(--error)';
setTimeout(() => { bar.style.width = '100%'; setTimeout(() => { bar.classList.remove('active'); bar.style.background = 'var(--accent)'; }, 200); }, 100);
}
}
// 侧边栏切换
function toggleSidebar() {
document.getElementById('app').classList.toggle('collapsed');
}
// 退出登录
async function doLogout() {
await fetch('./api/logout', { method: 'POST', credentials: 'include' });
window.location.href = './index.html';
}
// 点击侧边栏事件委托
document.addEventListener('click', (e) => {
const nav = e.target.closest('.nav-item');
if(nav) { loadPage(nav.dataset.page); e.preventDefault(); }
});