|
1 | | -import { app, Component } from 'apprun'; |
2 | | -import Store from './store'; |
| 1 | +import { app, View } from 'apprun'; |
| 2 | +import { state, update, State, Events } from './store'; |
3 | 3 |
|
4 | | -const store = new Store(); |
| 4 | +const getId = (elem: any) => elem.closest('tr').id; |
5 | 5 |
|
6 | | -const update = { |
7 | | - |
8 | | - run(store) { |
9 | | - store.run(); |
10 | | - return store; |
11 | | - }, |
12 | | - |
13 | | - add(store) { |
14 | | - store.add(); |
15 | | - return store; |
16 | | - }, |
17 | | - |
18 | | - remove(store, id) { |
19 | | - store.delete(id); |
20 | | - document.getElementById(id).remove(); |
21 | | - }, |
22 | | - |
23 | | - select(store, id) { |
24 | | - if (store.selected) { |
25 | | - document.getElementById(store.selected).className = ''; |
26 | | - } |
27 | | - store.select(id); |
28 | | - document.getElementById(id).className = 'danger'; |
29 | | - }, |
30 | | - |
31 | | - update(store) { |
32 | | - store.update(); |
33 | | - return store; |
34 | | - }, |
35 | | - |
36 | | - runlots(store) { |
37 | | - store.runLots(); |
38 | | - return store; |
39 | | - }, |
40 | | - |
41 | | - clear(store) { |
42 | | - store.clear(); |
43 | | - document.getElementById("main-table").innerHTML = ""; |
44 | | - }, |
45 | | - |
46 | | - swaprows(store) { |
47 | | - store.swapRows(); |
48 | | - return store; |
49 | | - } |
| 6 | +const click = (state: State, e: Event) => { |
| 7 | + const t = e.target as HTMLElement; |
| 8 | + if (!t) return; |
| 9 | + e.preventDefault(); |
| 10 | + if (t.tagName === 'BUTTON' && t.id) { |
| 11 | + component.run(t.id as Events); |
| 12 | + } else if (t.matches('.remove')) { |
| 13 | + const id = getId(t); |
| 14 | + component.run('delete', id); |
| 15 | + } else if (t.matches('.lbl')) { |
| 16 | + const id = getId(t); |
| 17 | + component.run('select', id); |
| 18 | + } |
50 | 19 | } |
51 | 20 |
|
52 | | -const view = (model) => { |
53 | | - const rows = model.data.map((curr) => { |
54 | | - const selected = curr.id == model.selected ? 'danger' : undefined; |
55 | | - const id = curr.id; |
56 | | - return <tr className={selected} id={id} key={id}> |
57 | | - <td className="col-md-1">{id}</td> |
58 | | - <td className="col-md-4"> |
59 | | - <a className="lbl">{curr.label}</a> |
60 | | - </td> |
61 | | - <td className="col-md-1"> |
62 | | - <a className="remove"> |
63 | | - <span className="glyphicon glyphicon-remove remove" aria-hidden="true"></span> |
64 | | - </a> |
65 | | - </td> |
66 | | - <td className="col-md-6"></td> |
67 | | - </tr>; |
68 | | - }); |
69 | | - |
70 | | - return (<div className="container" onclick={click}> |
71 | | - <div className="jumbotron"> |
72 | | - <div className="row"> |
73 | | - <div className="col-md-6"> |
74 | | - <h1>AppRun</h1> |
75 | | - </div> |
76 | | - <div className="col-md-6"> |
77 | | - <div className="row"> |
78 | | - <div className="col-sm-6 smallpad"> |
79 | | - <button type="button" className="btn btn-primary btn-block" id="run">Create 1,000 rows</button> |
80 | | - </div> |
81 | | - <div className="col-sm-6 smallpad"> |
82 | | - <button type="button" className="btn btn-primary btn-block" id="runlots">Create 10,000 rows</button> |
83 | | - </div> |
84 | | - <div className="col-sm-6 smallpad"> |
85 | | - <button type="button" className="btn btn-primary btn-block" id="add">Append 1,000 rows</button> |
86 | | - </div> |
87 | | - <div className="col-sm-6 smallpad"> |
88 | | - <button type="button" className="btn btn-primary btn-block" id="update">Update every 10th row</button> |
89 | | - </div> |
90 | | - <div className="col-sm-6 smallpad"> |
91 | | - <button type="button" className="btn btn-primary btn-block" id="clear">Clear</button> |
92 | | - </div> |
93 | | - <div className="col-sm-6 smallpad"> |
94 | | - <button type="button" className="btn btn-primary btn-block" id="swaprows">Swap Rows</button> |
95 | | - </div> |
96 | | - </div> |
97 | | - </div> |
98 | | - </div> |
| 21 | +const view:View<State> = state => <div class="container" $onclick={click}> |
| 22 | + <div class="jumbotron"> |
| 23 | + <div class="row"> |
| 24 | + <div class="col-md-6"> |
| 25 | + <h1>AppRun</h1> |
| 26 | + </div> |
| 27 | + <div class="col-md-6"> |
| 28 | + <div class="row"> |
| 29 | + <div class="col-sm-6 smallpad"> |
| 30 | + <button type="button" class="btn btn-primary btn-block" id="run">Create 1,000 rows</button> |
| 31 | + </div> |
| 32 | + <div class="col-sm-6 smallpad"> |
| 33 | + <button type="button" class="btn btn-primary btn-block" id="runlots">Create 10,000 rows</button> |
| 34 | + </div> |
| 35 | + <div class="col-sm-6 smallpad"> |
| 36 | + <button type="button" class="btn btn-primary btn-block" id="add">Append 1,000 rows</button> |
| 37 | + </div> |
| 38 | + <div class="col-sm-6 smallpad"> |
| 39 | + <button type="button" class="btn btn-primary btn-block" id="update">Update every 10th row</button> |
| 40 | + </div> |
| 41 | + <div class="col-sm-6 smallpad"> |
| 42 | + <button type="button" class="btn btn-primary btn-block" id="clear">Clear</button> |
| 43 | + </div> |
| 44 | + <div class="col-sm-6 smallpad"> |
| 45 | + <button type="button" class="btn btn-primary btn-block" id="swaprows">Swap Rows</button> |
| 46 | + </div> |
99 | 47 | </div> |
100 | | - <table className="table table-hover table-striped test-data" id="main-table"> |
101 | | - <tbody> |
102 | | - {rows} |
103 | | - </tbody> |
104 | | - </table> |
105 | | - <span className="preloadicon glyphicon glyphicon-remove" aria-hidden="true"></span> |
106 | | - </div>); |
107 | | -} |
| 48 | + </div> |
| 49 | + </div> |
| 50 | + </div> |
| 51 | + <table class="table table-hover table-striped test-data" id="main-table"> |
| 52 | + <tbody> |
| 53 | + {state.data.map(item => { |
| 54 | + const selected = item.id == state.selected ? 'danger' : undefined; |
| 55 | + return <tr class={selected} id={item.id} key={item.id}> |
| 56 | + <td class="col-md-1">{item.id}</td> |
| 57 | + <td class="col-md-4"> |
| 58 | + <a class="lbl">{item.label}</a> |
| 59 | + </td> |
| 60 | + <td class="col-md-1"> |
| 61 | + <a class="remove"> |
| 62 | + <span class="glyphicon glyphicon-remove remove" aria-hidden="true"></span> |
| 63 | + </a> |
| 64 | + </td> |
| 65 | + <td class="col-md-6"></td> |
| 66 | + </tr>; |
| 67 | + })} |
| 68 | + </tbody> |
| 69 | + </table> |
| 70 | + <span class="preloadicon glyphicon glyphicon-remove" aria-hidden="true"></span> |
| 71 | +</div>; |
108 | 72 |
|
109 | | -const getId = (elem) => { |
110 | | - while (elem) { |
111 | | - if (elem.tagName === "TR") { |
112 | | - return elem.id; |
113 | | - } |
114 | | - elem = elem.parentNode; |
115 | | - } |
116 | | - return undefined; |
117 | | -} |
118 | | - |
119 | | -const click = (e) => { |
120 | | - const t = e.target as HTMLElement; |
121 | | - if (!t) return; |
122 | | - if (t.tagName === 'BUTTON' && t.id) { |
123 | | - e.preventDefault(); |
124 | | - component.run(t.id); |
125 | | - } else if (t.matches('.remove')) { |
126 | | - e.preventDefault(); |
127 | | - component.run('remove', getId(t)); |
128 | | - } else if (t.matches('.lbl')) { |
129 | | - e.preventDefault(); |
130 | | - component.run('select', getId(t)); |
131 | | - } |
132 | | -}; |
| 73 | +const component = app.start<State, Events>('main', state, view, update); |
133 | 74 |
|
134 | | -const component = new Component(store, view, update); |
135 | | -component['-patch-vdom-on'] = true; |
136 | | -component.rendered = () => { |
137 | | - store.selected && (document.getElementById(store.selected).className = 'danger'); |
138 | | -} |
139 | | -component.start(document.getElementById('main')); |
0 commit comments