confirm-view.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /**
  2. * 全局确认对话�?
  3. * 加载�?index.html 中,覆盖整个页面
  4. */
  5. class GlobalConfirm {
  6. constructor() {
  7. this.overlay = null;
  8. this.message = null;
  9. this.okBtn = null;
  10. this.cancelBtn = null;
  11. this.resolveCallback = null;
  12. this.ready = false;
  13. // 立即初始�?
  14. this.initPromise = this.init();
  15. }
  16. async init() {
  17. // ('[GlobalConfirm] �?init 开�?);
  18. await this.ensureElements();
  19. // ('[GlobalConfirm] �?查找DOM元素...');
  20. this.overlay = document.getElementById('globalConfirmOverlay');
  21. this.message = document.getElementById('confirmMessage');
  22. this.okBtn = document.getElementById('confirmOkBtn');
  23. this.cancelBtn = document.getElementById('confirmCancelBtn');
  24. // ('[GlobalConfirm] overlay:', !!this.overlay);
  25. // ('[GlobalConfirm] message:', !!this.message);
  26. // ('[GlobalConfirm] okBtn:', !!this.okBtn);
  27. // ('[GlobalConfirm] cancelBtn:', !!this.cancelBtn);
  28. if (this.overlay && this.okBtn && this.cancelBtn) {
  29. this.bindEvents();
  30. this.ready = true;
  31. // ('[GlobalConfirm] �?初始化完成,ready=true');
  32. } else {
  33. // ('[GlobalConfirm] �?初始化失败,部分元素未找�?);
  34. }
  35. }
  36. async ensureElements() {
  37. // ('[GlobalConfirm] �?ensureElements 开�?);
  38. if (document.getElementById('globalConfirmOverlay')) {
  39. // ('[GlobalConfirm] �?元素已存在,跳过加载');
  40. return;
  41. }
  42. try {
  43. // ('[GlobalConfirm] �?开始加�?/page/confirm-view.html');
  44. const response = await fetch('/page/confirm-view.html', { cache: 'no-cache' });
  45. // ('[GlobalConfirm] 响应状�?', response.status, response.statusText);
  46. if (!response.ok) {
  47. throw new Error('加载确认对话框组件失败');
  48. }
  49. const html = await response.text();
  50. // ('[GlobalConfirm] �?HTML已获取,长度:', html.length);
  51. document.body.insertAdjacentHTML('beforeend', html);
  52. // ('[GlobalConfirm] �?HTML已插入到body');
  53. } catch (error) {
  54. // ('[GlobalConfirm] �?加载确认对话框组件失�?', error);
  55. }
  56. }
  57. bindEvents() {
  58. // ('[GlobalConfirm] �?绑定事件监听�?);
  59. this.okBtn.addEventListener('click', () => {
  60. // ('[GlobalConfirm] �?用户点击确认按钮');
  61. this.hide();
  62. if (this.resolveCallback) {
  63. // ('[GlobalConfirm] �?调用 resolveCallback(true)');
  64. this.resolveCallback(true);
  65. }
  66. });
  67. this.cancelBtn.addEventListener('click', () => {
  68. // ('[GlobalConfirm] �?用户点击取消按钮');
  69. this.hide();
  70. if (this.resolveCallback) {
  71. // ('[GlobalConfirm] �?调用 resolveCallback(false)');
  72. this.resolveCallback(false);
  73. }
  74. });
  75. // 点击遮罩层取�?
  76. this.overlay.addEventListener('click', (e) => {
  77. if (e.target === this.overlay) {
  78. this.hide();
  79. if (this.resolveCallback) {
  80. this.resolveCallback(false);
  81. }
  82. }
  83. });
  84. // ESC键取�?
  85. document.addEventListener('keydown', (e) => {
  86. if (e.key === 'Escape' && this.overlay && this.overlay.classList.contains('show')) {
  87. this.hide();
  88. if (this.resolveCallback) {
  89. this.resolveCallback(false);
  90. }
  91. }
  92. });
  93. }
  94. async show(message) {
  95. // ('[GlobalConfirm] �?show() 被调�?);
  96. // ('[GlobalConfirm] 消息:', message);
  97. // ('[GlobalConfirm] ready状�?', this.ready);
  98. // 确保初始化完�?
  99. // ('[GlobalConfirm] �?等待初始化完�?..');
  100. await this.initPromise;
  101. // ('[GlobalConfirm] �?初始化完�?);
  102. // ('[GlobalConfirm] overlay:', !!this.overlay);
  103. // ('[GlobalConfirm] message:', !!this.message);
  104. // ('[GlobalConfirm] okBtn:', !!this.okBtn);
  105. // ('[GlobalConfirm] cancelBtn:', !!this.cancelBtn);
  106. return new Promise((resolve) => {
  107. // ('[GlobalConfirm] �?创建Promise,等待用户操�?);
  108. this.resolveCallback = resolve;
  109. if (this.message) {
  110. this.message.textContent = message;
  111. // ('[GlobalConfirm] �?消息已设�?);
  112. }
  113. if (this.overlay) {
  114. this.overlay.classList.add('show');
  115. // ('[GlobalConfirm] �?对话框已显示');
  116. }
  117. });
  118. }
  119. hide() {
  120. // ('[GlobalConfirm] �?隐藏对话�?);
  121. if (this.overlay) {
  122. this.overlay.classList.remove('show');
  123. // ('[GlobalConfirm] �?对话框已隐藏');
  124. }
  125. }
  126. }
  127. // 全局单例
  128. // console.log('[GlobalConfirm] 创建全局实例...');
  129. window.GlobalConfirm = new GlobalConfirm();
  130. // console.log('[GlobalConfirm] ✓ 全局实例已创建');