router.js 5.6 KB

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