main.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. (function () {
  2. const STATIC_PREFIX = "/tool/annotator/static";
  3. const widgets = [
  4. { mountId: "topbarMount", name: "topbar", script: "topbar.js" },
  5. { mountId: "leftWindowMount", name: "left-window", script: "left-window.js" },
  6. { mountId: "centerPanelMount", name: "center-panel", script: "center-panel.js" },
  7. { mountId: "rightWindowMount", name: "right-window", script: "right-window.js" },
  8. ];
  9. function ensureRegistry() {
  10. if (!window.OASAnnotatorWidgets) {
  11. window.OASAnnotatorWidgets = {};
  12. }
  13. return window.OASAnnotatorWidgets;
  14. }
  15. async function fetchWidget(name) {
  16. const response = await fetch(`${STATIC_PREFIX}/widget/${name}.html`, { cache: "no-cache" });
  17. if (!response.ok) {
  18. throw new Error(`加载组件失败: ${name} (${response.status})`);
  19. }
  20. return response.text();
  21. }
  22. async function mountWidgets() {
  23. for (const widget of widgets) {
  24. const mount = document.getElementById(widget.mountId);
  25. if (!mount) {
  26. throw new Error(`缺少组件挂载点: ${widget.mountId}`);
  27. }
  28. mount.innerHTML = await fetchWidget(widget.name);
  29. }
  30. }
  31. function loadScript(path) {
  32. return new Promise((resolve, reject) => {
  33. const script = document.createElement("script");
  34. script.src = path;
  35. script.async = false;
  36. script.onload = () => resolve();
  37. script.onerror = () => reject(new Error(`脚本加载失败: ${path}`));
  38. document.body.appendChild(script);
  39. });
  40. }
  41. async function loadComponentScripts() {
  42. for (const widget of widgets) {
  43. await loadScript(`${STATIC_PREFIX}/js/${widget.script}`);
  44. }
  45. const registry = ensureRegistry();
  46. for (const widget of widgets) {
  47. const component = registry[widget.name];
  48. if (component && typeof component.mount === "function") {
  49. component.mount();
  50. }
  51. }
  52. }
  53. async function loadRuntimeScripts() {
  54. // 保持原有初始化顺序:先布局脚本,再业务脚本。
  55. await loadScript(`${STATIC_PREFIX}/js/layout-ui.js`);
  56. await loadScript(`${STATIC_PREFIX}/js/app.js`);
  57. }
  58. function showBootError(error) {
  59. console.error(error);
  60. const mount = document.createElement("pre");
  61. mount.className = "small-output";
  62. mount.textContent = `页面初始化失败: ${error.message || String(error)}`;
  63. document.body.appendChild(mount);
  64. }
  65. async function bootstrap() {
  66. ensureRegistry();
  67. await mountWidgets();
  68. await loadComponentScripts();
  69. await loadRuntimeScripts();
  70. }
  71. bootstrap().catch(showBootError);
  72. })();