2016-01-06 14:13:44 -08:00
|
|
|
import {isPresent, isBlank, Type} from 'angular2/src/facade/lang';
|
|
|
|
import {ListWrapper} from 'angular2/src/facade/collection';
|
2015-12-02 10:35:51 -08:00
|
|
|
import {BaseException} from 'angular2/src/facade/exceptions';
|
|
|
|
|
2016-01-06 14:13:44 -08:00
|
|
|
import {Injector} from 'angular2/src/core/di';
|
2015-12-02 10:35:51 -08:00
|
|
|
|
|
|
|
import {AppView} from './view';
|
|
|
|
import {ViewType} from './view_type';
|
|
|
|
import {ElementRef_} from './element_ref';
|
|
|
|
|
2016-01-06 14:13:44 -08:00
|
|
|
import {ViewContainerRef, ViewContainerRef_} from './view_container_ref';
|
2015-12-02 10:35:51 -08:00
|
|
|
|
2016-01-06 14:13:44 -08:00
|
|
|
import {QueryList} from './query_list';
|
2015-12-02 10:35:51 -08:00
|
|
|
|
2016-01-06 14:13:44 -08:00
|
|
|
export class AppElement {
|
|
|
|
public nestedViews: AppView<any>[] = null;
|
|
|
|
public componentView: AppView<any> = null;
|
2015-12-02 10:35:51 -08:00
|
|
|
|
2016-01-06 14:13:44 -08:00
|
|
|
private _ref: ElementRef_;
|
|
|
|
private _vcRef: ViewContainerRef_;
|
|
|
|
public component: any;
|
|
|
|
public componentConstructorViewQueries: QueryList<any>[];
|
2015-12-02 10:35:51 -08:00
|
|
|
|
2016-01-06 14:13:44 -08:00
|
|
|
constructor(public index: number, public parentIndex: number, public parentView: AppView<any>,
|
|
|
|
public nativeElement: any) {}
|
2015-12-02 10:35:51 -08:00
|
|
|
|
2016-01-06 14:13:44 -08:00
|
|
|
get ref(): ElementRef_ {
|
|
|
|
if (isBlank(this._ref)) {
|
|
|
|
this._ref = new ElementRef_(this);
|
2015-12-02 10:35:51 -08:00
|
|
|
}
|
2016-01-06 14:13:44 -08:00
|
|
|
return this._ref;
|
2015-12-02 10:35:51 -08:00
|
|
|
}
|
|
|
|
|
2016-01-06 14:13:44 -08:00
|
|
|
get vcRef(): ViewContainerRef_ {
|
|
|
|
if (isBlank(this._vcRef)) {
|
|
|
|
this._vcRef = new ViewContainerRef_(this);
|
2015-12-02 10:35:51 -08:00
|
|
|
}
|
2016-01-06 14:13:44 -08:00
|
|
|
return this._vcRef;
|
2015-12-02 10:35:51 -08:00
|
|
|
}
|
|
|
|
|
2016-01-06 14:13:44 -08:00
|
|
|
initComponent(component: any, componentConstructorViewQueries: QueryList<any>[],
|
|
|
|
view: AppView<any>) {
|
|
|
|
this.component = component;
|
|
|
|
this.componentConstructorViewQueries = componentConstructorViewQueries;
|
|
|
|
this.componentView = view;
|
2015-12-02 10:35:51 -08:00
|
|
|
}
|
|
|
|
|
2016-01-06 14:13:44 -08:00
|
|
|
get parentInjector(): Injector { return this.parentView.injector(this.parentIndex); }
|
|
|
|
get injector(): Injector { return this.parentView.injector(this.index); }
|
2015-12-02 10:35:51 -08:00
|
|
|
|
2016-01-06 14:13:44 -08:00
|
|
|
mapNestedViews(nestedViewClass: any, callback: Function): any[] {
|
|
|
|
var result = [];
|
|
|
|
if (isPresent(this.nestedViews)) {
|
|
|
|
this.nestedViews.forEach((nestedView) => {
|
|
|
|
if (nestedView.clazz === nestedViewClass) {
|
|
|
|
result.push(callback(nestedView));
|
2015-12-02 10:35:51 -08:00
|
|
|
}
|
2016-01-06 14:13:44 -08:00
|
|
|
});
|
2015-12-02 10:35:51 -08:00
|
|
|
}
|
2016-01-06 14:13:44 -08:00
|
|
|
return result;
|
2015-12-02 10:35:51 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-06 14:13:44 -08:00
|
|
|
attachView(view: AppView<any>, viewIndex: number) {
|
|
|
|
if (view.type === ViewType.COMPONENT) {
|
|
|
|
throw new BaseException(`Component views can't be moved!`);
|
2015-12-02 10:35:51 -08:00
|
|
|
}
|
2016-01-06 14:13:44 -08:00
|
|
|
var nestedViews = this.nestedViews;
|
|
|
|
if (nestedViews == null) {
|
|
|
|
nestedViews = [];
|
|
|
|
this.nestedViews = nestedViews;
|
2015-12-02 10:35:51 -08:00
|
|
|
}
|
2016-01-06 14:13:44 -08:00
|
|
|
ListWrapper.insert(nestedViews, viewIndex, view);
|
|
|
|
var refRenderNode;
|
|
|
|
if (viewIndex > 0) {
|
|
|
|
var prevView = nestedViews[viewIndex - 1];
|
|
|
|
refRenderNode = prevView.lastRootNode;
|
2015-12-02 10:35:51 -08:00
|
|
|
} else {
|
2016-01-06 14:13:44 -08:00
|
|
|
refRenderNode = this.nativeElement;
|
2015-12-02 10:35:51 -08:00
|
|
|
}
|
2016-01-06 14:13:44 -08:00
|
|
|
if (isPresent(refRenderNode)) {
|
|
|
|
view.renderer.attachViewAfter(refRenderNode, view.flatRootNodes);
|
2015-12-02 10:35:51 -08:00
|
|
|
}
|
2016-01-06 14:13:44 -08:00
|
|
|
this.parentView.addRenderContentChild(view);
|
2015-12-02 10:35:51 -08:00
|
|
|
}
|
|
|
|
|
2016-01-06 14:13:44 -08:00
|
|
|
detachView(viewIndex: number): AppView<any> {
|
|
|
|
var view = ListWrapper.removeAt(this.nestedViews, viewIndex);
|
|
|
|
if (view.type === ViewType.COMPONENT) {
|
|
|
|
throw new BaseException(`Component views can't be moved!`);
|
2015-12-02 10:35:51 -08:00
|
|
|
}
|
|
|
|
|
2016-01-06 14:13:44 -08:00
|
|
|
view.renderer.detachView(view.flatRootNodes);
|
2015-12-02 10:35:51 -08:00
|
|
|
|
2016-01-06 14:13:44 -08:00
|
|
|
view.renderParent.removeContentChild(view);
|
|
|
|
return view;
|
2015-12-02 10:35:51 -08:00
|
|
|
}
|
|
|
|
}
|