2016-06-23 16:47:54 +00: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-07-07 23:35:13 +00:00
|
|
|
import {Directive, Host, Input, TemplateRef, ViewContainerRef} from '@angular/core';
|
2016-06-08 23:38:52 +00:00
|
|
|
|
2016-07-07 23:35:13 +00:00
|
|
|
import {ListWrapper} from '../facade/collection';
|
2015-01-09 08:16:12 +00:00
|
|
|
|
2016-07-07 23:35:13 +00:00
|
|
|
const _CASE_DEFAULT = new Object();
|
2015-04-29 22:07:55 +00:00
|
|
|
|
2015-12-23 00:24:35 +00:00
|
|
|
export class SwitchView {
|
2016-06-08 23:38:52 +00:00
|
|
|
constructor(
|
|
|
|
|
private _viewContainerRef: ViewContainerRef, private _templateRef: TemplateRef<Object>) {}
|
2015-04-29 22:07:55 +00:00
|
|
|
|
2015-08-13 20:18:41 +00:00
|
|
|
create(): void { this._viewContainerRef.createEmbeddedView(this._templateRef); }
|
2015-04-29 22:07:55 +00:00
|
|
|
|
2015-08-13 20:18:41 +00:00
|
|
|
destroy(): void { this._viewContainerRef.clear(); }
|
2015-04-29 22:07:55 +00:00
|
|
|
}
|
|
|
|
|
|
2015-01-09 08:16:12 +00:00
|
|
|
/**
|
2016-09-12 17:20:33 +00:00
|
|
|
* @ngModule CommonModule
|
|
|
|
|
*
|
|
|
|
|
* @whatItDoes Adds / removes DOM sub-trees when the nest match expressions matches the switch
|
|
|
|
|
* expression.
|
|
|
|
|
*
|
|
|
|
|
* @howToUse
|
2015-01-09 08:16:12 +00:00
|
|
|
* ```
|
2016-09-12 17:20:33 +00: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>
|
|
|
|
|
* <some-element *ngSwitchDefault>...</p>
|
|
|
|
|
* </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
|
|
|
|
|
* be
|
|
|
|
|
* preserved at the location.
|
|
|
|
|
*
|
|
|
|
|
* 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 18:24:05 +00:00
|
|
|
*
|
2016-08-23 20:58:41 +00:00
|
|
|
* @stable
|
2015-01-09 08:16:12 +00:00
|
|
|
*/
|
2016-07-07 23:35:13 +00:00
|
|
|
@Directive({selector: '[ngSwitch]'})
|
2015-05-11 23:07:23 +00:00
|
|
|
export class NgSwitch {
|
2015-08-13 20:18:41 +00:00
|
|
|
private _switchValue: any;
|
|
|
|
|
private _useDefault: boolean = false;
|
2015-09-29 18:11:06 +00:00
|
|
|
private _valueViews = new Map<any, SwitchView[]>();
|
2015-08-28 18:29:19 +00:00
|
|
|
private _activeViews: SwitchView[] = [];
|
2015-01-09 08:16:12 +00:00
|
|
|
|
2016-07-07 23:35:13 +00:00
|
|
|
@Input()
|
2016-02-12 01:01:17 +00:00
|
|
|
set ngSwitch(value: any) {
|
2015-02-12 10:54:22 +00:00
|
|
|
// Empty the currently active ViewContainers
|
2015-04-29 22:07:55 +00:00
|
|
|
this._emptyAllActiveViews();
|
2015-01-09 08:16:12 +00:00
|
|
|
|
2015-02-12 10:54:22 +00:00
|
|
|
// Add the ViewContainers matching the value (with a fallback to default)
|
2015-01-09 08:16:12 +00:00
|
|
|
this._useDefault = false;
|
2016-09-09 02:25:25 +00:00
|
|
|
let views = this._valueViews.get(value);
|
|
|
|
|
if (!views) {
|
2015-01-09 08:16:12 +00:00
|
|
|
this._useDefault = true;
|
2016-09-09 02:25:25 +00:00
|
|
|
views = this._valueViews.get(_CASE_DEFAULT) || null;
|
2015-01-09 08:16:12 +00:00
|
|
|
}
|
2015-04-29 22:07:55 +00:00
|
|
|
this._activateViews(views);
|
2015-01-09 08:16:12 +00:00
|
|
|
|
|
|
|
|
this._switchValue = value;
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-10 00:21:25 +00:00
|
|
|
/** @internal */
|
2016-06-10 05:52:30 +00:00
|
|
|
_onCaseValueChanged(oldCase: any, newCase: any, view: SwitchView): void {
|
|
|
|
|
this._deregisterView(oldCase, view);
|
|
|
|
|
this._registerView(newCase, view);
|
2015-01-09 08:16:12 +00:00
|
|
|
|
2016-06-10 05:52:30 +00:00
|
|
|
if (oldCase === this._switchValue) {
|
2015-04-29 22:07:55 +00:00
|
|
|
view.destroy();
|
|
|
|
|
ListWrapper.remove(this._activeViews, view);
|
2016-06-10 05:52:30 +00:00
|
|
|
} else if (newCase === this._switchValue) {
|
2015-01-09 08:16:12 +00:00
|
|
|
if (this._useDefault) {
|
|
|
|
|
this._useDefault = false;
|
2015-04-29 22:07:55 +00:00
|
|
|
this._emptyAllActiveViews();
|
2015-01-09 08:16:12 +00:00
|
|
|
}
|
2015-04-29 22:07:55 +00:00
|
|
|
view.create();
|
2015-06-17 18:17:21 +00:00
|
|
|
this._activeViews.push(view);
|
2015-01-09 08:16:12 +00:00
|
|
|
}
|
|
|
|
|
|
2015-02-12 10:54:22 +00:00
|
|
|
// Switch to default when there is no more active ViewContainers
|
2015-04-29 22:07:55 +00:00
|
|
|
if (this._activeViews.length === 0 && !this._useDefault) {
|
2015-01-09 08:16:12 +00:00
|
|
|
this._useDefault = true;
|
2016-06-10 05:52:30 +00:00
|
|
|
this._activateViews(this._valueViews.get(_CASE_DEFAULT));
|
2015-01-09 08:16:12 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-10 00:21:25 +00:00
|
|
|
/** @internal */
|
2015-05-21 00:19:46 +00:00
|
|
|
_emptyAllActiveViews(): void {
|
2016-09-09 02:25:25 +00:00
|
|
|
const activeContainers = this._activeViews;
|
2015-02-12 10:54:22 +00:00
|
|
|
for (var i = 0; i < activeContainers.length; i++) {
|
2015-04-29 22:07:55 +00:00
|
|
|
activeContainers[i].destroy();
|
2015-01-09 08:16:12 +00:00
|
|
|
}
|
2015-06-17 18:17:21 +00:00
|
|
|
this._activeViews = [];
|
2015-01-09 08:16:12 +00:00
|
|
|
}
|
|
|
|
|
|
2015-10-10 00:21:25 +00:00
|
|
|
/** @internal */
|
2015-08-28 18:29:19 +00:00
|
|
|
_activateViews(views: SwitchView[]): void {
|
2015-04-29 22:07:55 +00:00
|
|
|
// TODO(vicb): assert(this._activeViews.length === 0);
|
2016-09-09 02:25:25 +00:00
|
|
|
if (views) {
|
2015-04-29 22:07:55 +00:00
|
|
|
for (var i = 0; i < views.length; i++) {
|
|
|
|
|
views[i].create();
|
2015-01-09 08:16:12 +00:00
|
|
|
}
|
2015-04-29 22:07:55 +00:00
|
|
|
this._activeViews = views;
|
2015-01-09 08:16:12 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-10 00:21:25 +00:00
|
|
|
/** @internal */
|
2016-02-12 01:01:17 +00:00
|
|
|
_registerView(value: any, view: SwitchView): void {
|
2016-09-09 02:25:25 +00:00
|
|
|
let views = this._valueViews.get(value);
|
|
|
|
|
if (!views) {
|
2015-06-17 18:17:21 +00:00
|
|
|
views = [];
|
2015-06-17 23:21:40 +00:00
|
|
|
this._valueViews.set(value, views);
|
2015-01-09 08:16:12 +00:00
|
|
|
}
|
2015-06-17 18:17:21 +00:00
|
|
|
views.push(view);
|
2015-01-09 08:16:12 +00:00
|
|
|
}
|
|
|
|
|
|
2015-10-10 00:21:25 +00:00
|
|
|
/** @internal */
|
2016-02-12 01:01:17 +00:00
|
|
|
_deregisterView(value: any, view: SwitchView): void {
|
2016-06-10 05:52:30 +00:00
|
|
|
// `_CASE_DEFAULT` is used a marker for non-registered cases
|
|
|
|
|
if (value === _CASE_DEFAULT) return;
|
2016-09-09 02:25:25 +00:00
|
|
|
const views = this._valueViews.get(value);
|
2015-04-29 22:07:55 +00:00
|
|
|
if (views.length == 1) {
|
2015-08-13 20:18:41 +00:00
|
|
|
this._valueViews.delete(value);
|
2015-01-09 08:16:12 +00:00
|
|
|
} else {
|
2015-04-29 22:07:55 +00:00
|
|
|
ListWrapper.remove(views, view);
|
2015-01-09 08:16:12 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-09-12 17:20:33 +00:00
|
|
|
* @ngModule CommonModule
|
2015-01-09 08:16:12 +00:00
|
|
|
*
|
2016-09-12 17:20:33 +00: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
|
|
|
|
|
* <container-element [ngSwitch]="switch_expression">
|
|
|
|
|
* <some-element *ngSwitchCase="match_expression_1">...</some-element>
|
|
|
|
|
* </container-element>
|
|
|
|
|
*
|
|
|
|
|
* @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 08:16:12 +00:00
|
|
|
*
|
2015-09-30 01:06:55 +00:00
|
|
|
* See {@link NgSwitch} for more details and example.
|
2016-05-27 18:24:05 +00:00
|
|
|
*
|
2016-08-23 20:58:41 +00:00
|
|
|
* @stable
|
2015-01-09 08:16:12 +00:00
|
|
|
*/
|
2016-08-16 23:48:32 +00:00
|
|
|
@Directive({selector: '[ngSwitchCase]'})
|
2016-06-10 05:52:30 +00:00
|
|
|
export class NgSwitchCase {
|
|
|
|
|
// `_CASE_DEFAULT` is used as a marker for a not yet initialized value
|
2015-10-10 00:21:25 +00:00
|
|
|
/** @internal */
|
2016-06-10 05:52:30 +00:00
|
|
|
_value: any = _CASE_DEFAULT;
|
2015-10-10 00:21:25 +00:00
|
|
|
/** @internal */
|
2015-04-29 22:07:55 +00:00
|
|
|
_view: SwitchView;
|
2015-09-30 01:06:55 +00:00
|
|
|
private _switch: NgSwitch;
|
2015-01-09 08:16:12 +00:00
|
|
|
|
2016-06-08 23:38:52 +00:00
|
|
|
constructor(
|
|
|
|
|
viewContainer: ViewContainerRef, templateRef: TemplateRef<Object>,
|
|
|
|
|
@Host() ngSwitch: NgSwitch) {
|
2015-09-30 01:06:55 +00:00
|
|
|
this._switch = ngSwitch;
|
2015-07-17 15:03:40 +00:00
|
|
|
this._view = new SwitchView(viewContainer, templateRef);
|
2015-04-29 22:07:55 +00:00
|
|
|
}
|
|
|
|
|
|
2016-07-07 23:35:13 +00:00
|
|
|
@Input()
|
2016-06-10 05:52:30 +00:00
|
|
|
set ngSwitchCase(value: any) {
|
|
|
|
|
this._switch._onCaseValueChanged(this._value, value, this._view);
|
|
|
|
|
this._value = value;
|
|
|
|
|
}
|
2015-01-09 08:16:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-09-12 17:20:33 +00:00
|
|
|
* @ngModule CommonModule
|
|
|
|
|
* @whatItDoes Creates a view that is added to the parent {@link NgSwitch} when no case expressions
|
|
|
|
|
* match the
|
|
|
|
|
* switch expression.
|
|
|
|
|
*
|
|
|
|
|
* @howToUse
|
|
|
|
|
* <container-element [ngSwitch]="switch_expression">
|
|
|
|
|
* <some-element *ngSwitchCase="match_expression_1">...</some-element>
|
|
|
|
|
* <some-other-element *ngSwitchDefault>...</some-other-element>
|
|
|
|
|
* </container-element>
|
|
|
|
|
*
|
|
|
|
|
* @description
|
|
|
|
|
*
|
|
|
|
|
* Insert the sub-tree when no case expressions evaluate to the same value as the enclosing switch
|
|
|
|
|
* expression.
|
2015-01-09 08:16:12 +00:00
|
|
|
*
|
2015-09-30 01:06:55 +00:00
|
|
|
* See {@link NgSwitch} for more details and example.
|
2016-05-27 18:24:05 +00:00
|
|
|
*
|
2016-08-23 20:58:41 +00:00
|
|
|
* @stable
|
2015-01-09 08:16:12 +00:00
|
|
|
*/
|
2015-11-24 00:02:19 +00:00
|
|
|
@Directive({selector: '[ngSwitchDefault]'})
|
2015-05-11 23:07:23 +00:00
|
|
|
export class NgSwitchDefault {
|
2016-06-08 23:38:52 +00:00
|
|
|
constructor(
|
|
|
|
|
viewContainer: ViewContainerRef, templateRef: TemplateRef<Object>,
|
|
|
|
|
@Host() sswitch: NgSwitch) {
|
2016-06-10 05:52:30 +00:00
|
|
|
sswitch._registerView(_CASE_DEFAULT, new SwitchView(viewContainer, templateRef));
|
2015-01-09 08:16:12 +00:00
|
|
|
}
|
|
|
|
|
}
|