/
delphin
/
CatRW
Обзор
Документация
Войти
/
delphin
/
CatRW
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
multilang
js/admin.js
152 строки
5 KB
delphin
multilang add
11 мар 2026, 09:25
Верифицирован
11 мар 2026, 09:25
90e28cc
Код
Авторство
О чём код?
// admin.js - общая логика админки + система уведомлений class Notifications { constructor() { this.containerId = 'globalAlerts'; this.initContainer(); } initContainer() { let container = document.getElementById(this.containerId); if (!container) { container = document.createElement('div'); container.id = this.containerId; container.style.cssText = ` position: fixed; top: 20px; right: 20px; z-index: 9999; max-width: 400px; `; document.body.appendChild(container); } this.container = container; } show(message, type = 'info', duration = 4000) { const icons = { success: '✅', error: '❌', warning: '⚠️', info: 'ℹ️' }; const icon = icons[type] || icons.info; const notificationId = 'alert-' + Date.now() + '-' + Math.random().toString(36).substr(2, 9); const notification = document.createElement('div'); notification.id = notificationId; notification.className = `alert alert-${type}`; notification.innerHTML = ` <span class="alert-icon">${icon}</span> <span class="alert-message">${this.escapeHtml(message)}</span> <button class="alert-close" onclick="window.Notifications.hide('${notificationId}')">✕</button> `; this.container.insertBefore(notification, this.container.firstChild); // Ограничиваем количество const allNotifications = this.container.querySelectorAll('.alert'); if (allNotifications.length > 3) { for (let i = 3; i < allNotifications.length; i++) { this.hide(allNotifications[i].id); } } if (type !== 'error' && duration > 0) { setTimeout(() => { this.hide(notificationId); }, duration); } return notificationId; } hide(notificationId) { const notification = document.getElementById(notificationId); if (notification && notification.parentElement) { notification.style.animation = 'fadeOut 0.3s ease-out forwards'; setTimeout(() => { if (notification.parentElement) { notification.remove(); } }, 300); } } success(message, duration = 4000) { return this.show(message, 'success', duration); } error(message, duration = 0) { return this.show(message, 'error', duration); } warning(message, duration = 4000) { return this.show(message, 'warning', duration); } info(message, duration = 4000) { return this.show(message, 'info', duration); } escapeHtml(text) { if (!text) return ''; const div = document.createElement('div'); div.textContent = text; return div.innerHTML; } } // Создаём глобальный экземпляр window.Notifications = new Notifications(); // ===== УПРАВЛЕНИЕ КЭШЕМ ПЕРЕВОДОВ ===== /** * Очищает кэш переводов при смене языка * Вызывается из обработчика смены языка */ window.clearTranslationCache = function() { if (typeof window.__clearCache === 'function') { window.__clearCache(); console.log('🧹 Кэш переводов очищен через admin.js'); } else { console.warn('⚠️ Функция очистки кэша переводов не найдена в i18n.js'); } }; /** * Перезагружает страницу с новым языком и очищает кэш * @param {string} lang - код языка (ru, en, de) */ window.changeLanguage = function(lang) { const currentLang = window.currentLang || 'ru'; if (lang !== currentLang) { // Очищаем кэш переводов window.clearTranslationCache(); // Устанавливаем куку document.cookie = `lang=${lang}; path=/; max-age=${60*60*24*30}`; // Перезагружаем страницу window.location.reload(); } }; // Для обратной совместимости window.showStatusAlert = function(message, type = 'info') { return Notifications[type] ? Notifications[type](message) : Notifications.info(message); }; // Автоматически очищаем кэш при загрузке страницы (на всякий случай) document.addEventListener('DOMContentLoaded', function() { // Небольшая задержка, чтобы i18n.js успел загрузиться setTimeout(() => { if (typeof window.__clearCache === 'function') { console.log('✅ Система кэширования переводов доступна'); } }, 100); });