Skip to content

Commit 1a6c6c5

Browse files
committed
Merge branch 'syduki-master'
2 parents 43d9d65 + 03b6634 commit 1a6c6c5

File tree

1 file changed

+34
-40
lines changed
  • frameworks/keyed/vanillajs-lite/src

1 file changed

+34
-40
lines changed

frameworks/keyed/vanillajs-lite/src/Main.js

Lines changed: 34 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,62 +2,56 @@ const adjectives = ['pretty', 'large', 'big', 'small', 'tall', 'short', 'long',
22
const colours = ['red', 'yellow', 'blue', 'green', 'pink', 'brown', 'purple', 'brown', 'white', 'black', 'orange'];
33
const nouns = ['table', 'chair', 'house', 'bbq', 'desk', 'car', 'pony', 'cookie', 'sandwich', 'burger', 'pizza', 'mouse', 'keyboard'];
44

5-
const pick = (dict => dict[Math.round(Math.random() * 1000) % dict.length]);
6-
const label = (() =>`${pick(adjectives)} ${pick(colours)} ${pick(nouns)}`);
5+
const pick = dict => dict[Math.round(Math.random() * 1000) % dict.length];
6+
const label = () =>`${pick(adjectives)} ${pick(colours)} ${pick(nouns)}`;
7+
const labelOf = r => r.firstChild.nextSibling.firstChild.firstChild;
78

8-
const {cloneNode, insertBefore} = Node.prototype;
9-
10-
let ID = 1, rows = [], selection;
11-
const ROW = Symbol(), ACTION = Symbol();
129
const [[table], [tbody], [trow], buttons] = 'table,tbody,#trow,button'
1310
.split(',').map(s => document.querySelectorAll(s));
11+
const {children: rows} = tbody;
1412

15-
const build = (TRow => () => {
16-
const tr = TRow();
17-
const td1 = tr.firstChild, td2 = td1.nextSibling, td3 = td2.nextSibling;
18-
const a1 = td2.firstChild, a2 = td3.firstChild;
19-
td1.firstChild.nodeValue = ID++;
20-
(tr.label = a1.firstChild).nodeValue = label();
21-
a1[ACTION] = select, a2[ACTION] = remove;
22-
return a1[ROW] = a2[ROW] = tr;
23-
})(cloneNode.bind(trow.content.firstChild, true));
24-
25-
const insert = (insertBefore.bind(tbody));
26-
27-
const create = (count, old) => {
28-
!old && clear();
29-
const data = [];
30-
for (let i = 0; i < count; i++)
31-
data[i] = insert(build(), null);
32-
rows = [...(old ?? []), ...data];
13+
const {cloneNode, insertBefore} = Node.prototype;
14+
const clone = n => cloneNode.call(n, true);
15+
const insert = insertBefore.bind(tbody);
16+
17+
let ID = 1, SEL, TMPL, SIZE;
18+
19+
const create = (count, add) => {
20+
if (SIZE !== count)
21+
TMPL = clone(trow.content), [...Array((SIZE = count) / 50 - 1)]
22+
.forEach(() => TMPL.appendChild(clone(TMPL.firstChild)));
23+
!add && (clear(), tbody.remove());
24+
while (count) {
25+
for (let r of TMPL.children)
26+
(r.$id ??= r.firstChild.firstChild).nodeValue = ID++,
27+
(r.$label ??= labelOf(r)).nodeValue = label(), count--;
28+
insert(clone(TMPL), null);
29+
}
30+
!add && table.appendChild(tbody);
3331
};
3432

35-
const select = row => (selection && (selection.className = ''),
36-
(selection = row).className = 'danger');
37-
38-
const remove = row =>
39-
(row.remove(), (rows = new Set(rows)).delete(row), rows = [...rows]);
40-
41-
const clear = () => (rows.length && (tbody.textContent = ''),
42-
rows = [], selection = null);
33+
const clear = () => (tbody.textContent = '', SEL = null);
4334

4435
buttons.forEach(function (b) { b.onclick = this[b.id]; }, {
4536
run () { create(1000); },
4637
runlots () { create(10000); },
47-
add () { create(1000, rows); },
38+
add () { create(1000, true); },
4839
clear,
4940
update () {
5041
for (let i = 0; i < rows.length; i += 10)
51-
rows[i].label.nodeValue += ' !!!';
42+
labelOf(rows[i]).nodeValue += ' !!!';
5243
},
5344
swaprows () {
54-
if (rows.length > 998)
55-
insert(rows[1], rows[998]), insert(rows[998], rows[2]),
56-
[rows[1], rows[998]] = [rows[998], rows[1]];
45+
const [, r1, r2] = rows, r998 = rows[998];
46+
r998 && (insert(r1, r998), insert(r998, r2));
5747
}
5848
});
5949

60-
table.onclick = e => {
61-
let {target: t} = e;
62-
e.stopPropagation(), (t[ACTION] ?? (t = t.parentNode)[ACTION])?.(t[ROW]);
50+
tbody.onclick = e => {
51+
const t = e.target, r = t.closest('tr');
52+
const td2 = r?.firstChild.nextSibling, td3 = td2?.nextSibling;
53+
e.stopPropagation();
54+
t === td2?.firstChild ?
55+
(SEL && (SEL.className = ''), (SEL = r).className = 'danger') :
56+
(t === td3?.firstChild || t === td3?.firstChild.firstChild) && r.remove();
6357
};

0 commit comments

Comments
 (0)