-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.ts
76 lines (61 loc) · 1.51 KB
/
config.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
import $ref from "./ref.js"
import { html } from "sinuous"
import { map } from "sinuous/map"
let $configs = new $ref([] as Config<unknown>[])
interface Config<T> {
name: string
value: T
type: string
onChange?(_: T): void
toHTML(): HTMLElement | DocumentFragment
}
class CheckboxConfig implements Config<boolean> {
name: string
value: boolean
type = 'checkbox'
onChange?: (_: boolean) => void
constructor(name: string, default_value: boolean, onChange?: (_: boolean) => void) {
this.name = name
this.value = default_value ?? false
this.onChange = onChange
}
toHTML() {
let input = html`<input type="checkbox" onchange=${this.onChange}>`
return html`
<p><label>${input}${this.name}</label></p>
`
}
}
class SelectConfig implements Config<string> {
name: string
value: string
type = 'select'
options: string[]
onChange?: (_: string) => void
constructor(name: string, options: string[], default_value: string, onChange?: (_: string) => void) {
this.name = name
this.options = options
this.value = default_value
this.onChange = onChange
}
toHTML() {
return html`
<h4>${this.name}</h4>
<select name=${this.name}>
${map(
() => this.options,
(option: string) => html`<option value=${option}>${option}</option>`
)}
</select>
`
}
}
class ConfigGroup {
configs: Config<unknown>[]
toHTML() {
return map(
() => this.configs,
(config: Config<unknown>) => config.toHTML()
)
}
}