92 lines
3.8 KiB
JavaScript
92 lines
3.8 KiB
JavaScript
|
|
// Pages
|
|
// Core
|
|
import PageHome from './pages/core/home.js';
|
|
import PageContact from './pages/core/contact.js';
|
|
import PageContactSuccess from './pages/core/contact-success.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';
|
|
|
|
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 };
|
|
this.pages[hashPageContact] = { name: 'PageContact', module: PageContact };
|
|
this.pages[hashPageContactSuccess] = { name: 'PageContact', module: PageContactSuccess };
|
|
// 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 };
|
|
// Routes
|
|
this.routes = {};
|
|
// Core
|
|
this.routes[hashPageHome] = (isPopState = false) => this.navigateToHash(hashPageHome, isPopState);
|
|
this.routes[hashPageContact] = (isPopState = false) => this.navigateToHash(hashPageContact, 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);
|
|
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();
|