Skip to content

Commit 44aaebb

Browse files
committed
Updated keyed implementation to better demonstrate the framework
1 parent ed25b2b commit 44aaebb

File tree

1 file changed

+68
-68
lines changed
  • frameworks/keyed/eventiveness/src

1 file changed

+68
-68
lines changed

frameworks/keyed/eventiveness/src/Main.js

Lines changed: 68 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -4,83 +4,83 @@ import { preventDefault, stopPropagation, eventListener, matchEventListener} fro
44
import { one } from 'eventiveness/onetomany';
55
import {range} from 'eventiveness/generational';
66

7+
78
function _random(max) {return Math.round(Math.random() * 1000) % max;}
89
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"];
910
const colours = ["red", "yellow", "blue", "green", "pink", "brown", "purple", "brown", "white", "black", "orange"];
1011
const nouns = ["table", "chair", "house", "bbq", "desk", "car", "pony", "cookie", "sandwich", "burger", "pizza", "mouse", "keyboard"];
1112

12-
class View {
13-
constructor(parent) {this.parent = parent;}
14-
create(data, index, n) {
15-
this.clear();
16-
this.append(data, index, n);
17-
}
18-
append(data, index, n) {
19-
let markup = [], length = data.length;
20-
for (let i = data.length - n; i < length; i++) markup.push(`<tr><td class='col-md-1'>${index[i]}</td><td class='col-md-4'><a class='lbl'>${data[i]}</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>`);
21-
this.parent.append(createTree(markup.join('')));
22-
}
23-
setLabel(at, data) {
24-
return set('a.lbl', at, {textContent: data}, this.parent);
25-
}
26-
swap() {
27-
const e998 = this.parent.children[998];
28-
this.parent.replaceChild(this.parent.children[1], e998);
29-
this.parent.insertBefore(e998, this.parent.children[1]);
30-
}
31-
clear() {this.parent.innerHTML = '';}
32-
}
3313

34-
35-
class Component {
36-
constructor(table) {
37-
Object.assign(this, {
38-
index: 1, data: [], indices: [], view: new View(table)
39-
});
40-
this.all = one([this.data, this.indices]);
41-
}
42-
*createIndices(n) {
43-
const start = this.index;
44-
const end = (this.index += n);
45-
for (let i = start; i < end; i++) yield i;
46-
}
47-
*createLabels(n) {
48-
for (let i = 0; i < n; i++) {
49-
yield adjectives[_random(adjectives.length)] + " " + colours[_random(colours.length)] + " " + nouns[_random(nouns.length)];
50-
}
51-
}
52-
build(n) {
53-
this.indices.push(...this.createIndices(n));
54-
this.data.push(...this.createLabels(n));
55-
return [this.data, this.indices, n];
56-
}
57-
create(n) {this.clear(); this.view.create(...this.build(n));}
58-
append(n) {this.view.append(...this.build(n));}
59-
update() {
60-
const length = this.data.length;
61-
for (let i = 0; i < length; i += 10) this.data[i] += ' !!!';
62-
this.view.setLabel([...range(0, length, 10)], this.data);
63-
}
64-
swap() {
65-
if (this.data.length >= 999) {
66-
[this.data[1], this.data[998]] = [this.data[998], this.data[1]];
67-
[this.indices[1], this.indices[998]] = [this.indices[998], this.indices[1]],
68-
this.view.swap();
14+
function data() {
15+
return {
16+
index: 1,
17+
*createIndices(n) {
18+
const start = this.index;
19+
const end = (this.index += n);
20+
for (let i = start; i < end; i++) yield i;
21+
},
22+
*createLabels(n) {
23+
for (let i = 0; i < n; i++) {
24+
yield adjectives[_random(adjectives.length)] + " " + colours[_random(colours.length)] + " " + nouns[_random(nouns.length)];
25+
}
26+
},
27+
build(n, context) {
28+
context.indices.push(...this.createIndices(n));
29+
context.data.push(...this.createLabels(n));
30+
},
31+
create(n, context) {
32+
this.clear(context);
33+
this.build(n, context);
34+
},
35+
append(n, context) {this.build(n, context)},
36+
update(context) {
37+
const length = context.data.length;
38+
for (let i = 0; i < length; i += 10) context.data[i] += ' !!!';
39+
},
40+
clear(context) {context.data = []; context.indices = [];},
41+
swap(context) {
42+
if (context.data.length >= 999) {
43+
[context.data[1], context.data[998]] = [context.data[998], context.data[1]];
44+
[context.indices[1], context.indices[998]] = [context.indices[998], context.indices[1]];
45+
}
46+
},
47+
remove(element, context) {
48+
const index = Array.from(element.parentNode.children).indexOf(element);
49+
context.indices.splice(index); context.data.splice(index);
6950
}
7051
}
71-
beforeRemove(element) {
72-
return this.all.splice([Array.from(this.view.parent.children).indexOf(element), 1]) && element;
73-
}
74-
clear() {
75-
this.data.length = this.indices.length = 0;
76-
this.view.clear();
77-
}
7852
}
7953

54+
function view(table) {
55+
return {
56+
create(n, context) {
57+
this.clear();
58+
this.append(n, context);
59+
},
60+
append(n, context) {
61+
let markup = [], length = context.data.length;
62+
for (let i = length - n; i < length; i++) markup.push(`<tr><td class='col-md-1'>${context.indices[i]}</td><td class='col-md-4'><a class='lbl'>${context.data[i]}</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>`);
63+
table.append(createTree(markup.join('')));
64+
},
65+
update(context) {
66+
set('a.lbl', [...range(0, context.data.length, 10)],
67+
{textContent: context.data}, table);
68+
},
69+
clear(context) {table.innerHTML = '';},
70+
swap(context) {
71+
if (table.children.length >= 999) {
72+
const e998 = table.children[998];
73+
table.replaceChild(table.children[1], e998);
74+
table.insertBefore(e998, table.children[1]);
75+
}
76+
},
77+
remove(element, context) {table.removeChild(element);}
78+
}
79+
}
8080

8181
apply({
8282
'tbody': table => {
83-
const component = new Component(table);
83+
const component = one([data(), view(table)]);
8484

8585
let selected;
8686
function select(node) {
@@ -94,7 +94,7 @@ apply({
9494
}
9595

9696
const removeListener = (e) => {
97-
table.removeChild(component.beforeRemove(parentSelector(e.target, 'tr')));
97+
component.remove([parentSelector(e.target, 'tr')]);
9898
};
9999

100100
table.addEventListener('click', matchEventListener({
@@ -105,9 +105,9 @@ apply({
105105
const btnListener = (fn) => btn => btn.addEventListener('click', fn);
106106

107107
apply({
108-
'#run': btnListener(() => component.create(1000)),
109-
'#runlots': btnListener(() => component.create(10000)),
110-
'#add': btnListener(() => component.append(1000)),
108+
'#run': btnListener(() => component.create([1000])),
109+
'#runlots': btnListener(() => component.create([10000])),
110+
'#add': btnListener(() => component.append([1000])),
111111
'#update': btnListener(() => component.update()),
112112
'#clear': btnListener(() => component.clear()),
113113
'#swaprows': btnListener(() => component.swap())

0 commit comments

Comments
 (0)