router.js 5.2 KB

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