-
Notifications
You must be signed in to change notification settings - Fork 5
/
x.js
195 lines (168 loc) · 7.03 KB
/
x.js
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
{
const {button, input, span} = van.tags
const Name = () => {
const data = vanX.reactive({name: {first: "Tao", last: "Xin"}})
return span(
"First name: ",
input({type: "text", value: () => data.name.first,
oninput: e => data.name.first = e.target.value}), " ",
"Last name: ",
input({type: "text", value: () => data.name.last,
oninput: e => data.name.last = e.target.value}), " ",
"Full name: ", () => `${data.name.first} ${data.name.last}`, " ",
button({onclick: () => data.name = {first: "Tao", last: "Xin"}}, "Reset"),
)
}
van.add(document.getElementById("demo-name"), Name())
}
{
const {button, input, span} = van.tags
const Name = () => {
const data = vanX.reactive({name: {first: "Tao", last: "Xin"}})
const derived = vanX.reactive({fullName: vanX.calc(() => `${data.name.first} ${data.name.last}`)})
return span(
"First name: ",
input({type: "text", value: () => data.name.first,
oninput: e => data.name.first = e.target.value}), " ",
"Last name: ",
input({type: "text", value: () => data.name.last,
oninput: e => data.name.last = e.target.value}), " ",
"Full name: ", () => derived.fullName, " ",
button({onclick: () => data.name = {first: "Tao", last: "Xin"}}, "Reset"),
)
}
van.add(document.getElementById("demo-name-calc"), Name())
}
{
const {button, div, input} = van.tags
const Name = () => {
const data = vanX.reactive({name: {first: "Tao", last: "Xin"}})
data.fullName = vanX.calc(() => `${data.name.first} ${data.name.last}`)
return div(
"First name: ",
input({type: "text", value: () => data.name.first,
oninput: e => data.name.first = e.target.value}), " ",
"Last name: ",
input({type: "text", value: () => data.name.last,
oninput: e => data.name.last = e.target.value}), " ",
"Full name: ", vanX.stateFields(data).fullName, " ",
button({onclick: () => data.name = {first: "Tao", last: "Xin"}}, "Reset"),
)
}
van.add(document.getElementById("demo-name-state-fields"), Name())
}
{
const {button, div, input} = van.tags
class Person {
constructor(firstName, lastName) { this.firstName = firstName; this.lastName = lastName }
get fullName() { return `${this.firstName} ${this.lastName}` }
}
const Name = () => {
const person = vanX.reactive(new Person("Tao", "Xin"))
return div(
"First name: ",
input({type: "text", value: () => person.firstName,
oninput: e => person.firstName = e.target.value}), " ",
"Last name: ",
input({type: "text", value: () => person.lastName,
oninput: e => person.lastName = e.target.value}), " ",
"Full name: ", () => person.fullName, " ",
button({onclick: () => (person.firstName = "Tao", person.lastName = "Xin")}, "Reset"),
)
}
van.add(document.getElementById("demo-name-existing-class"), Name())
}
{
const {a, button, del, div, input, span} = van.tags
const TodoList = () => {
const items = vanX.reactive(JSON.parse(localStorage.getItem("appState") ?? "[]"))
van.derive(() => localStorage.setItem("appState", JSON.stringify(vanX.compact(items))))
const inputDom = input({type: "text"})
return div(
inputDom, button({onclick: () => items.push({text: inputDom.value, done: false})}, "Add"),
vanX.list(div, items, ({val: v}, deleter) => div(
input({type: "checkbox", checked: () => v.done, onclick: e => v.done = e.target.checked}),
() => (v.done ? del : span)(v.text),
a({onclick: deleter}, "❌"),
)),
)
}
van.add(document.getElementById("demo-todo-list"), TodoList())
}
{
const {a, button, div, input, li, ul} = van.tags
const List = () => {
const items = vanX.reactive([])
const inputDom = input({type: "text"})
return div(
div(inputDom, button({onclick: () => items.push(inputDom.value)}, "Add")),
div(() => Object.keys(items).length, " item(s) in total"),
vanX.list(ul, items, (v, deleter) => li(v, " ", a({onclick: deleter}, "❌"))),
div(
button({onclick: () => vanX.replace(items, l => l.toSorted())}, "A -> Z"),
button({onclick: () => vanX.replace(items,
l => l.toSorted((a, b) => b.localeCompare(a)))}, "Z -> A"),
button({onclick: () => vanX.replace(items, l => l.map(v => v + "!"))}, 'Append "!"'),
),
)
}
van.add(document.getElementById("demo-example-list1"), List())
}
{
const {a, button, div, input} = van.tags
const TodoList = () => {
const items = vanX.reactive(JSON.parse(localStorage.getItem("items") ?? "{}"))
van.derive(() => localStorage.setItem("items", JSON.stringify(vanX.compact(items))))
const inputDom = input({type: "text"})
let id = Math.max(0, ...Object.keys(items).map(v => Number(v.slice(1))))
return div(
div(inputDom, button(
{onclick: () => items["k" + ++id] = {text: inputDom.value, done: false}}, "Add")),
div(() => Object.keys(items).length, " item(s) in total"),
vanX.list(div, items, ({val: v}, deleter) => div(
input({type: "checkbox", checked: () => v.done,
onclick: e => v.done = e.target.checked}), " ",
input({
type: "text", value: () => v.text,
style: () => v.done ? "text-decoration: line-through;" : "",
oninput: e => v.text = e.target.value,
}), " ",
a({onclick: deleter}, "❌"),
)),
div(
button({onclick: () => vanX.replace(items, l => l.filter(([_, v]) => !v.done))},
"Clear Completed"),
button({onclick: () => vanX.replace(items, l =>
l.toSorted(([_1, a], [_2, b]) => a.text.localeCompare(b.text)))}, "A -> Z"),
button({onclick: () => vanX.replace(items, l =>
l.toSorted(([_1, a], [_2, b]) => b.text.localeCompare(a.text)))}, "Z -> A"),
button({onclick: () => vanX.replace(items, l =>
l.flatMap(([k1, v1]) => [
[k1, v1],
["k" + ++id, {text: v1.text + " - copy", done: v1.done}],
]))},
"Duplicate List"),
button({onclick: () => Object.values(items).forEach(v => v.text += "!")}, 'Append "!"'),
),
)
}
van.add(document.getElementById("demo-example-list2"), TodoList())
}
{
const {a, button, del, div, input, span} = van.tags
const TodoListPlus = () => {
const appState = vanX.reactive(JSON.parse(
localStorage.getItem("appStatePlus") ?? '{"input":"","items":[]}'))
van.derive(() => localStorage.setItem("appStatePlus", JSON.stringify(vanX.compact(appState))))
return div(
input({type: "text", value: () => appState.input, oninput: e => appState.input = e.target.value}),
button({onclick: () => appState.items.push({text: appState.input, done: false})}, "Add"),
vanX.list(div, appState.items, ({val: v}, deleter) => div(
input({type: "checkbox", checked: () => v.done, onclick: e => v.done = e.target.checked}),
() => (v.done ? del : span)(v.text),
a({onclick: deleter}, "❌"),
)),
)
}
van.add(document.getElementById("demo-todo-list-plus"), TodoListPlus())
}