This repository was archived by the owner on Jan 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackpanel.ts
executable file
·91 lines (74 loc) · 1.53 KB
/
backpanel.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
declare var $: any;
declare var picodom: any;
module BackPanel
{
const root = document.getElementById("content");
var ws : WebSocket;
function initiateReset()
{
ws = new WebSocket("{{websocket_url}}");
ws.onopen = onOpen;
ws.onmessage = onMessage;
ws.onclose = onClose;
return ws;
}
// Requests
interface Reset
{
Case: "Reset";
}
interface Event_
{
Case: "Event";
Fields: [string];
}
// State
var currentElement: HTMLElement;
var currentDOM: any;
// Responses
interface Update
{
Case: "Update";
// version, DOM
Fields: [number, string];
}
function onOpen(event: Event)
{
$('#bp-connection-lost').modal("hide");
currentElement = null;
currentDOM = null;
root.innerHTML = "";
sendObject({ Case: "Reset" } as Reset);
}
function onMessage(event: MessageEvent)
{
const message: Update = JSON.parse(event.data);
switch (message.Case)
{
case "Update":
var newDOM = JSON.parse(message.Fields[1]);
currentElement = picodom.patch(currentDOM, newDOM, currentElement, root);
if (!currentDOM)
{
$('[remove-on-init="remove"]').remove();
$('[data-toggle="checkbox"]').radiocheck();
}
currentDOM = newDOM;
break;
}
}
export function sendEvent(payload: any)
{
sendObject({ Case: "Event", Fields: [JSON.stringify(payload)] } as Event_);
}
function sendObject(obj: any)
{
ws.send(JSON.stringify(obj));
}
function onClose(close: CloseEvent)
{
$('#bp-connection-lost').modal("show");
setTimeout(() => initiateReset(), 1000);
}
initiateReset();
}