|
1 |
| -import { bind, wire } from '../node_modules/hyperhtml/esm/index.js'; |
| 1 | +import {State} from 'js-framework-benchmark-utils'; |
| 2 | +import {bind} from 'hyperhtml'; |
2 | 3 |
|
3 |
| -let did = 1; |
4 |
| -const buildData = (count) => { |
5 |
| - 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"]; |
6 |
| - const colours = ["red", "yellow", "blue", "green", "pink", "brown", "purple", "brown", "white", "black", "orange"]; |
7 |
| - const nouns = ["table", "chair", "house", "bbq", "desk", "car", "pony", "cookie", "sandwich", "burger", "pizza", "mouse", "keyboard"]; |
8 |
| - const data = []; |
9 |
| - for (let i = 0; i < count; i++) { |
10 |
| - data.push({ |
11 |
| - id: did++, |
12 |
| - label: adjectives[_random(adjectives.length)] + " " + colours[_random(colours.length)] + " " + nouns[_random(nouns.length)] |
13 |
| - }); |
14 |
| - } |
15 |
| - return data; |
16 |
| -}; |
| 4 | +import Row from './row.js'; |
17 | 5 |
|
18 |
| -const _random = max => Math.round(Math.random() * 1000) % max; |
| 6 | +const state = State(update); |
| 7 | +const html = bind(document.getElementById('main')); |
19 | 8 |
|
20 |
| -const scope = { |
21 |
| - add() { |
22 |
| - scope.data = scope.data.concat(buildData(1000)); |
23 |
| - update(scope); |
24 |
| - }, |
25 |
| - run() { |
26 |
| - scope.data = buildData(1000); |
27 |
| - update(scope); |
28 |
| - }, |
29 |
| - runLots() { |
30 |
| - scope.data = buildData(10000); |
31 |
| - update(scope); |
32 |
| - }, |
33 |
| - clear() { |
34 |
| - scope.data = []; |
35 |
| - update(scope); |
36 |
| - }, |
37 |
| - update() { |
38 |
| - const {data} = scope; |
39 |
| - for (let i = 0, {length} = data; i < length; i += 10) |
40 |
| - data[i].label += ' !!!'; |
41 |
| - update(scope); |
42 |
| - }, |
43 |
| - swapRows() { |
44 |
| - const {data} = scope; |
45 |
| - if (data.length > 998) { |
46 |
| - const tmp = data[1]; |
47 |
| - data[1] = data[998]; |
48 |
| - data[998] = tmp; |
49 |
| - } |
50 |
| - update(scope); |
51 |
| - }, |
52 |
| - interact(event) { |
53 |
| - event.preventDefault(); |
54 |
| - const a = event.target.closest('a'); |
55 |
| - const id = parseInt(a.closest('tr').id, 10); |
56 |
| - scope[a.dataset.action](id); |
57 |
| - }, |
58 |
| - delete(id) { |
59 |
| - const {data} = scope; |
60 |
| - const idx = data.findIndex(d => d.id === id); |
61 |
| - data.splice(idx, 1); |
62 |
| - update(scope); |
63 |
| - }, |
64 |
| - select(id) { |
65 |
| - scope.selected = id; |
66 |
| - update(scope); |
67 |
| - }, |
68 |
| - selected: -1, |
69 |
| - data: [], |
| 9 | +const click = ({target}) => { |
| 10 | + const a = target.closest('a'); |
| 11 | + const {action} = a.dataset; |
| 12 | + state[action](+a.closest('tr').id); |
70 | 13 | };
|
71 | 14 |
|
72 |
| -const render = bind(document.getElementById('main')); |
73 |
| -update(scope); |
| 15 | +update(state); |
74 | 16 |
|
75 |
| -function update({data, selected, run, runLots, add, update, clear, swapRows, interact}) { |
76 |
| - render` |
| 17 | +function update({data, selected, run, runLots, add, update, clear, swapRows}) { |
| 18 | + html` |
77 | 19 | <div class="container">
|
78 | 20 | <div class="jumbotron">
|
79 | 21 | <div class="row">
|
80 | 22 | <div class="col-md-6">
|
81 |
| - <h1>lighterhtml</h1> |
| 23 | + <h1>hyper(HTML) keyed</h1> |
82 | 24 | </div>
|
83 | 25 | <div class="col-md-6">
|
84 | 26 | <div class="row">
|
@@ -110,25 +52,8 @@ function update({data, selected, run, runLots, add, update, clear, swapRows, int
|
110 | 52 | </div>
|
111 | 53 | </div>
|
112 | 54 | </div>
|
113 |
| - <table onclick=${interact} class="table table-hover table-striped test-data"> |
114 |
| - <tbody>${ |
115 |
| - data.map(item => { |
116 |
| - const {id, label} = item; |
117 |
| - return wire(item)` |
118 |
| - <tr id=${id} class=${id === selected ? 'danger' : ''}> |
119 |
| - <td class="col-md-1">${id}</td> |
120 |
| - <td class="col-md-4"> |
121 |
| - <a data-action='select'>${label}</a> |
122 |
| - </td> |
123 |
| - <td class="col-md-1"> |
124 |
| - <a data-action='delete'> |
125 |
| - <span class="glyphicon glyphicon-remove" aria-hidden="true"></span> |
126 |
| - </a> |
127 |
| - </td> |
128 |
| - <td class="col-md-6"></td> |
129 |
| - </tr>`; |
130 |
| - }) |
131 |
| - }</tbody> |
| 55 | + <table onclick=${click} class="table table-hover table-striped test-data"> |
| 56 | + <tbody>${data.map(item => Row(data, selected, item))}</tbody> |
132 | 57 | </table>
|
133 | 58 | <span class="preloadicon glyphicon glyphicon-remove" aria-hidden="true"></span>
|
134 | 59 | </div>`;
|
|
0 commit comments