/** * Toast Notification System * Displays temporary notification messages */ const TOAST_DEFAULTS = { MAX_VISIBLE: 3, DEFAULT_DURATION: 3500, ERROR_DURATION: 6000, }; // Configuration cache let TOAST_CONFIG = null; function getToastConfig() { if (TOAST_CONFIG !== null) { return TOAST_CONFIG; } // Try to get CONFIG from window (set by config.js or other scripts) try { const configSource = window.CONFIG || {}; TOAST_CONFIG = { ...TOAST_DEFAULTS, ...(configSource?.TOAST || {}), }; } catch (e) { // Fallback to defaults if CONFIG not available TOAST_CONFIG = { ...TOAST_DEFAULTS }; } return TOAST_CONFIG; } export class Toast { static container = null; static toasts = []; static maxToasts = TOAST_DEFAULTS.MAX_VISIBLE; /** * Initialize toast container */ static init() { if (this.container) return; this.container = document.getElementById('toast-container'); if (!this.container) { this.container = document.createElement('div'); this.container.id = 'toast-container'; this.container.className = 'toast-container'; document.body.appendChild(this.container); } } /** * Show a toast notification */ static show(message, type = 'info', options = {}) { this.init(); const config = getToastConfig(); const toast = { id: Date.now() + Math.random(), message, type, duration: options.duration ?? (type === 'error' ? config.ERROR_DURATION : config.DEFAULT_DURATION), dismissible: options.dismissible !== false, action: options.action || null, }; // Remove oldest toast if at max if (this.toasts.length >= this.maxToasts) { const oldest = this.toasts.shift(); this.dismiss(oldest.id); } this.toasts.push(toast); this.render(toast); // Auto-dismiss if (toast.duration > 0) { setTimeout(() => this.dismiss(toast.id), toast.duration); } return toast.id; } /** * Render toast element */ static render(toast) { const el = document.createElement('div'); el.className = `toast toast-${toast.type}`; el.setAttribute('data-toast-id', toast.id); el.setAttribute('role', 'alert'); el.setAttribute('aria-live', 'polite'); const icon = this.getIcon(toast.type); el.innerHTML = `