Skip to content

Commit 141a8dd

Browse files
committed
Further tweaks to keyed implementation
1 parent 8ff11f8 commit 141a8dd

File tree

1 file changed

+16
-21
lines changed
  • frameworks/keyed/vanillajs-3/src

1 file changed

+16
-21
lines changed

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

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
11
const adjectives = ["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"];
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"];
4-
const lengths = [adjectives.length, colours.length, nouns.length];
5-
function* _random(n) {
6-
for (let max of lengths) {
7-
const arr = new Array(n);
8-
for (i = 0; i < n; i++) arr[i] = Math.round(Math.random() * 1000) % max;
9-
yield arr
10-
}
11-
}
12-
const data = [], nTemplates = (n) => 10, tbody = document.getElementsByTagName('tbody')[0];
13-
let index = 1, i, lbl, selected;
4+
const [l1, l2, l3] = [adjectives.length, colours.length, nouns.length];
145

15-
function create(n = 1000) { if (data.length) clear(); append(n); }
6+
const nTemplates = (n) => 10, tbody = document.getElementsByTagName('tbody')[0];
7+
let data = [], index = 1, i, lbl, selected;
8+
9+
function create(n = 1000) { if (tbody.children.length) clear(); append(n); }
1610
function append(n = 1000) {
17-
const [r1, r2, r3] = _random(n), nt = nTemplates(n), arr = new Array(nt);; let j = 0;
11+
const nt = nTemplates(n); let j = 0, r1, r2, r3;
1812
const itemTemplate = document.getElementById('itemTemplate').content;
1913
while (nt >= itemTemplate.children.length * 2) itemTemplate.appendChild(itemTemplate.cloneNode(true));
2014
while (nt > itemTemplate.children.length) itemTemplate.appendChild(itemTemplate.firstElementChild.cloneNode(true));
@@ -24,21 +18,22 @@ function append(n = 1000) {
2418

2519
while ((n -= nt) >= 0) {
2620
for (i = 0; i < nt; i++, j++) {
27-
ids[i].nodeValue = index++;
28-
labels[i].nodeValue = arr[i] = `${adjectives[r1[j]]} ${colours[r2[j]]} ${nouns[r3[j]]}`
29-
}
30-
data.push(...arr);
21+
r1 = Math.round(Math.random() * 1000) % l1;
22+
r2 = Math.round(Math.random() * 1000) % l2;
23+
r3 = Math.round(Math.random() * 1000) % l3;
24+
(ids[i].nodeValue = index++) && data.push(labels[i].nodeValue = `${adjectives[r1]} ${colours[r2]} ${nouns[r3]}`)
25+
}
3126
tbody.appendChild(itemTemplate.cloneNode(true));
3227
}
3328
}
3429
function update() {
3530
const labels = tbody.querySelectorAll('a.lbl'), length = labels.length;
36-
for (i = 0; i < length; i += 10) labels[i].firstChild.nodeValue = data[i] += ' !!!';
31+
for (i = 0; i < length; i += 10) labels[i].firstChild.nodeValue = data[i] += ' !!!'
3732
}
38-
function clear() { data.length = 0; tbody.textContent = '' }
33+
function clear() { (data = []) && (tbody.textContent = '') }
3934

4035
function swap() {
41-
if (data.length < 999) return; const first = tbody.firstElementChild;
36+
if (tbody.children.length < 999) return; const first = tbody.firstElementChild;
4237
[data[1], data[998]] = [data[998], data[1]];
4338
tbody.insertBefore(tbody.insertBefore(first.nextElementSibling,
4439
tbody.children[998]).nextElementSibling, first.nextElementSibling);
@@ -50,11 +45,11 @@ tbody.onclick = (e) => {
5045
if (element === selected) selected.className = selected.className ? "" : "danger";
5146
else {
5247
if (selected) selected.className = "";
53-
element.className = "danger"; selected = element
48+
(element.className = "danger") && (selected = element)
5449
}
5550
} else if (e.target.matches('span.remove')) {
5651
const element = e.target.parentNode.parentNode.parentNode;
57-
data.splice(Array.prototype.indexOf.call(tbody.children, element), 1) && tbody.removeChild(element);
52+
data.splice(Array.prototype.indexOf.call(tbody.children, element)) && tbody.removeChild(element);
5853
}
5954
}
6055
for (let [key, fn] of Object.entries({

0 commit comments

Comments
 (0)