/
Gabryelf
/
Icons-Customize-Gen
Обзор
Документация
Войти
/
Gabryelf
/
Icons-Customize-Gen
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
js/core/Router.js
61 строка
2 KB
Valeev Serj
update_to_version_0.0.1
15 июл 2026, 23:13
15 июл 2026, 23:13
68eecb6
Код
Авторство
О чём код?
// регистратор и переключатель путей // core/Router.js export class Router { constructor(routes, eventBus) { this.routes = routes; this.currentPage = null; this.events = eventBus; this.container = document.getElementById('appContainer'); if (!this.container) { this.container = document.createElement('div'); this.container.id = 'appContainer'; document.body.appendChild(this.container); } // Обработка навигации window.addEventListener('popstate', () => { const path = window.location.pathname.replace('/', '') || 'dashboard'; this.navigate(path); }); // Подписка на события смены страницы this.events.on('page:change', (page) => { this.navigate(page); }); this.events.on('project:open', (projectId) => { this.navigate('editor', { projectId }); }); } navigate(path, data = {}) { // Очищаем контейнер if (this.container) { this.container.innerHTML = ''; } // Находим маршрут const route = this.routes.find(r => r.path === path); if (!route) { this.navigate('dashboard'); return; } // Создаем экземпляр страницы try { const PageClass = route.component; const page = new PageClass(this.container, this.events, data); page.render(); this.currentPage = page; // Обновляем URL const url = path === 'dashboard' ? '/' : `/${path}`; history.pushState({}, '', url); } catch (error) { console.error('Ошибка навигации:', error); this.navigate('dashboard'); } } }