|
| 1 | +import { |
| 2 | + OsfModel, |
| 3 | + OsfRegion, |
| 4 | + OsfModelView, |
| 5 | +} from 'oldskull'; |
| 6 | + |
| 7 | +import type { IData } from './BenchmarkController'; |
| 8 | +import { ItemView } from './ItemView'; |
| 9 | +import { ItemModel } from './ItemModel'; |
| 10 | +import { ItemCollectionView } from './ItemCollectionView'; |
| 11 | +import { getData } from './utils'; |
| 12 | + |
| 13 | +export class BenchmarkView extends OsfModelView<OsfModel<IData>> { |
| 14 | + getHTML() { |
| 15 | + return ` |
| 16 | + <div class="container"> |
| 17 | + <div class="jumbotron"> |
| 18 | + <div class="row"> |
| 19 | + <div class="col-md-6"> |
| 20 | + <h1>Old Skull Framework</h1> |
| 21 | + </div> |
| 22 | + <div class="col-md-6"> |
| 23 | + <div class="row"> |
| 24 | + <div class="col-sm-6 smallpad"> |
| 25 | + <button type='button' class='btn btn-primary btn-block' id='run'>Create 1,000 rows</button> |
| 26 | + </div> |
| 27 | + <div class="col-sm-6 smallpad"> |
| 28 | + <button type='button' class='btn btn-primary btn-block' id='runlots'>Create 10,000 rows</button> |
| 29 | + </div> |
| 30 | + <div class="col-sm-6 smallpad"> |
| 31 | + <button type='button' class='btn btn-primary btn-block' id='add'>Append 1,000 rows</button> |
| 32 | + </div> |
| 33 | + <div class="col-sm-6 smallpad"> |
| 34 | + <button type='button' class='btn btn-primary btn-block' id='update'>Update every 10th row</button> |
| 35 | + </div> |
| 36 | + <div class="col-sm-6 smallpad"> |
| 37 | + <button type='button' class='btn btn-primary btn-block' id='clear'>Clear</button> |
| 38 | + </div> |
| 39 | + <div class="col-sm-6 smallpad"> |
| 40 | + <button type='button' class='btn btn-primary btn-block' id='swaprows'>Swap Rows</button> |
| 41 | + </div> |
| 42 | + </div> |
| 43 | + </div> |
| 44 | + </div> |
| 45 | + </div> |
| 46 | + <table class="table table-hover table-striped test-data"> |
| 47 | +
|
| 48 | + </table> |
| 49 | + <span class="preloadicon glyphicon glyphicon-remove" aria-hidden="true"></span> |
| 50 | + </div>`; |
| 51 | + } |
| 52 | + tableRegion = new OsfRegion(this, 'table'); |
| 53 | + domEvents = [ |
| 54 | + { |
| 55 | + el: '#run', on: 'click', call: this.createThousandRows.bind(this), |
| 56 | + }, |
| 57 | + { |
| 58 | + el: '#runlots', on: 'click', call: this.createTenThousandRows.bind(this), |
| 59 | + }, |
| 60 | + { |
| 61 | + el: '#add', on: 'click', call: this.appendThousandRows.bind(this), |
| 62 | + }, |
| 63 | + { |
| 64 | + el: '#update', on: 'click', call: this.updateEveryTenthRow.bind(this), |
| 65 | + }, |
| 66 | + { |
| 67 | + el: '#clear', on: 'click', call: this.clear.bind(this), |
| 68 | + }, |
| 69 | + { |
| 70 | + el: '#swaprows', on: 'click', call: this.swap.bind(this), |
| 71 | + }, |
| 72 | + ]; |
| 73 | + async afterInit() { |
| 74 | + const itemsCollectionView = new ItemCollectionView(this.model.attrs.items, ItemView); |
| 75 | + await this.tableRegion.show(itemsCollectionView); |
| 76 | + } |
| 77 | + createThousandRows(): void { |
| 78 | + const items = getData(1000); |
| 79 | + const models = items.map(item => new ItemModel(item)); |
| 80 | + this.model.attrs.items.set(models, true); |
| 81 | + } |
| 82 | + createTenThousandRows(): void { |
| 83 | + const items = getData(10000); |
| 84 | + const models = items.map(item => new ItemModel(item)); |
| 85 | + this.model.attrs.items.set(models, true); |
| 86 | + } |
| 87 | + appendThousandRows() { |
| 88 | + const items = getData(1000); |
| 89 | + const models = items.map(item => new ItemModel(item)); |
| 90 | + this.model.attrs.items.add(models); |
| 91 | + } |
| 92 | + updateEveryTenthRow() { |
| 93 | + const models = this.model.attrs.items.models; |
| 94 | + for (let i = 0; i < models.length; i += 10) { |
| 95 | + const model = models[i]; |
| 96 | + model.set({ |
| 97 | + id: model.attrs.id, |
| 98 | + label: `${model.attrs.label} !!!`, |
| 99 | + }); |
| 100 | + } |
| 101 | + } |
| 102 | + clear() { |
| 103 | + this.model.attrs.items.set([], true); |
| 104 | + } |
| 105 | + swap() { |
| 106 | + if (this.tableRegion.view && this.tableRegion.view instanceof ItemCollectionView) { |
| 107 | + this.tableRegion.view.swap(); |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments