| 
 | 1 | +import app, { Component } from 'apprun'  | 
 | 2 | +import Store from './store';  | 
 | 3 | + | 
 | 4 | +var startTime;  | 
 | 5 | +var lastMeasure;  | 
 | 6 | +var startMeasure = function (name) {  | 
 | 7 | +    startTime = performance.now();  | 
 | 8 | +    lastMeasure = name;  | 
 | 9 | +}  | 
 | 10 | +var stopMeasure = function () {  | 
 | 11 | +    window.setTimeout(function () {  | 
 | 12 | +        var stop = performance.now();  | 
 | 13 | +        console.log(lastMeasure + " took " + (stop - startTime));  | 
 | 14 | +    }, 0);  | 
 | 15 | +}  | 
 | 16 | + | 
 | 17 | +const store = new Store();  | 
 | 18 | + | 
 | 19 | +const update = {  | 
 | 20 | +    '#benchmark': (store) => store,  | 
 | 21 | + | 
 | 22 | +    run(store) {  | 
 | 23 | +        store.run();  | 
 | 24 | +        return store;  | 
 | 25 | +    },  | 
 | 26 | + | 
 | 27 | +    add(store) {  | 
 | 28 | +        store.add();  | 
 | 29 | +        return store;  | 
 | 30 | +    },  | 
 | 31 | + | 
 | 32 | +    remove(store, id) {  | 
 | 33 | +        document.getElementById(id).remove();  | 
 | 34 | +        store.delete(id);  | 
 | 35 | +        store.no_render = true;  | 
 | 36 | +        return store;  | 
 | 37 | +    },  | 
 | 38 | + | 
 | 39 | +    select(store, id) {  | 
 | 40 | +        if (store.selected) {  | 
 | 41 | +            document.getElementById(store.selected).className = '';  | 
 | 42 | +        }  | 
 | 43 | +        document.getElementById(id).className = 'danger';  | 
 | 44 | +        store.select(id);  | 
 | 45 | +        store.no_render = true;  | 
 | 46 | +        return store;  | 
 | 47 | +    },  | 
 | 48 | + | 
 | 49 | +    update(store) {  | 
 | 50 | +        store.update();  | 
 | 51 | +        return store;  | 
 | 52 | +    },  | 
 | 53 | + | 
 | 54 | +    runlots(store) {  | 
 | 55 | +        store.runLots();  | 
 | 56 | +        return store;  | 
 | 57 | +    },  | 
 | 58 | + | 
 | 59 | +    clear(store) {  | 
 | 60 | +        store.clear();  | 
 | 61 | +        return store;  | 
 | 62 | +    },  | 
 | 63 | + | 
 | 64 | +    swaprows(store) {  | 
 | 65 | +        store.swapRows();  | 
 | 66 | +        return store;  | 
 | 67 | +    }  | 
 | 68 | +}  | 
 | 69 | + | 
 | 70 | +const view = (model) => {  | 
 | 71 | +    if (model.no_render) {  | 
 | 72 | +        delete model.no_render;  | 
 | 73 | +        return;  | 
 | 74 | +    }  | 
 | 75 | +    const rows = model.data.map((curr) => {  | 
 | 76 | +        const selected = curr.id == model.selected ? 'danger' : '';  | 
 | 77 | +        const id = curr.id;  | 
 | 78 | +        return <tr className={selected} id={id} key={id}>  | 
 | 79 | +            <td className="col-md-1">{id}</td>  | 
 | 80 | +            <td className="col-md-4">  | 
 | 81 | +                <a className="lbl">{curr.label}</a>  | 
 | 82 | +            </td>  | 
 | 83 | +            <td className="col-md-1">  | 
 | 84 | +                <a className="remove">  | 
 | 85 | +                    <span className="glyphicon glyphicon-remove remove" aria-hidden="true"></span>  | 
 | 86 | +                </a>  | 
 | 87 | +            </td>  | 
 | 88 | +            <td className="col-md-6"></td>  | 
 | 89 | +        </tr>;  | 
 | 90 | +    });  | 
 | 91 | + | 
 | 92 | +    return (<div className="container">  | 
 | 93 | +        <div className="jumbotron">  | 
 | 94 | +            <div className="row">  | 
 | 95 | +                <div className="col-md-6">  | 
 | 96 | +                    <h1>AppRun</h1>  | 
 | 97 | +                </div>  | 
 | 98 | +                <div className="col-md-6">  | 
 | 99 | +                    <div className="row">  | 
 | 100 | +                        <div className="col-sm-6 smallpad">  | 
 | 101 | +                            <button type="button" className="btn btn-primary btn-block" id="run">Create 1,000 rows</button>  | 
 | 102 | +                        </div>  | 
 | 103 | +                        <div className="col-sm-6 smallpad">  | 
 | 104 | +                            <button type="button" className="btn btn-primary btn-block" id="runlots">Create 10,000 rows</button>  | 
 | 105 | +                        </div>  | 
 | 106 | +                        <div className="col-sm-6 smallpad">  | 
 | 107 | +                            <button type="button" className="btn btn-primary btn-block" id="add">Append 1,000 rows</button>  | 
 | 108 | +                        </div>  | 
 | 109 | +                        <div className="col-sm-6 smallpad">  | 
 | 110 | +                            <button type="button" className="btn btn-primary btn-block" id="update">Update every 10th row</button>  | 
 | 111 | +                        </div>  | 
 | 112 | +                        <div className="col-sm-6 smallpad">  | 
 | 113 | +                            <button type="button" className="btn btn-primary btn-block" id="clear">Clear</button>  | 
 | 114 | +                        </div>  | 
 | 115 | +                        <div className="col-sm-6 smallpad">  | 
 | 116 | +                            <button type="button" className="btn btn-primary btn-block" id="swaprows">Swap Rows</button>  | 
 | 117 | +                        </div>  | 
 | 118 | +                    </div>  | 
 | 119 | +                </div>  | 
 | 120 | +            </div>  | 
 | 121 | +        </div>  | 
 | 122 | +        <table className="table table-hover table-striped test-data">  | 
 | 123 | +            <tbody>  | 
 | 124 | +                {rows}  | 
 | 125 | +            </tbody>  | 
 | 126 | +        </table>  | 
 | 127 | +        <span className="preloadicon glyphicon glyphicon-remove" aria-hidden="true"></span>  | 
 | 128 | +    </div>);  | 
 | 129 | +}  | 
 | 130 | + | 
 | 131 | +const getId = (elem) => {  | 
 | 132 | +    while (elem) {  | 
 | 133 | +        if (elem.tagName === "TR") {  | 
 | 134 | +            return elem.id;  | 
 | 135 | +        }  | 
 | 136 | +        elem = elem.parentNode;  | 
 | 137 | +    }  | 
 | 138 | +    return undefined;  | 
 | 139 | +}  | 
 | 140 | + | 
 | 141 | +document.body.addEventListener('click', e => {  | 
 | 142 | +    const t = e.target as HTMLElement;  | 
 | 143 | +    if (!t) return;  | 
 | 144 | +    if (t.tagName === 'BUTTON' && t.id) {  | 
 | 145 | +        e.preventDefault();  | 
 | 146 | +        startMeasure(t.id);  | 
 | 147 | +        component.run(t.id);  | 
 | 148 | +        stopMeasure();  | 
 | 149 | +    } else if (t.matches('.remove')) {  | 
 | 150 | +        e.preventDefault();  | 
 | 151 | +        startMeasure('remove');  | 
 | 152 | +        component.run('remove', getId(t));  | 
 | 153 | +        stopMeasure();  | 
 | 154 | +    } else if (t.matches('.lbl')) {  | 
 | 155 | +        e.preventDefault();  | 
 | 156 | +        startMeasure('select');  | 
 | 157 | +        component.run('select', getId(t));  | 
 | 158 | +        stopMeasure();  | 
 | 159 | +    }  | 
 | 160 | +});  | 
 | 161 | + | 
 | 162 | +app.on('//', _ => { })  | 
 | 163 | +app.on('#', _ => { })  | 
 | 164 | + | 
 | 165 | +let component = new Component(store, view, update);  | 
 | 166 | +component.start(document.getElementById('main'));  | 
0 commit comments