HM-37. Документирование классов RouteService, RoutePagesContainer. До… (#15)

This commit is contained in:
Nikolay
2020-07-16 09:27:57 +03:00
committed by GitHub
parent e1bc9e6dcd
commit 8ef0e49948
4 changed files with 115 additions and 4 deletions

View File

@ -4,28 +4,62 @@ import NotFoundPage from '../not-found-page';
import './RouterPagesContainer.css';
/**
* @interface Route
* @property {string} url - маршрут страницы начинается с "/"
* @property {Component} pageComponent - компонент (класс) страницы
*/
/**
* Класс для рендера страниц при изменении роутинга
*/
class RouterPagesContainer extends Component {
/**
* Список всех маршрутов
* @type {Route[]}
*/
routes = [];
/**
* Текущая открытая страница
* @type {Component}
*/
currentPage;
/**
* Текущий открытый url
* @type {string}
*/
url;
constructor () {
super('#page-container', document.body);
routeService.onChange(({url}) => {
// Если под указанный url нет pageComponent, то будет испольщована страница NotFound
const {pageComponent: PageComponent = NotFoundPage} = this.routes.find((route) => {
return route.url === url;
}) || {};
// Удаляет предыдущую страницу
if (this.currentPage) {
this.currentPage.destroy();
}
this.currentPage = new PageComponent('#page', this.mainNode);
// Рендерит новую страницу, если url изменился
if (url !== this.currentUrl) {
this.currentUrl = url;
this.currentPage = new PageComponent('#page', this.mainNode);
}
});
}
/**
* Добавляет страницы в компонент, чтобы рендерить их при изменении маршрута. Рекомендуется
* все страницы передавать в app.js в одном месте
* @param {Route[]} routes - список маршрутов с компонентами
*/
addRoutes = (routes) => {
this.routes = this.routes.concat(routes);
}