| 8 | // ============================================ |
| 9 | |
| 10 | class ToastManager { |
| 11 | constructor() { |
| 12 | this.container = null; |
| 13 | this.init(); |
| 14 | } |
| 15 | |
| 16 | init() { |
| 17 | this.container = document.createElement('div'); |
| 18 | this.container.className = 'toast-container'; |
| 19 | document.body.appendChild(this.container); |
| 20 | } |
| 21 | |
| 22 | show(message, type = 'info', duration = 4000) { |
| 23 | const toast = document.createElement('div'); |
| 24 | toast.className = `toast ${type}`; |
| 25 | |
| 26 | const icon = this.getIcon(type); |
| 27 | toast.innerHTML = ` |
| 28 | <span class="toast-icon">${icon}</span> |
| 29 | <span class="toast-message">${this.escapeHtml(message)}</span> |
| 30 | <button class="toast-close" onclick="this.parentElement.remove()">×</button> |
| 31 | `; |
| 32 | |
| 33 | this.container.appendChild(toast); |
| 34 | |
| 35 | // 自动移除 |
| 36 | setTimeout(() => { |
| 37 | toast.style.animation = 'slideOut 0.3s ease forwards'; |
| 38 | setTimeout(() => toast.remove(), 300); |
| 39 | }, duration); |
| 40 | |
| 41 | return toast; |
| 42 | } |
| 43 | |
| 44 | getIcon(type) { |
| 45 | const icons = { |
| 46 | success: '✓', |
| 47 | error: '✕', |
| 48 | warning: '⚠', |
| 49 | info: 'ℹ' |
| 50 | }; |
| 51 | return icons[type] || icons.info; |
| 52 | } |
| 53 | |
| 54 | escapeHtml(text) { |
| 55 | const div = document.createElement('div'); |
| 56 | div.textContent = text; |
| 57 | return div.innerHTML; |
| 58 | } |
| 59 | |
| 60 | success(message, duration) { |
| 61 | return this.show(message, 'success', duration); |
| 62 | } |
| 63 | |
| 64 | error(message, duration) { |
| 65 | return this.show(message, 'error', duration); |
| 66 | } |
| 67 |
nothing calls this directly
no outgoing calls
no test coverage detected