EDUrocks's picture
Add files using upload-large-folder tool
37d6cb3 verified
Raw History Blame Contribute Delete
7.74 kB
/**
* EDUrocks Notification System
* Toast notifications for save sync status, achievements, etc.
*/
(function() {
'use strict';
const NotificationSystem = {
container: null,
notifications: [],
maxNotifications: 3,
init: function() {
// Create notification container
this.container = document.createElement('div');
this.container.id = 'edurocks-notifications';
this.container.style.cssText = `
position: fixed;
top: 20px;
right: 20px;
z-index: 999999;
pointer-events: none;
`;
document.body.appendChild(this.container);
console.log('[Notifications] System initialized');
},
show: function(options) {
const {
type = 'info', // 'success', 'error', 'warning', 'info', 'achievement'
title = 'Notification',
message = '',
duration = 4000,
icon = null
} = options;
// Remove oldest if too many
if (this.notifications.length >= this.maxNotifications) {
this.notifications[0].remove();
}
const notification = document.createElement('div');
notification.className = `edurocks-notification edurocks-notification-${type}`;
const iconMap = {
success: '✓',
error: '✕',
warning: '⚠',
info: 'ℹ',
achievement: '🏆'
};
const displayIcon = icon || iconMap[type] || iconMap.info;
notification.innerHTML = `
<div class="notification-content">
<div class="notification-icon">${displayIcon}</div>
<div class="notification-text">
<div class="notification-title">${this.escapeHtml(title)}</div>
${message ? `<div class="notification-message">${this.escapeHtml(message)}</div>` : ''}
</div>
<button class="notification-close">&times;</button>
</div>
`;
// Add styles
notification.style.cssText = `
background: ${this.getColorForType(type)};
color: white;
padding: 16px;
margin-bottom: 10px;
border-radius: 8px;
box-shadow: 0 4px 12px rgba(0,0,0,0.3);
min-width: 300px;
max-width: 400px;
pointer-events: all;
animation: slideInRight 0.3s ease-out;
display: flex;
align-items: center;
font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
`;
const content = notification.querySelector('.notification-content');
content.style.cssText = `
display: flex;
align-items: flex-start;
gap: 12px;
width: 100%;
`;
const iconEl = notification.querySelector('.notification-icon');
iconEl.style.cssText = `
font-size: 24px;
flex-shrink: 0;
`;
const textEl = notification.querySelector('.notification-text');
textEl.style.cssText = `
flex: 1;
min-width: 0;
`;
const titleEl = notification.querySelector('.notification-title');
titleEl.style.cssText = `
font-weight: 600;
font-size: 14px;
margin-bottom: 4px;
`;
const messageEl = notification.querySelector('.notification-message');
if (messageEl) {
messageEl.style.cssText = `
font-size: 13px;
opacity: 0.9;
line-height: 1.4;
`;
}
const closeBtn = notification.querySelector('.notification-close');
closeBtn.style.cssText = `
background: none;
border: none;
color: white;
font-size: 20px;
cursor: pointer;
padding: 0;
width: 24px;
height: 24px;
flex-shrink: 0;
opacity: 0.7;
transition: opacity 0.2s;
`;
closeBtn.onmouseover = () => closeBtn.style.opacity = '1';
closeBtn.onmouseout = () => closeBtn.style.opacity = '0.7';
closeBtn.onclick = () => this.remove(notification);
this.container.appendChild(notification);
this.notifications.push(notification);
// Auto-remove after duration
if (duration > 0) {
setTimeout(() => this.remove(notification), duration);
}
return notification;
},
remove: function(notification) {
notification.style.animation = 'slideOutRight 0.3s ease-in';
setTimeout(() => {
if (notification.parentNode) {
notification.parentNode.removeChild(notification);
}
const index = this.notifications.indexOf(notification);
if (index > -1) {
this.notifications.splice(index, 1);
}
}, 300);
},
getColorForType: function(type) {
const colors = {
success: '#10b981',
error: '#ef4444',
warning: '#f59e0b',
info: '#3b82f6',
achievement: '#8b5cf6'
};
return colors[type] || colors.info;
},
escapeHtml: function(text) {
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
},
// Convenience methods
success: function(title, message) {
return this.show({ type: 'success', title, message });
},
error: function(title, message) {
return this.show({ type: 'error', title, message });
},
warning: function(title, message) {
return this.show({ type: 'warning', title, message });
},
info: function(title, message) {
return this.show({ type: 'info', title, message });
},
achievement: function(title, message, icon) {
return this.show({ type: 'achievement', title, message, icon, duration: 6000 });
}
};
// Add animation styles
const style = document.createElement('style');
style.textContent = `
@keyframes slideInRight {
from {
transform: translateX(400px);
opacity: 0;
}
to {
transform: translateX(0);
opacity: 1;
}
}
@keyframes slideOutRight {
from {
transform: translateX(0);
opacity: 1;
}
to {
transform: translateX(400px);
opacity: 0;
}
}
`;
document.head.appendChild(style);
// Auto-initialize when DOM is ready
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', () => NotificationSystem.init());
} else {
NotificationSystem.init();
}
// Make globally available
window.EDUrocksNotifications = NotificationSystem;
})();