angular-cn/modules/angular2/src/change_detection/abstract_change_detector.ts

83 lines
2.4 KiB
TypeScript
Raw Normal View History

import {isPresent} from 'angular2/src/facade/lang';
import {List, ListWrapper} from 'angular2/src/facade/collection';
import {ChangeDetectorRef} from './change_detector_ref';
import {ChangeDetector} from './interfaces';
import {Locals} from './parser/locals';
import {CHECK_ALWAYS, CHECK_ONCE, CHECKED, DETACHED, ON_PUSH} from './constants';
export class AbstractChangeDetector implements ChangeDetector {
2015-06-16 08:47:24 +02:00
lightDomChildren: List<any> = [];
shadowDomChildren: List<any> = [];
parent: ChangeDetector;
2015-06-16 08:47:24 +02:00
mode: string = null;
ref: ChangeDetectorRef;
constructor(public id: string) { this.ref = new ChangeDetectorRef(this); }
2015-06-16 08:47:24 +02:00
addChild(cd: ChangeDetector): void {
this.lightDomChildren.push(cd);
cd.parent = this;
}
2015-06-16 08:47:24 +02:00
removeChild(cd: ChangeDetector): void { ListWrapper.remove(this.lightDomChildren, cd); }
2015-06-16 08:47:24 +02:00
addShadowDomChild(cd: ChangeDetector): void {
this.shadowDomChildren.push(cd);
cd.parent = this;
}
2015-06-16 08:47:24 +02:00
removeShadowDomChild(cd: ChangeDetector): void { ListWrapper.remove(this.shadowDomChildren, cd); }
2015-06-16 08:47:24 +02:00
remove(): void { this.parent.removeChild(this); }
2015-06-16 08:47:24 +02:00
detectChanges(): void { this._detectChanges(false); }
2015-06-16 08:47:24 +02:00
checkNoChanges(): void { this._detectChanges(true); }
2015-06-16 08:47:24 +02:00
_detectChanges(throwOnChange: boolean): void {
if (this.mode === DETACHED || this.mode === CHECKED) return;
this.detectChangesInRecords(throwOnChange);
this._detectChangesInLightDomChildren(throwOnChange);
if (throwOnChange === false) this.callOnAllChangesDone();
this._detectChangesInShadowDomChildren(throwOnChange);
if (this.mode === CHECK_ONCE) this.mode = CHECKED;
}
2015-06-16 08:47:24 +02:00
detectChangesInRecords(throwOnChange: boolean): void {}
hydrate(context: any, locals: Locals, directives: any): void {}
dehydrate(): void {}
2015-06-16 08:47:24 +02:00
callOnAllChangesDone(): void {}
2015-06-16 08:47:24 +02:00
_detectChangesInLightDomChildren(throwOnChange: boolean): void {
var c = this.lightDomChildren;
for (var i = 0; i < c.length; ++i) {
c[i]._detectChanges(throwOnChange);
}
}
2015-06-16 08:47:24 +02:00
_detectChangesInShadowDomChildren(throwOnChange: boolean): void {
var c = this.shadowDomChildren;
for (var i = 0; i < c.length; ++i) {
c[i]._detectChanges(throwOnChange);
}
}
2015-06-16 08:47:24 +02:00
markAsCheckOnce(): void { this.mode = CHECK_ONCE; }
2015-06-16 08:47:24 +02:00
markPathToRootAsCheckOnce(): void {
var c: ChangeDetector = this;
while (isPresent(c) && c.mode != DETACHED) {
if (c.mode === CHECKED) c.mode = CHECK_ONCE;
c = c.parent;
}
}
}