|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +/** @jsx glasgow */ |
| 4 | + |
| 5 | +import { Store } from './store.es6' |
| 6 | +import glasgow from 'glasgow' |
| 7 | + |
| 8 | +function selectRow(props) { |
| 9 | + props.store.select(props.id); |
| 10 | +} |
| 11 | + |
| 12 | +function deleteRow(props) { |
| 13 | + props.store.delete(props.id); |
| 14 | +} |
| 15 | + |
| 16 | +function Row({ d, id, styleClass }) { |
| 17 | + return ( |
| 18 | + <tr className={styleClass}> |
| 19 | + <td className="col-md-1">{''+id}</td> |
| 20 | + <td className="col-md-4"> |
| 21 | + <a onclick={selectRow}>{d.label}</a> |
| 22 | + </td> |
| 23 | + <td className="col-md-1"> |
| 24 | + <a onclick={deleteRow}> |
| 25 | + <span className="glyphicon glyphicon-remove" aria-hidden="true"/> |
| 26 | + </a> |
| 27 | + </td> |
| 28 | + <td className="col-md-6"/> |
| 29 | + </tr> |
| 30 | + ) |
| 31 | +} |
| 32 | + |
| 33 | +function createRows(store) { |
| 34 | + const rows = []; |
| 35 | + const data = store.data; |
| 36 | + const selected = store.selected; |
| 37 | + |
| 38 | + for (let i = 0; i < data.length; i++) { |
| 39 | + const d = data[i]; |
| 40 | + const id = d.id; |
| 41 | + |
| 42 | + rows.push( |
| 43 | + <Row |
| 44 | + store={store} |
| 45 | + styleClass={id === selected ? 'danger' : null} |
| 46 | + key={id} |
| 47 | + d={d} |
| 48 | + id={id} |
| 49 | + selected={selected} |
| 50 | + /> |
| 51 | + ); |
| 52 | + } |
| 53 | + |
| 54 | + return <tbody>{rows}</tbody>; |
| 55 | +} |
| 56 | + |
| 57 | +function bind(obj,name) { |
| 58 | + let func = obj[name]; |
| 59 | + return function(...args) { |
| 60 | + return func.apply(obj, args); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +export function Controller(props) { |
| 65 | + |
| 66 | + return <div className="container"> |
| 67 | + <div className="jumbotron"> |
| 68 | + <div className="row"> |
| 69 | + <div className="col-md-6"> |
| 70 | + <h1>Glasgow - keyed</h1> |
| 71 | + </div> |
| 72 | + <div className="col-md-6"> |
| 73 | + <div className="row"> |
| 74 | + <div className="col-sm-6 smallpad"> |
| 75 | + <button type="button" className="btn btn-primary btn-block" id="run" onclick={bind(store,'run')}>Create 1,000 |
| 76 | + rows |
| 77 | + </button> |
| 78 | + </div> |
| 79 | + <div className="col-sm-6 smallpad"> |
| 80 | + <button type="button" className="btn btn-primary btn-block" id="runlots" onclick={bind(store,'runLots')}>Create |
| 81 | + 10,000 rows |
| 82 | + </button> |
| 83 | + </div> |
| 84 | + <div className="col-sm-6 smallpad"> |
| 85 | + <button type="button" className="btn btn-primary btn-block" id="add" onclick={bind(store,'add')}>Append 1,000 |
| 86 | + rows |
| 87 | + </button> |
| 88 | + </div> |
| 89 | + <div className="col-sm-6 smallpad"> |
| 90 | + <button type="button" className="btn btn-primary btn-block" id="update" onclick={bind(store,'update')}>Update |
| 91 | + every 10th row |
| 92 | + </button> |
| 93 | + </div> |
| 94 | + <div className="col-sm-6 smallpad"> |
| 95 | + <button type="button" className="btn btn-primary btn-block" id="clear" onclick={bind(store,'clear')}>Clear |
| 96 | + </button> |
| 97 | + </div> |
| 98 | + <div className="col-sm-6 smallpad"> |
| 99 | + <button type="button" className="btn btn-primary btn-block" id="swaprows" onclick={bind(store,'swapRows')}>Swap |
| 100 | + Rows |
| 101 | + </button> |
| 102 | + </div> |
| 103 | + </div> |
| 104 | + </div> |
| 105 | + </div> |
| 106 | + </div> |
| 107 | + <table className="table table-hover table-striped test-data"> |
| 108 | + {createRows(store)} |
| 109 | + </table> |
| 110 | + <span className="preloadicon glyphicon glyphicon-remove" aria-hidden="true"/> |
| 111 | + </div> |
| 112 | +} |
| 113 | + |
| 114 | + |
| 115 | +const store = new Store(); |
| 116 | +glasgow.setDebug(0); |
| 117 | +glasgow.mount(document.getElementById("main"), <Controller store={store}/>); |
| 118 | + |
0 commit comments