|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +import { Store } from './store.es6' |
| 4 | +import { linkEvent, Component, render, createTextVNode } from 'inferno' |
| 5 | + |
| 6 | +function Row({ d, id, styleClass, deleteFunc, selectFunc }) { |
| 7 | + /* |
| 8 | + * Only <td className="col-md-1"> and <a onClick={linkEvent(id, selectFunc)}/>, nodes needs $NoNormalize flags |
| 9 | + * Because they have dynamic children. We can pre-define children type by using $NoNormalize |
| 10 | + * default value for Non-normalized vNode is another vNode so we need to create virtual text node by hand |
| 11 | + * |
| 12 | + * other elements don't have children so $NoNormalize is not needed there |
| 13 | + */ |
| 14 | + return ( |
| 15 | + <tr className={styleClass}> |
| 16 | + <td className="col-md-1" $NoNormalize>{createTextVNode(id + '')}</td> |
| 17 | + <td className="col-md-4"> |
| 18 | + <a onClick={linkEvent(id, selectFunc)} $NoNormalize>{createTextVNode(d.label)}</a> |
| 19 | + </td> |
| 20 | + <td className="col-md-1"> |
| 21 | + <a onClick={linkEvent(id, deleteFunc)}> |
| 22 | + <span className="glyphicon glyphicon-remove" aria-hidden="true"/> |
| 23 | + </a> |
| 24 | + </td> |
| 25 | + <td className="col-md-6"/> |
| 26 | + </tr> |
| 27 | + ) |
| 28 | +} |
| 29 | + |
| 30 | +function onComponentShouldUpdate(lastProps, nextProps) { |
| 31 | + return nextProps.d !== lastProps.d || nextProps.styleClass !== lastProps.styleClass; |
| 32 | +} |
| 33 | + |
| 34 | +function createRows(store, deleteFunc, selectFunc) { |
| 35 | + const rows = []; |
| 36 | + const data = store.data; |
| 37 | + const selected = store.selected; |
| 38 | + |
| 39 | + for (let i = 0; i < data.length; i++) { |
| 40 | + const d = data[i]; |
| 41 | + const id = d.id; |
| 42 | + |
| 43 | + rows.push( |
| 44 | + <Row |
| 45 | + styleClass={id === selected ? 'danger' : null} |
| 46 | + key={id} |
| 47 | + d={d} |
| 48 | + id={id} |
| 49 | + selected={selected} |
| 50 | + deleteFunc={deleteFunc} |
| 51 | + selectFunc={selectFunc} |
| 52 | + onComponentShouldUpdate={onComponentShouldUpdate} |
| 53 | + /> |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + /* |
| 58 | + * We can optimize rendering rows by pre-defining children types. |
| 59 | + * In this case all children are keyed: so we add flag $HasKeyedChildren and $NoNormalize |
| 60 | + * when $NoNormalize is used we need to make sure there are no holes in the array and are keys are unique |
| 61 | + */ |
| 62 | + return <tbody $HasKeyedChildren $NoNormalize>{rows}</tbody>; |
| 63 | +} |
| 64 | + |
| 65 | +export class Controller extends Component { |
| 66 | + constructor(props) { |
| 67 | + super(props); |
| 68 | + this.state = { store: new Store() }; |
| 69 | + this.select = this.select.bind(this); |
| 70 | + this.delete = this.delete.bind(this); |
| 71 | + this.add = this.add.bind(this); |
| 72 | + this.run = this.run.bind(this); |
| 73 | + this.update = this.update.bind(this); |
| 74 | + this.runLots = this.runLots.bind(this); |
| 75 | + this.clear = this.clear.bind(this); |
| 76 | + this.swapRows = this.swapRows.bind(this); |
| 77 | + this.start = 0; |
| 78 | + } |
| 79 | + |
| 80 | + run() { |
| 81 | + event.stopPropagation(); |
| 82 | + this.state.store.run(); |
| 83 | + this.setState({ store: this.state.store }); |
| 84 | + } |
| 85 | + |
| 86 | + add() { |
| 87 | + event.stopPropagation(); |
| 88 | + this.state.store.add(); |
| 89 | + this.setState({ store: this.state.store }); |
| 90 | + } |
| 91 | + |
| 92 | + update() { |
| 93 | + event.stopPropagation(); |
| 94 | + this.state.store.update(); |
| 95 | + this.setState({ store: this.state.store }); |
| 96 | + } |
| 97 | + |
| 98 | + select(id, event) { |
| 99 | + event.stopPropagation(); |
| 100 | + this.state.store.select(id); |
| 101 | + this.setState({ store: this.state.store }); |
| 102 | + } |
| 103 | + |
| 104 | + delete(id, event) { |
| 105 | + event.stopPropagation(); |
| 106 | + this.state.store.delete(id); |
| 107 | + this.setState({ store: this.state.store }); |
| 108 | + } |
| 109 | + |
| 110 | + runLots() { |
| 111 | + event.stopPropagation(); |
| 112 | + this.state.store.runLots(); |
| 113 | + this.setState({ store: this.state.store }); |
| 114 | + } |
| 115 | + |
| 116 | + clear(event) { |
| 117 | + event.stopPropagation(); |
| 118 | + this.state.store.clear(); |
| 119 | + this.setState({ store: this.state.store }); |
| 120 | + } |
| 121 | + |
| 122 | + swapRows() { |
| 123 | + event.stopPropagation(); |
| 124 | + this.state.store.swapRows(); |
| 125 | + this.setState({ store: this.state.store }); |
| 126 | + } |
| 127 | + |
| 128 | + render() { |
| 129 | + /* |
| 130 | + * Only <table> needs $NoNormalize flag everything else is static |
| 131 | + * tables children is tbody so another vNode, no other flags needed |
| 132 | + */ |
| 133 | + return (<div className="container"> |
| 134 | + <div className="jumbotron"> |
| 135 | + <div className="row"> |
| 136 | + <div className="col-md-6"> |
| 137 | + <h1>Inferno - keyed</h1> |
| 138 | + </div> |
| 139 | + <div className="col-md-6"> |
| 140 | + <div className="row"> |
| 141 | + <div className="col-sm-6 smallpad"> |
| 142 | + <button type="button" className="btn btn-primary btn-block" id="run" onClick={this.run}>Create 1,000 |
| 143 | + rows |
| 144 | + </button> |
| 145 | + </div> |
| 146 | + <div className="col-sm-6 smallpad"> |
| 147 | + <button type="button" className="btn btn-primary btn-block" id="runlots" onClick={this.runLots}>Create |
| 148 | + 10,000 rows |
| 149 | + </button> |
| 150 | + </div> |
| 151 | + <div className="col-sm-6 smallpad"> |
| 152 | + <button type="button" className="btn btn-primary btn-block" id="add" onClick={this.add}>Append 1,000 |
| 153 | + rows |
| 154 | + </button> |
| 155 | + </div> |
| 156 | + <div className="col-sm-6 smallpad"> |
| 157 | + <button type="button" className="btn btn-primary btn-block" id="update" onClick={this.update}>Update |
| 158 | + every 10th row |
| 159 | + </button> |
| 160 | + </div> |
| 161 | + <div className="col-sm-6 smallpad"> |
| 162 | + <button type="button" className="btn btn-primary btn-block" id="clear" onClick={this.clear}>Clear |
| 163 | + </button> |
| 164 | + </div> |
| 165 | + <div className="col-sm-6 smallpad"> |
| 166 | + <button type="button" className="btn btn-primary btn-block" id="swaprows" onClick={this.swapRows}>Swap |
| 167 | + Rows |
| 168 | + </button> |
| 169 | + </div> |
| 170 | + </div> |
| 171 | + </div> |
| 172 | + </div> |
| 173 | + </div> |
| 174 | + <table className="table table-hover table-striped test-data" $NoNormalize> |
| 175 | + {createRows(this.state.store, this.delete, this.select)} |
| 176 | + </table> |
| 177 | + <span className="preloadicon glyphicon glyphicon-remove" aria-hidden="true"/> |
| 178 | + </div>); |
| 179 | + } |
| 180 | +} |
| 181 | + |
| 182 | +render(<Controller/>, document.getElementById("main")); |
0 commit comments