File size: 7,403 Bytes
8b7b267 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
/**
* ============================================
* TOAST NOTIFICATION SYSTEM
* Enterprise Edition - Crypto Monitor Ultimate
* ============================================
*
* Beautiful toast notifications with:
* - Multiple types (success, error, warning, info)
* - Auto-dismiss
* - Progress bar
* - Stack management
* - Accessibility support
*/
class ToastManager {
constructor() {
this.toasts = [];
this.container = null;
this.maxToasts = 5;
this.defaultDuration = 5000;
this.init();
}
/**
* Initialize toast container
*/
init() {
// Create container if it doesn't exist
if (!document.getElementById('toast-container')) {
this.container = document.createElement('div');
this.container.id = 'toast-container';
this.container.className = 'toast-container';
this.container.setAttribute('role', 'region');
this.container.setAttribute('aria-label', 'Notifications');
this.container.setAttribute('aria-live', 'polite');
document.body.appendChild(this.container);
} else {
this.container = document.getElementById('toast-container');
}
console.log('[Toast] Toast manager initialized');
}
/**
* Show a toast notification
* @param {string} message - Toast message
* @param {string} type - Toast type (success, error, warning, info)
* @param {object} options - Additional options
*/
show(message, type = 'info', options = {}) {
const {
duration = this.defaultDuration,
title = null,
icon = null,
dismissible = true,
action = null
} = options;
// Remove oldest toast if max reached
if (this.toasts.length >= this.maxToasts) {
this.dismiss(this.toasts[0].id);
}
const toast = {
id: this.generateId(),
message,
type,
title,
icon: icon || this.getDefaultIcon(type),
dismissible,
action,
duration,
createdAt: Date.now()
};
this.toasts.push(toast);
this.render(toast);
// Auto dismiss if duration is set
if (duration > 0) {
setTimeout(() => this.dismiss(toast.id), duration);
}
return toast.id;
}
/**
* Show success toast
*/
success(message, options = {}) {
return this.show(message, 'success', options);
}
/**
* Show error toast
*/
error(message, options = {}) {
return this.show(message, 'error', { ...options, duration: options.duration || 7000 });
}
/**
* Show warning toast
*/
warning(message, options = {}) {
return this.show(message, 'warning', options);
}
/**
* Show info toast
*/
info(message, options = {}) {
return this.show(message, 'info', options);
}
/**
* Dismiss a toast
*/
dismiss(toastId) {
const toastElement = document.getElementById(`toast-${toastId}`);
if (!toastElement) return;
// Add exit animation
toastElement.classList.add('toast-exit');
setTimeout(() => {
toastElement.remove();
this.toasts = this.toasts.filter(t => t.id !== toastId);
}, 300);
}
/**
* Dismiss all toasts
*/
dismissAll() {
const toastIds = this.toasts.map(t => t.id);
toastIds.forEach(id => this.dismiss(id));
}
/**
* Render a toast
*/
render(toast) {
const toastElement = document.createElement('div');
toastElement.id = `toast-${toast.id}`;
toastElement.className = `toast toast-${toast.type} glass-effect`;
toastElement.setAttribute('role', 'alert');
toastElement.setAttribute('aria-atomic', 'true');
const iconHtml = window.getIcon
? window.getIcon(toast.icon, 24)
: '';
const titleHtml = toast.title
? `<div class="toast-title">${toast.title}</div>`
: '';
const actionHtml = toast.action
? `<button class="toast-action" onclick="${toast.action.onClick}">${toast.action.label}</button>`
: '';
const closeButton = toast.dismissible
? `<button class="toast-close" onclick="window.toastManager.dismiss('${toast.id}')" aria-label="Close notification">
${window.getIcon ? window.getIcon('close', 20) : '×'}
</button>`
: '';
const progressBar = toast.duration > 0
? `<div class="toast-progress" style="animation-duration: ${toast.duration}ms"></div>`
: '';
toastElement.innerHTML = `
<div class="toast-icon">
${iconHtml}
</div>
<div class="toast-content">
${titleHtml}
<div class="toast-message">${toast.message}</div>
${actionHtml}
</div>
${closeButton}
${progressBar}
`;
this.container.appendChild(toastElement);
// Trigger entrance animation
setTimeout(() => toastElement.classList.add('toast-enter'), 10);
}
/**
* Get default icon for type
*/
getDefaultIcon(type) {
const icons = {
success: 'checkCircle',
error: 'alertCircle',
warning: 'alertCircle',
info: 'info'
};
return icons[type] || 'info';
}
/**
* Generate unique ID
*/
generateId() {
return `${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
}
/**
* Show provider error toast
*/
showProviderError(providerName, error) {
return this.error(
`Failed to connect to ${providerName}`,
{
title: 'Provider Error',
duration: 7000,
action: {
label: 'Retry',
onClick: `window.providerDiscovery.checkProviderHealth('${providerName}')`
}
}
);
}
/**
* Show provider success toast
*/
showProviderSuccess(providerName) {
return this.success(
`Successfully connected to ${providerName}`,
{
title: 'Provider Online',
duration: 3000
}
);
}
/**
* Show API rate limit warning
*/
showRateLimitWarning(providerName, retryAfter) {
return this.warning(
`Rate limit reached for ${providerName}. Retry after ${retryAfter}s`,
{
title: 'Rate Limit',
duration: 6000
}
);
}
}
// Export singleton instance
window.toastManager = new ToastManager();
// Utility shortcuts
window.showToast = (message, type, options) => window.toastManager.show(message, type, options);
window.toast = {
success: (msg, opts) => window.toastManager.success(msg, opts),
error: (msg, opts) => window.toastManager.error(msg, opts),
warning: (msg, opts) => window.toastManager.warning(msg, opts),
info: (msg, opts) => window.toastManager.info(msg, opts)
};
console.log('[Toast] Toast notification system ready');
|