Skip to content

Commit fc51cbe

Browse files
committed
Merge branch 'mksunny1-Vanillajs-3'
2 parents 13182a0 + d9f4c11 commit fc51cbe

File tree

4 files changed

+42
-215
lines changed

4 files changed

+42
-215
lines changed

frameworks/keyed/vanillajs-3/index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ <h1>Vanillajs-3-"keyed"</h1>
7878
<span class="preloadicon glyphicon glyphicon-remove" aria-hidden="true"></span>
7979
</div>
8080
</div>
81-
<template id="itemTemplate"><tr><td class='col-md-1'>{id}</td><td class='col-md-4'><a class='lbl'>{lbl}</a></td><td class='col-md-1'><a class='remove'><span class='remove glyphicon glyphicon-remove' aria-hidden='true'></span></a></td><td class='col-md-6'></td></tr></template>
81+
<template id="t1000"><tr><td class='col-md-1'>{id}</td><td class='col-md-4'><a class='lbl'>{lbl}</a></td><td class='col-md-1'><a class='remove'><span class='remove glyphicon glyphicon-remove' aria-hidden='true'></span></a></td><td class='col-md-6'></td></tr></template>
82+
<template id="t10000"><tr><td class='col-md-1'>{id}</td><td class='col-md-4'><a class='lbl'>{lbl}</a></td><td class='col-md-1'><a class='remove'><span class='remove glyphicon glyphicon-remove' aria-hidden='true'></span></a></td><td class='col-md-6'></td></tr></template>
8283
<script src='src/Main.js'></script>
8384
</body>
8485
</html>

frameworks/keyed/vanillajs-3/index_backup.html

Lines changed: 0 additions & 84 deletions
This file was deleted.
Lines changed: 40 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,21 @@
11
"use strict";
2+
(function() {
3+
let index = 1, data = [], labels = null, invalidLabels = true, selected = null;
4+
const tbody = document.getElementsByTagName('tbody')[0];
25

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};
6+
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"];
7+
const colours = ["red", "yellow", "blue", "green", "pink", "brown", "purple", "brown", "white", "black", "orange"];
8+
const nouns = ["table", "chair", "house", "bbq", "desk", "car", "pony", "cookie", "sandwich", "burger", "pizza", "mouse", "keyboard"];
9+
const l1 = adjectives.length, l2 = colours.length, l3 = nouns.length;
10+
const nts = (n) => Math.round(n / 100)
711

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;
21-
const l1 = adjectives.length, l2 = colours.length, l3 = nouns.length;
22-
const nt = nts[n];
12+
function run(n = 1000) { if (data.length) clear(); add(n); }
13+
function runlots() { run(10000) }
14+
function add(n = 1000) {
15+
const nt = nts(n);
2316
let i, j = 0, r1, r2, r3;;
2417

25-
const itemTemplates = document.getElementById('itemTemplate').content.cloneNode(true);
18+
const itemTemplates = document.getElementById(`t${n}`).content // .cloneNode(true);
2619
if (itemTemplates.children.length < nt) {
2720
const itemTemplate = itemTemplates.firstElementChild;
2821
while (nt >= itemTemplates.children.length * 2) itemTemplates.appendChild(itemTemplates.cloneNode(true));
@@ -36,44 +29,42 @@ new class {
3629
r1 = Math.round(Math.random() * 1000) % l1;
3730
r2 = Math.round(Math.random() * 1000) % l2;
3831
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]}`);
32+
ids[i].nodeValue = index++;
33+
data.push(labels[i].nodeValue = `${adjectives[r1]} ${colours[r2]} ${nouns[r3]}`);
4134
}
42-
this.tbody.appendChild(itemTemplates.cloneNode(true));
43-
} this.invalidLabels = true;
35+
tbody.appendChild(itemTemplates.cloneNode(true));
36+
} invalidLabels = true;
4437
};
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;
38+
function update() {
39+
if (invalidLabels) labels = tbody.querySelectorAll('a.lbl'); invalidLabels = false;
40+
const length = labels.length;
4841
let i; for (i = 0; i < length; i += 10) labels[i].firstChild.nodeValue = data[i] += ' !!!';
4942
};
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;
43+
function clear() { tbody.textContent = ''; data = []; invalidLabels = true; };
44+
function swaprows() {
45+
if (tbody.children.length < 999) return; invalidLabels = true;
5446
const first = tbody.firstElementChild;
5547
[data[1], data[998]] = [data[998], data[1]];
5648
tbody.insertBefore(tbody.insertBefore(first.nextElementSibling,
5749
tbody.children[998]).nextElementSibling, first.nextElementSibling);
5850
};
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);
51+
tbody.onclick = (e) => {
52+
e.stopPropagation;
53+
if (e.target.matches('a.lbl')) {
54+
e.preventDefault;
55+
const element = e.target.parentNode.parentNode;
56+
if (element === selected) selected.className = selected.className ? "" : "danger";
57+
else {
58+
if (selected) selected.className = "";
59+
element.className = "danger"; selected = element;
7660
}
61+
} else if (e.target.matches('span.remove')) {
62+
const element = e.target.parentNode.parentNode.parentNode;
63+
const index = Array.prototype.indexOf.call(tbody.children, element);
64+
element.remove(); data.splice(index, 1); invalidLabels = true;
7765
}
7866
}
79-
}();
67+
for (let [key, value] of Object.entries({run, runlots, add, update, clear, swaprows})) {
68+
document.getElementById(key).onclick = (e) => { e.stopPropagation(); value(); }
69+
}
70+
})();

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

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

0 commit comments

Comments
 (0)