"use strict"; import * as USIMP from "./usimp.js"; export class App { account; defaultLocation; main; windows; session; constructor() { if (localStorage.session === undefined) { this.defaultLocation = '/welcome'; this.account = null; } else { const session = JSON.parse(localStorage.session); this.defaultLocation = '/'; this.account = session.account; } this.main = document.getElementsByTagName("main")[0]; this.windows = document.getElementById("windows"); window.addEventListener("hashchange", event => { this.handleUrl(event.newURL); }); this.handleUrl(document.URL); } setHash(hash) { const url = new URL(document.URL); url.hash = hash; location.href = url.toString(); } handleUrl(url) { this.handleHash(new URL(url).hash); } handleHash(hash) { if (hash[0] === '#') hash = hash.substr(1); const defaultCase = () => { history.replaceState(null, null, `#${this.defaultLocation}`); this.handleHash(this.defaultLocation); } switch (hash) { case "/": if (!this.session) { return defaultCase(); } this.hideWindows(); this.showMain(); this.removeAllWindows(); this.setupMain(); break; case "/welcome": this.hideMain(); this.showWindows(); this.removeAllWindows(); this.addWelcomeWindow(); break; case "/login": if (this.session) { return defaultCase(); } this.hideMain(); this.showWindows(); this.removeAllWindows(); this.addLoginWindow(); break; default: console.warn(`Invalid url hash #${hash}`); return defaultCase(); } //this.setHash(hash); } async login(accountName, domainName, password) { let domain; try { domain = await USIMP.Domain.fromName(domainName); } catch (error) { if (error instanceof DOMException) { throw Error("Connection timed out"); } else { throw Error("Invalid USIMP domain"); } } const session = new USIMP.Session(domain); const rtt = await session.chooseDomainServer(); console.log(rtt); const response = await session.authenticate(accountName, password); if (response.status === "success") { this.session = session; this.defaultLocation = "/"; this.setHash("/"); } else { throw Error(response.message); } } hideMain() { this.main.style.visibility = "hidden"; } showMain() { this.main.style.visibility = "visible"; } hideWindows() { this.windows.style.visibility = "hidden"; } showWindows() { this.windows.style.visibility = "visible"; } removeAllWindows() { while (this.windows.lastChild) this.windows.removeChild(this.windows.lastChild); } addWelcomeWindow() { const win = document.createElement("div"); win.classList.add("window-welcome"); win.innerHTML = `