|
1 | | -import { html, render } from '../node_modules/lighterhtml/esm/index.js'; |
| 1 | +import {State} from 'js-framework-benchmark-utils'; |
| 2 | +import {html, render} from 'lighterhtml'; |
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 | | -}; |
17 | | - |
18 | | -const _random = max => Math.round(Math.random() * 1000) % max; |
19 | | - |
20 | | -const scope = { |
21 | | - add() { |
22 | | - scope.data = scope.data.concat(buildData(1000)); |
23 | | - update(main, scope); |
24 | | - }, |
25 | | - run() { |
26 | | - scope.data = buildData(1000); |
27 | | - update(main, scope); |
28 | | - }, |
29 | | - runLots() { |
30 | | - scope.data = buildData(10000); |
31 | | - update(main, scope); |
32 | | - }, |
33 | | - clear() { |
34 | | - scope.data = []; |
35 | | - update(main, 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(main, 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(main, 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(main, scope); |
63 | | - }, |
64 | | - select(id) { |
65 | | - scope.selected = id; |
66 | | - update(main, scope); |
67 | | - }, |
68 | | - selected: -1, |
69 | | - data: [], |
70 | | -}; |
| 4 | +import Jumbotron from './jumbotron.js'; |
| 5 | +import Table from './table.js'; |
71 | 6 |
|
| 7 | +const state = State(update); |
72 | 8 | const main = document.getElementById('container'); |
73 | | -update(main, scope); |
74 | 9 |
|
75 | | -function update( |
76 | | - where, |
77 | | - {data, selected, run, runLots, add, update, clear, swapRows, interact} |
78 | | -) { |
79 | | - render(where, html` |
| 10 | +update(state); |
| 11 | + |
| 12 | +function update(state) { |
| 13 | + render(main, html` |
80 | 14 | <div class="container"> |
81 | | - <div class="jumbotron"> |
82 | | - <div class="row"> |
83 | | - <div class="col-md-6"> |
84 | | - <h1>lighterhtml keyed</h1> |
85 | | - </div> |
86 | | - <div class="col-md-6"> |
87 | | - <div class="row"> |
88 | | - <div class="col-sm-6 smallpad"> |
89 | | - <button type="button" class="btn btn-primary btn-block" |
90 | | - id="run" onclick=${run}>Create 1,000 rows</button> |
91 | | - </div> |
92 | | - <div class="col-sm-6 smallpad"> |
93 | | - <button type="button" class="btn btn-primary btn-block" |
94 | | - id="runlots" onclick=${runLots}>Create 10,000 rows</button> |
95 | | - </div> |
96 | | - <div class="col-sm-6 smallpad"> |
97 | | - <button type="button" class="btn btn-primary btn-block" |
98 | | - id="add" onclick=${add}>Append 1,000 rows</button> |
99 | | - </div> |
100 | | - <div class="col-sm-6 smallpad"> |
101 | | - <button type="button" class="btn btn-primary btn-block" |
102 | | - id="update" onclick=${update}>Update every 10th row</button> |
103 | | - </div> |
104 | | - <div class="col-sm-6 smallpad"> |
105 | | - <button type="button" class="btn btn-primary btn-block" |
106 | | - id="clear" onclick=${clear}>Clear</button> |
107 | | - </div> |
108 | | - <div class="col-sm-6 smallpad"> |
109 | | - <button type="button" class="btn btn-primary btn-block" |
110 | | - id="swaprows" onclick=${swapRows}>Swap Rows</button> |
111 | | - </div> |
112 | | - </div> |
113 | | - </div> |
114 | | - </div> |
115 | | - </div> |
116 | | - <table onclick=${interact} class="table table-hover table-striped test-data"> |
117 | | - <tbody>${ |
118 | | - data.map(item => { |
119 | | - const {id, label} = item; |
120 | | - return html.for(item)` |
121 | | - <tr id=${id} class=${id === selected ? 'danger' : ''}> |
122 | | - <td class="col-md-1">${id}</td> |
123 | | - <td class="col-md-4"> |
124 | | - <a data-action='select'>${label}</a> |
125 | | - </td> |
126 | | - <td class="col-md-1"> |
127 | | - <a data-action='delete'> |
128 | | - <span class="glyphicon glyphicon-remove" aria-hidden="true"></span> |
129 | | - </a> |
130 | | - </td> |
131 | | - <td class="col-md-6"></td> |
132 | | - </tr>`; |
133 | | - }) |
134 | | - }</tbody> |
135 | | - </table> |
136 | | - <span class="preloadicon glyphicon glyphicon-remove" aria-hidden="true"></span> |
| 15 | + ${Jumbotron(state)} |
| 16 | + ${Table(state)} |
| 17 | + <span class="preloadicon glyphicon glyphicon-remove" aria-hidden="true" /> |
137 | 18 | </div>`); |
138 | 19 | } |
0 commit comments