|
| 1 | +import { observable, trigger, html, map } from 'naiv' |
| 2 | + |
| 3 | +// data |
| 4 | +let id = 1 |
| 5 | +const random = max => Math.round(Math.random() * 1000) % max |
| 6 | +const buildData = (count = 1000) => { |
| 7 | + 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'] |
| 8 | + const colours = ['red', 'yellow', 'blue', 'green', 'pink', 'brown', 'purple', 'brown', 'white', 'black', 'orange'] |
| 9 | + const nouns = ['table', 'chair', 'house', 'bbq', 'desk', 'car', 'pony', 'cookie', 'sandwich', 'burger', 'pizza', 'mouse', 'keyboard'] |
| 10 | + const data = [] |
| 11 | + |
| 12 | + for (var i = 0; i < count; i++) { |
| 13 | + const label = `${adjectives[random(adjectives.length)]} ${colours[random(colours.length)]} ${nouns[random(nouns.length)]}` |
| 14 | + data.push({ |
| 15 | + id: id++, |
| 16 | + label: observable(label) |
| 17 | + }) |
| 18 | + } |
| 19 | + |
| 20 | + return data |
| 21 | +} |
| 22 | + |
| 23 | +// components |
| 24 | +const Button = (id, onclick, label) => html` |
| 25 | + <div class='col-sm-6 smallpad'> |
| 26 | + <button |
| 27 | + id=${id} |
| 28 | + type='button' |
| 29 | + class='btn btn-primary btn-block' |
| 30 | + onclick=${onclick}> |
| 31 | + ${label} |
| 32 | + </button> |
| 33 | + </div> |
| 34 | +` |
| 35 | + |
| 36 | +const App = () => { |
| 37 | + let selectedRow |
| 38 | + const data = observable([]) |
| 39 | + |
| 40 | + // methods |
| 41 | + const run = () => data(buildData(1000)) |
| 42 | + const runlots = () => data(buildData(10000)) |
| 43 | + const add = () => data(data().concat(buildData(1000))) |
| 44 | + const update = () => { |
| 45 | + for (let i = 0; i < data.length; i += 10) { |
| 46 | + const label = data[i].label |
| 47 | + label(label() + ' !!!') |
| 48 | + } |
| 49 | + } |
| 50 | + const clear = () => data([]) |
| 51 | + const swaprows = () => { |
| 52 | + if (data.length > 998) { |
| 53 | + const arr = data() |
| 54 | + const temp = arr[1] |
| 55 | + arr[1] = arr[998] |
| 56 | + arr[998] = temp |
| 57 | + trigger(data) |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + return html` |
| 62 | + <div class='container'> |
| 63 | + <div class='jumbotron'> |
| 64 | + <div class='row'> |
| 65 | + <div class='col-md-6'> |
| 66 | + <h1>naiv-keyed</h1> |
| 67 | + </div> |
| 68 | + <div class='col-md-6'> |
| 69 | + <div class='row'> |
| 70 | + ${Button('run', run, 'Create 1,000 rows')} |
| 71 | + ${Button('runlots', runlots, 'Create 10,000 rows')} |
| 72 | + ${Button('add', add, 'Append 1,000 rows')} |
| 73 | + ${Button('update', update, 'Update every 10th row')} |
| 74 | + ${Button('clear', clear, 'Clear')} |
| 75 | + ${Button('swaprows', swaprows, 'Swap Rows')} |
| 76 | + </div> |
| 77 | + </div> |
| 78 | + </div> |
| 79 | + </div> |
| 80 | + <table class='table table-hover table-striped test-data'> |
| 81 | + ${map( |
| 82 | + data, |
| 83 | + item => item.id, |
| 84 | + html`<tbody id='tbody'></tbody>`, |
| 85 | + (item, index) => { |
| 86 | + const selected = observable() |
| 87 | +
|
| 88 | + // methods |
| 89 | + const select = () => { |
| 90 | + selected(true) |
| 91 | + selectedRow && selectedRow(false) |
| 92 | + selectedRow = selected |
| 93 | + } |
| 94 | +
|
| 95 | + const remove = () => { |
| 96 | + data.splice(index(), 1) |
| 97 | + trigger(data) |
| 98 | + } |
| 99 | +
|
| 100 | + return html` |
| 101 | + <tr class=${() => selected() ? 'danger' : ''}> |
| 102 | + <td class="col-md-1">${item.id}</td> |
| 103 | + <td class="col-md-4"> |
| 104 | + <a onclick=${select}>${item.label}</a> |
| 105 | + </td> |
| 106 | + <td class="col-md-1"> |
| 107 | + <a onclick=${remove}> |
| 108 | + <span class="glyphicon glyphicon-remove" aria-hidden="true"></span> |
| 109 | + </a> |
| 110 | + </td> |
| 111 | + <td class="col-md-6"></td> |
| 112 | + </tr>` |
| 113 | + } |
| 114 | + )} |
| 115 | + </table> |
| 116 | + <span class='preloadicon glyphicon glyphicon-remove' aria-hidden='true'></span> |
| 117 | + </div> |
| 118 | + ` |
| 119 | +} |
| 120 | + |
| 121 | +document.body.append(App()) |
0 commit comments