|
| 1 | +import {int, isPresent} from 'angular2/src/facade/lang'; |
| 2 | +import {reflector} from 'angular2/src/reflection/reflection'; |
| 3 | +import {getIntParameter, bindAction} from 'angular2/src/test_lib/benchmark_util'; |
| 4 | +import {bootstrap, Component, Template, TemplateConfig, ViewPort, Compiler} |
| 5 | + from 'angular2/angular2'; |
| 6 | +import {PromiseWrapper} from 'angular2/src/facade/async'; |
| 7 | +import {ListWrapper} from 'angular2/src/facade/collection'; |
| 8 | +import {ScrollAreaComponent} from './scroll_area'; |
| 9 | +import {NgIf, NgRepeat} from 'angular2/directives'; |
| 10 | +import {DOM, document, Element} from 'angular2/src/facade/dom'; |
| 11 | + |
| 12 | +export class App { |
| 13 | + scrollAreas:List<int>; |
| 14 | + iterationCount:int; |
| 15 | + scrollIncrement:int; |
| 16 | + |
| 17 | + constructor() { |
| 18 | + var appSize = getIntParameter('appSize'); |
| 19 | + this.iterationCount = getIntParameter('iterationCount'); |
| 20 | + this.scrollIncrement = getIntParameter('scrollIncrement'); |
| 21 | + appSize = appSize > 1 ? appSize - 1 : 0; // draw at least one table |
| 22 | + this.scrollAreas = []; |
| 23 | + for (var i = 0; i < appSize; i++) { |
| 24 | + ListWrapper.push(this.scrollAreas, i); |
| 25 | + } |
| 26 | + // TODO(tbosch): change to bindAction when it works in pub serve |
| 27 | + DOM.on(DOM.query('scroll-app /deep/ #run-btn'), 'click', (_) => { |
| 28 | + this.runBenchmark(); |
| 29 | + }); |
| 30 | + DOM.on(DOM.query('scroll-app /deep/ #reset-btn'), 'click', (_) => { |
| 31 | + this._getScrollDiv().scrollTop = 0; |
| 32 | + var existingMarker = this._locateFinishedMarker(); |
| 33 | + if (isPresent(existingMarker)) { |
| 34 | + DOM.removeChild(document.body, existingMarker); |
| 35 | + } |
| 36 | + }); |
| 37 | + } |
| 38 | + |
| 39 | + runBenchmark() { |
| 40 | + var scrollDiv = this._getScrollDiv(); |
| 41 | + var n:int = this.iterationCount; |
| 42 | + var scheduleScroll; |
| 43 | + scheduleScroll = () => { |
| 44 | + PromiseWrapper.setTimeout(() => { |
| 45 | + scrollDiv.scrollTop += this.scrollIncrement; |
| 46 | + n--; |
| 47 | + if (n > 0) { |
| 48 | + scheduleScroll(); |
| 49 | + } else { |
| 50 | + this._scheduleFinishedMarker(); |
| 51 | + } |
| 52 | + }, 0); |
| 53 | + } |
| 54 | + scheduleScroll(); |
| 55 | + } |
| 56 | + |
| 57 | + // Puts a marker indicating that the test is finished. |
| 58 | + _scheduleFinishedMarker() { |
| 59 | + var existingMarker = this._locateFinishedMarker(); |
| 60 | + if (isPresent(existingMarker)) { |
| 61 | + // Nothing to do, the marker is already there |
| 62 | + return; |
| 63 | + } |
| 64 | + PromiseWrapper.setTimeout(() => { |
| 65 | + var finishedDiv = DOM.createElement('div'); |
| 66 | + finishedDiv.id = 'done'; |
| 67 | + DOM.setInnerHTML(finishedDiv, 'Finished'); |
| 68 | + DOM.appendChild(document.body, finishedDiv); |
| 69 | + }, 0); |
| 70 | + } |
| 71 | + |
| 72 | + _locateFinishedMarker():Element { |
| 73 | + return DOM.querySelector(document.body, '#done'); |
| 74 | + } |
| 75 | + |
| 76 | + _getScrollDiv() { |
| 77 | + return DOM.query('body /deep/ #testArea /deep/ #scrollDiv'); |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +export function setupReflectorForApp() { |
| 82 | + reflector.registerType(App, { |
| 83 | + 'factory': () => { return new App(); }, |
| 84 | + 'parameters': [], |
| 85 | + 'annotations': [ |
| 86 | + new Component({ |
| 87 | + selector: 'scroll-app', |
| 88 | + template: new TemplateConfig({ |
| 89 | + directives: [ScrollAreaComponent, NgIf, NgRepeat], |
| 90 | + inline: ` |
| 91 | + <div> |
| 92 | + <div style="display: flex"> |
| 93 | + <scroll-area id="testArea"></scroll-area> |
| 94 | + <div style="padding-left: 20px"> |
| 95 | + <button id="run-btn">Run</button> |
| 96 | + <button id="reset-btn">Reset</button> |
| 97 | + </div> |
| 98 | + </div> |
| 99 | + <div template="ng-if scrollAreas.length > 0"> |
| 100 | + <p>Following tables are only here to add weight to the UI:</p> |
| 101 | + <scroll-area template="ng-repeat #scrollArea in scrollAreas"></scroll-area> |
| 102 | + </div> |
| 103 | + </div>` |
| 104 | + }) |
| 105 | + }) |
| 106 | + ] |
| 107 | + }); |
| 108 | +} |
0 commit comments