11import { Directive } from 'angular2/annotations' ;
22import { Host } from 'angular2/di' ;
33import { ViewContainerRef , TemplateRef } from 'angular2/core' ;
4- import { isPresent , isBlank , normalizeBlank } from 'angular2/src/facade/lang' ;
5- import { ListWrapper , List , MapWrapper , Map } from 'angular2/src/facade/collection' ;
4+ import { isPresent , isBlank , normalizeBlank , CONST_EXPR } from 'angular2/src/facade/lang' ;
5+ import { ListWrapper , List , Map } from 'angular2/src/facade/collection' ;
66
7- export class SwitchView {
8- _viewContainerRef : ViewContainerRef ;
9- _templateRef : TemplateRef ;
7+ const _WHEN_DEFAULT = CONST_EXPR ( new Object ( ) ) ;
108
11- constructor ( viewContainerRef : ViewContainerRef , templateRef : TemplateRef ) {
12- this . _templateRef = templateRef ;
13- this . _viewContainerRef = viewContainerRef ;
14- }
9+ export class SwitchView {
10+ constructor ( private _viewContainerRef : ViewContainerRef , private _templateRef : TemplateRef ) { }
1511
16- create ( ) { this . _viewContainerRef . createEmbeddedView ( this . _templateRef ) ; }
12+ create ( ) : void { this . _viewContainerRef . createEmbeddedView ( this . _templateRef ) ; }
1713
18- destroy ( ) { this . _viewContainerRef . clear ( ) ; }
14+ destroy ( ) : void { this . _viewContainerRef . clear ( ) ; }
1915}
2016
2117/**
@@ -45,16 +41,10 @@ export class SwitchView {
4541 */
4642@Directive ( { selector : '[ng-switch]' , properties : [ 'ngSwitch' ] } )
4743export class NgSwitch {
48- _switchValue : any ;
49- _useDefault : boolean ;
50- _valueViews : Map < any , List < SwitchView > > ;
51- _activeViews : List < SwitchView > ;
52-
53- constructor ( ) {
54- this . _valueViews = new Map ( ) ;
55- this . _activeViews = [ ] ;
56- this . _useDefault = false ;
57- }
44+ private _switchValue : any ;
45+ private _useDefault : boolean = false ;
46+ private _valueViews : Map < any , List < SwitchView > > = new Map ( ) ;
47+ private _activeViews : List < SwitchView > = [ ] ;
5848
5949 set ngSwitch ( value ) {
6050 // Empty the currently active ViewContainers
@@ -65,7 +55,7 @@ export class NgSwitch {
6555 var views = this . _valueViews . get ( value ) ;
6656 if ( isBlank ( views ) ) {
6757 this . _useDefault = true ;
68- views = normalizeBlank ( this . _valueViews . get ( _whenDefault ) ) ;
58+ views = normalizeBlank ( this . _valueViews . get ( _WHEN_DEFAULT ) ) ;
6959 }
7060 this . _activateViews ( views ) ;
7161
@@ -91,7 +81,7 @@ export class NgSwitch {
9181 // Switch to default when there is no more active ViewContainers
9282 if ( this . _activeViews . length === 0 && ! this . _useDefault ) {
9383 this . _useDefault = true ;
94- this . _activateViews ( this . _valueViews . get ( _whenDefault ) ) ;
84+ this . _activateViews ( this . _valueViews . get ( _WHEN_DEFAULT ) ) ;
9585 }
9686 }
9787
@@ -123,18 +113,17 @@ export class NgSwitch {
123113 }
124114
125115 _deregisterView ( value , view : SwitchView ) : void {
126- // `_whenDefault ` is used a marker for non-registered whens
127- if ( value == _whenDefault ) return ;
116+ // `_WHEN_DEFAULT ` is used a marker for non-registered whens
117+ if ( value === _WHEN_DEFAULT ) return ;
128118 var views = this . _valueViews . get ( value ) ;
129119 if ( views . length == 1 ) {
130- MapWrapper . delete ( this . _valueViews , value ) ;
120+ this . _valueViews . delete ( value ) ;
131121 } else {
132122 ListWrapper . remove ( views , view ) ;
133123 }
134124 }
135125}
136126
137-
138127/**
139128 * Defines a case statement as an expression.
140129 *
@@ -152,27 +141,21 @@ export class NgSwitch {
152141 */
153142@Directive ( { selector : '[ng-switch-when]' , properties : [ 'ngSwitchWhen' ] } )
154143export class NgSwitchWhen {
155- _value : any ;
156- _switch : NgSwitch ;
144+ // `_WHEN_DEFAULT` is used as a marker for a not yet initialized value
145+ _value : any = _WHEN_DEFAULT ;
157146 _view : SwitchView ;
158147
159148 constructor ( viewContainer : ViewContainerRef , templateRef : TemplateRef ,
160- @Host ( ) sswitch : NgSwitch ) {
161- // `_whenDefault` is used as a marker for a not yet initialized value
162- this . _value = _whenDefault ;
163- this . _switch = sswitch ;
149+ @Host ( ) private _switch : NgSwitch ) {
164150 this . _view = new SwitchView ( viewContainer , templateRef ) ;
165151 }
166152
167- onDestroy ( ) { this . _switch ; }
168-
169153 set ngSwitchWhen ( value ) {
170154 this . _switch . _onWhenValueChanged ( this . _value , value , this . _view ) ;
171155 this . _value = value ;
172156 }
173157}
174158
175-
176159/**
177160 * Defines a default case statement.
178161 *
@@ -188,8 +171,6 @@ export class NgSwitchWhen {
188171export class NgSwitchDefault {
189172 constructor ( viewContainer : ViewContainerRef , templateRef : TemplateRef ,
190173 @Host ( ) sswitch : NgSwitch ) {
191- sswitch . _registerView ( _whenDefault , new SwitchView ( viewContainer , templateRef ) ) ;
174+ sswitch . _registerView ( _WHEN_DEFAULT , new SwitchView ( viewContainer , templateRef ) ) ;
192175 }
193176}
194-
195- var _whenDefault = new Object ( ) ;
0 commit comments