2015-05-26 12:25:39 -04:00
|
|
|
import {Inject, Injectable} from 'angular2/di';
|
2015-05-06 13:49:42 -04:00
|
|
|
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';
|
2015-05-06 13:49:42 -04:00
|
|
|
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-06 13:49:42 -04:00
|
|
|
|
2015-05-26 12:25:39 -04:00
|
|
|
constructor(viewRef: RenderViewRef) {
|
2015-05-06 13:49:42 -04:00
|
|
|
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-06 13:49:42 -04:00
|
|
|
|
2015-05-26 12:25:39 -04:00
|
|
|
constructor(log: List<List<any>>) { this.log = log; }
|
2015-05-06 13:49:42 -04:00
|
|
|
|
2015-05-26 12:25:39 -04:00
|
|
|
dispatchEvent(elementIndex: number, eventName: string, locals: Map<string, any>) {
|
2015-05-06 13:49:42 -04:00
|
|
|
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;
|
2015-05-06 13:49:42 -04:00
|
|
|
rootEl;
|
|
|
|
|
2015-05-26 12:25:39 -04:00
|
|
|
constructor(renderer: DomRenderer, compiler: DefaultDomCompiler,
|
|
|
|
@Inject(DOCUMENT_TOKEN) document) {
|
2015-05-06 13:49:42 -04:00
|
|
|
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++) {
|
2015-05-06 13:49:42 -04:00
|
|
|
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-06 13:49:42 -04:00
|
|
|
}
|
|
|
|
|
2015-05-26 12:25:39 -04:00
|
|
|
_createTestView(viewRef: RenderViewRef) {
|
2015-05-06 13:49:42 -04:00
|
|
|
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 {
|
2015-05-15 12:55:43 -04:00
|
|
|
var viewRef = this.renderer.createRootHostView(rootProtoView.render, '#root');
|
2015-05-06 13:49:42 -04:00
|
|
|
this.renderer.hydrateView(viewRef);
|
|
|
|
return this._createTestView(viewRef);
|
|
|
|
}
|
|
|
|
|
2015-05-26 12:25:39 -04:00
|
|
|
createComponentView(parentViewRef: RenderViewRef, boundElementIndex: number,
|
|
|
|
componentProtoView: ProtoViewDto): TestView {
|
2015-05-06 13:49:42 -04:00
|
|
|
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> {
|
2015-05-06 13:49:42 -04:00
|
|
|
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++) {
|
2015-05-06 13:49:42 -04:00
|
|
|
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) {
|
2015-05-06 13:49:42 -04:00
|
|
|
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 {
|
2015-05-06 13:49:42 -04:00
|
|
|
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) {
|
2015-05-06 13:49:42 -04:00
|
|
|
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) {
|
2015-06-02 13:15:16 -04:00
|
|
|
var element = resolveInternalDomView(viewRef).boundElements[boundElementIndex].element;
|
2015-05-06 13:49:42 -04:00
|
|
|
dispatchEvent(element, eventName);
|
|
|
|
}
|
|
|
|
}
|