This commit is contained in:
2024-02-01 12:58:17 +01:00
15 changed files with 127 additions and 51 deletions

View File

@ -19,10 +19,10 @@ import TournamentPageView from "./views/tournament/TournamentPageView.js";
import TournamentsView from "./views/tournament/TournamentsListView.js";
import TournamentCreateView from "./views/tournament/TournamentCreateView.js";
let client = new Client(location.protocol + "//" + location.host)
let client = new Client(location.origin);
let lastView = undefined
let lastPageUrlBeforeLogin = undefined
let lastView = undefined;
let lastPageUrlBeforeLogin = undefined;
const pathToRegex = path => new RegExp("^" + path.replace(/\//g, "\\/").replace(/:\w+/g, "(.+)") + "$");
@ -48,9 +48,11 @@ const navigateTo = async (uri) => {
}
};
const reloadView = async _ => renderView(lastView);
async function renderView(view)
{
let content = await view.getHtml();
let content = await view?.getHtml();
if (content == null)
return 1;
@ -108,14 +110,16 @@ const router = async(uri) => {
if (lastView !== undefined)
await lastView.leavePage();
if (uri !== '/login' && uri !== '/register' && uri !== '/logout')
lastPageUrlBeforeLogin = uri;
else
lastPageUrlBeforeLogin = undefined;
const view = new match.route.view(getParams(match), lastPageUrlBeforeLogin);
if (view instanceof AbstractRedirectView && await view.redirect())
return 1;
lastView = view;
if (uri !== '/login' && uri !== '/register' && uri !== '/logout')
lastPageUrlBeforeLogin = uri;
if (await renderView(view))
return 1;
@ -139,12 +143,19 @@ document.addEventListener("DOMContentLoaded", async () => {
//Languages
Array.from(document.getElementById('languageSelector').children).forEach(el => {
el.onclick = _ => client.lang.changeLanguage(el.value);
el.onclick = async _ => {
if (await client.lang.changeLanguage(el.value))
return;
document.querySelector('#languageSelector > .active')?.classList.remove('active');
el.classList.add('active');
};
});
document.querySelector(`#languageSelector > [value=${client.lang.chosenLang}]`)
?.classList.add('active');
await client.isAuthentificate();
router(location.pathname);
document.querySelector('a[href=\'' + location.pathname + '\']')?.classList.add('active');
});
export { client, navigateTo }
export { client, navigateTo, reloadView }