var routes = {
page1: "pages/page1.html",
page2: "pages/page2.html",
page3: "pages/page3.html",
page4: "pages/page4.html",
page5: "pages/page5.html",
page6: "pages/page6.html",
page7: "pages/page7.html",
page8: "pages/page8.html",
};
var currentPage = null;
var loadingPage = false;
var pageHandlers = {
page1: {
init: function () {
if (window.initPage1) window.initPage1();
},
exit: function () {
if (window.exitPage1) window.exitPage1();
},
},
page2: {
init: function () {
if (window.initPage2) window.initPage2();
},
exit: function () {
if (window.exitPage2) window.exitPage2();
},
},
page3: {
init: function () {
if (window.initPage3) window.initPage3();
},
exit: function () {
if (window.exitPage3) window.exitPage3();
},
},
page4: {
init: function () {
if (window.initPage4) window.initPage4();
},
exit: function () {
if (window.exitPage4) window.exitPage4();
},
},
page5: {
init: function () {
if (window.initPage5) window.initPage5();
},
exit: function () {
if (window.exitPage5) window.exitPage5();
},
},
page6: {
init: function () {
if (window.initPage6) window.initPage6();
},
exit: function () {
if (window.exitPage6) window.exitPage6();
},
},
page7: {
init: function () {
if (window.initPage7) window.initPage7();
},
exit: function () {
if (window.exitPage7) window.exitPage7();
},
},
page8: {
init: function () {
if (window.initPage8) window.initPage8();
},
exit: function () {
if (window.exitPage8) window.exitPage8();
},
},
};
function clearOldScripts() {
document.querySelectorAll("script[data-dynamic='1']").forEach(function (s) {
return s.remove();
});
}
function runScripts(container) {
var scripts = container.querySelectorAll("script");
scripts.forEach(function (oldScript) {
var s = document.createElement("script");
s.dataset.dynamic = "1";
if (oldScript.src) {
s.src = oldScript.src;
} else {
s.textContent = oldScript.textContent;
}
document.body.appendChild(s);
});
}
function loadPage(name) {
var view = document.getElementById("view");
var url = routes[name];
if (!url) {
view.innerHTML = "无法找到页面";
return;
}
if (loadingPage) {
return;
}
if (currentPage === name) {
return;
}
loadingPage = true;
if (currentPage && pageHandlers[currentPage] && pageHandlers[currentPage].exit) {
console.log("exit:", currentPage);
pageHandlers[currentPage].exit();
}
clearOldScripts();
fetch(url, { credentials: "include" })
.then(function (res) {
return res.text();
})
.then(function (html) {
if (html.includes("login.html") || html.includes("登录")) {
window.location.href = "/";
return;
}
view.innerHTML = html;
runScripts(view);
currentPage = name;
if (pageHandlers[name] && pageHandlers[name].init) {
console.log("init:", name);
pageHandlers[name].init();
}
})
.catch(function (e) {
console.error("loadPage error:", e);
view.innerHTML = "页面加载失败";
})
.finally(function () {
loadingPage = false;
});
}
function setText(id, value, page, icon) {
if (page && currentPage !== page) {
return;
}
var el = document.getElementById(id);
if (!el) {
return;
}
// 后端状态值: {level: ok|warn|err|info, text}, 渲染 CSS 色点
if (value && typeof value === "object" && value.text !== undefined) {
var level = ["ok", "warn", "err", "info"].includes(value.level) ? value.level : "info";
var txt =
value.text === null || value.text === undefined || value.text === ""
? "--"
: String(value.text);
el.innerHTML = '' + escapeHtml(txt);
return;
}
var text = value !== null && value !== undefined ? value : "--";
// 普通值可选携带行内 SVG 图标 (仅在有有效数据时显示, "--" 不显示)
if (icon && text !== "--") {
el.innerHTML =
'' + escapeHtml(String(text));
return;
}
el.textContent = text;
}
function setBtnStatus(id, enabled, page) {
if (page && currentPage !== page) {
return;
}
var btn = document.getElementById(id);
if (btn) btn.disabled = !enabled;
}