/* Toast notification system */ function _toastContainer() { let c = document.getElementById('toast-container'); if (!c) { c = document.createElement('div'); c.id = 'toast-container'; c.className = 'toast-container'; document.body.appendChild(c); } return c; } function _esc(s) { return String(s).replace(/&/g,'&').replace(//g,'>'); } function showToast(message, type = 'success') { const tone = type === 'error' ? 'error' : (type === 'info' ? 'info' : 'success'); const icon = tone === 'success' ? '' : tone === 'error' ? '' : ''; const el = document.createElement('div'); el.className = `toast toast-${tone}`; el.innerHTML = `
${icon}
${_esc(message)}
`; _toastContainer().appendChild(el); setTimeout(() => { el.classList.add('out'); el.addEventListener('animationend', () => el.remove(), { once: true }); }, 3000); } /** * Show a persistent progress toast that stays open until finish() is called. * * @param {string} label Initial label text shown inside the toast. * @returns {{ update(done, total): void, finish(msg, type?): void, dismiss(): void }} * * Usage: * const p = showProgressToast('正在刷新 Usage…'); * p.update(10, 50); // update progress bar and counter * p.finish('完成:50 个', 'success'); // switch to result state, auto-dismiss */ function showProgressToast(label) { const SPIN = ''; const el = document.createElement('div'); el.className = 'toast toast-info'; el.innerHTML = `
${SPIN}
${_esc(label)}
`; _toastContainer().appendChild(el); const iconEl = el.querySelector('.toast-icon'); const labelEl = el.querySelector('.toast-progress-label'); const fill = el.querySelector('.toast-progress-fill'); const countEl = el.querySelector('.toast-progress-count'); function _dismiss() { el.classList.add('out'); el.addEventListener('animationend', () => el.remove(), { once: true }); } return { update(done, total) { const pct = total > 0 ? Math.min(100, Math.round(done / total * 100)) : 0; fill.style.width = pct + '%'; countEl.textContent = total > 0 ? `${done} / ${total}` : `${done}`; }, finish(msg, type = 'success') { const tone = type === 'error' ? 'error' : 'success'; const icon = tone === 'success' ? '' : ''; el.className = `toast toast-${tone}`; iconEl.innerHTML = icon; fill.style.width = '100%'; labelEl.textContent = msg; countEl.textContent = ''; setTimeout(_dismiss, 3000); }, dismiss: _dismiss, }; }