refactor(render): use `RenderElementRef` in all renderer methods

BREAKING CHANGES:
- Almost all methods in `Renderer` now take a `RenderElementRef` instead
  of a `ViewRef` + `boundElementIndex`.
- These methods can be called with the `ElementRef` from the app side
  directly.

Closes #2706
Related to #2476
This commit is contained in:
Tobias Bosch 2015-06-23 11:21:56 -07:00
parent 2c3c235969
commit ba9fecd068
13 changed files with 139 additions and 127 deletions

View File

@ -771,9 +771,7 @@ export class ElementInjector extends TreeNode<ElementInjector> {
getComponent(): any { return this._strategy.getComponent(); } getComponent(): any { return this._strategy.getComponent(); }
getElementRef(): ElementRef { getElementRef(): ElementRef { return this._preBuiltObjects.view.elementRefs[this._proto.index]; }
return new ElementRef(new ViewRef(this._preBuiltObjects.view), this._proto.index);
}
getViewContainerRef(): ViewContainerRef { getViewContainerRef(): ViewContainerRef {
return new ViewContainerRef(this._preBuiltObjects.viewManager, this.getElementRef()); return new ViewContainerRef(this._preBuiltObjects.viewManager, this.getElementRef());

View File

@ -1,14 +1,20 @@
import {DOM} from 'angular2/src/dom/dom_adapter'; import {DOM} from 'angular2/src/dom/dom_adapter';
import {normalizeBlank} from 'angular2/src/facade/lang'; import {normalizeBlank, BaseException} from 'angular2/src/facade/lang';
import {ViewRef} from './view_ref'; import {ViewRef} from './view_ref';
import {resolveInternalDomView} from 'angular2/src/render/dom/view/view'; import {resolveInternalDomView} from 'angular2/src/render/dom/view/view';
import {RenderViewRef, RenderElementRef} from 'angular2/src/render/api';
/** /**
* @exportedAs angular2/view * @exportedAs angular2/view
*/ */
export class ElementRef { export class ElementRef implements RenderElementRef {
constructor(public parentView: ViewRef, public boundElementIndex: number) {} constructor(public parentView: ViewRef, public boundElementIndex: number) {}
get renderView() { return this.parentView.render; }
// TODO(tbosch): remove this once Typescript supports declaring interfaces
// that contain getters
set renderView(value: any) { throw new BaseException('Abstract setter'); }
/** /**
* Exposes the underlying DOM element. * Exposes the underlying DOM element.
* (DEPRECATED way of accessing the DOM, replacement coming) * (DEPRECATED way of accessing the DOM, replacement coming)

View File

@ -21,6 +21,8 @@ import {ElementBinder} from './element_binder';
import {isPresent, isBlank, BaseException} from 'angular2/src/facade/lang'; import {isPresent, isBlank, BaseException} from 'angular2/src/facade/lang';
import * as renderApi from 'angular2/src/render/api'; import * as renderApi from 'angular2/src/render/api';
import {EventDispatcher} from 'angular2/src/render/api'; import {EventDispatcher} from 'angular2/src/render/api';
import {ViewRef} from './view_ref';
import {ElementRef} from './element_ref';
export class AppViewContainer { export class AppViewContainer {
// The order in this list matches the DOM order. // The order in this list matches the DOM order.
@ -40,6 +42,8 @@ export class AppView implements ChangeDispatcher, EventDispatcher {
componentChildViews: List<AppView> = null; componentChildViews: List<AppView> = null;
viewContainers: List<AppViewContainer>; viewContainers: List<AppViewContainer>;
preBuiltObjects: List<PreBuiltObjects> = null; preBuiltObjects: List<PreBuiltObjects> = null;
elementRefs: List<ElementRef>;
ref: ViewRef;
/** /**
* The context against which data-binding expressions in this view are evaluated against. * The context against which data-binding expressions in this view are evaluated against.
@ -58,6 +62,11 @@ export class AppView implements ChangeDispatcher, EventDispatcher {
constructor(public renderer: renderApi.Renderer, public proto: AppProtoView, constructor(public renderer: renderApi.Renderer, public proto: AppProtoView,
protoLocals: Map<string, any>) { protoLocals: Map<string, any>) {
this.viewContainers = ListWrapper.createFixedSize(this.proto.elementBinders.length); this.viewContainers = ListWrapper.createFixedSize(this.proto.elementBinders.length);
this.elementRefs = ListWrapper.createFixedSize(this.proto.elementBinders.length);
this.ref = new ViewRef(this);
for (var i = 0; i < this.elementRefs.length; i++) {
this.elementRefs[i] = new ElementRef(this.ref, i);
}
this.locals = new Locals(null, MapWrapper.clone(protoLocals)); // TODO optimize this this.locals = new Locals(null, MapWrapper.clone(protoLocals)); // TODO optimize this
} }
@ -100,14 +109,16 @@ export class AppView implements ChangeDispatcher, EventDispatcher {
// dispatch to element injector or text nodes based on context // dispatch to element injector or text nodes based on context
notifyOnBinding(b: BindingRecord, currentValue: any): void { notifyOnBinding(b: BindingRecord, currentValue: any): void {
if (b.isElementProperty()) { if (b.isElementProperty()) {
this.renderer.setElementProperty(this.render, b.elementIndex, b.propertyName, currentValue); this.renderer.setElementProperty(this.elementRefs[b.elementIndex], b.propertyName,
currentValue);
} else if (b.isElementAttribute()) { } else if (b.isElementAttribute()) {
this.renderer.setElementAttribute(this.render, b.elementIndex, b.propertyName, currentValue); this.renderer.setElementAttribute(this.elementRefs[b.elementIndex], b.propertyName,
currentValue);
} else if (b.isElementClass()) { } else if (b.isElementClass()) {
this.renderer.setElementClass(this.render, b.elementIndex, b.propertyName, currentValue); this.renderer.setElementClass(this.elementRefs[b.elementIndex], b.propertyName, currentValue);
} else if (b.isElementStyle()) { } else if (b.isElementStyle()) {
var unit = isPresent(b.propertyUnit) ? b.propertyUnit : ''; var unit = isPresent(b.propertyUnit) ? b.propertyUnit : '';
this.renderer.setElementStyle(this.render, b.elementIndex, b.propertyName, this.renderer.setElementStyle(this.elementRefs[b.elementIndex], b.propertyName,
`${currentValue}${unit}`); `${currentValue}${unit}`);
} else if (b.isTextNode()) { } else if (b.isTextNode()) {
this.renderer.setText(this.render, b.elementIndex, currentValue); this.renderer.setText(this.render, b.elementIndex, currentValue);
@ -134,7 +145,7 @@ export class AppView implements ChangeDispatcher, EventDispatcher {
} }
invokeElementMethod(elementIndex: number, methodName: string, args: List<any>) { invokeElementMethod(elementIndex: number, methodName: string, args: List<any>) {
this.renderer.invokeElementMethod(this.render, elementIndex, methodName, args); this.renderer.invokeElementMethod(this.elementRefs[elementIndex], methodName, args);
} }
// implementation of EventDispatcher#dispatchEvent // implementation of EventDispatcher#dispatchEvent

View File

@ -24,7 +24,7 @@ export class ViewContainerRef {
} }
} }
get(index: number): ViewRef { return new ViewRef(this._getViews()[index]); } get(index: number): ViewRef { return this._getViews()[index].ref; }
get length(): number { return this._getViews().length; } get length(): number { return this._getViews().length; }

View File

@ -22,7 +22,7 @@ export class AppViewManager {
getComponentView(hostLocation: ElementRef): ViewRef { getComponentView(hostLocation: ElementRef): ViewRef {
var hostView = internalView(hostLocation.parentView); var hostView = internalView(hostLocation.parentView);
var boundElementIndex = hostLocation.boundElementIndex; var boundElementIndex = hostLocation.boundElementIndex;
return new ViewRef(hostView.componentChildViews[boundElementIndex]); return hostView.componentChildViews[boundElementIndex].ref;
} }
getViewContainer(location: ElementRef): ViewContainerRef { getViewContainer(location: ElementRef): ViewContainerRef {
@ -45,7 +45,7 @@ export class AppViewManager {
if (isBlank(elementIndex)) { if (isBlank(elementIndex)) {
throw new BaseException(`Could not find variable ${variableName}`); throw new BaseException(`Could not find variable ${variableName}`);
} }
return new ElementRef(new ViewRef(componentView), elementIndex); return componentView.elementRefs[elementIndex];
} }
getComponent(hostLocation: ElementRef): any { getComponent(hostLocation: ElementRef): any {
@ -69,7 +69,7 @@ export class AppViewManager {
this._utils.hydrateRootHostView(hostView, injector); this._utils.hydrateRootHostView(hostView, injector);
this._viewHydrateRecurse(hostView); this._viewHydrateRecurse(hostView);
return new ViewRef(hostView); return hostView.ref;
} }
destroyRootHostView(hostViewRef: ViewRef) { destroyRootHostView(hostViewRef: ViewRef) {
@ -98,14 +98,13 @@ export class AppViewManager {
var view = this._createPooledView(protoView); var view = this._createPooledView(protoView);
this._renderer.attachViewInContainer(parentView.render, boundElementIndex, atIndex, this._renderer.attachViewInContainer(viewContainerLocation, atIndex, view.render);
view.render);
this._utils.attachViewInContainer(parentView, boundElementIndex, contextView, this._utils.attachViewInContainer(parentView, boundElementIndex, contextView,
contextBoundElementIndex, atIndex, view); contextBoundElementIndex, atIndex, view);
this._utils.hydrateViewInContainer(parentView, boundElementIndex, contextView, this._utils.hydrateViewInContainer(parentView, boundElementIndex, contextView,
contextBoundElementIndex, atIndex, injector); contextBoundElementIndex, atIndex, injector);
this._viewHydrateRecurse(view); this._viewHydrateRecurse(view);
return new ViewRef(view); return view.ref;
} }
destroyViewInContainer(viewContainerLocation: ElementRef, atIndex: number) { destroyViewInContainer(viewContainerLocation: ElementRef, atIndex: number) {
@ -126,8 +125,7 @@ export class AppViewManager {
// Right now we are destroying any special // Right now we are destroying any special
// context view that might have been used. // context view that might have been used.
this._utils.attachViewInContainer(parentView, boundElementIndex, null, null, atIndex, view); this._utils.attachViewInContainer(parentView, boundElementIndex, null, null, atIndex, view);
this._renderer.attachViewInContainer(parentView.render, boundElementIndex, atIndex, this._renderer.attachViewInContainer(viewContainerLocation, atIndex, view.render);
view.render);
return viewRef; return viewRef;
} }
@ -137,9 +135,8 @@ export class AppViewManager {
var viewContainer = parentView.viewContainers[boundElementIndex]; var viewContainer = parentView.viewContainers[boundElementIndex];
var view = viewContainer.views[atIndex]; var view = viewContainer.views[atIndex];
this._utils.detachViewInContainer(parentView, boundElementIndex, atIndex); this._utils.detachViewInContainer(parentView, boundElementIndex, atIndex);
this._renderer.detachViewInContainer(parentView.render, boundElementIndex, atIndex, this._renderer.detachViewInContainer(viewContainerLocation, atIndex, view.render);
view.render); return view.ref;
return new ViewRef(view);
} }
_createPooledView(protoView: viewModule.AppProtoView): viewModule.AppView { _createPooledView(protoView: viewModule.AppProtoView): viewModule.AppView {
@ -160,7 +157,7 @@ export class AppViewManager {
var binder = binders[binderIdx]; var binder = binders[binderIdx];
if (binder.hasStaticComponent()) { if (binder.hasStaticComponent()) {
var childView = this._createPooledView(binder.nestedProtoView); var childView = this._createPooledView(binder.nestedProtoView);
this._renderer.attachComponentView(view.render, binderIdx, childView.render); this._renderer.attachComponentView(view.elementRefs[binderIdx], childView.render);
this._utils.attachComponentView(view, binderIdx, childView); this._utils.attachComponentView(view, binderIdx, childView);
} }
} }
@ -179,14 +176,15 @@ export class AppViewManager {
var view = viewContainer.views[atIndex]; var view = viewContainer.views[atIndex];
this._viewDehydrateRecurse(view, false); this._viewDehydrateRecurse(view, false);
this._utils.detachViewInContainer(parentView, boundElementIndex, atIndex); this._utils.detachViewInContainer(parentView, boundElementIndex, atIndex);
this._renderer.detachViewInContainer(parentView.render, boundElementIndex, atIndex, this._renderer.detachViewInContainer(parentView.elementRefs[boundElementIndex], atIndex,
view.render); view.render);
this._destroyPooledView(view); this._destroyPooledView(view);
} }
_destroyComponentView(hostView, boundElementIndex, componentView) { _destroyComponentView(hostView, boundElementIndex, componentView) {
this._viewDehydrateRecurse(componentView, false); this._viewDehydrateRecurse(componentView, false);
this._renderer.detachComponentView(hostView.render, boundElementIndex, componentView.render); this._renderer.detachComponentView(hostView.elementRefs[boundElementIndex],
componentView.render);
this._utils.detachComponentView(hostView, boundElementIndex); this._utils.detachComponentView(hostView, boundElementIndex);
this._destroyPooledView(componentView); this._destroyPooledView(componentView);
} }

View File

@ -49,6 +49,5 @@ function _throwError(dir: NgControl, message: string): void {
export function setProperty(renderer: Renderer, elementRef: ElementRef, propName: string, export function setProperty(renderer: Renderer, elementRef: ElementRef, propName: string,
propValue: any) { propValue: any) {
renderer.setElementProperty(elementRef.parentView.render, elementRef.boundElementIndex, propName, renderer.setElementProperty(elementRef, propName, propValue);
propValue);
} }

View File

@ -306,6 +306,11 @@ export class RenderCompiler {
compile(template: ViewDefinition): Promise<ProtoViewDto> { return null; } compile(template: ViewDefinition): Promise<ProtoViewDto> { return null; }
} }
export interface RenderElementRef {
renderView: RenderViewRef;
boundElementIndex: number;
}
export class Renderer { export class Renderer {
/** /**
* Creates a root host view that includes the given element. * Creates a root host view that includes the given element.
@ -333,29 +338,25 @@ export class Renderer {
/** /**
* Attaches a componentView into the given hostView at the given element * Attaches a componentView into the given hostView at the given element
*/ */
attachComponentView(hostViewRef: RenderViewRef, elementIndex: number, attachComponentView(location: RenderElementRef, componentViewRef: RenderViewRef) {}
componentViewRef: RenderViewRef) {}
/** /**
* Detaches a componentView into the given hostView at the given element * Detaches a componentView into the given hostView at the given element
*/ */
detachComponentView(hostViewRef: RenderViewRef, boundElementIndex: number, detachComponentView(location: RenderElementRef, componentViewRef: RenderViewRef) {}
componentViewRef: RenderViewRef) {}
/** /**
* Attaches a view into a ViewContainer (in the given parentView at the given element) at the * Attaches a view into a ViewContainer (in the given parentView at the given element) at the
* given index. * given index.
*/ */
attachViewInContainer(parentViewRef: RenderViewRef, boundElementIndex: number, atIndex: number, attachViewInContainer(location: RenderElementRef, atIndex: number, viewRef: RenderViewRef) {}
viewRef: RenderViewRef) {}
/** /**
* Detaches a view into a ViewContainer (in the given parentView at the given element) at the * Detaches a view into a ViewContainer (in the given parentView at the given element) at the
* given index. * given index.
*/ */
// TODO(tbosch): this should return a promise as it can be animated! // TODO(tbosch): this should return a promise as it can be animated!
detachViewInContainer(parentViewRef: RenderViewRef, boundElementIndex: number, atIndex: number, detachViewInContainer(location: RenderElementRef, atIndex: number, viewRef: RenderViewRef) {}
viewRef: RenderViewRef) {}
/** /**
* Hydrates a view after it has been attached. Hydration/dehydration is used for reusing views * Hydrates a view after it has been attached. Hydration/dehydration is used for reusing views
@ -372,32 +373,27 @@ export class Renderer {
/** /**
* Sets a property on an element. * Sets a property on an element.
*/ */
setElementProperty(viewRef: RenderViewRef, elementIndex: number, propertyName: string, setElementProperty(location: RenderElementRef, propertyName: string, propertyValue: any) {}
propertyValue: any) {}
/** /**
* Sets an attribute on an element. * Sets an attribute on an element.
*/ */
setElementAttribute(viewRef: RenderViewRef, elementIndex: number, attributeName: string, setElementAttribute(location: RenderElementRef, attributeName: string, attributeValue: string) {}
attributeValue: string) {}
/** /**
* Sets a class on an element. * Sets a class on an element.
*/ */
setElementClass(viewRef: RenderViewRef, elementIndex: number, className: string, isAdd: boolean) { setElementClass(location: RenderElementRef, className: string, isAdd: boolean) {}
}
/** /**
* Sets a style on an element. * Sets a style on an element.
*/ */
setElementStyle(viewRef: RenderViewRef, elementIndex: number, styleName: string, setElementStyle(location: RenderElementRef, styleName: string, styleValue: string) {}
styleValue: string) {}
/** /**
* Calls a method on an element. * Calls a method on an element.
*/ */
invokeElementMethod(viewRef: RenderViewRef, elementIndex: number, methodName: string, invokeElementMethod(location: RenderElementRef, methodName: string, args: List<any>) {}
args: List<any>) {}
/** /**
* Sets the value of a text node. * Sets the value of a text node.

View File

@ -20,7 +20,7 @@ import {DomElement} from './view/element';
import {DomViewContainer} from './view/view_container'; import {DomViewContainer} from './view/view_container';
import {NG_BINDING_CLASS_SELECTOR, NG_BINDING_CLASS} from './util'; import {NG_BINDING_CLASS_SELECTOR, NG_BINDING_CLASS} from './util';
import {Renderer, RenderProtoViewRef, RenderViewRef} from '../api'; import {Renderer, RenderProtoViewRef, RenderViewRef, RenderElementRef} from '../api';
export const DOCUMENT_TOKEN = CONST_EXPR(new OpaqueToken('DocumentToken')); export const DOCUMENT_TOKEN = CONST_EXPR(new OpaqueToken('DocumentToken'));
@ -53,12 +53,11 @@ export class DomRenderer extends Renderer {
// noop for now // noop for now
} }
attachComponentView(hostViewRef: RenderViewRef, elementIndex: number, attachComponentView(location: RenderElementRef, componentViewRef: RenderViewRef) {
componentViewRef: RenderViewRef) { var hostView = resolveInternalDomView(location.renderView);
var hostView = resolveInternalDomView(hostViewRef);
var componentView = resolveInternalDomView(componentViewRef); var componentView = resolveInternalDomView(componentViewRef);
var element = hostView.boundElements[elementIndex].element; var element = hostView.boundElements[location.boundElementIndex].element;
var lightDom = hostView.boundElements[elementIndex].lightDom; var lightDom = hostView.boundElements[location.boundElementIndex].lightDom;
if (isPresent(lightDom)) { if (isPresent(lightDom)) {
lightDom.attachShadowDomView(componentView); lightDom.attachShadowDomView(componentView);
} }
@ -79,12 +78,11 @@ export class DomRenderer extends Renderer {
return resolveInternalDomView(viewRef).rootNodes; return resolveInternalDomView(viewRef).rootNodes;
} }
detachComponentView(hostViewRef: RenderViewRef, boundElementIndex: number, detachComponentView(location: RenderElementRef, componentViewRef: RenderViewRef) {
componentViewRef: RenderViewRef) { var hostView = resolveInternalDomView(location.renderView);
var hostView = resolveInternalDomView(hostViewRef);
var componentView = resolveInternalDomView(componentViewRef); var componentView = resolveInternalDomView(componentViewRef);
this._removeViewNodes(componentView); this._removeViewNodes(componentView);
var lightDom = hostView.boundElements[boundElementIndex].lightDom; var lightDom = hostView.boundElements[location.boundElementIndex].lightDom;
if (isPresent(lightDom)) { if (isPresent(lightDom)) {
lightDom.detachShadowDomView(); lightDom.detachShadowDomView();
} }
@ -92,19 +90,18 @@ export class DomRenderer extends Renderer {
componentView.shadowRoot = null; componentView.shadowRoot = null;
} }
attachViewInContainer(parentViewRef: RenderViewRef, boundElementIndex: number, atIndex: number, attachViewInContainer(location: RenderElementRef, atIndex: number, viewRef: RenderViewRef) {
viewRef: RenderViewRef) { var parentView = resolveInternalDomView(location.renderView);
var parentView = resolveInternalDomView(parentViewRef);
var view = resolveInternalDomView(viewRef); var view = resolveInternalDomView(viewRef);
var viewContainer = this._getOrCreateViewContainer(parentView, boundElementIndex); var viewContainer = this._getOrCreateViewContainer(parentView, location.boundElementIndex);
ListWrapper.insert(viewContainer.views, atIndex, view); ListWrapper.insert(viewContainer.views, atIndex, view);
view.hostLightDom = parentView.hostLightDom; view.hostLightDom = parentView.hostLightDom;
var directParentLightDom = this._directParentLightDom(parentView, boundElementIndex); var directParentLightDom = this._directParentLightDom(parentView, location.boundElementIndex);
if (isBlank(directParentLightDom)) { if (isBlank(directParentLightDom)) {
var siblingToInsertAfter; var siblingToInsertAfter;
if (atIndex == 0) { if (atIndex == 0) {
siblingToInsertAfter = parentView.boundElements[boundElementIndex].element; siblingToInsertAfter = parentView.boundElements[location.boundElementIndex].element;
} else { } else {
siblingToInsertAfter = ListWrapper.last(viewContainer.views[atIndex - 1].rootNodes); siblingToInsertAfter = ListWrapper.last(viewContainer.views[atIndex - 1].rootNodes);
} }
@ -118,14 +115,13 @@ export class DomRenderer extends Renderer {
} }
} }
detachViewInContainer(parentViewRef: RenderViewRef, boundElementIndex: number, atIndex: number, detachViewInContainer(location: RenderElementRef, atIndex: number, viewRef: RenderViewRef) {
viewRef: RenderViewRef) { var parentView = resolveInternalDomView(location.renderView);
var parentView = resolveInternalDomView(parentViewRef);
var view = resolveInternalDomView(viewRef); var view = resolveInternalDomView(viewRef);
var viewContainer = parentView.boundElements[boundElementIndex].viewContainer; var viewContainer = parentView.boundElements[location.boundElementIndex].viewContainer;
var detachedView = viewContainer.views[atIndex]; var detachedView = viewContainer.views[atIndex];
ListWrapper.removeAt(viewContainer.views, atIndex); ListWrapper.removeAt(viewContainer.views, atIndex);
var directParentLightDom = this._directParentLightDom(parentView, boundElementIndex); var directParentLightDom = this._directParentLightDom(parentView, location.boundElementIndex);
if (isBlank(directParentLightDom)) { if (isBlank(directParentLightDom)) {
this._removeViewNodes(detachedView); this._removeViewNodes(detachedView);
} else { } else {
@ -181,34 +177,30 @@ export class DomRenderer extends Renderer {
view.hydrated = false; view.hydrated = false;
} }
setElementProperty(viewRef: RenderViewRef, elementIndex: number, propertyName: string, setElementProperty(location: RenderElementRef, propertyName: string, propertyValue: any): void {
propertyValue: any): void { var view = resolveInternalDomView(location.renderView);
var view = resolveInternalDomView(viewRef); view.setElementProperty(location.boundElementIndex, propertyName, propertyValue);
view.setElementProperty(elementIndex, propertyName, propertyValue);
} }
setElementAttribute(viewRef: RenderViewRef, elementIndex: number, attributeName: string, setElementAttribute(location: RenderElementRef, attributeName: string,
attributeValue: string): void { attributeValue: string): void {
var view = resolveInternalDomView(viewRef); var view = resolveInternalDomView(location.renderView);
view.setElementAttribute(elementIndex, attributeName, attributeValue); view.setElementAttribute(location.boundElementIndex, attributeName, attributeValue);
} }
setElementClass(viewRef: RenderViewRef, elementIndex: number, className: string, setElementClass(location: RenderElementRef, className: string, isAdd: boolean): void {
isAdd: boolean): void { var view = resolveInternalDomView(location.renderView);
var view = resolveInternalDomView(viewRef); view.setElementClass(location.boundElementIndex, className, isAdd);
view.setElementClass(elementIndex, className, isAdd);
} }
setElementStyle(viewRef: RenderViewRef, elementIndex: number, styleName: string, setElementStyle(location: RenderElementRef, styleName: string, styleValue: string): void {
styleValue: string): void { var view = resolveInternalDomView(location.renderView);
var view = resolveInternalDomView(viewRef); view.setElementStyle(location.boundElementIndex, styleName, styleValue);
view.setElementStyle(elementIndex, styleName, styleValue);
} }
invokeElementMethod(viewRef: RenderViewRef, elementIndex: number, methodName: string, invokeElementMethod(location: RenderElementRef, methodName: string, args: List<any>): void {
args: List<any>): void { var view = resolveInternalDomView(location.renderView);
var view = resolveInternalDomView(viewRef); view.invokeElementMethod(location.boundElementIndex, methodName, args);
view.invokeElementMethod(elementIndex, methodName, args);
} }
setText(viewRef: RenderViewRef, textNodeIndex: number, text: string): void { setText(viewRef: RenderViewRef, textNodeIndex: number, text: string): void {

View File

@ -51,7 +51,6 @@ import {ViewContainerRef} from 'angular2/src/core/compiler/view_container_ref';
import {ProtoViewRef} from 'angular2/src/core/compiler/view_ref'; import {ProtoViewRef} from 'angular2/src/core/compiler/view_ref';
import {ElementRef} from 'angular2/src/core/compiler/element_ref'; import {ElementRef} from 'angular2/src/core/compiler/element_ref';
import {DynamicChangeDetector, ChangeDetectorRef, Parser, Lexer} from 'angular2/change_detection'; import {DynamicChangeDetector, ChangeDetectorRef, Parser, Lexer} from 'angular2/change_detection';
import {Renderer} from 'angular2/src/render/api';
import {QueryList} from 'angular2/src/core/compiler/query_list'; import {QueryList} from 'angular2/src/core/compiler/query_list';
@proxy @proxy
@ -59,14 +58,25 @@ import {QueryList} from 'angular2/src/core/compiler/query_list';
class DummyView extends SpyObject { class DummyView extends SpyObject {
componentChildViews; componentChildViews;
changeDetector; changeDetector;
constructor() { elementRefs;
super(); constructor(elementCount = 0) {
super(AppView);
this.componentChildViews = []; this.componentChildViews = [];
this.changeDetector = null; this.changeDetector = null;
this.elementRefs = ListWrapper.createFixedSize(elementCount);
for (var i=0; i<elementCount; i++) {
this.elementRefs[i] = new DummyElementRef();
}
} }
noSuchMethod(m) { return super.noSuchMethod(m); } noSuchMethod(m) { return super.noSuchMethod(m); }
} }
@proxy
@IMPLEMENTS(ElementRef)
class DummyElementRef extends SpyObject {
constructor() { super(ElementRef); }
noSuchMethod(m) { return super.noSuchMethod(m); }
}
class SimpleDirective {} class SimpleDirective {}
@ -225,7 +235,7 @@ class TestNode extends TreeNode<TestNode> {
} }
export function main() { export function main() {
var defaultPreBuiltObjects = new PreBuiltObjects(null, null, null); var defaultPreBuiltObjects = new PreBuiltObjects(null, <any>new DummyView(1), null);
var appInjector = Injector.resolveAndCreate([]); var appInjector = Injector.resolveAndCreate([]);
// An injector with more than 10 bindings will switch to the dynamic strategy // An injector with more than 10 bindings will switch to the dynamic strategy
@ -893,7 +903,7 @@ export function main() {
describe("refs", () => { describe("refs", () => {
it("should inject ElementRef", () => { it("should inject ElementRef", () => {
var inj = injector(ListWrapper.concat([NeedsElementRef], extraBindings)); var inj = injector(ListWrapper.concat([NeedsElementRef], extraBindings));
expect(inj.get(NeedsElementRef).elementRef).toBeAnInstanceOf(ElementRef); expect(inj.get(NeedsElementRef).elementRef).toBe(defaultPreBuiltObjects.view.elementRefs[0]);
}); });
it('should inject ChangeDetectorRef', () => { it('should inject ChangeDetectorRef', () => {
@ -1005,7 +1015,7 @@ export function main() {
var inj = injector(ListWrapper.concat(dirs, extraBindings), null, var inj = injector(ListWrapper.concat(dirs, extraBindings), null,
false, preBuildObjects, null, dirVariableBindings); false, preBuildObjects, null, dirVariableBindings);
expect(inj.get(NeedsQueryByVarBindings).query.first).toBeAnInstanceOf(ElementRef); expect(inj.get(NeedsQueryByVarBindings).query.first).toBe(defaultPreBuiltObjects.view.elementRefs[0]);
}); });
it('should contain directives on the same injector when querying by variable bindings' + it('should contain directives on the same injector when querying by variable bindings' +
@ -1139,14 +1149,3 @@ class ContextWithHandler {
handler; handler;
constructor(handler) { this.handler = handler; } constructor(handler) { this.handler = handler; }
} }
class FakeRenderer extends Renderer {
log: List<List<any>>;
constructor() {
super();
this.log = [];
}
setElementProperty(viewRef, elementIndex, propertyName, value) {
this.log.push([viewRef, elementIndex, propertyName, value]);
}
}

View File

@ -292,13 +292,13 @@ export function main() {
it('should attach the view', () => { it('should attach the view', () => {
var contextView = createView(); var contextView = createView();
manager.createViewInContainer(elementRef(wrapView(parentView), 0), 0, var elRef = elementRef(wrapView(parentView), 0);
wrapPv(childProtoView), manager.createViewInContainer(elRef, 0, wrapPv(childProtoView),
elementRef(wrapView(contextView), 1), null); elementRef(wrapView(contextView), 1), null);
expect(utils.spy('attachViewInContainer')) expect(utils.spy('attachViewInContainer'))
.toHaveBeenCalledWith(parentView, 0, contextView, 1, 0, createdViews[0]); .toHaveBeenCalledWith(parentView, 0, contextView, 1, 0, createdViews[0]);
expect(renderer.spy('attachViewInContainer')) expect(renderer.spy('attachViewInContainer'))
.toHaveBeenCalledWith(parentView.render, 0, 0, createdViews[0].render); .toHaveBeenCalledWith(elRef, 0, createdViews[0].render);
}); });
it('should hydrate the view', () => { it('should hydrate the view', () => {
@ -349,10 +349,11 @@ export function main() {
}); });
it('should detach', () => { it('should detach', () => {
manager.destroyViewInContainer(elementRef(wrapView(parentView), 0), 0); var elRef = elementRef(wrapView(parentView), 0);
manager.destroyViewInContainer(elRef, 0);
expect(utils.spy('detachViewInContainer')).toHaveBeenCalledWith(parentView, 0, 0); expect(utils.spy('detachViewInContainer')).toHaveBeenCalledWith(parentView, 0, 0);
expect(renderer.spy('detachViewInContainer')) expect(renderer.spy('detachViewInContainer'))
.toHaveBeenCalledWith(parentView.render, 0, 0, childView.render); .toHaveBeenCalledWith(elRef, 0, childView.render);
}); });
it('should return the view to the pool', () => { it('should return the view to the pool', () => {
@ -381,7 +382,7 @@ export function main() {
manager.destroyRootHostView(wrapView(parentView)); manager.destroyRootHostView(wrapView(parentView));
expect(utils.spy('detachViewInContainer')).toHaveBeenCalledWith(parentView, 0, 0); expect(utils.spy('detachViewInContainer')).toHaveBeenCalledWith(parentView, 0, 0);
expect(renderer.spy('detachViewInContainer')) expect(renderer.spy('detachViewInContainer'))
.toHaveBeenCalledWith(parentView.render, 0, 0, childView.render); .toHaveBeenCalledWith(parentView.elementRefs[0], 0, childView.render);
}); });
it('should return the view to the pool', () => { it('should return the view to the pool', () => {

View File

@ -16,7 +16,7 @@ import {
import {MapWrapper} from 'angular2/src/facade/collection'; import {MapWrapper} from 'angular2/src/facade/collection';
import {DOM} from 'angular2/src/dom/dom_adapter'; import {DOM} from 'angular2/src/dom/dom_adapter';
import {DomTestbed, TestView} from './dom_testbed'; import {DomTestbed, TestView, elRef} from './dom_testbed';
import {ViewDefinition, DirectiveMetadata, RenderViewRef} from 'angular2/src/render/api'; import {ViewDefinition, DirectiveMetadata, RenderViewRef} from 'angular2/src/render/api';
@ -106,20 +106,20 @@ export function main() {
var cmpView = tb.createComponentView(rootView.viewRef, 0, protoViewDtos[1]); var cmpView = tb.createComponentView(rootView.viewRef, 0, protoViewDtos[1]);
var el = DOM.childNodes(tb.rootEl)[0]; var el = DOM.childNodes(tb.rootEl)[0];
tb.renderer.setElementProperty(cmpView.viewRef, 0, 'value', 'hello'); tb.renderer.setElementProperty(elRef(cmpView.viewRef, 0), 'value', 'hello');
expect(el.value).toEqual('hello'); expect(el.value).toEqual('hello');
tb.renderer.setElementClass(cmpView.viewRef, 0, 'a', true); tb.renderer.setElementClass(elRef(cmpView.viewRef, 0), 'a', true);
expect(DOM.childNodes(tb.rootEl)[0].value).toEqual('hello'); expect(DOM.childNodes(tb.rootEl)[0].value).toEqual('hello');
tb.renderer.setElementClass(cmpView.viewRef, 0, 'a', false); tb.renderer.setElementClass(elRef(cmpView.viewRef, 0), 'a', false);
expect(DOM.hasClass(el, 'a')).toBe(false); expect(DOM.hasClass(el, 'a')).toBe(false);
tb.renderer.setElementStyle(cmpView.viewRef, 0, 'width', '10px'); tb.renderer.setElementStyle(elRef(cmpView.viewRef, 0), 'width', '10px');
expect(DOM.getStyle(el, 'width')).toEqual('10px'); expect(DOM.getStyle(el, 'width')).toEqual('10px');
tb.renderer.setElementStyle(cmpView.viewRef, 0, 'width', null); tb.renderer.setElementStyle(elRef(cmpView.viewRef, 0), 'width', null);
expect(DOM.getStyle(el, 'width')).toEqual(''); expect(DOM.getStyle(el, 'width')).toEqual('');
tb.renderer.setElementAttribute(cmpView.viewRef, 0, 'someAttr', 'someValue'); tb.renderer.setElementAttribute(elRef(cmpView.viewRef, 0), 'someAttr', 'someValue');
expect(DOM.getAttribute(el, 'some-attr')).toEqual('someValue'); expect(DOM.getAttribute(el, 'some-attr')).toEqual('someValue');
async.done(); async.done();
@ -141,7 +141,7 @@ export function main() {
var views = tb.createRootViews(protoViewDtos); var views = tb.createRootViews(protoViewDtos);
var componentView = views[1]; var componentView = views[1];
tb.renderer.invokeElementMethod(componentView.viewRef, 0, 'setAttribute', tb.renderer.invokeElementMethod(elRef(componentView.viewRef, 0), 'setAttribute',
['a', 'b']); ['a', 'b']);
expect(DOM.getAttribute(DOM.childNodes(tb.rootEl)[0], 'a')).toEqual('b'); expect(DOM.getAttribute(DOM.childNodes(tb.rootEl)[0], 'a')).toEqual('b');

View File

@ -11,7 +11,8 @@ import {
ProtoViewDto, ProtoViewDto,
ViewDefinition, ViewDefinition,
EventDispatcher, EventDispatcher,
DirectiveMetadata DirectiveMetadata,
RenderElementRef
} from 'angular2/src/render/api'; } from 'angular2/src/render/api';
import {resolveInternalDomView} from 'angular2/src/render/dom/view/view'; import {resolveInternalDomView} from 'angular2/src/render/dom/view/view';
import {el, dispatchEvent} from 'angular2/test_lib'; import {el, dispatchEvent} from 'angular2/test_lib';
@ -28,6 +29,13 @@ export class TestView {
} }
} }
export function elRef(renderView: RenderViewRef, boundElementIndex: number) {
return new TestRenderElementRef(renderView, boundElementIndex);
}
class TestRenderElementRef implements RenderElementRef {
constructor(public renderView: RenderViewRef, public boundElementIndex: number) {}
}
class LoggingEventDispatcher implements EventDispatcher { class LoggingEventDispatcher implements EventDispatcher {
log: List<List<any>>; log: List<List<any>>;
@ -85,7 +93,7 @@ export class DomTestbed {
createComponentView(parentViewRef: RenderViewRef, boundElementIndex: number, createComponentView(parentViewRef: RenderViewRef, boundElementIndex: number,
componentProtoView: ProtoViewDto): TestView { componentProtoView: ProtoViewDto): TestView {
var componentViewRef = this.renderer.createView(componentProtoView.render); var componentViewRef = this.renderer.createView(componentProtoView.render);
this.renderer.attachComponentView(parentViewRef, boundElementIndex, componentViewRef); this.renderer.attachComponentView(elRef(parentViewRef, boundElementIndex), componentViewRef);
this.renderer.hydrateView(componentViewRef); this.renderer.hydrateView(componentViewRef);
return this._createTestView(componentViewRef); return this._createTestView(componentViewRef);
} }
@ -104,13 +112,13 @@ export class DomTestbed {
destroyComponentView(parentViewRef: RenderViewRef, boundElementIndex: number, destroyComponentView(parentViewRef: RenderViewRef, boundElementIndex: number,
componentView: RenderViewRef) { componentView: RenderViewRef) {
this.renderer.dehydrateView(componentView); this.renderer.dehydrateView(componentView);
this.renderer.detachComponentView(parentViewRef, boundElementIndex, componentView); this.renderer.detachComponentView(elRef(parentViewRef, boundElementIndex), componentView);
} }
createViewInContainer(parentViewRef: RenderViewRef, boundElementIndex: number, atIndex: number, createViewInContainer(parentViewRef: RenderViewRef, boundElementIndex: number, atIndex: number,
protoView: ProtoViewDto): TestView { protoView: ProtoViewDto): TestView {
var viewRef = this.renderer.createView(protoView.render); var viewRef = this.renderer.createView(protoView.render);
this.renderer.attachViewInContainer(parentViewRef, boundElementIndex, atIndex, viewRef); this.renderer.attachViewInContainer(elRef(parentViewRef, boundElementIndex), atIndex, viewRef);
this.renderer.hydrateView(viewRef); this.renderer.hydrateView(viewRef);
return this._createTestView(viewRef); return this._createTestView(viewRef);
} }
@ -118,7 +126,7 @@ export class DomTestbed {
destroyViewInContainer(parentViewRef: RenderViewRef, boundElementIndex: number, atIndex: number, destroyViewInContainer(parentViewRef: RenderViewRef, boundElementIndex: number, atIndex: number,
viewRef: RenderViewRef) { viewRef: RenderViewRef) {
this.renderer.dehydrateView(viewRef); this.renderer.dehydrateView(viewRef);
this.renderer.detachViewInContainer(parentViewRef, boundElementIndex, atIndex, viewRef); this.renderer.detachViewInContainer(elRef(parentViewRef, boundElementIndex), atIndex, viewRef);
this.renderer.destroyView(viewRef); this.renderer.destroyView(viewRef);
} }

View File

@ -32,7 +32,7 @@ import {
import {StyleUrlResolver} from 'angular2/src/render/dom/shadow_dom/style_url_resolver'; import {StyleUrlResolver} from 'angular2/src/render/dom/shadow_dom/style_url_resolver';
import {StyleInliner} from 'angular2/src/render/dom/shadow_dom/style_inliner'; import {StyleInliner} from 'angular2/src/render/dom/shadow_dom/style_inliner';
import {DomTestbed} from './dom_testbed'; import {DomTestbed, elRef} from './dom_testbed';
export function main() { export function main() {
describe('ShadowDom integration tests', function() { describe('ShadowDom integration tests', function() {
@ -425,17 +425,21 @@ export function main() {
expect(tb.rootEl).toHaveText('(TAB(0))'); expect(tb.rootEl).toHaveText('(TAB(0))');
tb.renderer.dehydrateView(tabChildView.viewRef); tb.renderer.dehydrateView(tabChildView.viewRef);
tb.renderer.detachViewInContainer(tab1View.viewRef, 0, 0, tabChildView.viewRef); tb.renderer.detachViewInContainer(elRef(tab1View.viewRef, 0), 0,
tabChildView.viewRef);
tb.renderer.attachViewInContainer(tab2View.viewRef, 0, 0, tabChildView.viewRef); tb.renderer.attachViewInContainer(elRef(tab2View.viewRef, 0), 0,
tabChildView.viewRef);
tb.renderer.hydrateView(tabChildView.viewRef); tb.renderer.hydrateView(tabChildView.viewRef);
expect(tb.rootEl).toHaveText('(TAB(1))'); expect(tb.rootEl).toHaveText('(TAB(1))');
tb.renderer.dehydrateView(tabChildView.viewRef); tb.renderer.dehydrateView(tabChildView.viewRef);
tb.renderer.detachViewInContainer(tab2View.viewRef, 0, 0, tabChildView.viewRef); tb.renderer.detachViewInContainer(elRef(tab2View.viewRef, 0), 0,
tabChildView.viewRef);
tb.renderer.attachViewInContainer(tab3View.viewRef, 0, 0, tabChildView.viewRef); tb.renderer.attachViewInContainer(elRef(tab3View.viewRef, 0), 0,
tabChildView.viewRef);
tb.renderer.hydrateView(tabChildView.viewRef); tb.renderer.hydrateView(tabChildView.viewRef);
expect(tb.rootEl).toHaveText('(TAB(2))'); expect(tb.rootEl).toHaveText('(TAB(2))');