/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* Page.js :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: edbernar 0 && performance.getEntriesByType("navigation")[0].type === "reload") for (let i = 0; i < this.availablePages.length; i++) { if (window.location.pathname == this.availablePages[i].url || (thisClass.availablePages[i].suffix && window.location.pathname.startsWith(thisClass.availablePages[i].url))) { let arg = window.location.pathname.slice(thisClass.availablePages[i].url.length + 1); if (arg == "" || !thisClass.availablePages[i].suffix) arg = null; this.changePage(this.availablePages[i].name, false, arg, !thisClass.availablePages[i].suffix); return ; } } this.#showUnknownPage(); } changePage(name, isBack = false, arg = null, needToChangePath = true) { if (this.actualPage != null) this.actualPage.dispose(); for (let i = 0; i < this.availablePages.length; i++) { if (name === this.availablePages[i].name) { fetch(this.availablePages[i].servUrl, { method: "POST", }) .then(response => { if (response.status != 200) throw Error("Page '" + name + "' can't be loaded") return (response); }) .then(data => { data.text().then(text => { document.body.innerHTML = text; for (let i = 0; i < this.availablePages.length; i++) { if (this.actualPage === this.availablePages[i].class) document.precedentPage = this.availablePages[i].name; } this.actualPage = this.availablePages[i].class; document.title = this.availablePages[i].title; if (!isBack && !this.wasRefresh) { if (needToChangePath) history.pushState({}, this.availablePages[i].title, this.availablePages[i].url); else history.pushState({}, this.availablePages[i].title, window.location.pathname); } this.wasRefresh = false; if (arg) this.actualPage.create(arg); else this.actualPage.create(); }) }) .catch(error => { window.location.href = '/'; }); return ; } } throw Error("Page '" + name + "' not exist"); } #showUnknownPage() { if (this.actualPage != null) this.actualPage.dispose(); fetch('/404') .then(response => { if (response.status != 200) throw Error("Page '" + name + "' can't be loaded") return (response); }) .then(data => { data.text().then(text => { document.body.innerHTML = text; this.actualPage = null; document.title = 'METH - Page not found'; history.pushState({}, document.title, window.location.pathname); }) }) .catch(error => { window.location.href = '/'; throw Error(error); }); } disconnect() { if (this.actualPage == MultiOnlineGamePage || this.actualPage == WaitingGamePage) this.changePage('lobbyPage'); setTimeout(() => { CN.new("Error", "Connection lost"); }, 1000) } }; export { Page }