|
| 1 | +import {h, setupSyntheticEvent} from 'stage0' |
| 2 | +import {reconcile} from 'stage0/reconcile' |
| 3 | + |
| 4 | +let did = 1 |
| 5 | +function buildData(count) { |
| 6 | + var 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"]; |
| 7 | + var colours = ["red", "yellow", "blue", "green", "pink", "brown", "purple", "brown", "white", "black", "orange"]; |
| 8 | + var nouns = ["table", "chair", "house", "bbq", "desk", "car", "pony", "cookie", "sandwich", "burger", "pizza", "mouse", "keyboard"]; |
| 9 | + var data = []; |
| 10 | + for (var i = 0; i < count; i++) { |
| 11 | + data.push({ |
| 12 | + id: did++, |
| 13 | + label: adjectives[_random(adjectives.length)] + " " + colours[_random(colours.length)] + " " + nouns[_random(nouns.length)] |
| 14 | + }); |
| 15 | + } |
| 16 | + return data; |
| 17 | +} |
| 18 | +function _random(max) { |
| 19 | + return Math.round(Math.random() * 1000) % max; |
| 20 | +} |
| 21 | + |
| 22 | +const itemView = h` |
| 23 | + <tr> |
| 24 | + <td class="col-md-1">#id</td> |
| 25 | + <td class="col-md-4"> |
| 26 | + <a #select>#label</a> |
| 27 | + </td> |
| 28 | + <td class="col-md-1"><a #del><span class="glyphicon glyphicon-remove" aria-hidden="true"></span></a></td> |
| 29 | + <td class="col-md-6"></td> |
| 30 | + </tr> |
| 31 | +` |
| 32 | +function Item(item, scope) { |
| 33 | + const root = itemView.cloneNode(true) |
| 34 | + const refs = itemView.collect(root) |
| 35 | + |
| 36 | + const {id, label, select, del} = refs |
| 37 | + |
| 38 | + id.nodeValue = item.id |
| 39 | + label.nodeValue = item.label |
| 40 | + select.__click = () => scope.select(item) |
| 41 | + del.__click = () => scope.del(item) |
| 42 | + |
| 43 | + let a = '', a2, |
| 44 | + b = item.label, b2 |
| 45 | + root.update = function(selected) { |
| 46 | + a2 = item.id === selected ? 'danger' : '' |
| 47 | + b2 = item.label |
| 48 | + |
| 49 | + if (a2 !== a) a = root.className = a2 |
| 50 | + if (b2 !== b) b = label.nodeValue = b2 |
| 51 | + } |
| 52 | + |
| 53 | + return root |
| 54 | +} |
| 55 | + |
| 56 | +const mainView = h` |
| 57 | + <div class="container" id="main"> |
| 58 | + <div class="jumbotron"> |
| 59 | + <div class="row"> |
| 60 | + <div class="col-md-6"> |
| 61 | + <h1>stage0</h1> |
| 62 | + </div> |
| 63 | + <div class="col-md-6"> |
| 64 | + <div class="row"> |
| 65 | + <div class="col-sm-6 smallpad"> |
| 66 | + <button type="button" class="btn btn-primary btn-block" id="run" #run>Create 1,000 rows</button> |
| 67 | + </div> |
| 68 | + <div class="col-sm-6 smallpad"> |
| 69 | + <button type="button" class="btn btn-primary btn-block" id="runlots" #runlots>Create 10,000 rows</button> |
| 70 | + </div> |
| 71 | + <div class="col-sm-6 smallpad"> |
| 72 | + <button type="button" class="btn btn-primary |
| 73 | + btn-block" id="add" #add>Append 1,000 rows</button> |
| 74 | + </div> |
| 75 | + <div class="col-sm-6 smallpad"> |
| 76 | + <button type="button" class="btn btn-primary |
| 77 | + btn-block" id="update" #update>Update every 10th row</button> |
| 78 | + </div> |
| 79 | + <div class="col-sm-6 smallpad"> |
| 80 | + <button type="button" class="btn btn-primary |
| 81 | + btn-block" id="clear" #cleardata>Clear</button> |
| 82 | + </div> |
| 83 | + <div class="col-sm-6 smallpad"> |
| 84 | + <button type="button" class="btn btn-primary |
| 85 | + btn-block" id="swaprows" #swaprows>Swap Rows</button> |
| 86 | + </div> |
| 87 | + </div> |
| 88 | + </div> |
| 89 | + </div> |
| 90 | + </div> |
| 91 | + <table class="table table-hover table-striped test-data"> |
| 92 | + <tbody #tbody> |
| 93 | + </tbody> |
| 94 | + </table> |
| 95 | + <span class="preloadicon glyphicon glyphicon-remove" aria-hidden="true"></span> |
| 96 | + </div> |
| 97 | +` |
| 98 | +function Main() { |
| 99 | + setupSyntheticEvent('click') |
| 100 | + |
| 101 | + let root = mainView |
| 102 | + let refs = mainView.collect(root) |
| 103 | + |
| 104 | + let data = [], |
| 105 | + selected |
| 106 | + |
| 107 | + const {tbody} = refs |
| 108 | + |
| 109 | + refs.run.__click = () => { |
| 110 | + data = buildData(1000) |
| 111 | + update(0) |
| 112 | + } |
| 113 | + refs.runlots.__click = () => { |
| 114 | + data = buildData(10000) |
| 115 | + update(0) |
| 116 | + } |
| 117 | + refs.add.__click = () => { |
| 118 | + data = data.concat(buildData(1000)) |
| 119 | + update(0) |
| 120 | + } |
| 121 | + refs.update.__click = () => { |
| 122 | + for (let i = 0; i < data.length; i += 10) { |
| 123 | + data[i].label += ' !!!' |
| 124 | + } |
| 125 | + update(1) |
| 126 | + } |
| 127 | + refs.cleardata.__click = () => { |
| 128 | + data = [] |
| 129 | + update(0) |
| 130 | + } |
| 131 | + refs.swaprows.__click = () => { |
| 132 | + if(data.length > 998) { |
| 133 | + var tmp = data[1]; |
| 134 | + data[1] = data[998]; |
| 135 | + data[998] = tmp; |
| 136 | + } |
| 137 | + update(0) |
| 138 | + } |
| 139 | + |
| 140 | + const scope = { |
| 141 | + select: item => { |
| 142 | + selected = parseInt(item.id) |
| 143 | + update(1) |
| 144 | + }, |
| 145 | + del: item => { |
| 146 | + const id = item.id |
| 147 | + const idx = data.findIndex(d => d.id === id); |
| 148 | + data.splice(idx, 1) |
| 149 | + update(0) |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + let renderedData = [] |
| 154 | + function update(deep) { |
| 155 | + reconcile( |
| 156 | + tbody, |
| 157 | + renderedData, |
| 158 | + data, |
| 159 | + item => Item(item, scope), |
| 160 | + deep ? |
| 161 | + node => node.update(selected) : |
| 162 | + () => {} |
| 163 | + ) |
| 164 | + renderedData = data.slice() |
| 165 | + } |
| 166 | + |
| 167 | + return root |
| 168 | +} |
| 169 | + |
| 170 | +const app = Main() |
| 171 | +document.getElementById('main').appendChild(app) |
0 commit comments