Index.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. // 全局 Loading 控制器
  2. window.GlobalLoading = (function() {
  3. let overlay = null;
  4. let loadingText = null;
  5. function init() {
  6. overlay = document.getElementById('globalLoadingOverlay');
  7. loadingText = overlay ? overlay.querySelector('.global-loading-text') : null;
  8. }
  9. function show(text = '正在处理...') {
  10. // console.log('[GlobalLoading] show() 被调用');
  11. // console.log('[GlobalLoading] 文本:', text);
  12. if (!overlay) {
  13. // console.log('[GlobalLoading] → 初始化overlay元素...');
  14. init();
  15. }
  16. if (!overlay) {
  17. // console.error('[GlobalLoading] ✗ overlay元素未找到!');
  18. return;
  19. }
  20. // console.log('[GlobalLoading] ✓ overlay元素存在');
  21. if (loadingText) {
  22. loadingText.textContent = text;
  23. // console.log('[GlobalLoading] ✓ 设置Loading文本:', text);
  24. }
  25. overlay.classList.add('is-visible');
  26. // console.log('[GlobalLoading] ✓ 添加is-visible类,Loading应该可见了');
  27. }
  28. function hide() {
  29. // console.log('[GlobalLoading] hide() 被调用');
  30. if (!overlay) {
  31. // console.error('[GlobalLoading] ✗ overlay元素不存在');
  32. return;
  33. }
  34. overlay.classList.remove('is-visible');
  35. // console.log('[GlobalLoading] ✓ 移除is-visible类,Loading已隐藏');
  36. }
  37. return {
  38. show,
  39. hide
  40. };
  41. })();
  42. // 全局 Alert 控制器
  43. window.GlobalAlert = (function() {
  44. let alertContainer = null;
  45. let alertMessage = null;
  46. let hideTimer = null;
  47. function init() {
  48. alertContainer = document.getElementById('globalAlert');
  49. alertMessage = alertContainer ? alertContainer.querySelector('#alertMessage') : null;
  50. }
  51. function show(text, duration = 1500) {
  52. if (!alertContainer) init();
  53. if (!alertContainer || !alertMessage) return;
  54. // 清除之前的自动隐藏定时器
  55. if (hideTimer) {
  56. clearTimeout(hideTimer);
  57. hideTimer = null;
  58. }
  59. alertMessage.textContent = text;
  60. alertContainer.classList.add('show');
  61. // 自动隐藏
  62. if (duration > 0) {
  63. hideTimer = setTimeout(() => {
  64. hide();
  65. }, duration);
  66. }
  67. }
  68. function hide() {
  69. if (!alertContainer) return;
  70. alertContainer.classList.remove('show');
  71. if (hideTimer) {
  72. clearTimeout(hideTimer);
  73. hideTimer = null;
  74. }
  75. }
  76. return {
  77. show,
  78. hide
  79. };
  80. })();
  81. // 页面管理:负责切换 iframe 中的子页面,并保持与导航栏状态同步
  82. (function () {
  83. const DEFAULT_PAGE = "assets";
  84. function getPageFrame() {
  85. return document.getElementById("pageFrame");
  86. }
  87. function getNavigationFrame() {
  88. return document.getElementById("navigationFrame");
  89. }
  90. function switchPage(page) {
  91. const frame = getPageFrame();
  92. if (!frame) {
  93. return;
  94. }
  95. switch (page) {
  96. case "store":
  97. frame.src = "page/store/store.html";
  98. break;
  99. case "assets":
  100. default:
  101. frame.src = "page/assets/assets.html";
  102. break;
  103. }
  104. const navigationFrame = getNavigationFrame();
  105. if (navigationFrame && navigationFrame.contentWindow) {
  106. navigationFrame.contentWindow.postMessage(
  107. { type: "navigation", page },
  108. "*"
  109. );
  110. }
  111. }
  112. window.addEventListener("message", (event) => {
  113. // console.log('[Index] ← 收到message事件:', event.data);
  114. if (event.origin !== window.location.origin) {
  115. // console.log('[Index] ⚠ origin不匹配,忽略:', event.origin);
  116. return;
  117. }
  118. const { data } = event;
  119. if (data && data.type === "navigation" && data.page) {
  120. switchPage(data.page);
  121. } else if (data && data.type === "global-loading") {
  122. // 处理全局loading显示/隐藏
  123. // console.log('[Index] 收到global-loading消息:', data);
  124. if (data.action === "show") {
  125. // console.log('[Index] 显示Loading:', data.text);
  126. window.GlobalLoading.show(data.text);
  127. } else if (data.action === "hide") {
  128. // console.log('[Index] 隐藏Loading');
  129. window.GlobalLoading.hide();
  130. }
  131. } else if (data && data.type === "global-alert") {
  132. // 处理全局alert提示
  133. // console.log('[Index] 收到global-alert消息:', data);
  134. if (data.text) {
  135. window.GlobalAlert.show(data.text, data.duration);
  136. }
  137. } else if (data && data.type === "global-confirm") {
  138. // 处理全局confirm对话框
  139. // console.log('[Index] 收到global-confirm消息:', data);
  140. if (data.message && window.GlobalConfirm) {
  141. // 确保 GlobalConfirm 初始化完成后再显示
  142. window.GlobalConfirm.show(data.message).then((confirmed) => {
  143. // console.log('[Index] 用户选择:', confirmed ? '确认' : '取消');
  144. // 将结果发送回请求的子窗口
  145. event.source.postMessage({
  146. type: 'global-confirm-response',
  147. id: data.id,
  148. confirmed: confirmed
  149. }, event.origin);
  150. }).catch(error => {
  151. // console.error('[Index] GlobalConfirm显示失败:', error);
  152. // 失败时返回取消
  153. event.source.postMessage({
  154. type: 'global-confirm-response',
  155. id: data.id,
  156. confirmed: false
  157. }, event.origin);
  158. });
  159. }
  160. } else if (data && data.type === "open-export-view") {
  161. // 处理打开导出弹出框
  162. // console.log('[Index] 收到open-export-view消息:', data);
  163. if (data.folderName && window.ExportViewManager) {
  164. // 直接打开弹出框,传递文件夹名称
  165. window.ExportViewManager.show(data.folderName).then((confirmed) => {
  166. // console.log('[Index] 用户选择:', confirmed ? '确认导出' : '取消');
  167. // 如果用户确认,可以在这里处理实际的导出下载逻辑
  168. if (confirmed) {
  169. // TODO: 处理实际的导出下载逻辑
  170. // console.log('[Index] 用户确认导出,文件夹:', data.folderName);
  171. }
  172. }).catch(error => {
  173. // console.error('[Index] ExportViewManager显示失败:', error);
  174. });
  175. }
  176. }
  177. });
  178. window.addEventListener("DOMContentLoaded", () => {
  179. switchPage(DEFAULT_PAGE);
  180. });
  181. })();