-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.js
225 lines (202 loc) · 4.15 KB
/
main.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import Bau from "@grucloud/bau";
const bau = Bau();
const { a, button, div, tr, td, table, tbody, h1, span } = bau.tags;
const random = (max) => Math.round(Math.random() * 1000) % max;
const A = [
"pretty",
"large",
"big",
"small",
"tall",
"short",
"long",
"handsome",
"plain",
"quaint",
"clean",
"elegant",
"easy",
"angry",
"crazy",
"helpful",
"mushy",
"odd",
"unsightly",
"adorable",
"important",
"inexpensive",
"cheap",
"expensive",
"fancy",
];
const C = [
"red",
"yellow",
"blue",
"green",
"pink",
"brown",
"purple",
"brown",
"white",
"black",
"orange",
];
const N = [
"table",
"chair",
"house",
"bbq",
"desk",
"car",
"pony",
"cookie",
"sandwich",
"burger",
"pizza",
"mouse",
"keyboard",
];
let nextId = 1;
const buildData = (count) => {
const data = new Array(count);
for (let i = 0; i < count; i++) {
data[i] = {
id: nextId++,
label: `${A[random(A.length)]} ${C[random(C.length)]} ${
N[random(N.length)]
}`,
};
}
return data;
};
const dataState = bau.state([]);
const selectedState = bau.state(0);
const run = () => {
dataState.val = buildData(1000);
selectedState.val = 0;
};
const runLots = () => {
dataState.val = buildData(10000);
selectedState.val = 0;
};
const add = () => {
dataState.val.push(...buildData(1000));
};
const update = () => {
for (let i = 0; i < dataState.val.length; i += 10) {
const r = dataState.val[i];
dataState.val[i].label = r.label + " !!!";
}
};
const swapRows = () => {
if (dataState.val.length > 998) {
const data = dataState.val;
const dataTmp = data[1];
dataState.val[1] = data[998];
dataState.val[998] = dataTmp;
}
};
const clear = () => {
dataState.val = [];
selectedState.val = 0;
};
const remove = ({ id, event }) => {
event.preventDefault();
const idx = dataState.val.findIndex((d) => d.id === id);
if (idx > -1) {
dataState.val.splice(idx, 1);
}
};
const select = ({ id, event }) => {
event.preventDefault();
selectedState.val = id;
};
const Row = ({ id, label }) =>
tr(
{
class: () => (selectedState.val == id ? "danger" : ""),
},
td({ class: "col-md-1" }, id),
td(
{ class: "col-md-4" },
a({ href: "", onclick: (event) => select({ id, event }) }, label)
),
td(
{ class: "col-md-1" },
a(
{ onclick: (event) => remove({ id, event }) },
span({ class: "glyphicon glyphicon-remove", "aria-hidden": true })
)
),
td({ class: "col-md-6" })
);
const Button = ({ id, title, onclick }) =>
div(
{ class: "col-sm-6 smallpad" },
button(
{ type: "button", class: "btn btn-primary btn-block", id, onclick },
title
)
);
const Jumbotron = ({}) =>
div(
{ class: "jumbotron" },
div(
{ class: "row" },
div({ class: "col-md-6" }, h1("Bau Benchmark Local")),
div(
{ class: "col-md-6" },
div(
{ class: "row" },
Button({ id: "run", title: "Create 1,000 rows", onclick: run }),
Button({
id: "runlots",
title: "Create 10,000 rows",
onclick: runLots,
}),
Button({
id: "add",
title: "Append 1,000 rows",
onclick: add,
}),
Button({
id: "update",
title: "Update every 10th row",
onclick: update,
}),
Button({
id: "clear",
title: "Clear",
onclick: clear,
}),
Button({
id: "swaprows",
title: "Swap Row",
onclick: swapRows,
})
)
)
)
);
const Main = () =>
div(
{ class: "container" },
Jumbotron({}),
table(
{ class: "table table-hover table-striped test-data" },
bau.loop(dataState, tbody(), (value) =>
Row({
id: value.id,
item: value,
label: value.label,
})
),
span({
class: "preloadicon glyphicon glyphicon-remove",
"aria-hidden": true,
})
)
);
const app = document.getElementById("app");
app.replaceChildren(Main({}));