refactor(directives): minor cleanup & refactoring

Closes #3629
This commit is contained in:
Victor Berchet 2015-08-13 13:18:41 -07:00
parent 89a0f2457d
commit ee5df00146
2 changed files with 28 additions and 53 deletions

View File

@ -26,23 +26,17 @@ import {isBlank} from 'angular2/src/facade/lang';
*/ */
@Directive({selector: '[ng-if]', properties: ['ngIf']}) @Directive({selector: '[ng-if]', properties: ['ngIf']})
export class NgIf { export class NgIf {
viewContainer: ViewContainerRef; private _prevCondition: boolean = null;
templateRef: TemplateRef;
prevCondition: boolean;
constructor(viewContainer: ViewContainerRef, templateRef: TemplateRef) { constructor(private _viewContainer: ViewContainerRef, private _templateRef: TemplateRef) {}
this.viewContainer = viewContainer;
this.prevCondition = null;
this.templateRef = templateRef;
}
set ngIf(newCondition /* boolean */) { set ngIf(newCondition /* boolean */) {
if (newCondition && (isBlank(this.prevCondition) || !this.prevCondition)) { if (newCondition && (isBlank(this._prevCondition) || !this._prevCondition)) {
this.prevCondition = true; this._prevCondition = true;
this.viewContainer.createEmbeddedView(this.templateRef); this._viewContainer.createEmbeddedView(this._templateRef);
} else if (!newCondition && (isBlank(this.prevCondition) || this.prevCondition)) { } else if (!newCondition && (isBlank(this._prevCondition) || this._prevCondition)) {
this.prevCondition = false; this._prevCondition = false;
this.viewContainer.clear(); this._viewContainer.clear();
} }
} }
} }

View File

@ -1,21 +1,17 @@
import {Directive} from 'angular2/annotations'; import {Directive} from 'angular2/annotations';
import {Host} from 'angular2/di'; import {Host} from 'angular2/di';
import {ViewContainerRef, TemplateRef} from 'angular2/core'; import {ViewContainerRef, TemplateRef} from 'angular2/core';
import {isPresent, isBlank, normalizeBlank} from 'angular2/src/facade/lang'; import {isPresent, isBlank, normalizeBlank, CONST_EXPR} from 'angular2/src/facade/lang';
import {ListWrapper, List, MapWrapper, Map} from 'angular2/src/facade/collection'; import {ListWrapper, List, Map} from 'angular2/src/facade/collection';
const _WHEN_DEFAULT = CONST_EXPR(new Object());
export class SwitchView { export class SwitchView {
_viewContainerRef: ViewContainerRef; constructor(private _viewContainerRef: ViewContainerRef, private _templateRef: TemplateRef) {}
_templateRef: TemplateRef;
constructor(viewContainerRef: ViewContainerRef, templateRef: TemplateRef) { create(): void { this._viewContainerRef.createEmbeddedView(this._templateRef); }
this._templateRef = templateRef;
this._viewContainerRef = viewContainerRef;
}
create() { this._viewContainerRef.createEmbeddedView(this._templateRef); } destroy(): void { this._viewContainerRef.clear(); }
destroy() { this._viewContainerRef.clear(); }
} }
/** /**
@ -45,16 +41,10 @@ export class SwitchView {
*/ */
@Directive({selector: '[ng-switch]', properties: ['ngSwitch']}) @Directive({selector: '[ng-switch]', properties: ['ngSwitch']})
export class NgSwitch { export class NgSwitch {
_switchValue: any; private _switchValue: any;
_useDefault: boolean; private _useDefault: boolean = false;
_valueViews: Map<any, List<SwitchView>>; private _valueViews: Map<any, List<SwitchView>> = new Map();
_activeViews: List<SwitchView>; private _activeViews: List<SwitchView> = [];
constructor() {
this._valueViews = new Map();
this._activeViews = [];
this._useDefault = false;
}
set ngSwitch(value) { set ngSwitch(value) {
// Empty the currently active ViewContainers // Empty the currently active ViewContainers
@ -65,7 +55,7 @@ export class NgSwitch {
var views = this._valueViews.get(value); var views = this._valueViews.get(value);
if (isBlank(views)) { if (isBlank(views)) {
this._useDefault = true; this._useDefault = true;
views = normalizeBlank(this._valueViews.get(_whenDefault)); views = normalizeBlank(this._valueViews.get(_WHEN_DEFAULT));
} }
this._activateViews(views); this._activateViews(views);
@ -91,7 +81,7 @@ export class NgSwitch {
// Switch to default when there is no more active ViewContainers // Switch to default when there is no more active ViewContainers
if (this._activeViews.length === 0 && !this._useDefault) { if (this._activeViews.length === 0 && !this._useDefault) {
this._useDefault = true; this._useDefault = true;
this._activateViews(this._valueViews.get(_whenDefault)); this._activateViews(this._valueViews.get(_WHEN_DEFAULT));
} }
} }
@ -123,18 +113,17 @@ export class NgSwitch {
} }
_deregisterView(value, view: SwitchView): void { _deregisterView(value, view: SwitchView): void {
// `_whenDefault` is used a marker for non-registered whens // `_WHEN_DEFAULT` is used a marker for non-registered whens
if (value == _whenDefault) return; if (value === _WHEN_DEFAULT) return;
var views = this._valueViews.get(value); var views = this._valueViews.get(value);
if (views.length == 1) { if (views.length == 1) {
MapWrapper.delete(this._valueViews, value); this._valueViews.delete(value);
} else { } else {
ListWrapper.remove(views, view); ListWrapper.remove(views, view);
} }
} }
} }
/** /**
* Defines a case statement as an expression. * Defines a case statement as an expression.
* *
@ -152,27 +141,21 @@ export class NgSwitch {
*/ */
@Directive({selector: '[ng-switch-when]', properties: ['ngSwitchWhen']}) @Directive({selector: '[ng-switch-when]', properties: ['ngSwitchWhen']})
export class NgSwitchWhen { export class NgSwitchWhen {
_value: any; // `_WHEN_DEFAULT` is used as a marker for a not yet initialized value
_switch: NgSwitch; _value: any = _WHEN_DEFAULT;
_view: SwitchView; _view: SwitchView;
constructor(viewContainer: ViewContainerRef, templateRef: TemplateRef, constructor(viewContainer: ViewContainerRef, templateRef: TemplateRef,
@Host() sswitch: NgSwitch) { @Host() private _switch: NgSwitch) {
// `_whenDefault` is used as a marker for a not yet initialized value
this._value = _whenDefault;
this._switch = sswitch;
this._view = new SwitchView(viewContainer, templateRef); this._view = new SwitchView(viewContainer, templateRef);
} }
onDestroy() { this._switch; }
set ngSwitchWhen(value) { set ngSwitchWhen(value) {
this._switch._onWhenValueChanged(this._value, value, this._view); this._switch._onWhenValueChanged(this._value, value, this._view);
this._value = value; this._value = value;
} }
} }
/** /**
* Defines a default case statement. * Defines a default case statement.
* *
@ -188,8 +171,6 @@ export class NgSwitchWhen {
export class NgSwitchDefault { export class NgSwitchDefault {
constructor(viewContainer: ViewContainerRef, templateRef: TemplateRef, constructor(viewContainer: ViewContainerRef, templateRef: TemplateRef,
@Host() sswitch: NgSwitch) { @Host() sswitch: NgSwitch) {
sswitch._registerView(_whenDefault, new SwitchView(viewContainer, templateRef)); sswitch._registerView(_WHEN_DEFAULT, new SwitchView(viewContainer, templateRef));
} }
} }
var _whenDefault = new Object();