2022-08-26 22:09:08 +03:00
|
|
|
import Banner from "./Banner.js";
|
|
|
|
import connection, { ConnectionEvent } from "./connection.js";
|
|
|
|
import KeyboardController from "./Keyboard.js";
|
|
|
|
import { setItem } from "./storage.js";
|
|
|
|
import LoginPrompt from "./LoginPrompt.js";
|
|
|
|
import TouchpadController from "./Touchpad.js";
|
2021-11-08 00:25:27 +02:00
|
|
|
|
|
|
|
class App {
|
2022-08-26 22:09:08 +03:00
|
|
|
constructor() {
|
|
|
|
this.connection = connection;
|
2021-11-08 00:25:27 +02:00
|
|
|
this.touchpad = new TouchpadController(this.connection);
|
|
|
|
this.keyboard = new KeyboardController(this.connection);
|
2022-08-26 22:09:08 +03:00
|
|
|
this.bannerComponent = new Banner();
|
|
|
|
this.loginPromptComponent = new LoginPrompt(this.connection);
|
|
|
|
this.connectionHandlers = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
mountOn(mount, keepMountChildren=false) {
|
|
|
|
this.mountElement = mount;
|
2021-11-08 00:25:27 +02:00
|
|
|
|
2022-08-26 22:09:08 +03:00
|
|
|
if (!keepMountChildren) {
|
|
|
|
while (mount.firstChild) {
|
|
|
|
mount.removeChild(mount.lastChild);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.connectionHandlers.push([ConnectionEvent.Closed, this.connection.subscribe(ConnectionEvent.Closed, (code) => {
|
2021-11-08 00:25:27 +02:00
|
|
|
this.unmountApp();
|
|
|
|
if (code === 4001) { // 4001 - code for bad auth
|
|
|
|
this.transitionTo("login");
|
2022-01-25 17:10:35 +02:00
|
|
|
} else {
|
|
|
|
this.transitionTo("reconnectingBanner");
|
2021-11-08 00:25:27 +02:00
|
|
|
}
|
2022-08-26 22:09:08 +03:00
|
|
|
})]);
|
|
|
|
|
|
|
|
this.connectionHandlers.push([ConnectionEvent.Ready, this.connection.subscribe(ConnectionEvent.Ready, () => {
|
2021-11-08 00:25:27 +02:00
|
|
|
this.transitionTo("app");
|
2022-08-26 22:09:08 +03:00
|
|
|
})]);
|
|
|
|
}
|
|
|
|
|
|
|
|
unmount() {
|
|
|
|
if (this.mountElement) {
|
|
|
|
this.unmountApp();
|
|
|
|
this.unmountBannerComponent();
|
|
|
|
this.unmountLoginComponent();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.connectionHandlers.forEach(([ event, handler ]) => {
|
|
|
|
this.connection.unsubscribe(event, handler);
|
|
|
|
});
|
2021-11-08 00:25:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
transitionTo(type) {
|
|
|
|
switch (type) {
|
|
|
|
case "login":
|
2022-01-25 17:10:35 +02:00
|
|
|
this.unmountBannerComponent();
|
2021-11-08 00:25:27 +02:00
|
|
|
this.unmountApp();
|
|
|
|
this.mountLoginComponent();
|
|
|
|
break;
|
|
|
|
case "app":
|
2022-01-25 17:10:35 +02:00
|
|
|
this.unmountBannerComponent();
|
2021-11-08 00:25:27 +02:00
|
|
|
this.unmountLoginComponent();
|
|
|
|
this.mountApp();
|
|
|
|
break;
|
2022-01-25 17:10:35 +02:00
|
|
|
case "reconnectingBanner":
|
|
|
|
this.unmountApp();
|
|
|
|
this.unmountLoginComponent();
|
|
|
|
this.mountBannerComponent("Connecting...", "Looks like you've lost connection. We're trying to reconnect you.");
|
|
|
|
break;
|
2021-11-08 00:25:27 +02:00
|
|
|
default:
|
|
|
|
throw new Error(`transitionTo type ${type} is invalid`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-25 17:10:35 +02:00
|
|
|
mountBannerComponent(title, text) {
|
|
|
|
this.bannerComponent.mountOn(this.mountElement);
|
|
|
|
this.bannerComponent.updateTitle(title);
|
|
|
|
this.bannerComponent.updateText(text);
|
|
|
|
}
|
|
|
|
|
|
|
|
unmountBannerComponent() {
|
2022-08-26 22:09:08 +03:00
|
|
|
this.bannerComponent.unmount();
|
2022-01-25 17:10:35 +02:00
|
|
|
}
|
|
|
|
|
2021-11-08 00:25:27 +02:00
|
|
|
mountLoginComponent() {
|
|
|
|
this.loginPromptComponent.mountOn(this.mountElement);
|
2022-01-25 17:10:35 +02:00
|
|
|
this.loginPromptComponent.onPasswordSubmitted = (p) => {
|
2022-08-26 22:09:08 +03:00
|
|
|
setItem("auth:token", p);
|
|
|
|
this.connection.connect();
|
2021-11-08 00:25:27 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
unmountLoginComponent() {
|
|
|
|
if (this.loginPromptComponent)
|
|
|
|
this.loginPromptComponent.unmount();
|
|
|
|
}
|
|
|
|
|
|
|
|
mountApp() {
|
|
|
|
this.touchpad.mountOn(this.mountElement);
|
|
|
|
this.keyboard.mountOn(this.mountElement);
|
|
|
|
}
|
|
|
|
|
|
|
|
unmountApp() {
|
|
|
|
this.touchpad.unmount();
|
|
|
|
this.keyboard.unmount();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default App;
|