// js/ui/ui-notifications.js // 控制通知浮层的显示与关闭,供全局调用。 (function(global) { 'use strict'; const notificationContainer = document.getElementById('notification-container'); function appendToContainer(element) { if (!notificationContainer) { console.warn('notification-container not found, notification skipped.'); return null; } notificationContainer.appendChild(element); return element; } /** * 在屏幕右上角显示一个通知消息。 * 通知可以有不同的类型(info, success, warning, error),并会在指定时间后自动消失。 * 用户也可以手动点击关闭按钮来关闭通知。 * * @param {string} message - 要显示的通知消息文本。 * @param {'info' | 'success' | 'warning' | 'error'} [type='info'] - 通知的类型,决定了其图标和边框颜色。 * @param {number} [duration=5000] - 通知显示的持续时间(毫秒),之后会自动关闭。 * @returns {HTMLElement | null} 返回创建的通知 DOM 元素,主要用于测试或特殊情况下的直接操作。 */ function showNotification(message, type = 'info', duration = 5000) { const notification = document.createElement('div'); notification.className = 'pointer-events-auto w-full max-w-lg overflow-hidden rounded-lg bg-white shadow-lg ring-1 ring-black ring-opacity-5 mb-2 transition-all duration-300 ease-in-out transform translate-x-full opacity-0'; let iconName, iconColor, borderColor; switch (type) { case 'success': iconName = 'carbon:checkmark-filled'; iconColor = 'text-green-500'; borderColor = 'border-green-500'; break; case 'error': iconName = 'carbon:error-filled'; iconColor = 'text-red-500'; borderColor = 'border-red-500'; break; case 'warning': iconName = 'carbon:warning-filled'; iconColor = 'text-yellow-500'; borderColor = 'border-yellow-500'; break; default: iconName = 'carbon:information-filled'; iconColor = 'text-blue-500'; borderColor = 'border-blue-500'; break; } notification.innerHTML = `
通知
${message}