UNPKG

@eolme/vma-router

Version:
132 lines (131 loc) 4.93 kB
import History from './History'; import Route from './Route'; import { log } from '../utils/report'; class Router { constructor(routes) { this.cache = new Map(); this.history = new History(routes); this.history.on('update', this._saveCache.bind(this)); this.history.on('reload', this._replaceFromLocation.bind(this)); } _saveCache(event) { log(Router._name, 'Auto save cache.'); this.cache.set(event.next.view, event.next.panel); this.cache.set(event.prev.view, event.prev.panel); } _replaceFromLocation() { log(Router._name, 'Auto replace from location.'); const nextRoute = Route.buildFromLocation(this.history.routes, this.history.location); this.history.replace(nextRoute); } pushPage(page, params = {}) { log(Router._name, 'Call pushPage with', page); const nextRoute = Route.buildFromPage(this.history.routes, page, params); this.history.push(nextRoute); } replacePage(page, params = {}) { log(Router._name, 'Call replacePage with', page); const nextRoute = Route.buildFromPage(this.history.routes, page, params); this.history.replace(nextRoute); } popPage() { log(Router._name, 'Call popPage.'); this.history.back(); } pushPageAfterMove(prevPage, nextPage, params = {}) { log(Router._name, 'Call pushPageAfterMove from', prevPage, 'to', nextPage); const prevRoute = Route.buildFromPage(this.history.routes, prevPage); const nextRoute = Route.buildFromPage(this.history.routes, nextPage, params); this.history.pushAfterMove(prevRoute, nextRoute); } pushModal(modal, params = {}) { log(Router._name, 'Call pushModal with', modal); const currentRoute = this.history.route; const nextRoute = currentRoute.clone(); nextRoute.modal = modal; nextRoute.params = params; nextRoute.compile(currentRoute.page); this.history.push(nextRoute); } replaceModal(modal, params = {}) { log(Router._name, 'Call replaceModal with', modal); const currentRoute = this.history.route; const nextRoute = currentRoute.clone(); nextRoute.modal = modal; nextRoute.params = params; nextRoute.compile(currentRoute.page); if (currentRoute.hasModal) { this.history.replace(nextRoute); } else { this.history.push(nextRoute); } } pushPopup(popup, params = {}) { log(Router._name, 'Call pushPopup with', popup); const currentRoute = this.history.route; const nextRoute = currentRoute.clone(); nextRoute.popout = popup; nextRoute.params = params; nextRoute.compile(currentRoute.page); this.history.push(nextRoute); } replacePopup(popup, params = {}) { log(Router._name, 'Call replacePopup with', popup); const currentRoute = this.history.route; const nextRoute = currentRoute.clone(); nextRoute.popout = popup; nextRoute.params = params; nextRoute.compile(currentRoute.page); if (currentRoute.hasPopout) { this.history.replace(nextRoute); } else { this.history.push(nextRoute); } } popPageIfModal() { log(Router._name, 'Call popPageIfModal.'); const currentRoute = this.history.route; if (currentRoute.hasModal) { this.history.back(); } else { log(Router._name, 'No modal was found.'); } } popPageIfPopup() { log(Router._name, 'Call popPageIfPopup.'); const currentRoute = this.history.route; if (currentRoute.hasPopout) { this.history.back(); } else { log(Router._name, 'No popout was found.'); } } popPageIfModalOrPopup() { log(Router._name, 'Call popPageIfModalOrPopup.'); const currentRoute = this.history.route; if (currentRoute.hasModal || currentRoute.hasPopout) { this.history.back(); } else { log(Router._name, 'No modal or popup was found.'); } } canMoveTo(page, params = {}) { const route = Route.buildFromPage(this.history.routes, page, params); const index = this.history.lastIndexOf(route); return this.history.canMoveTo(index); } moveTo(page, params = {}) { log(Router._name, 'Call moveTo with', page); const route = Route.buildFromPage(this.history.routes, page, params); const index = this.history.lastIndexOf(route); this.history.moveTo(index); } } Router._name = '[Router]'; export { Router }; export default Router;