Skip to content

Commit b9dfe5c

Browse files
committed
karyon implementation update
1 parent 98b28e3 commit b9dfe5c

File tree

1 file changed

+19
-31
lines changed
  • frameworks/keyed/karyon/src

1 file changed

+19
-31
lines changed

frameworks/keyed/karyon/src/app.js

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,53 +4,42 @@ const adjectives = ["pretty", "large", "big", "small", "tall", "short", "long",
44
const colours = ["red", "yellow", "blue", "green", "pink", "brown", "purple", "brown", "white", "black", "orange"];
55
const nouns = ["table", "chair", "house", "bbq", "desk", "car", "pony", "cookie", "sandwich", "burger", "pizza", "mouse", "keyboard"];
66

7-
const rows = State([]);
8-
const selection = State();
7+
const rows = State([]), selection = State();
98

109
const create = ((ID, $) => count => [...Array(count)].map(() =>
1110
Row(++ID, State(`${$(adjectives)} ${$(colours)} ${$(nouns)}`))
1211
))(0, $ => $[Math.round(Math.random() * 1000) % $.length]);
1312

1413
const select = function () { selection(this.ID); };
1514

16-
const remove = (filter => function () {
17-
rows(rows().filter(filter, this.ID));
18-
})(function (row) { return row?.ID !== this; });
15+
const remove = function () { rows(rows().filter(r => r?.ID !== this.ID)); };
1916

20-
const track = (ref, value, resolve) => {
21-
const tracker = State(resolve(State.peek(ref) === value));
22-
return State.on(ref, (i, o) =>
23-
i === value ? tracker(resolve(true)) :
24-
o === value && tracker(resolve(false))), tracker;
25-
};
17+
State.on(selection, (i, o) => rows().forEach(({ID, class: $}) =>
18+
ID === i ? $('danger') : ID === o && $(null)));
2619

27-
const Row = (selected => (ID, text) =>
28-
({is: 'tr', content: [
29-
{is: 'td', class: 'col-md-1', content: ID},
30-
{is: 'td', class: 'col-md-4', content:
31-
{is: 'a', action: select, content: text}},
32-
{is: 'td', class: 'col-md-1', content:
33-
{is: 'a', action: remove, content: Icon('')}},
34-
{is: 'td', class: 'col-md-6'}
35-
], class: track(selection, ID, selected), ID, text})
36-
)(on => on && 'danger');
20+
const Row = (ID, text) => ({is: 'tr', content: [
21+
{is: 'td', class: 'col-md-1', content: ID},
22+
{is: 'td', class: 'col-md-4', content:
23+
{is: 'a', action: select, content: text}},
24+
{is: 'td', class: 'col-md-1', content:
25+
{is: 'a', action: remove, content: {is: 'span', attrs: icon}}},
26+
{is: 'td', class: 'col-md-6'}
27+
], ID, text, class: State()});
3728

38-
const Icon = name =>
39-
({is: 'span', attrs: {'aria-hidden': 'true'},
40-
class: `glyphicon glyphicon-remove ${name}`});
29+
const icon = {class: 'glyphicon glyphicon-remove', 'aria-hidden': 'true'};
4130

4231
const actions = [
4332
['Create 1,000 rows', 'run', () => rows(create(1000))],
4433
['Create 10,000 rows', 'runlots', () => rows(create(10000))],
4534
['Append 1,000 rows', 'add', () => rows([...rows(), ...create(1000)])],
4635
['Update every 10th row', 'update', () => {
47-
for (let r = rows(), i = 0; i < r.length; i += 10)
48-
r[i]?.text(`${r[i].text()} !!!`);
36+
for (let r = rows(), t, i = 0; t = r[i]?.text; i += 10)
37+
t(t() + ' !!!');
4938
}],
5039
['Clear', 'clear', () => rows([])],
5140
['Swap Rows', 'swaprows', () => {
52-
const r = rows();
53-
[r[1], r[998]] = [r[998], r[1]], rows([...r]);
41+
const r = [...rows()];
42+
r[998] && ([r[1], r[998]] = [r[998], r[1]], rows(r));
5443
}]].map(([content, id, click]) =>
5544
({class: 'col-sm-6 smallpad', content:
5645
{is: 'button', id, class: 'btn btn-primary btn-block',
@@ -64,10 +53,9 @@ insert({id: 'main', content:
6453
{is: 'h1', content: document.title}},
6554
{class: 'col-md-6', content: {class: 'row', content: actions}}
6655
]}
67-
},
56+
}, {is: 'span', attrs: icon, class: {preloadicon: true}},
6857
{is: 'table', class: 'table table-hover table-striped test-data',
6958
content: {is: 'tbody', id: 'tbody', content: rows},
70-
listen: {click: (event, context) => context?.action?.()}},
71-
Icon('preloadicon')
59+
listen: {click: (e, context) => context?.action?.()}}
7260
]}
7361
}, document.body);

0 commit comments

Comments
 (0)