2016-06-23 09:47:54 -07:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2016-11-07 12:22:36 -08:00
|
|
|
import {Directive, DoCheck, Host, Input, TemplateRef, ViewContainerRef} from '@angular/core';
|
2015-04-29 15:07:55 -07:00
|
|
|
|
2015-12-22 16:24:35 -08:00
|
|
|
export class SwitchView {
|
2016-11-07 12:22:36 -08:00
|
|
|
private _created = false;
|
|
|
|
|
2016-06-08 16:38:52 -07:00
|
|
|
constructor(
|
|
|
|
private _viewContainerRef: ViewContainerRef, private _templateRef: TemplateRef<Object>) {}
|
2015-04-29 15:07:55 -07:00
|
|
|
|
2016-11-07 12:22:36 -08:00
|
|
|
create(): void {
|
|
|
|
this._created = true;
|
|
|
|
this._viewContainerRef.createEmbeddedView(this._templateRef);
|
|
|
|
}
|
2015-04-29 15:07:55 -07:00
|
|
|
|
2016-11-07 12:22:36 -08:00
|
|
|
destroy(): void {
|
|
|
|
this._created = false;
|
|
|
|
this._viewContainerRef.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
enforceState(created: boolean) {
|
|
|
|
if (created && !this._created) {
|
|
|
|
this.create();
|
|
|
|
} else if (!created && this._created) {
|
|
|
|
this.destroy();
|
|
|
|
}
|
|
|
|
}
|
2015-04-29 15:07:55 -07:00
|
|
|
}
|
|
|
|
|
2015-01-09 09:16:12 +01:00
|
|
|
/**
|
2016-09-12 10:20:33 -07:00
|
|
|
* @ngModule CommonModule
|
|
|
|
*
|
|
|
|
* @whatItDoes Adds / removes DOM sub-trees when the nest match expressions matches the switch
|
|
|
|
* expression.
|
|
|
|
*
|
|
|
|
* @howToUse
|
2015-01-09 09:16:12 +01:00
|
|
|
* ```
|
2016-09-12 10:20:33 -07:00
|
|
|
* <container-element [ngSwitch]="switch_expression">
|
|
|
|
* <some-element *ngSwitchCase="match_expression_1">...</some-element>
|
|
|
|
* <some-element *ngSwitchCase="match_expression_2">...</some-element>
|
|
|
|
* <some-other-element *ngSwitchCase="match_expression_3">...</some-other-element>
|
|
|
|
* <ng-container *ngSwitchCase="match_expression_3">
|
|
|
|
* <!-- use a ng-container to group multiple root nodes -->
|
|
|
|
* <inner-element></inner-element>
|
|
|
|
* <inner-other-element></inner-other-element>
|
|
|
|
* </ng-container>
|
2016-10-18 06:51:56 +01:00
|
|
|
* <some-element *ngSwitchDefault>...</some-element>
|
2016-09-12 10:20:33 -07:00
|
|
|
* </container-element>
|
|
|
|
* ```
|
|
|
|
* @description
|
|
|
|
*
|
|
|
|
* `NgSwitch` stamps out nested views when their match expression value matches the value of the
|
|
|
|
* switch expression.
|
|
|
|
*
|
|
|
|
* In other words:
|
|
|
|
* - you define a container element (where you place the directive with a switch expression on the
|
|
|
|
* `[ngSwitch]="..."` attribute)
|
|
|
|
* - you define inner views inside the `NgSwitch` and place a `*ngSwitchCase` attribute on the view
|
|
|
|
* root elements.
|
|
|
|
*
|
|
|
|
* Elements within `NgSwitch` but outside of a `NgSwitchCase` or `NgSwitchDefault` directives will
|
2016-10-12 15:20:22 -07:00
|
|
|
* be preserved at the location.
|
2016-09-12 10:20:33 -07:00
|
|
|
*
|
|
|
|
* The `ngSwitchCase` directive informs the parent `NgSwitch` of which view to display when the
|
|
|
|
* expression is evaluated.
|
|
|
|
* When no matching expression is found on a `ngSwitchCase` view, the `ngSwitchDefault` view is
|
|
|
|
* stamped out.
|
2016-05-27 11:24:05 -07:00
|
|
|
*
|
2016-08-23 13:58:41 -07:00
|
|
|
* @stable
|
2015-01-09 09:16:12 +01:00
|
|
|
*/
|
2016-07-07 16:35:13 -07:00
|
|
|
@Directive({selector: '[ngSwitch]'})
|
2015-05-11 16:07:23 -07:00
|
|
|
export class NgSwitch {
|
2016-11-07 12:22:36 -08:00
|
|
|
private _defaultViews: SwitchView[];
|
|
|
|
private _defaultUsed = false;
|
|
|
|
private _caseCount = 0;
|
|
|
|
private _lastCaseCheckIndex = 0;
|
|
|
|
private _lastCasesMatched = false;
|
|
|
|
private _ngSwitch: any;
|
2015-01-09 09:16:12 +01:00
|
|
|
|
2016-07-07 16:35:13 -07:00
|
|
|
@Input()
|
2016-11-07 12:22:36 -08:00
|
|
|
set ngSwitch(newValue: any) {
|
|
|
|
this._ngSwitch = newValue;
|
|
|
|
if (this._caseCount === 0) {
|
|
|
|
this._updateDefaultCases(true);
|
2015-01-09 09:16:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-09 17:21:25 -07:00
|
|
|
/** @internal */
|
2016-11-07 12:22:36 -08:00
|
|
|
_addCase(): number { return this._caseCount++; }
|
2015-01-09 09:16:12 +01:00
|
|
|
|
2016-11-07 12:22:36 -08:00
|
|
|
/** @internal */
|
|
|
|
_addDefault(view: SwitchView) {
|
|
|
|
if (!this._defaultViews) {
|
|
|
|
this._defaultViews = [];
|
2015-01-09 09:16:12 +01:00
|
|
|
}
|
2016-11-07 12:22:36 -08:00
|
|
|
this._defaultViews.push(view);
|
2015-01-09 09:16:12 +01:00
|
|
|
}
|
|
|
|
|
2015-10-09 17:21:25 -07:00
|
|
|
/** @internal */
|
2016-11-07 12:22:36 -08:00
|
|
|
_matchCase(value: any): boolean {
|
|
|
|
const matched = value == this._ngSwitch;
|
|
|
|
this._lastCasesMatched = this._lastCasesMatched || matched;
|
|
|
|
this._lastCaseCheckIndex++;
|
|
|
|
if (this._lastCaseCheckIndex === this._caseCount) {
|
|
|
|
this._updateDefaultCases(!this._lastCasesMatched);
|
|
|
|
this._lastCaseCheckIndex = 0;
|
|
|
|
this._lastCasesMatched = false;
|
2015-01-09 09:16:12 +01:00
|
|
|
}
|
2016-11-07 12:22:36 -08:00
|
|
|
return matched;
|
2015-01-09 09:16:12 +01:00
|
|
|
}
|
|
|
|
|
2016-11-07 12:22:36 -08:00
|
|
|
private _updateDefaultCases(useDefault: boolean) {
|
|
|
|
if (this._defaultViews && useDefault !== this._defaultUsed) {
|
|
|
|
this._defaultUsed = useDefault;
|
2016-11-12 14:08:58 +01:00
|
|
|
for (let i = 0; i < this._defaultViews.length; i++) {
|
2016-11-07 12:22:36 -08:00
|
|
|
const defaultView = this._defaultViews[i];
|
|
|
|
defaultView.enforceState(useDefault);
|
|
|
|
}
|
2015-01-09 09:16:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-09-12 10:20:33 -07:00
|
|
|
* @ngModule CommonModule
|
2015-01-09 09:16:12 +01:00
|
|
|
*
|
2016-09-12 10:20:33 -07:00
|
|
|
* @whatItDoes Creates a view that will be added/removed from the parent {@link NgSwitch} when the
|
|
|
|
* given expression evaluate to respectively the same/different value as the switch
|
|
|
|
* expression.
|
|
|
|
*
|
|
|
|
* @howToUse
|
2016-09-15 09:09:00 -07:00
|
|
|
* ```
|
|
|
|
* <container-element [ngSwitch]="switch_expression">
|
|
|
|
* <some-element *ngSwitchCase="match_expression_1">...</some-element>
|
|
|
|
* </container-element>
|
|
|
|
*```
|
2016-09-12 10:20:33 -07:00
|
|
|
* @description
|
|
|
|
*
|
|
|
|
* Insert the sub-tree when the expression evaluates to the same value as the enclosing switch
|
|
|
|
* expression.
|
|
|
|
*
|
|
|
|
* If multiple match expressions match the switch expression value, all of them are displayed.
|
2015-01-09 09:16:12 +01:00
|
|
|
*
|
2015-09-29 18:06:55 -07:00
|
|
|
* See {@link NgSwitch} for more details and example.
|
2016-05-27 11:24:05 -07:00
|
|
|
*
|
2016-08-23 13:58:41 -07:00
|
|
|
* @stable
|
2015-01-09 09:16:12 +01:00
|
|
|
*/
|
2016-08-16 16:48:32 -07:00
|
|
|
@Directive({selector: '[ngSwitchCase]'})
|
2016-11-07 12:22:36 -08:00
|
|
|
export class NgSwitchCase implements DoCheck {
|
2016-09-22 10:34:00 -07:00
|
|
|
private _view: SwitchView;
|
2016-11-07 12:22:36 -08:00
|
|
|
|
|
|
|
@Input()
|
|
|
|
ngSwitchCase: any;
|
2015-01-09 09:16:12 +01:00
|
|
|
|
2016-06-08 16:38:52 -07:00
|
|
|
constructor(
|
|
|
|
viewContainer: ViewContainerRef, templateRef: TemplateRef<Object>,
|
2016-11-07 12:22:36 -08:00
|
|
|
@Host() private ngSwitch: NgSwitch) {
|
|
|
|
ngSwitch._addCase();
|
2015-07-17 08:03:40 -07:00
|
|
|
this._view = new SwitchView(viewContainer, templateRef);
|
2015-04-29 15:07:55 -07:00
|
|
|
}
|
|
|
|
|
2016-11-07 12:22:36 -08:00
|
|
|
ngDoCheck() { this._view.enforceState(this.ngSwitch._matchCase(this.ngSwitchCase)); }
|
2015-01-09 09:16:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-09-12 10:20:33 -07:00
|
|
|
* @ngModule CommonModule
|
|
|
|
* @whatItDoes Creates a view that is added to the parent {@link NgSwitch} when no case expressions
|
|
|
|
* match the
|
|
|
|
* switch expression.
|
|
|
|
*
|
|
|
|
* @howToUse
|
2016-09-15 09:09:00 -07:00
|
|
|
* ```
|
|
|
|
* <container-element [ngSwitch]="switch_expression">
|
|
|
|
* <some-element *ngSwitchCase="match_expression_1">...</some-element>
|
|
|
|
* <some-other-element *ngSwitchDefault>...</some-other-element>
|
|
|
|
* </container-element>
|
|
|
|
* ```
|
2016-09-12 10:20:33 -07:00
|
|
|
*
|
|
|
|
* @description
|
|
|
|
*
|
|
|
|
* Insert the sub-tree when no case expressions evaluate to the same value as the enclosing switch
|
|
|
|
* expression.
|
2015-01-09 09:16:12 +01:00
|
|
|
*
|
2015-09-29 18:06:55 -07:00
|
|
|
* See {@link NgSwitch} for more details and example.
|
2016-05-27 11:24:05 -07:00
|
|
|
*
|
2016-08-23 13:58:41 -07:00
|
|
|
* @stable
|
2015-01-09 09:16:12 +01:00
|
|
|
*/
|
2015-11-23 16:02:19 -08:00
|
|
|
@Directive({selector: '[ngSwitchDefault]'})
|
2015-05-11 16:07:23 -07:00
|
|
|
export class NgSwitchDefault {
|
2016-06-08 16:38:52 -07:00
|
|
|
constructor(
|
|
|
|
viewContainer: ViewContainerRef, templateRef: TemplateRef<Object>,
|
2016-11-07 12:22:36 -08:00
|
|
|
@Host() ngSwitch: NgSwitch) {
|
|
|
|
ngSwitch._addDefault(new SwitchView(viewContainer, templateRef));
|
2015-01-09 09:16:12 +01:00
|
|
|
}
|
|
|
|
}
|