angular-cn/modules/angular2/test/render/dom/dom_testbed.ts

130 lines
4.6 KiB
TypeScript
Raw Normal View History

2015-05-26 12:25:39 -04:00
import {Inject, Injectable} from 'angular2/di';
import {MapWrapper, ListWrapper, List, Map} from 'angular2/src/facade/collection';
import {PromiseWrapper, Promise} from 'angular2/src/facade/async';
import {DOM} from 'angular2/src/dom/dom_adapter';
import {DomRenderer, DOCUMENT_TOKEN} from 'angular2/src/render/dom/dom_renderer';
import {DefaultDomCompiler} from 'angular2/src/render/dom/compiler/compiler';
import {DomView} from 'angular2/src/render/dom/view/view';
2015-05-26 12:25:39 -04:00
import {
RenderViewRef,
ProtoViewDto,
ViewDefinition,
EventDispatcher,
DirectiveMetadata
} from 'angular2/src/render/api';
import {resolveInternalDomView} from 'angular2/src/render/dom/view/view';
import {el, dispatchEvent} from 'angular2/test_lib';
2015-05-18 14:57:20 -04:00
export class TestView {
2015-05-26 12:25:39 -04:00
rawView: DomView;
viewRef: RenderViewRef;
events: List<List<any>>;
2015-05-26 12:25:39 -04:00
constructor(viewRef: RenderViewRef) {
this.viewRef = viewRef;
this.rawView = resolveInternalDomView(viewRef);
this.events = [];
}
}
2015-05-26 12:25:39 -04:00
class LoggingEventDispatcher implements EventDispatcher {
log: List<List<any>>;
2015-05-26 12:25:39 -04:00
constructor(log: List<List<any>>) { this.log = log; }
2015-05-26 12:25:39 -04:00
dispatchEvent(elementIndex: number, eventName: string, locals: Map<string, any>) {
ListWrapper.push(this.log, [elementIndex, eventName, locals]);
return true;
}
}
@Injectable()
export class DomTestbed {
2015-05-26 12:25:39 -04:00
renderer: DomRenderer;
compiler: DefaultDomCompiler;
rootEl;
2015-05-26 12:25:39 -04:00
constructor(renderer: DomRenderer, compiler: DefaultDomCompiler,
@Inject(DOCUMENT_TOKEN) document) {
this.renderer = renderer;
this.compiler = compiler;
this.rootEl = el('<div id="root"></div>');
var oldRoots = DOM.querySelectorAll(document, '#root');
2015-05-26 12:25:39 -04:00
for (var i = 0; i < oldRoots.length; i++) {
DOM.remove(oldRoots[i]);
}
DOM.appendChild(DOM.querySelector(document, 'body'), this.rootEl);
}
2015-05-26 12:25:39 -04:00
compileAll(directivesOrViewDefinitions: List<DirectiveMetadata |
ViewDefinition>): Promise<List<ProtoViewDto>> {
return PromiseWrapper.all(ListWrapper.map(directivesOrViewDefinitions, (entry) => {
if (entry instanceof DirectiveMetadata) {
return this.compiler.compileHost(entry);
} else {
return this.compiler.compile(entry);
}
}));
}
2015-05-26 12:25:39 -04:00
_createTestView(viewRef: RenderViewRef) {
var testView = new TestView(viewRef);
this.renderer.setEventDispatcher(viewRef, new LoggingEventDispatcher(testView.events));
return testView;
}
2015-05-26 12:25:39 -04:00
createRootView(rootProtoView: ProtoViewDto): TestView {
var viewRef = this.renderer.createRootHostView(rootProtoView.render, '#root');
this.renderer.hydrateView(viewRef);
return this._createTestView(viewRef);
}
2015-05-26 12:25:39 -04:00
createComponentView(parentViewRef: RenderViewRef, boundElementIndex: number,
componentProtoView: ProtoViewDto): TestView {
var componentViewRef = this.renderer.createView(componentProtoView.render);
this.renderer.attachComponentView(parentViewRef, boundElementIndex, componentViewRef);
this.renderer.hydrateView(componentViewRef);
return this._createTestView(componentViewRef);
}
2015-05-26 12:25:39 -04:00
createRootViews(protoViews: List<ProtoViewDto>): List<TestView> {
var views = [];
var lastView = this.createRootView(protoViews[0]);
ListWrapper.push(views, lastView);
2015-05-26 12:25:39 -04:00
for (var i = 1; i < protoViews.length; i++) {
lastView = this.createComponentView(lastView.viewRef, 0, protoViews[i]);
ListWrapper.push(views, lastView);
}
return views;
}
2015-05-26 12:25:39 -04:00
destroyComponentView(parentViewRef: RenderViewRef, boundElementIndex: number,
componentView: RenderViewRef) {
this.renderer.dehydrateView(componentView);
this.renderer.detachComponentView(parentViewRef, boundElementIndex, componentView);
}
2015-05-26 12:25:39 -04:00
createViewInContainer(parentViewRef: RenderViewRef, boundElementIndex: number, atIndex: number,
protoView: ProtoViewDto): TestView {
var viewRef = this.renderer.createView(protoView.render);
this.renderer.attachViewInContainer(parentViewRef, boundElementIndex, atIndex, viewRef);
this.renderer.hydrateView(viewRef);
return this._createTestView(viewRef);
}
2015-05-26 12:25:39 -04:00
destroyViewInContainer(parentViewRef: RenderViewRef, boundElementIndex: number, atIndex: number,
viewRef: RenderViewRef) {
this.renderer.dehydrateView(viewRef);
this.renderer.detachViewInContainer(parentViewRef, boundElementIndex, atIndex, viewRef);
this.renderer.destroyView(viewRef);
}
2015-05-26 12:25:39 -04:00
triggerEvent(viewRef: RenderViewRef, boundElementIndex: number, eventName: string) {
var element = resolveInternalDomView(viewRef).boundElements[boundElementIndex].element;
dispatchEvent(element, eventName);
}
}