ft_transcendence/frontend/static/js/index.js

107 lines
3.1 KiB
JavaScript
Raw Normal View History

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-30 10:36:21 -05:00
import Search from "./views/Search.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
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";
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]];
}));
};
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/: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-30 10:36:21 -05:00
{ path: "/search", view: Search },
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,
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
},
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-21 13:15:16 -05:00
const view = new match.route.view(getParams(match));
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-12-16 12:00:38 -05:00
await client.isAuthentificate();
let content = await view.getHtml();
if (content == null)
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
await view.postInit();
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();
navigateTo(e.target.href.slice(location.origin.length));
2023-11-21 13:15:16 -05:00
}
});
router(location.pathname);
2023-11-23 11:26:09 -05:00
});
2023-12-04 10:51:24 -05:00
export { client, navigateTo }