/** * 模态框组件 */ import { addClass, removeClass, hasClass } from '../core/utils.js'; export class Modal { constructor(options = {}) { this.id = options.id || `modal-${Date.now()}`; this.title = options.title || ''; this.content = options.content || ''; this.size = options.size || ''; // sm, lg, xl this.backdrop = options.backdrop !== false; this.keyboard = options.keyboard !== false; this.centered = options.centered || false; this.scrollable = options.scrollable || false; this.static = options.static || false; this.className = options.className || ''; this.footer = options.footer || null; this.show = false; this.onShow = options.onShow || (() => {}); this.onShown = options.onShown || (() => {}); this.onHide = options.onHide || (() => {}); this.onHidden = options.onHidden || (() => {}); this.init(); } init() { this.createModal(); this.bindEvents(); } createModal() { // 创建模态框容器 this.modal = document.createElement('div'); this.modal.className = 'modal fade'; this.modal.id = this.id; this.modal.setAttribute('tabindex', '-1'); this.modal.setAttribute('aria-labelledby', `${this.id}-label`); this.modal.setAttribute('aria-hidden', 'true'); // 模态框对话框 const dialog = document.createElement('div'); dialog.className = `modal-dialog ${this.size ? `modal-${this.size}` : ''} ${this.centered ? 'modal-dialog-centered' : ''} ${this.scrollable ? 'modal-dialog-scrollable' : ''}`; // 模态框内容 const content = document.createElement('div'); content.className = 'modal-content'; if (this.className) { addClass(content, this.className); } // 构建模态框HTML let modalHTML = `
${options.message || '确定要执行此操作吗?'}
${options.message || ''}