|
| 1 | +import * as b from 'bobril'; |
| 2 | +import { Store } from "./store"; |
| 3 | + |
| 4 | +// For who ever will read this: in Bobril you normally don't use bootstrap or class names directly, you use b.styleDef instead. |
| 5 | +b.asset("../../css/bootstrap/dist/css/bootstrap.min.css"); |
| 6 | +b.asset("../../css/main.css"); |
| 7 | + |
| 8 | +var startTime: number; |
| 9 | +var lastMeasure: string; |
| 10 | +var startMeasure = function (name) { |
| 11 | + startTime = performance.now(); |
| 12 | + lastMeasure = name; |
| 13 | +} |
| 14 | +var stopMeasure = function () { |
| 15 | + var last = lastMeasure; |
| 16 | + if (lastMeasure) { |
| 17 | + window.setTimeout(function () { |
| 18 | + lastMeasure = null; |
| 19 | + var stop = performance.now(); |
| 20 | + var duration = 0; |
| 21 | + console.log(last + " took " + (stop - startTime)); |
| 22 | + }, 0); |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +function divWithClass(name: string, children: b.IBobrilChildren) { |
| 27 | + return { tag: "div", className: name, children }; |
| 28 | +} |
| 29 | + |
| 30 | +function tdWithClass(name: string, children: b.IBobrilChildren) { |
| 31 | + return { tag: "td", className: name, children }; |
| 32 | +} |
| 33 | + |
| 34 | +let store = new Store(); |
| 35 | + |
| 36 | +interface IButtonData { |
| 37 | + children?: b.IBobrilChildren; |
| 38 | + id: string; |
| 39 | + action: () => void; |
| 40 | +} |
| 41 | + |
| 42 | +interface IButtonCtx extends b.IBobrilCtx { |
| 43 | + data: IButtonData; |
| 44 | +} |
| 45 | + |
| 46 | +const Button = b.createVirtualComponent<IButtonData>({ |
| 47 | + render(ctx: IButtonCtx, me: b.IBobrilNode) { |
| 48 | + const d = ctx.data; |
| 49 | + me.tag = "button"; |
| 50 | + me.className = "btn btn-primary btn-block"; |
| 51 | + me.attrs = { id: d.id }; |
| 52 | + me.children = d.children; |
| 53 | + }, |
| 54 | + onClick(ctx: IButtonCtx): boolean { |
| 55 | + startMeasure(ctx.data.id); |
| 56 | + ctx.data.action(); |
| 57 | + b.invalidate(); |
| 58 | + return true; |
| 59 | + } |
| 60 | +}); |
| 61 | + |
| 62 | +interface IHeaderData { |
| 63 | +} |
| 64 | + |
| 65 | +interface IHeaderCtx extends b.IBobrilCtx { |
| 66 | + data: IHeaderData; |
| 67 | +} |
| 68 | + |
| 69 | +const Header = b.createComponent<IHeaderData>({ |
| 70 | + shouldChange() { return false; }, |
| 71 | + render(ctx: IHeaderCtx, me: b.IBobrilNode) { |
| 72 | + const d = ctx.data; |
| 73 | + me.className = "jumbotron"; |
| 74 | + me.children = divWithClass("row", [ |
| 75 | + divWithClass("col-md-6", { tag: 'h1', children: "Bobril v4.42.0" }), |
| 76 | + divWithClass("col-md-6", [ |
| 77 | + divWithClass("col-sm-6 smallpad", Button({ id: "run", action: () => store.run() }, "Create 1,000 rows")), |
| 78 | + divWithClass("col-sm-6 smallpad", Button({ id: "runlots", action: () => store.runLots() }, "Create 10,000 rows")), |
| 79 | + divWithClass("col-sm-6 smallpad", Button({ id: "add", action: () => store.add() }, "Append 1,000 rows")), |
| 80 | + divWithClass("col-sm-6 smallpad", Button({ id: "update", action: () => store.update() }, "Update every 10th row")), |
| 81 | + divWithClass("col-sm-6 smallpad", Button({ id: "clear", action: () => store.clear() }, "Clear")), |
| 82 | + divWithClass("col-sm-6 smallpad", Button({ id: "swaprows", action: () => store.swapRows() }, "Swap Rows")), |
| 83 | + ]) |
| 84 | + ]); |
| 85 | + } |
| 86 | +}); |
| 87 | + |
| 88 | +const ClickSelect = { |
| 89 | + onClick(ctx: { data: number }) { |
| 90 | + startMeasure("select"); |
| 91 | + store.select(ctx.data); |
| 92 | + b.invalidate(); |
| 93 | + return true; |
| 94 | + } |
| 95 | +}; |
| 96 | + |
| 97 | +const ClickRemove = { |
| 98 | + onClick(ctx: { data: number }) { |
| 99 | + startMeasure("delete"); |
| 100 | + store.delete(ctx.data); |
| 101 | + b.invalidate(); |
| 102 | + return true; |
| 103 | + } |
| 104 | +}; |
| 105 | + |
| 106 | +interface IRowData { |
| 107 | + item: { id: number, label: string }; |
| 108 | + selected: boolean; |
| 109 | +} |
| 110 | + |
| 111 | +interface IRowCtx extends b.IBobrilCtx { |
| 112 | + data: IRowData; |
| 113 | +} |
| 114 | + |
| 115 | +const Row = b.createVirtualComponent<IRowData>({ |
| 116 | + init(ctx: IRowCtx) { |
| 117 | + }, |
| 118 | + shouldChange(ctx: IRowCtx, me: b.IBobrilNode): boolean { |
| 119 | + return ctx.data.item !== me.data.item || ctx.data.selected !== me.data.selected; |
| 120 | + }, |
| 121 | + render(ctx: IRowCtx, me: b.IBobrilNode) { |
| 122 | + const d = ctx.data; |
| 123 | + me.tag = "tr"; |
| 124 | + me.className = d.selected ? "danger" : ""; |
| 125 | + const id = d.item.id; |
| 126 | + me.children = [ |
| 127 | + tdWithClass("col-md-1", id), |
| 128 | + tdWithClass("col-md-4", { tag: "a", data: id, component: ClickSelect, children: d.item.label }), |
| 129 | + tdWithClass("col-md-1", { tag: "a", data: id, component: ClickRemove, children: { tag: "span", className: "glyphicon glyphicon-remove", attrs: { "aria-hidden": "true" } } }), |
| 130 | + tdWithClass("col-md-6", "") |
| 131 | + ]; |
| 132 | + } |
| 133 | +}); |
| 134 | + |
| 135 | +b.init(() => { |
| 136 | + return { |
| 137 | + tag: "div", className: "container", component: { |
| 138 | + postUpdateDom: stopMeasure |
| 139 | + }, children: [ |
| 140 | + Header(), |
| 141 | + { |
| 142 | + tag: "table", className: "table table-hover table-striped test-data", children: { |
| 143 | + tag: "tbody", children: store.data.map((item) => b.withKey(Row({ item, selected: item.id === store.selected }), <any>item.id)) |
| 144 | + } |
| 145 | + }, |
| 146 | + { tag: "span", className: "preloadicon glyphicon glyphicon-remove", attrs: { "aria-hidden": "true" } } |
| 147 | + ] |
| 148 | + }; |
| 149 | +}); |
0 commit comments