|
| 1 | +import { ComponentContext, jsx, ObservableEvent } from 'butterfloat' |
| 2 | +import { AppViewModel } from './app-vm.js' |
| 3 | +import { Row } from './row.js' |
| 4 | +import { map, withLatestFrom } from 'rxjs' |
| 5 | + |
| 6 | +interface AppEvents { |
| 7 | + run: ObservableEvent<MouseEvent> |
| 8 | + runlots: ObservableEvent<MouseEvent> |
| 9 | + add: ObservableEvent<MouseEvent> |
| 10 | + update: ObservableEvent<MouseEvent> |
| 11 | + clear: ObservableEvent<MouseEvent> |
| 12 | + swaprows: ObservableEvent<MouseEvent> |
| 13 | + tbodyAttach: ObservableEvent<HTMLElement> |
| 14 | +} |
| 15 | + |
| 16 | +export function App( |
| 17 | + _props: unknown, |
| 18 | + { bindEffect, bindImmediateEffect, events }: ComponentContext<AppEvents>, |
| 19 | +) { |
| 20 | + const vm = new AppViewModel() |
| 21 | + |
| 22 | + const children = vm.rows.pipe(map((row) => () => <Row vm={row} />)) |
| 23 | + |
| 24 | + bindImmediateEffect(events.run, () => vm.createRows(1000)) |
| 25 | + bindImmediateEffect(events.runlots, () => vm.createRows(10000)) |
| 26 | + bindImmediateEffect(events.add, () => vm.appendRows(1000)) |
| 27 | + bindImmediateEffect(events.clear, () => vm.clear()) |
| 28 | + bindEffect( |
| 29 | + events.update.pipe(withLatestFrom(events.tbodyAttach)), |
| 30 | + ([_, tbody]) => { |
| 31 | + const rows = tbody.querySelectorAll('tr') |
| 32 | + for (let i = 0; i < rows.length; i += 10) { |
| 33 | + const row = rows[i] |
| 34 | + const id = Number.parseInt(row.dataset.id!, 10) |
| 35 | + vm.updateRow(id) |
| 36 | + } |
| 37 | + }, |
| 38 | + ) |
| 39 | + bindEffect( |
| 40 | + events.swaprows.pipe(withLatestFrom(events.tbodyAttach)), |
| 41 | + ([_, tbody]) => { |
| 42 | + const rows = tbody.querySelectorAll('tr') |
| 43 | + if (rows.length > 998) { |
| 44 | + const row0 = rows[0] |
| 45 | + const row1 = rows[1] |
| 46 | + const row997 = rows[997] |
| 47 | + const row998 = rows[998] |
| 48 | + row0.after(row998) |
| 49 | + row997.after(row1) |
| 50 | + } |
| 51 | + }, |
| 52 | + ) |
| 53 | + |
| 54 | + return ( |
| 55 | + <div class="container"> |
| 56 | + <div class="jumbotron"> |
| 57 | + <div class="row"> |
| 58 | + <div class="col-md-6"> |
| 59 | + <h1>Butterfloat</h1> |
| 60 | + </div> |
| 61 | + <div class="col-md-6"> |
| 62 | + <div class="row"> |
| 63 | + <div class="col-sm-6 smallpad"> |
| 64 | + <button |
| 65 | + type="button" |
| 66 | + class="btn btn-primary btn-block" |
| 67 | + id="run" |
| 68 | + events={{ click: events.run }} |
| 69 | + > |
| 70 | + Create 1,000 rows |
| 71 | + </button> |
| 72 | + </div> |
| 73 | + <div class="col-sm-6 smallpad"> |
| 74 | + <button |
| 75 | + type="button" |
| 76 | + class="btn btn-primary btn-block" |
| 77 | + id="runlots" |
| 78 | + events={{ click: events.runlots }} |
| 79 | + > |
| 80 | + Create 10,000 rows |
| 81 | + </button> |
| 82 | + </div> |
| 83 | + <div class="col-sm-6 smallpad"> |
| 84 | + <button |
| 85 | + type="button" |
| 86 | + class="btn btn-primary btn-block" |
| 87 | + id="add" |
| 88 | + events={{ click: events.add }} |
| 89 | + > |
| 90 | + Append 1,000 rows |
| 91 | + </button> |
| 92 | + </div> |
| 93 | + <div class="col-sm-6 smallpad"> |
| 94 | + <button |
| 95 | + type="button" |
| 96 | + class="btn btn-primary btn-block" |
| 97 | + id="update" |
| 98 | + events={{ click: events.update }} |
| 99 | + > |
| 100 | + Update every 10th row |
| 101 | + </button> |
| 102 | + </div> |
| 103 | + <div class="col-sm-6 smallpad"> |
| 104 | + <button |
| 105 | + type="button" |
| 106 | + class="btn btn-primary btn-block" |
| 107 | + id="clear" |
| 108 | + events={{ click: events.clear }} |
| 109 | + > |
| 110 | + Clear |
| 111 | + </button> |
| 112 | + </div> |
| 113 | + <div class="col-sm-6 smallpad"> |
| 114 | + <button |
| 115 | + type="button" |
| 116 | + class="btn btn-primary btn-block" |
| 117 | + id="swaprows" |
| 118 | + events={{ click: events.swaprows }} |
| 119 | + > |
| 120 | + Swap Rows |
| 121 | + </button> |
| 122 | + </div> |
| 123 | + </div> |
| 124 | + </div> |
| 125 | + </div> |
| 126 | + </div> |
| 127 | + <table class="table table-hover table-striped test-data"> |
| 128 | + <tbody |
| 129 | + id="tbody" |
| 130 | + childrenBind={children} |
| 131 | + childrenBindMode="append" |
| 132 | + events={{ bfDomAttach: events.tbodyAttach }} |
| 133 | + ></tbody> |
| 134 | + </table> |
| 135 | + </div> |
| 136 | + ) |
| 137 | +} |
0 commit comments