router.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. var routes = {
  2. page1: "pages/page1.html",
  3. page2: "pages/page2.html",
  4. page3: "pages/page3.html",
  5. page4: "pages/page4.html",
  6. page5: "pages/page5.html",
  7. page6: "pages/page6.html",
  8. page7: "pages/page7.html",
  9. page8: "pages/page8.html",
  10. };
  11. var currentPage = null;
  12. var loadingPage = false;
  13. var pageHandlers = {
  14. page1: {
  15. init: function () {
  16. if (window.initPage1) window.initPage1();
  17. },
  18. exit: function () {
  19. if (window.exitPage1) window.exitPage1();
  20. },
  21. },
  22. page2: {
  23. init: function () {
  24. if (window.initPage2) window.initPage2();
  25. },
  26. exit: function () {
  27. if (window.exitPage2) window.exitPage2();
  28. },
  29. },
  30. page3: {
  31. init: function () {
  32. if (window.initPage3) window.initPage3();
  33. },
  34. exit: function () {
  35. if (window.exitPage3) window.exitPage3();
  36. },
  37. },
  38. page4: {
  39. init: function () {
  40. if (window.initPage4) window.initPage4();
  41. },
  42. exit: function () {
  43. if (window.exitPage4) window.exitPage4();
  44. },
  45. },
  46. page5: {
  47. init: function () {
  48. if (window.initPage5) window.initPage5();
  49. },
  50. exit: function () {
  51. if (window.exitPage5) window.exitPage5();
  52. },
  53. },
  54. page6: {
  55. init: function () {
  56. if (window.initPage6) window.initPage6();
  57. },
  58. exit: function () {
  59. if (window.exitPage6) window.exitPage6();
  60. },
  61. },
  62. page7: {
  63. init: function () {
  64. if (window.initPage7) window.initPage7();
  65. },
  66. exit: function () {
  67. if (window.exitPage7) window.exitPage7();
  68. },
  69. },
  70. page8: {
  71. init: function () {
  72. if (window.initPage8) window.initPage8();
  73. },
  74. exit: function () {
  75. if (window.exitPage8) window.exitPage8();
  76. },
  77. },
  78. };
  79. function clearOldScripts() {
  80. document.querySelectorAll("script[data-dynamic='1']").forEach(function (s) {
  81. return s.remove();
  82. });
  83. }
  84. function runScripts(container) {
  85. var scripts = container.querySelectorAll("script");
  86. scripts.forEach(function (oldScript) {
  87. var s = document.createElement("script");
  88. s.dataset.dynamic = "1";
  89. if (oldScript.src) {
  90. s.src = oldScript.src;
  91. } else {
  92. s.textContent = oldScript.textContent;
  93. }
  94. document.body.appendChild(s);
  95. });
  96. }
  97. function loadPage(name) {
  98. var view = document.getElementById("view");
  99. var url = routes[name];
  100. if (!url) {
  101. view.innerHTML = "无法找到页面";
  102. return;
  103. }
  104. if (loadingPage) {
  105. return;
  106. }
  107. if (currentPage === name) {
  108. return;
  109. }
  110. loadingPage = true;
  111. if (currentPage && pageHandlers[currentPage] && pageHandlers[currentPage].exit) {
  112. console.log("exit:", currentPage);
  113. pageHandlers[currentPage].exit();
  114. }
  115. clearOldScripts();
  116. fetch(url, { credentials: "include" })
  117. .then(function (res) {
  118. return res.text();
  119. })
  120. .then(function (html) {
  121. if (html.includes("login.html") || html.includes("登录")) {
  122. window.location.href = "/";
  123. return;
  124. }
  125. view.innerHTML = html;
  126. runScripts(view);
  127. currentPage = name;
  128. if (pageHandlers[name] && pageHandlers[name].init) {
  129. console.log("init:", name);
  130. pageHandlers[name].init();
  131. }
  132. })
  133. .catch(function (e) {
  134. console.error("loadPage error:", e);
  135. view.innerHTML = "页面加载失败";
  136. })
  137. .finally(function () {
  138. loadingPage = false;
  139. });
  140. }
  141. function setText(id, value, page, icon) {
  142. if (page && currentPage !== page) {
  143. return;
  144. }
  145. var el = document.getElementById(id);
  146. if (!el) {
  147. return;
  148. }
  149. // 后端状态值: {level: ok|warn|err|info, text}, 渲染 CSS 色点
  150. if (value && typeof value === "object" && value.text !== undefined) {
  151. var level = ["ok", "warn", "err", "info"].includes(value.level) ? value.level : "info";
  152. var txt =
  153. value.text === null || value.text === undefined || value.text === ""
  154. ? "--"
  155. : String(value.text);
  156. el.innerHTML = '<span class="dot ' + level + '"></span>' + escapeHtml(txt);
  157. return;
  158. }
  159. var text = value !== null && value !== undefined ? value : "--";
  160. // 普通值可选携带行内 SVG 图标 (仅在有有效数据时显示, "--" 不显示)
  161. if (icon && text !== "--") {
  162. el.innerHTML =
  163. '<svg class="icon"><use href="#' + icon + '"/></svg>' + escapeHtml(String(text));
  164. return;
  165. }
  166. el.textContent = text;
  167. }
  168. function setBtnStatus(id, enabled, page) {
  169. if (page && currentPage !== page) {
  170. return;
  171. }
  172. var btn = document.getElementById(id);
  173. if (btn) btn.disabled = !enabled;
  174. }