Spaces:
Running
Running
File size: 12,820 Bytes
a79063d |
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 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 |
// نظام الإشعارات والتنبيهات
class NotificationManager {
constructor() {
this.notifications = [];
this.toastContainer = null;
this.modalContainer = null;
this.init();
}
// تهيئة نظام الإشعارات
init() {
this.createToastContainer();
this.createModalContainer();
this.loadStoredNotifications();
}
// إنشاء حاوية التوست
createToastContainer() {
this.toastContainer = document.createElement('div');
this.toastContainer.className = 'toast-container';
this.toastContainer.innerHTML = '';
document.body.appendChild(this.toastContainer);
}
// إنشاء حاوية المودال
createModalContainer() {
this.modalContainer = document.createElement('div');
this.modalContainer.className = 'modal-container';
this.modalContainer.innerHTML = `
<div class="modal-overlay" onclick="this.parentElement.style.display='none'"></div>
<div class="modal-content">
<div class="modal-header">
<h3 class="modal-title"></h3>
<button class="modal-close" onclick="this.closest('.modal-container').style.display='none'">
<i class="fas fa-times"></i>
</button>
</div>
<div class="modal-body"></div>
<div class="modal-footer"></div>
</div>
`;
document.body.appendChild(this.modalContainer);
}
// عرض تنبيه توست
showToast(message, type = 'info', duration = 5000) {
const toast = document.createElement('div');
toast.className = `toast toast-${type}`;
const icon = this.getToastIcon(type);
toast.innerHTML = `
<div class="toast-icon">
<i class="${icon}"></i>
</div>
<div class="toast-content">
<div class="toast-message">${message}</div>
</div>
<button class="toast-close" onclick="this.parentElement.remove()">
<i class="fas fa-times"></i>
</button>
`;
// إضافة التوست
this.toastContainer.appendChild(toast);
// تحريك التوست للداخل
setTimeout(() => {
toast.classList.add('toast-show');
}, 100);
// إزالة التوست تلقائياً
if (duration > 0) {
setTimeout(() => {
this.removeToast(toast);
}, duration);
}
return toast;
}
// إزالة التوست
removeToast(toast) {
toast.classList.add('toast-hide');
setTimeout(() => {
if (toast.parentElement) {
toast.parentElement.removeChild(toast);
}
}, 300);
}
// الحصول على أيقونة التوست
getToastIcon(type) {
const icons = {
success: 'fas fa-check-circle',
error: 'fas fa-exclamation-circle',
warning: 'fas fa-exclamation-triangle',
info: 'fas fa-info-circle',
loading: 'fas fa-spinner fa-spin'
};
return icons[type] || icons.info;
}
// عرض مودال
showModal(title, content, buttons = []) {
const modal = this.modalContainer;
modal.querySelector('.modal-title').textContent = title;
modal.querySelector('.modal-body').innerHTML = content;
// إضافة الأزرار
const footer = modal.querySelector('.modal-footer');
footer.innerHTML = '';
buttons.forEach(button => {
const btn = document.createElement('button');
btn.className = `btn ${button.class || 'btn-primary'}`;
btn.textContent = button.text;
btn.onclick = () => {
if (button.action) button.action();
modal.style.display = 'none';
};
footer.appendChild(btn);
});
// إضافة زر إغلاق افتراضي إذا لم توجد أزرار
if (buttons.length === 0) {
const closeBtn = document.createElement('button');
closeBtn.className = 'btn btn-secondary';
closeBtn.textContent = 'إغلاق';
closeBtn.onclick = () => modal.style.display = 'none';
footer.appendChild(closeBtn);
}
modal.style.display = 'flex';
return modal;
}
// عرض مودال تأكيد
showConfirm(title, message, onConfirm, onCancel) {
return this.showModal(title, `<p>${message}</p>`, [
{
text: 'تأكيد',
class: 'btn-primary',
action: onConfirm
},
{
text: 'إلغاء',
class: 'btn-secondary',
action: onCancel
}
]);
}
// عرض مودال تحميل
showLoading(message = 'جاري التحميل...') {
const loadingContent = `
<div class="loading-content">
<div class="loading-spinner"></div>
<p>${message}</p>
</div>
`;
const modal = this.showModal('', loadingContent, []);
modal.classList.add('loading-modal');
// منع إغلاق مودال التحميل بالنقر خارجه
modal.querySelector('.modal-overlay').onclick = null;
modal.querySelector('.modal-close').style.display = 'none';
return modal;
}
// إخفاء مودال التحميل
hideLoading() {
const loadingModal = document.querySelector('.loading-modal');
if (loadingModal) {
loadingModal.style.display = 'none';
loadingModal.classList.remove('loading-modal');
}
}
// إضافة إشعار
addNotification(notification) {
const newNotification = {
id: Date.now(),
timestamp: new Date().toISOString(),
read: false,
...notification
};
this.notifications.unshift(newNotification);
this.saveNotifications();
this.updateNotificationBadge();
return newNotification;
}
// وضع علامة مقروء على الإشعار
markAsRead(notificationId) {
const notification = this.notifications.find(n => n.id === notificationId);
if (notification) {
notification.read = true;
this.saveNotifications();
this.updateNotificationBadge();
}
}
// وضع علامة مقروء على جميع الإشعارات
markAllAsRead() {
this.notifications.forEach(n => n.read = true);
this.saveNotifications();
this.updateNotificationBadge();
}
// حذف إشعار
deleteNotification(notificationId) {
this.notifications = this.notifications.filter(n => n.id !== notificationId);
this.saveNotifications();
this.updateNotificationBadge();
}
// الحصول على الإشعارات غير المقروءة
getUnreadNotifications() {
return this.notifications.filter(n => !n.read);
}
// الحصول على عدد الإشعارات غير المقروءة
getUnreadCount() {
return this.getUnreadNotifications().length;
}
// تحديث شارة الإشعارات
updateNotificationBadge() {
const badge = document.querySelector('#notifications-btn .badge');
const unreadCount = this.getUnreadCount();
if (badge) {
if (unreadCount > 0) {
badge.textContent = unreadCount > 99 ? '99+' : unreadCount;
badge.style.display = 'block';
} else {
badge.style.display = 'none';
}
}
}
// عرض قائمة الإشعارات
showNotificationsList() {
const notificationsHtml = this.notifications.length > 0 ?
this.notifications.map(n => this.renderNotification(n)).join('') :
'<div class="no-notifications">لا توجد إشعارات</div>';
const content = `
<div class="notifications-header">
<button class="btn btn-link" onclick="window.notificationManager.markAllAsRead()">
وضع علامة مقروء على الكل
</button>
</div>
<div class="notifications-list">
${notificationsHtml}
</div>
`;
this.showModal('الإشعارات', content, []);
}
// عرض إشعار واحد
renderNotification(notification) {
const timeAgo = this.getTimeAgo(notification.timestamp);
const readClass = notification.read ? 'read' : 'unread';
return `
<div class="notification-item ${readClass}" data-id="${notification.id}">
<div class="notification-icon ${notification.type}">
<i class="${this.getNotificationIcon(notification.type)}"></i>
</div>
<div class="notification-content">
<h4>${notification.title}</h4>
<p>${notification.message}</p>
<span class="notification-time">${timeAgo}</span>
</div>
<div class="notification-actions">
${!notification.read ? `
<button onclick="window.notificationManager.markAsRead(${notification.id})" title="وضع علامة مقروء">
<i class="fas fa-check"></i>
</button>
` : ''}
<button onclick="window.notificationManager.deleteNotification(${notification.id})" title="حذف">
<i class="fas fa-trash"></i>
</button>
</div>
</div>
`;
}
// الحصول على أيقونة الإشعار
getNotificationIcon(type) {
const icons = {
transaction: 'fas fa-exchange-alt',
security: 'fas fa-shield-alt',
system: 'fas fa-cog',
promotion: 'fas fa-gift',
warning: 'fas fa-exclamation-triangle'
};
return icons[type] || 'fas fa-bell';
}
// حساب الوقت المنقضي
getTimeAgo(timestamp) {
const now = new Date();
const time = new Date(timestamp);
const diffInSeconds = Math.floor((now - time) / 1000);
if (diffInSeconds < 60) return 'الآن';
if (diffInSeconds < 3600) return `${Math.floor(diffInSeconds / 60)} دقيقة`;
if (diffInSeconds < 86400) return `${Math.floor(diffInSeconds / 3600)} ساعة`;
if (diffInSeconds < 2592000) return `${Math.floor(diffInSeconds / 86400)} يوم`;
return time.toLocaleDateString('ar-SA');
}
// حفظ الإشعارات
saveNotifications() {
localStorage.setItem('unifiedWallet_notifications', JSON.stringify(this.notifications));
}
// تحميل الإشعارات المحفوظة
loadStoredNotifications() {
const stored = localStorage.getItem('unifiedWallet_notifications');
if (stored) {
this.notifications = JSON.parse(stored);
this.updateNotificationBadge();
}
}
// إضافة إشعار معاملة
addTransactionNotification(type, amount, wallet) {
const messages = {
send: `تم إرسال ${amount.toLocaleString()} ر.ي من ${wallet}`,
receive: `تم استلام ${amount.toLocaleString()} ر.ي في ${wallet}`,
bill: `تم دفع فاتورة بمبلغ ${amount.toLocaleString()} ر.ي من ${wallet}`
};
this.addNotification({
type: 'transaction',
title: 'معاملة جديدة',
message: messages[type] || `معاملة بمبلغ ${amount.toLocaleString()} ر.ي`
});
}
// إضافة إشعار أمني
addSecurityNotification(message) {
this.addNotification({
type: 'security',
title: 'تنبيه أمني',
message: message
});
}
// إضافة إشعار نظام
addSystemNotification(message) {
this.addNotification({
type: 'system',
title: 'إشعار النظام',
message: message
});
}
}
// تصدير الكلاس للاستخدام في التطبيق الرئيسي
window.NotificationManager = NotificationManager;
|