Skip to content

Commit 3c7a6c4

Browse files
committed
Updated keyed implementation to use a closure
1 parent a010ec1 commit 3c7a6c4

File tree

2 files changed

+38
-130
lines changed

2 files changed

+38
-130
lines changed
Lines changed: 38 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,16 @@
11
"use strict";
2-
3-
const gAdjectives = ["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"];
4-
const gColours = ["red", "yellow", "blue", "green", "pink", "brown", "purple", "brown", "white", "black", "orange"];
5-
const gNouns = ["table", "chair", "house", "bbq", "desk", "car", "pony", "cookie", "sandwich", "burger", "pizza", "mouse", "keyboard"];
6-
const nts = {1000: 5, 10000: 125};
7-
8-
new class {
9-
index = 1; data = []; labels = null; invalidLabels = true;
10-
tbody = document.getElementsByTagName('tbody')[0];
11-
constructor() {
12-
this.tbody.onclick = this.onclick();
13-
for (let key of ['run', 'runlots', 'add', 'update', 'clear', 'swaprows']) {
14-
document.getElementById(key).onclick = (e) => { e.stopPropagation(); this[key](); }
15-
}
16-
};
17-
run(n = 1000) { if (this.data.length) this.clear(); this.add(n); };
18-
runlots() { this.run(10000) };
19-
add(n = 1000) {
20-
const adjectives = gAdjectives, colours = gColours, nouns = gNouns;
2+
(function() {
3+
let index = 1, data = [], labels = null, invalidLabels = true, selected = null;
4+
const tbody = document.getElementsByTagName('tbody')[0];
5+
6+
function run(n = 1000) { if (data.length) clear(); add(n); }
7+
function runlots() { run(10000) }
8+
function add(n = 1000) {
9+
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"];
10+
const colours = ["red", "yellow", "blue", "green", "pink", "brown", "purple", "brown", "white", "black", "orange"];
11+
const nouns = ["table", "chair", "house", "bbq", "desk", "car", "pony", "cookie", "sandwich", "burger", "pizza", "mouse", "keyboard"];
2112
const l1 = adjectives.length, l2 = colours.length, l3 = nouns.length;
22-
const nt = nts[n];
13+
const nts = {1000: 5, 10000: 125}, nt = nts[n];
2314
let i, j = 0, r1, r2, r3;;
2415

2516
const itemTemplates = document.getElementById('itemTemplate').content.cloneNode(true);
@@ -36,44 +27,42 @@ new class {
3627
r1 = Math.round(Math.random() * 1000) % l1;
3728
r2 = Math.round(Math.random() * 1000) % l2;
3829
r3 = Math.round(Math.random() * 1000) % l3;
39-
ids[i].nodeValue = this.index++;
40-
this.data.push(labels[i].nodeValue = `${adjectives[r1]} ${colours[r2]} ${nouns[r3]}`);
30+
ids[i].nodeValue = index++;
31+
data.push(labels[i].nodeValue = `${adjectives[r1]} ${colours[r2]} ${nouns[r3]}`);
4132
}
42-
this.tbody.appendChild(itemTemplates.cloneNode(true));
43-
} this.invalidLabels = true;
33+
tbody.appendChild(itemTemplates.cloneNode(true));
34+
} invalidLabels = true;
4435
};
45-
update() {
46-
if (this.invalidLabels) this.labels = this.tbody.querySelectorAll('a.lbl'); this.invalidLabels = false;
47-
const labels = this.labels, length = labels.length, data = this.data;
36+
function update() {
37+
if (invalidLabels) labels = tbody.querySelectorAll('a.lbl'); invalidLabels = false;
38+
const length = labels.length;
4839
let i; for (i = 0; i < length; i += 10) labels[i].firstChild.nodeValue = data[i] += ' !!!';
4940
};
50-
clear() { this.tbody.textContent = ''; this.data = []; this.invalidLabels = true; };
51-
swaprows() {
52-
const tbody = this.tbody, data = this.data;
53-
if (tbody.children.length < 999) return; this.invalidLabels = true;
41+
function clear() { tbody.textContent = ''; data = []; invalidLabels = true; };
42+
function swaprows() {
43+
if (tbody.children.length < 999) return; invalidLabels = true;
5444
const first = tbody.firstElementChild;
5545
[data[1], data[998]] = [data[998], data[1]];
5646
tbody.insertBefore(tbody.insertBefore(first.nextElementSibling,
5747
tbody.children[998]).nextElementSibling, first.nextElementSibling);
5848
};
59-
onclick() {
60-
let selected;
61-
return (e) => {
62-
e.stopPropagation;
63-
if (e.target.matches('a.lbl')) {
64-
e.preventDefault;
65-
const element = e.target.parentNode.parentNode;
66-
if (element === selected) selected.className = selected.className ? "" : "danger";
67-
else {
68-
if (selected) selected.className = "";
69-
element.className = "danger"; selected = element;
70-
}
71-
} else if (e.target.matches('span.remove')) {
72-
const element = e.target.parentNode.parentNode.parentNode;
73-
const index = Array.prototype.indexOf.call(this.tbody.children, element);
74-
element.remove(); this.invalidLabels = true;
75-
this.data.splice(index, 1);
49+
tbody.onclick = (e) => {
50+
e.stopPropagation;
51+
if (e.target.matches('a.lbl')) {
52+
e.preventDefault;
53+
const element = e.target.parentNode.parentNode;
54+
if (element === selected) selected.className = selected.className ? "" : "danger";
55+
else {
56+
if (selected) selected.className = "";
57+
element.className = "danger"; selected = element;
7658
}
59+
} else if (e.target.matches('span.remove')) {
60+
const element = e.target.parentNode.parentNode.parentNode;
61+
const index = Array.prototype.indexOf.call(tbody.children, element);
62+
element.remove(); data.splice(index, 1); invalidLabels = true;
7763
}
7864
}
79-
}();
65+
for (let [key, value] of Object.entries({run, runlots, add, update, clear, swaprows})) {
66+
document.getElementById(key).onclick = (e) => { e.stopPropagation(); value(); }
67+
}
68+
})();

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

Lines changed: 0 additions & 81 deletions
This file was deleted.

0 commit comments

Comments
 (0)