HM-37. Документирование классов RouteService, RoutePagesContainer. До… (#15)
This commit is contained in:
@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user