// toast.js — Unified toast notification system 'use strict'; (function() { var container = null; var toasts = []; // active toast objects var serviceToasts = {}; // serviceKey → toast id var nextId = 1; var MAX_VISIBLE = 5; // Auto-dismiss defaults (ms). 0 = no auto-dismiss. var DEFAULTS = { success: 5000, info: 5000, warning: 10000, error: 0 }; var ICONS = { error: '\u2718', warning: '\u26A0', success: '\u2714', info: '\u2139' }; function ensureContainer() { if (container && document.body.contains(container)) return; container = document.createElement('div'); container.className = 'toast-container'; container.setAttribute('aria-live', 'assertive'); container.setAttribute('role', 'status'); document.body.appendChild(container); } /** * Show a toast notification. * @param {string} message - The message text. * @param {Object} [opts] - Options. * @param {string} [opts.type='info'] - 'error', 'warning', 'success', 'info'. * @param {number} [opts.duration] - Auto-dismiss in ms. 0 = stays. * @param {Function} [opts.action] - Callback for single action button. * @param {string} [opts.actionLabel] - Text for single action button. * @param {Array} [opts.actions] - Multiple buttons: [{label, callback, className}]. * @param {string} [opts.serviceKey] - Links toast to a service for auto-dismiss. * @returns {number} Toast ID. */ function toast(message, opts) { opts = opts || {}; var type = opts.type || 'info'; var duration = opts.duration !== undefined ? opts.duration : (DEFAULTS[type] || 5000); ensureContainer(); // If serviceKey set, dismiss existing toast for that service if (opts.serviceKey && serviceToasts[opts.serviceKey]) { dismissToast(serviceToasts[opts.serviceKey]); } // Enforce max visible — remove oldest non-error first while (toasts.length >= MAX_VISIBLE) { var removed = false; for (var i = 0; i < toasts.length; i++) { if (toasts[i].type !== 'error') { dismissToast(toasts[i].id); removed = true; break; } } if (!removed) { // All are errors — remove the oldest dismissToast(toasts[0].id); } } var id = nextId++; var el = document.createElement('div'); el.className = 'toast toast-' + type; el.setAttribute('role', 'alert'); // Icon var icon = document.createElement('span'); icon.className = 'toast-icon'; icon.setAttribute('aria-hidden', 'true'); icon.textContent = ICONS[type] || ICONS.info; el.appendChild(icon); // Body var body = document.createElement('div'); body.className = 'toast-body'; var msg = document.createElement('span'); msg.className = 'toast-message'; msg.textContent = message; body.appendChild(msg); // Action buttons — support single (action/actionLabel) or multiple (actions array) var actionList = opts.actions || []; if (!actionList.length && opts.action && opts.actionLabel) { actionList = [{ label: opts.actionLabel, callback: opts.action }]; } if (actionList.length) { var actionsEl = document.createElement('div'); actionsEl.className = 'toast-actions'; for (var a = 0; a < actionList.length; a++) { (function(act) { var btn = document.createElement('button'); btn.type = 'button'; btn.className = 'toast-action-btn' + (act.className ? ' ' + act.className : ''); btn.textContent = act.label; btn.addEventListener('click', function() { act.callback(); }); actionsEl.appendChild(btn); })(actionList[a]); } body.appendChild(actionsEl); } el.appendChild(body); // Dismiss button var dismiss = document.createElement('button'); dismiss.type = 'button'; dismiss.className = 'toast-dismiss'; dismiss.setAttribute('aria-label', typeof i18n === 'function' ? i18n('toast.dismiss', 'Dismiss') : 'Dismiss'); dismiss.textContent = '\u00D7'; dismiss.addEventListener('click', function() { dismissToast(id); }); el.appendChild(dismiss); container.appendChild(el); var entry = { id: id, el: el, type: type, serviceKey: opts.serviceKey || null, timer: null }; if (duration > 0) { entry.timer = setTimeout(function() { dismissToast(id); }, duration); } toasts.push(entry); if (opts.serviceKey) { serviceToasts[opts.serviceKey] = id; } return id; } /** * Dismiss a toast by ID. */ function dismissToast(id) { for (var i = 0; i < toasts.length; i++) { if (toasts[i].id === id) { var entry = toasts[i]; if (entry.timer) clearTimeout(entry.timer); if (entry.serviceKey && serviceToasts[entry.serviceKey] === id) { delete serviceToasts[entry.serviceKey]; } // Animate out entry.el.classList.add('toast-exit'); var el = entry.el; setTimeout(function() { if (el.parentNode) el.parentNode.removeChild(el); }, 200); toasts.splice(i, 1); return; } } } /** * Dismiss the toast linked to a service key. */ function dismissServiceToast(serviceKey) { if (serviceToasts[serviceKey]) { dismissToast(serviceToasts[serviceKey]); } } // Expose on the PiStar namespace (created by pistar.js) if (!window.pistar) window.pistar = {}; window.pistar.toast = toast; window.pistar.dismissToast = dismissToast; window.pistar.dismissServiceToast = dismissServiceToast; })();