-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscreenbuilder.ts
44 lines (39 loc) · 929 Bytes
/
screenbuilder.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
// deno-lint-ignore-file no-explicit-any ban-ts-comment
import { Screen } from "./screen.ts";
import * as elements from "./elements.ts";
export enum ElemType {
TextElem,
InputElem,
ButtonElem,
HR,
BR
}
const types = {
0: elements.Text,
1: elements.Input,
2: elements.Button,
3: elements.HR,
4: elements.BR
}
type BuilderElem = {
type: ElemType,
id: string,
data?: any[],
}
type Data = {
elements: BuilderElem[],
focus?: string,
name: string
onload?: (screen: Screen) => any
}
export function build(data: Data) {
const screen = new Screen(data.name);
for (const element of data.elements) {
if (!element.data) element.data = []
//@ts-ignore
screen.addElement(element.id, new types[element.type](...element.data))
}
if (data.focus) screen.focus(data.focus);
screen.ready()
if (data.onload) data.onload(screen)
}