2023-12-12 09:20:53 -05:00
|
|
|
import { Client } from "./api/client.js";
|
|
|
|
|
2023-11-23 11:26:09 -05:00
|
|
|
import LoginView from "./views/accounts/LoginView.js";
|
2023-11-21 13:15:16 -05:00
|
|
|
import Dashboard from "./views/Dashboard.js";
|
2023-11-27 08:31:47 -05:00
|
|
|
import Chat from "./views/Chat.js";
|
2023-11-27 13:15:15 -05:00
|
|
|
import HomeView from "./views/HomeView.js";
|
|
|
|
import RegisterView from "./views/accounts/RegisterView.js";
|
2023-11-29 10:05:49 -05:00
|
|
|
import LogoutView from "./views/accounts/LogoutView.js";
|
2023-12-04 10:51:24 -05:00
|
|
|
import GameView from "./views/Game.js"
|
|
|
|
|
2023-12-14 07:55:31 -05:00
|
|
|
import PageNotFoundView from './views/PageNotFoundView.js'
|
2023-12-13 06:35:06 -05:00
|
|
|
|
2023-11-29 13:55:44 -05:00
|
|
|
import AbstractRedirectView from "./views/AbstractRedirectView.js";
|
2023-11-30 19:29:56 -05:00
|
|
|
import MeView from "./views/MeView.js";
|
2023-12-09 15:51:32 -05:00
|
|
|
import ProfilePageView from "./views/profiles/ProfilePageView.js";
|
|
|
|
import ProfilesView from "./views/profiles/ProfilesView.js";
|
2023-12-11 07:40:01 -05:00
|
|
|
import MatchMakingView from "./views/MatchMakingView.js";
|
2023-11-23 11:26:09 -05:00
|
|
|
|
|
|
|
let client = new Client(location.protocol + "//" + location.host)
|
|
|
|
|
2023-11-27 15:59:41 -05:00
|
|
|
let lastView = undefined
|
|
|
|
|
2023-11-21 13:15:16 -05:00
|
|
|
const pathToRegex = path => new RegExp("^" + path.replace(/\//g, "\\/").replace(/:\w+/g, "(.+)") + "$");
|
|
|
|
|
|
|
|
const getParams = match => {
|
|
|
|
const values = match.result.slice(1);
|
|
|
|
const keys = Array.from(match.route.path.matchAll(/:(\w+)/g)).map(result => result[1]);
|
|
|
|
|
|
|
|
return Object.fromEntries(keys.map((key, i) => {
|
|
|
|
return [key, values[i]];
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
|
2023-11-29 11:04:00 -05:00
|
|
|
const navigateTo = async (uri) => {
|
|
|
|
if (await router(uri) === 0)
|
|
|
|
history.pushState(null, null, uri);
|
2023-11-21 13:15:16 -05:00
|
|
|
};
|
|
|
|
|
2023-12-11 09:54:56 -05:00
|
|
|
const router = async (uri) => {
|
2023-11-21 13:15:16 -05:00
|
|
|
const routes = [
|
|
|
|
{ path: "/", view: Dashboard },
|
2023-12-09 15:51:32 -05:00
|
|
|
{ path: "/profiles", view: ProfilesView},
|
|
|
|
{ path: "/profiles/:id", view: ProfilePageView },
|
2023-11-23 11:26:09 -05:00
|
|
|
{ path: "/login", view: LoginView },
|
2023-11-29 10:05:49 -05:00
|
|
|
{ path: "/logout", view: LogoutView },
|
2023-11-27 09:32:17 -05:00
|
|
|
{ path: "/register", view: RegisterView },
|
2023-11-27 08:31:47 -05:00
|
|
|
{ path: "/chat", view: Chat },
|
2023-11-27 13:15:15 -05:00
|
|
|
{ path: "/home", view: HomeView },
|
2023-11-30 19:29:56 -05:00
|
|
|
{ path: "/me", view: MeView },
|
2023-12-11 07:40:01 -05:00
|
|
|
{ path: "/matchmaking", view: MatchMakingView },
|
2023-12-13 06:40:47 -05:00
|
|
|
{ path: "/game/offline", view: GameView },
|
2023-11-21 13:15:16 -05:00
|
|
|
];
|
|
|
|
|
|
|
|
// Test each route for potential match
|
|
|
|
const potentialMatches = routes.map(route => {
|
|
|
|
return {
|
|
|
|
route: route,
|
2023-11-29 11:04:00 -05:00
|
|
|
result: uri.match(pathToRegex(route.path))
|
2023-11-21 13:15:16 -05:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
let match = potentialMatches.find(potentialMatch => potentialMatch.result !== null);
|
|
|
|
|
|
|
|
if (!match) {
|
|
|
|
match = {
|
2023-12-14 07:55:31 -05:00
|
|
|
route: {
|
|
|
|
path: uri,
|
|
|
|
view: PageNotFoundView
|
|
|
|
},
|
2023-11-29 11:04:00 -05:00
|
|
|
result: [uri]
|
2023-11-21 13:15:16 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-11-27 15:59:41 -05:00
|
|
|
if (lastView !== undefined)
|
|
|
|
await lastView.leavePage();
|
2023-11-29 12:36:08 -05:00
|
|
|
|
2023-11-21 13:15:16 -05:00
|
|
|
const view = new match.route.view(getParams(match));
|
2023-11-29 13:55:44 -05:00
|
|
|
|
|
|
|
if (view instanceof AbstractRedirectView && await view.redirect())
|
|
|
|
return 1;
|
|
|
|
|
2023-11-27 15:59:41 -05:00
|
|
|
lastView = view;
|
2023-11-21 13:15:16 -05:00
|
|
|
|
2023-11-29 10:18:54 -05:00
|
|
|
let content = await view.getHtml();
|
|
|
|
if (content == null)
|
2023-11-29 11:04:00 -05:00
|
|
|
return 1;
|
|
|
|
|
2023-11-30 07:05:46 -05:00
|
|
|
view.setTitle();
|
2023-11-29 14:03:48 -05:00
|
|
|
document.querySelector("#app").innerHTML = content
|
2023-11-27 08:31:47 -05:00
|
|
|
|
|
|
|
await view.postInit();
|
2023-11-29 11:04:00 -05:00
|
|
|
return 0;
|
2023-11-21 13:15:16 -05:00
|
|
|
};
|
|
|
|
|
2023-12-11 09:59:15 -05:00
|
|
|
window.addEventListener("popstate", function() {router(location.pathname)});
|
2023-11-21 13:15:16 -05:00
|
|
|
|
|
|
|
document.addEventListener("DOMContentLoaded", () => {
|
|
|
|
document.body.addEventListener("click", e => {
|
|
|
|
if (e.target.matches("[data-link]")) {
|
|
|
|
e.preventDefault();
|
2023-11-29 11:04:00 -05:00
|
|
|
navigateTo(e.target.href.slice(location.origin.length));
|
2023-11-21 13:15:16 -05:00
|
|
|
}
|
|
|
|
});
|
2023-11-29 11:04:00 -05:00
|
|
|
router(location.pathname);
|
2023-11-23 11:26:09 -05:00
|
|
|
});
|
|
|
|
|
2023-12-04 10:51:24 -05:00
|
|
|
export { client, navigateTo }
|