Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(directives): minor cleanup & refactoring #3629

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 8 additions & 14 deletions modules/angular2/src/directives/ng_if.ts
Expand Up @@ -26,23 +26,17 @@ import {isBlank} from 'angular2/src/facade/lang';
*/
@Directive({selector: '[ng-if]', properties: ['ngIf']})
export class NgIf {
viewContainer: ViewContainerRef;
templateRef: TemplateRef;
prevCondition: boolean;
private _prevCondition: boolean = null;

constructor(viewContainer: ViewContainerRef, templateRef: TemplateRef) {
this.viewContainer = viewContainer;
this.prevCondition = null;
this.templateRef = templateRef;
}
constructor(private _viewContainer: ViewContainerRef, private _templateRef: TemplateRef) {}

set ngIf(newCondition /* boolean */) {
if (newCondition && (isBlank(this.prevCondition) || !this.prevCondition)) {
this.prevCondition = true;
this.viewContainer.createEmbeddedView(this.templateRef);
} else if (!newCondition && (isBlank(this.prevCondition) || this.prevCondition)) {
this.prevCondition = false;
this.viewContainer.clear();
if (newCondition && (isBlank(this._prevCondition) || !this._prevCondition)) {
this._prevCondition = true;
this._viewContainer.createEmbeddedView(this._templateRef);
} else if (!newCondition && (isBlank(this._prevCondition) || this._prevCondition)) {
this._prevCondition = false;
this._viewContainer.clear();
}
}
}
59 changes: 20 additions & 39 deletions modules/angular2/src/directives/ng_switch.ts
@@ -1,21 +1,17 @@
import {Directive} from 'angular2/annotations';
import {Host} from 'angular2/di';
import {ViewContainerRef, TemplateRef} from 'angular2/core';
import {isPresent, isBlank, normalizeBlank} from 'angular2/src/facade/lang';
import {ListWrapper, List, MapWrapper, Map} from 'angular2/src/facade/collection';
import {isPresent, isBlank, normalizeBlank, CONST_EXPR} from 'angular2/src/facade/lang';
import {ListWrapper, List, Map} from 'angular2/src/facade/collection';

export class SwitchView {
_viewContainerRef: ViewContainerRef;
_templateRef: TemplateRef;
const _WHEN_DEFAULT = CONST_EXPR(new Object());

constructor(viewContainerRef: ViewContainerRef, templateRef: TemplateRef) {
this._templateRef = templateRef;
this._viewContainerRef = viewContainerRef;
}
export class SwitchView {
constructor(private _viewContainerRef: ViewContainerRef, private _templateRef: TemplateRef) {}

create() { this._viewContainerRef.createEmbeddedView(this._templateRef); }
create(): void { this._viewContainerRef.createEmbeddedView(this._templateRef); }

destroy() { this._viewContainerRef.clear(); }
destroy(): void { this._viewContainerRef.clear(); }
}

/**
Expand Down Expand Up @@ -45,16 +41,10 @@ export class SwitchView {
*/
@Directive({selector: '[ng-switch]', properties: ['ngSwitch']})
export class NgSwitch {
_switchValue: any;
_useDefault: boolean;
_valueViews: Map<any, List<SwitchView>>;
_activeViews: List<SwitchView>;

constructor() {
this._valueViews = new Map();
this._activeViews = [];
this._useDefault = false;
}
private _switchValue: any;
private _useDefault: boolean = false;
private _valueViews: Map<any, List<SwitchView>> = new Map();
private _activeViews: List<SwitchView> = [];

set ngSwitch(value) {
// Empty the currently active ViewContainers
Expand All @@ -65,7 +55,7 @@ export class NgSwitch {
var views = this._valueViews.get(value);
if (isBlank(views)) {
this._useDefault = true;
views = normalizeBlank(this._valueViews.get(_whenDefault));
views = normalizeBlank(this._valueViews.get(_WHEN_DEFAULT));
}
this._activateViews(views);

Expand All @@ -91,7 +81,7 @@ export class NgSwitch {
// Switch to default when there is no more active ViewContainers
if (this._activeViews.length === 0 && !this._useDefault) {
this._useDefault = true;
this._activateViews(this._valueViews.get(_whenDefault));
this._activateViews(this._valueViews.get(_WHEN_DEFAULT));
}
}

Expand Down Expand Up @@ -123,18 +113,17 @@ export class NgSwitch {
}

_deregisterView(value, view: SwitchView): void {
// `_whenDefault` is used a marker for non-registered whens
if (value == _whenDefault) return;
// `_WHEN_DEFAULT` is used a marker for non-registered whens
if (value === _WHEN_DEFAULT) return;
var views = this._valueViews.get(value);
if (views.length == 1) {
MapWrapper.delete(this._valueViews, value);
this._valueViews.delete(value);
} else {
ListWrapper.remove(views, view);
}
}
}


/**
* Defines a case statement as an expression.
*
Expand All @@ -152,27 +141,21 @@ export class NgSwitch {
*/
@Directive({selector: '[ng-switch-when]', properties: ['ngSwitchWhen']})
export class NgSwitchWhen {
_value: any;
_switch: NgSwitch;
// `_WHEN_DEFAULT` is used as a marker for a not yet initialized value
_value: any = _WHEN_DEFAULT;
_view: SwitchView;

constructor(viewContainer: ViewContainerRef, templateRef: TemplateRef,
@Host() sswitch: NgSwitch) {
// `_whenDefault` is used as a marker for a not yet initialized value
this._value = _whenDefault;
this._switch = sswitch;
@Host() private _switch: NgSwitch) {
this._view = new SwitchView(viewContainer, templateRef);
}

onDestroy() { this._switch; }

set ngSwitchWhen(value) {
this._switch._onWhenValueChanged(this._value, value, this._view);
this._value = value;
}
}


/**
* Defines a default case statement.
*
Expand All @@ -188,8 +171,6 @@ export class NgSwitchWhen {
export class NgSwitchDefault {
constructor(viewContainer: ViewContainerRef, templateRef: TemplateRef,
@Host() sswitch: NgSwitch) {
sswitch._registerView(_whenDefault, new SwitchView(viewContainer, templateRef));
sswitch._registerView(_WHEN_DEFAULT, new SwitchView(viewContainer, templateRef));
}
}

var _whenDefault = new Object();