partey_workadventure/front/src/Url/UrlManager.ts

60 lines
1.9 KiB
TypeScript
Raw Normal View History

import type { Room } from "../Connexion/Room";
2021-09-06 18:36:31 +02:00
import { localUserStore } from "../Connexion/LocalUserStore";
export enum GameConnexionTypes {
anonymous = 1,
organization,
register,
2020-10-13 17:20:20 +02:00
empty,
unknown,
jwt,
2021-09-06 18:36:31 +02:00
login,
}
//this class is responsible with analysing and editing the game's url
class UrlManager {
public getGameConnexionType(): GameConnexionTypes {
const url = window.location.pathname.toString();
2021-09-06 18:36:31 +02:00
if (url === "/login") {
return GameConnexionTypes.login;
} else if (url === "/jwt") {
return GameConnexionTypes.jwt;
} else if (url.includes("_/")) {
return GameConnexionTypes.anonymous;
} else if (url.includes("@/")) {
return GameConnexionTypes.organization;
} else if (url.includes("register/")) {
2020-10-13 17:20:20 +02:00
return GameConnexionTypes.register;
} else if (url === "/") {
2020-10-13 17:20:20 +02:00
return GameConnexionTypes.empty;
} else {
2020-10-13 17:20:20 +02:00
return GameConnexionTypes.unknown;
}
}
public getOrganizationToken(): string | null {
const match = /\/register\/(.+)/.exec(window.location.pathname.toString());
return match ? match[1] : null;
}
public pushRoomIdToUrl(room: Room): void {
if (window.location.pathname === room.id) return;
2021-09-06 18:36:31 +02:00
//Set last room visited! (connected or nor, must to be saved in localstorage and cache API)
localUserStore.setLastRoomUrl(room.key);
2020-11-25 17:17:48 +01:00
const hash = window.location.hash;
const search = room.search.toString();
history.pushState({}, "WorkAdventure", room.id + (search ? "?" + search : "") + hash);
2020-11-25 17:17:48 +01:00
}
public getStartLayerNameFromUrl(): string | null {
const hash = window.location.hash;
return hash.length > 1 ? hash.substring(1) : null;
}
pushStartLayerNameToUrl(startLayerName: string): void {
window.location.hash = startLayerName;
}
}
export const urlManager = new UrlManager();