// Pages // Core import PageHome from './pages/core/home.js'; // Dog import PageDogHome from './pages/dog/home.js'; import PageDogCommandCategories from './pages/dog/command_categories.js'; import PageDogCommands from './pages/dog/commands.js'; import PageDogDogCommandLinks from './pages/dog/dog_command_links.js'; // import PageDogDogs from './pages/dog/dogs.js'; import PageDogLocations from './pages/dog/locations.js'; import PageDogButtonIcons from './pages/dog/button_icons.js'; import PageDogCommandButtonLinks from './pages/dog/command_button_links.js'; // Legal import PageAccessibilityReport from './pages/legal/accessibility_report.js'; import PageAccessibilityStatement from './pages/legal/accessibility_statement.js'; import PageLicense from './pages/legal/license.js'; import PagePrivacyPolicy from './pages/legal/privacy_policy.js'; import PageRetentionSchedule from './pages/legal/retention_schedule.js'; // User // import PageUserLogin from './pages/user/login.js'; // import PageUserLogout from './pages/user/logout.js'; // import PageUserAccount from './pages/user/account.js'; import API from './api.js'; import DOM from './dom.js'; import Utils from './lib/utils.js'; export default class Router { constructor() { // Pages this.pages = {}; // Core this.pages[hashPageHome] = { name: 'PageHome', module: PageHome }; // Dog this.pages[hashPageDogHome] = { name: 'PageDogHome', module: PageDogHome }; this.pages[hashPageDogCommandCategories] = { name: 'PageDogCommands', module: PageDogCommandCategories }; this.pages[hashPageDogCommands] = { name: 'PageDogCommands', module: PageDogCommands }; this.pages[hashPageDogDogCommandLinks] = { name: 'PageDogDogCommandLinks', module: PageDogDogCommandLinks }; // this.pages[hashPageDogDogs] = { name: 'PageDogDogs', module: PageDogDogs }; this.pages[hashPageDogLocations] = { name: 'PageDogLocations', module: PageDogLocations }; this.pages[hashPageDogButtonIcons] = { name: 'PageDogButtonIcons', module: PageDogButtonIcons }; this.pages[hashPageDogCommandButtonLinks] = { name: 'PageDogCommandButtonLinks', module: PageDogCommandButtonLinks }; // Legal this.pages[hashPageAccessibilityStatement] = { name: 'PageAccessibilityStatement', module: PageAccessibilityStatement }; this.pages[hashPageDataRetentionSchedule] = { name: 'PageDataRetentionSchedule', module: PageRetentionSchedule }; this.pages[hashPageLicense] = { name: 'PageLicense', module: PageLicense }; this.pages[hashPagePrivacyPolicy] = { name: 'PagePrivacyPolicy', module: PagePrivacyPolicy }; // User // this.pages[hashPageUserLogin] = { name: 'PageUserLogin', module: PageUserLogin }; // pathModule: './pages/user/login.js' }; // this.pages[hashPageUserLogout] = { name: 'PageUserLogout', module: PageUserLogout }; // pathModule: './pages/user/logout.js' }; // this.pages[hashPageUserAccount] = { name: 'PageUserAccount', module: PageUserAccount }; // pathModule: './pages/user/account.js' }; // Routes this.routes = {}; // Core this.routes[hashPageHome] = (isPopState = false) => this.navigateToHash(hashPageHome, isPopState); // Dog this.routes[hashPageDogHome] = (isPopState = false) => this.navigateToHash(hashPageDogHome, isPopState); this.routes[hashPageDogCommandCategories] = (isPopState = false) => this.navigateToHash(hashPageDogCommandCategories, isPopState); this.routes[hashPageDogCommands] = (isPopState = false) => this.navigateToHash(hashPageDogCommands, isPopState); this.routes[hashPageDogDogCommandLinks] = (isPopState = false) => this.navigateToHash(hashPageDogDogCommandLinks, isPopState); // this.routes[hashPageDogDogs] = (isPopState = false) => this.navigateToHash(hashPageDogDogs, isPopState); this.routes[hashPageDogLocations] = (isPopState = false) => this.navigateToHash(hashPageDogLocations, isPopState); this.routes[hashPageDogButtonIcons] = (isPopState = false) => this.navigateToHash(hashPageDogButtonIcons, isPopState); this.routes[hashPageDogCommandButtonLinks] = (isPopState = false) => this.navigateToHash(hashPageDogCommandButtonLinks, isPopState); // Legal this.routes[hashPageAccessibilityStatement] = (isPopState = false) => this.navigateToHash(hashPageAccessibilityStatement, isPopState); this.routes[hashPageDataRetentionSchedule] = (isPopState = false) => this.navigateToHash(hashPageDataRetentionSchedule, isPopState); this.routes[hashPageLicense] = (isPopState = false) => this.navigateToHash(hashPageLicense, isPopState); this.routes[hashPagePrivacyPolicy] = (isPopState = false) => this.navigateToHash(hashPagePrivacyPolicy, isPopState); // User // this.routes[hashPageUserLogin] = (isPopState = false) => this.navigateToHash(hashPageUserLogin, isPopState); // this.routes[hashPageUserLogout] = (isPopState = false) => this.navigateToHash(hashPageUserLogout, isPopState); // this.routes[hashPageUserAccount] = (isPopState = false) => this.navigateToHash(hashPageUserAccount, isPopState); this.initialize(); } loadPage(hashPage, isPopState = false) { const PageClass = this.getClassPageFromHash(hashPage); this.currentPage = new PageClass(this); this.currentPage.initialize(isPopState); window.addEventListener('beforeunload', () => this.currentPage.leave()); } getClassPageFromHash(hashPage) { let pageJson = this.pages[hashPage]; try { const module = pageJson.module; return module; } catch (error) { Utils.consoleLogIfNotProductionEnvironment("this.pages: ", this.pages); console.error('Page not found:', hashPage); throw error; } } initialize() { window.addEventListener('popstate', this.handlePopState.bind(this)); } handlePopState(event) { this.loadPageCurrent(); } loadPageCurrent() { const hashPageCurrent = DOM.getHashPageCurrent(); this.loadPage(hashPageCurrent); } navigateToHash(hash, data = null, params = null, isPopState = false) { let url = API.getUrlFromHash(hash, params); history.pushState({data: data, params: params}, '', hash); API.goToUrl(url, data); } navigateToUrl(url, data = null, appendHistory = true) { // this.beforeLeave(); if (appendHistory) history.pushState(data, '', url); url = API.parameteriseUrl(url, data); API.goToUrl(url); } static loadPageBodyFromResponse(response) { DOM.loadPageBody(response.data); } } export const router = new Router();