Skip to content

Commit ee2b2a0

Browse files
committed
Merge branch 'syduki-master'
2 parents 55e5d0d + a33ab5f commit ee2b2a0

File tree

1 file changed

+32
-39
lines changed
  • frameworks/keyed/vanillajs-lite/src

1 file changed

+32
-39
lines changed

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

Lines changed: 32 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,57 +2,50 @@ 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];
5+
const pick = (dict => dict[Math.round(Math.random() * 1000) % dict.length]);
6+
const label = (() =>`${pick(adjectives)} ${pick(colours)} ${pick(nouns)}`);
7+
8+
const {cloneNode, insertBefore} = Node.prototype;
69

710
let ID = 1, rows = [], selection;
811
const ROW = Symbol(), ACTION = Symbol();
12+
const [[table], [tbody], [trow], buttons] = 'table,tbody,#trow,button'
13+
.split(',').map(s => document.querySelectorAll(s));
914

10-
const table = document.querySelector('table');
11-
let tbody = document.querySelector('tbody');
12-
const trow = document.querySelector('#trow');
13-
14-
const {cloneNode, insertBefore} = Node.prototype;
15-
const TBody = (cloneNode.bind(tbody, false));
16-
const TRow = (cloneNode.bind(trow.content.firstChild, true));
17-
const insert = ((row, before = null) => insertBefore.call(tbody, row, before));
18-
19-
const build = (() => {
15+
const build = (TRow => () => {
2016
const tr = TRow();
2117
const td1 = tr.firstChild, td2 = td1.nextSibling, td3 = td2.nextSibling;
2218
const a1 = td2.firstChild, a2 = td3.firstChild;
23-
const label = `${pick(adjectives)} ${pick(colours)} ${pick(nouns)}`;
2419
td1.firstChild.nodeValue = ID++;
25-
(tr.label = a1.firstChild).nodeValue = label;
20+
(tr.label = a1.firstChild).nodeValue = label();
2621
a1[ACTION] = select, a2[ACTION] = remove;
27-
return insert(a1[ROW] = a2[ROW] = tr);
28-
});
22+
return a1[ROW] = a2[ROW] = tr;
23+
})(cloneNode.bind(trow.content.firstChild, true));
2924

30-
const create = count => [...Array(count)].map(build);
25+
const insert = (insertBefore.bind(tbody));
3126

32-
const select = (set => row => {
33-
set('remove'), selection = row, set('add');
34-
})(setter => selection?.classList[setter]('danger'));
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];
33+
};
3534

36-
const remove = (match => row => {
37-
rows = rows.filter(match, row), row.remove();
38-
})(function (row) { return row !== this; });
35+
const select = row => (selection && (selection.className = ''),
36+
(selection = row).className = 'danger');
3937

40-
const clear = patch => {
41-
rows = [], selection = null;
42-
const empty = !tbody.firstChild;
43-
if (!empty || patch)
44-
!empty && patch ? (tbody.textContent = '', patch()) :
45-
(tbody.remove(), tbody = TBody(), patch?.(),
46-
insertBefore.call(table, tbody, null));
47-
};
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);
4843

49-
document.querySelectorAll('button').forEach(function (button) {
50-
button.addEventListener('click', this[button.id]);
51-
}, {
52-
run () { clear(() => rows = create(1000)); },
53-
runlots () { clear(() => rows = create(10000)); },
54-
add () { rows = [...rows, ...create(1000)]; },
55-
clear () { clear(); },
44+
buttons.forEach(function (b) { b.onclick = this[b.id]; }, {
45+
run () { create(1000); },
46+
runlots () { create(10000); },
47+
add () { create(1000, rows); },
48+
clear,
5649
update () {
5750
for (let i = 0; i < rows.length; i += 10)
5851
rows[i].label.nodeValue += ' !!!';
@@ -64,7 +57,7 @@ document.querySelectorAll('button').forEach(function (button) {
6457
}
6558
});
6659

67-
table.addEventListener('click', e => {
60+
table.onclick = e => {
6861
let {target: t} = e;
6962
e.stopPropagation(), (t[ACTION] ?? (t = t.parentNode)[ACTION])?.(t[ROW]);
70-
});
63+
};

0 commit comments

Comments
 (0)