Feat: Multiplayer sessions added using CRUD database.
This commit is contained in:
110
static/js/router.js
Normal file
110
static/js/router.js
Normal file
@@ -0,0 +1,110 @@
|
||||
|
||||
// Pages
|
||||
// Core
|
||||
// TCG
|
||||
import PageMtgGame from './pages/tcg/mtg_game.js';
|
||||
import PageMtgGames from './pages/tcg/mtg_games.js';
|
||||
import PageMtgHome from './pages/tcg/mtg_home.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 PageUser from './pages/user/user.js';
|
||||
import PageUsers from './pages/user/users.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
|
||||
// TCG
|
||||
this.pages[hashPageMtgGame] = { name: 'PageMtgGame', module: PageMtgGame };
|
||||
this.pages[hashPageMtgGames] = { name: 'PageMtgGames', module: PageMtgGames };
|
||||
this.pages[hashPageMtgHome] = { name: 'PageMtgGame', module: PageMtgHome };
|
||||
// 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: 'PageUser', module: PageUser };
|
||||
this.pages[hashPageUserAccounts] = { name: 'PageUsers', module: PageUsers };
|
||||
// Routes
|
||||
this.routes = {};
|
||||
// Core
|
||||
// TCG
|
||||
this.routes[hashPageMtgGame] = (isPopState = false) => this.navigateToHash(hashPageMtgGame, isPopState);
|
||||
this.routes[hashPageMtgGames] = (isPopState = false) => this.navigateToHash(hashPageMtgGames, isPopState);
|
||||
this.routes[hashPageMtgHome] = (isPopState = false) => this.navigateToHash(hashPageMtgHome, 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.routes[hashPageUserAccounts] = (isPopState = false) => this.navigateToHash(hashPageUserAccounts, 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();
|
||||
Reference in New Issue
Block a user