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-06-08 16:38:52 -07:00
|
|
|
import {Directive, Host, TemplateRef, ViewContainerRef} from '@angular/core';
|
|
|
|
|
2016-05-31 15:22:59 -07:00
|
|
|
import {ListWrapper, Map} from '../facade/collection';
|
2016-06-08 16:38:52 -07:00
|
|
|
import {isBlank, isPresent, normalizeBlank} from '../facade/lang';
|
2015-01-09 09:16:12 +01:00
|
|
|
|
2016-06-09 22:52:30 -07:00
|
|
|
const _CASE_DEFAULT = /*@ts2dart_const*/ new Object();
|
2015-04-29 15:07:55 -07:00
|
|
|
|
2016-06-22 14:48:50 -07:00
|
|
|
// TODO: remove when fully deprecated
|
|
|
|
let _warned: boolean = false;
|
|
|
|
|
2015-12-22 16:24:35 -08:00
|
|
|
export class SwitchView {
|
2016-06-08 16:38:52 -07:00
|
|
|
constructor(
|
|
|
|
private _viewContainerRef: ViewContainerRef, private _templateRef: TemplateRef<Object>) {}
|
2015-04-29 15:07:55 -07:00
|
|
|
|
2015-08-13 13:18:41 -07:00
|
|
|
create(): void { this._viewContainerRef.createEmbeddedView(this._templateRef); }
|
2015-04-29 15:07:55 -07:00
|
|
|
|
2015-08-13 13:18:41 -07:00
|
|
|
destroy(): void { this._viewContainerRef.clear(); }
|
2015-04-29 15:07:55 -07:00
|
|
|
}
|
|
|
|
|
2015-01-09 09:16:12 +01:00
|
|
|
/**
|
2015-09-29 18:06:55 -07:00
|
|
|
* Adds or removes DOM sub-trees when their match expressions match the switch expression.
|
|
|
|
*
|
2016-06-09 22:52:30 -07:00
|
|
|
* Elements within `NgSwitch` but without `ngSwitchCase` or `NgSwitchDefault` directives will be
|
2015-01-09 09:16:12 +01:00
|
|
|
* preserved at the location as specified in the template.
|
|
|
|
*
|
2015-09-29 18:06:55 -07:00
|
|
|
* `NgSwitch` simply inserts nested elements based on which match expression matches the value
|
|
|
|
* obtained from the evaluated switch expression. In other words, you define a container element
|
|
|
|
* (where you place the directive with a switch expression on the
|
2016-04-09 16:16:14 -04:00
|
|
|
* `[ngSwitch]="..."` attribute), define any inner elements inside of the directive and
|
2016-06-09 22:52:30 -07:00
|
|
|
* place a `[ngSwitchCase]` attribute per element.
|
2015-01-09 09:16:12 +01:00
|
|
|
*
|
2016-06-09 22:52:30 -07:00
|
|
|
* The `ngSwitchCase` property is used to inform `NgSwitch` which element to display when the
|
|
|
|
* expression is evaluated. If a matching expression is not found via a `ngSwitchCase` property
|
2015-11-23 16:02:19 -08:00
|
|
|
* then an element with the `ngSwitchDefault` attribute is displayed.
|
2015-01-09 09:16:12 +01:00
|
|
|
*
|
2015-09-29 18:06:55 -07:00
|
|
|
* ### Example ([live demo](http://plnkr.co/edit/DQMTII95CbuqWrl3lYAs?p=preview))
|
|
|
|
*
|
|
|
|
* ```typescript
|
2016-03-08 13:36:48 -08:00
|
|
|
* @Component({
|
|
|
|
* selector: 'app',
|
2015-09-29 18:06:55 -07:00
|
|
|
* template: `
|
|
|
|
* <p>Value = {{value}}</p>
|
|
|
|
* <button (click)="inc()">Increment</button>
|
|
|
|
*
|
2015-11-23 16:02:19 -08:00
|
|
|
* <div [ngSwitch]="value">
|
2016-06-09 22:52:30 -07:00
|
|
|
* <p *ngSwitchCase="'init'">increment to start</p>
|
|
|
|
* <p *ngSwitchCase="0">0, increment again</p>
|
|
|
|
* <p *ngSwitchCase="1">1, increment again</p>
|
|
|
|
* <p *ngSwitchCase="2">2, stop incrementing</p>
|
2015-11-23 16:02:19 -08:00
|
|
|
* <p *ngSwitchDefault>> 2, STOP!</p>
|
2015-09-29 18:06:55 -07:00
|
|
|
* </div>
|
|
|
|
*
|
|
|
|
* <!-- alternate syntax -->
|
|
|
|
*
|
2015-11-23 16:02:19 -08:00
|
|
|
* <p [ngSwitch]="value">
|
2016-06-09 22:52:30 -07:00
|
|
|
* <template ngSwitchCase="init">increment to start</template>
|
|
|
|
* <template [ngSwitchCase]="0">0, increment again</template>
|
|
|
|
* <template [ngSwitchCase]="1">1, increment again</template>
|
|
|
|
* <template [ngSwitchCase]="2">2, stop incrementing</template>
|
2015-11-23 16:02:19 -08:00
|
|
|
* <template ngSwitchDefault>> 2, STOP!</template>
|
2015-09-29 18:06:55 -07:00
|
|
|
* </p>
|
|
|
|
* `,
|
2016-06-09 22:52:30 -07:00
|
|
|
* directives: [NgSwitch, ngSwitchCase, NgSwitchDefault]
|
2015-09-29 18:06:55 -07:00
|
|
|
* })
|
|
|
|
* export class App {
|
|
|
|
* value = 'init';
|
|
|
|
*
|
|
|
|
* inc() {
|
|
|
|
* this.value = this.value === 'init' ? 0 : this.value + 1;
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* bootstrap(App).catch(err => console.error(err));
|
2015-01-09 09:16:12 +01:00
|
|
|
* ```
|
2016-05-27 11:24:05 -07:00
|
|
|
*
|
|
|
|
* @experimental
|
2015-01-09 09:16:12 +01:00
|
|
|
*/
|
2015-11-23 16:02:19 -08:00
|
|
|
@Directive({selector: '[ngSwitch]', inputs: ['ngSwitch']})
|
2015-05-11 16:07:23 -07:00
|
|
|
export class NgSwitch {
|
2015-08-13 13:18:41 -07:00
|
|
|
private _switchValue: any;
|
|
|
|
private _useDefault: boolean = false;
|
2015-09-29 11:11:06 -07:00
|
|
|
private _valueViews = new Map<any, SwitchView[]>();
|
2015-08-28 11:29:19 -07:00
|
|
|
private _activeViews: SwitchView[] = [];
|
2015-01-09 09:16:12 +01:00
|
|
|
|
2016-02-11 17:01:17 -08:00
|
|
|
set ngSwitch(value: any) {
|
2015-02-12 11:54:22 +01:00
|
|
|
// Empty the currently active ViewContainers
|
2015-04-29 15:07:55 -07:00
|
|
|
this._emptyAllActiveViews();
|
2015-01-09 09:16:12 +01:00
|
|
|
|
2015-02-12 11:54:22 +01:00
|
|
|
// Add the ViewContainers matching the value (with a fallback to default)
|
2015-01-09 09:16:12 +01:00
|
|
|
this._useDefault = false;
|
2015-06-17 16:21:40 -07:00
|
|
|
var views = this._valueViews.get(value);
|
2015-04-29 15:07:55 -07:00
|
|
|
if (isBlank(views)) {
|
2015-01-09 09:16:12 +01:00
|
|
|
this._useDefault = true;
|
2016-06-09 22:52:30 -07:00
|
|
|
views = normalizeBlank(this._valueViews.get(_CASE_DEFAULT));
|
2015-01-09 09:16:12 +01:00
|
|
|
}
|
2015-04-29 15:07:55 -07:00
|
|
|
this._activateViews(views);
|
2015-01-09 09:16:12 +01:00
|
|
|
|
|
|
|
this._switchValue = value;
|
|
|
|
}
|
|
|
|
|
2015-10-09 17:21:25 -07:00
|
|
|
/** @internal */
|
2016-06-09 22:52:30 -07:00
|
|
|
_onCaseValueChanged(oldCase: any, newCase: any, view: SwitchView): void {
|
|
|
|
this._deregisterView(oldCase, view);
|
|
|
|
this._registerView(newCase, view);
|
2015-01-09 09:16:12 +01:00
|
|
|
|
2016-06-09 22:52:30 -07:00
|
|
|
if (oldCase === this._switchValue) {
|
2015-04-29 15:07:55 -07:00
|
|
|
view.destroy();
|
|
|
|
ListWrapper.remove(this._activeViews, view);
|
2016-06-09 22:52:30 -07:00
|
|
|
} else if (newCase === this._switchValue) {
|
2015-01-09 09:16:12 +01:00
|
|
|
if (this._useDefault) {
|
|
|
|
this._useDefault = false;
|
2015-04-29 15:07:55 -07:00
|
|
|
this._emptyAllActiveViews();
|
2015-01-09 09:16:12 +01:00
|
|
|
}
|
2015-04-29 15:07:55 -07:00
|
|
|
view.create();
|
2015-06-17 11:17:21 -07:00
|
|
|
this._activeViews.push(view);
|
2015-01-09 09:16:12 +01:00
|
|
|
}
|
|
|
|
|
2015-02-12 11:54:22 +01:00
|
|
|
// Switch to default when there is no more active ViewContainers
|
2015-04-29 15:07:55 -07:00
|
|
|
if (this._activeViews.length === 0 && !this._useDefault) {
|
2015-01-09 09:16:12 +01:00
|
|
|
this._useDefault = true;
|
2016-06-09 22:52:30 -07:00
|
|
|
this._activateViews(this._valueViews.get(_CASE_DEFAULT));
|
2015-01-09 09:16:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-09 17:21:25 -07:00
|
|
|
/** @internal */
|
2015-05-20 17:19:46 -07:00
|
|
|
_emptyAllActiveViews(): void {
|
2015-04-29 15:07:55 -07:00
|
|
|
var activeContainers = this._activeViews;
|
2015-02-12 11:54:22 +01:00
|
|
|
for (var i = 0; i < activeContainers.length; i++) {
|
2015-04-29 15:07:55 -07:00
|
|
|
activeContainers[i].destroy();
|
2015-01-09 09:16:12 +01:00
|
|
|
}
|
2015-06-17 11:17:21 -07:00
|
|
|
this._activeViews = [];
|
2015-01-09 09:16:12 +01:00
|
|
|
}
|
|
|
|
|
2015-10-09 17:21:25 -07:00
|
|
|
/** @internal */
|
2015-08-28 11:29:19 -07:00
|
|
|
_activateViews(views: SwitchView[]): void {
|
2015-04-29 15:07:55 -07:00
|
|
|
// TODO(vicb): assert(this._activeViews.length === 0);
|
|
|
|
if (isPresent(views)) {
|
|
|
|
for (var i = 0; i < views.length; i++) {
|
|
|
|
views[i].create();
|
2015-01-09 09:16:12 +01:00
|
|
|
}
|
2015-04-29 15:07:55 -07:00
|
|
|
this._activeViews = views;
|
2015-01-09 09:16:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-09 17:21:25 -07:00
|
|
|
/** @internal */
|
2016-02-11 17:01:17 -08:00
|
|
|
_registerView(value: any, view: SwitchView): void {
|
2015-06-17 16:21:40 -07:00
|
|
|
var views = this._valueViews.get(value);
|
2015-04-29 15:07:55 -07:00
|
|
|
if (isBlank(views)) {
|
2015-06-17 11:17:21 -07:00
|
|
|
views = [];
|
2015-06-17 16:21:40 -07:00
|
|
|
this._valueViews.set(value, views);
|
2015-01-09 09:16:12 +01:00
|
|
|
}
|
2015-06-17 11:17:21 -07:00
|
|
|
views.push(view);
|
2015-01-09 09:16:12 +01:00
|
|
|
}
|
|
|
|
|
2015-10-09 17:21:25 -07:00
|
|
|
/** @internal */
|
2016-02-11 17:01:17 -08:00
|
|
|
_deregisterView(value: any, view: SwitchView): void {
|
2016-06-09 22:52:30 -07:00
|
|
|
// `_CASE_DEFAULT` is used a marker for non-registered cases
|
|
|
|
if (value === _CASE_DEFAULT) return;
|
2015-06-17 16:21:40 -07:00
|
|
|
var views = this._valueViews.get(value);
|
2015-04-29 15:07:55 -07:00
|
|
|
if (views.length == 1) {
|
2015-08-13 13:18:41 -07:00
|
|
|
this._valueViews.delete(value);
|
2015-01-09 09:16:12 +01:00
|
|
|
} else {
|
2015-04-29 15:07:55 -07:00
|
|
|
ListWrapper.remove(views, view);
|
2015-01-09 09:16:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-06-09 22:52:30 -07:00
|
|
|
* Insert the sub-tree when the `ngSwitchCase` expression evaluates to the same value as the
|
2015-09-29 18:06:55 -07:00
|
|
|
* enclosing switch expression.
|
2015-01-09 09:16:12 +01:00
|
|
|
*
|
2015-09-29 18:06:55 -07:00
|
|
|
* If multiple match expression 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
|
|
|
*
|
|
|
|
* @experimental
|
2015-01-09 09:16:12 +01:00
|
|
|
*/
|
2016-06-09 22:52:30 -07:00
|
|
|
@Directive({selector: '[ngSwitchCase],[ngSwitchWhen]', inputs: ['ngSwitchCase', 'ngSwitchWhen']})
|
|
|
|
export class NgSwitchCase {
|
|
|
|
// `_CASE_DEFAULT` is used as a marker for a not yet initialized value
|
2015-10-09 17:21:25 -07:00
|
|
|
/** @internal */
|
2016-06-09 22:52:30 -07:00
|
|
|
_value: any = _CASE_DEFAULT;
|
2015-10-09 17:21:25 -07:00
|
|
|
/** @internal */
|
2015-04-29 15:07:55 -07:00
|
|
|
_view: SwitchView;
|
2015-09-29 18:06:55 -07:00
|
|
|
private _switch: NgSwitch;
|
2015-01-09 09:16:12 +01:00
|
|
|
|
2016-06-08 16:38:52 -07:00
|
|
|
constructor(
|
|
|
|
viewContainer: ViewContainerRef, templateRef: TemplateRef<Object>,
|
|
|
|
@Host() ngSwitch: NgSwitch) {
|
2015-09-29 18:06:55 -07:00
|
|
|
this._switch = ngSwitch;
|
2015-07-17 08:03:40 -07:00
|
|
|
this._view = new SwitchView(viewContainer, templateRef);
|
2015-04-29 15:07:55 -07:00
|
|
|
}
|
|
|
|
|
2016-06-09 22:52:30 -07:00
|
|
|
set ngSwitchCase(value: any) {
|
|
|
|
this._switch._onCaseValueChanged(this._value, value, this._view);
|
|
|
|
this._value = value;
|
|
|
|
}
|
|
|
|
|
2016-02-11 17:01:17 -08:00
|
|
|
set ngSwitchWhen(value: any) {
|
2016-06-22 14:48:50 -07:00
|
|
|
if (!_warned) {
|
|
|
|
_warned = true;
|
2016-06-09 22:52:30 -07:00
|
|
|
console.warn('*ngSwitchWhen is deprecated and will be removed. Use *ngSwitchCase instead');
|
|
|
|
}
|
|
|
|
this._switch._onCaseValueChanged(this._value, value, this._view);
|
2015-01-09 09:16:12 +01:00
|
|
|
this._value = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-09-29 18:06:55 -07:00
|
|
|
* Default case statements are displayed when no match expression matches the switch expression
|
|
|
|
* value.
|
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
|
|
|
*
|
|
|
|
* @experimental
|
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>,
|
|
|
|
@Host() sswitch: NgSwitch) {
|
2016-06-09 22:52:30 -07:00
|
|
|
sswitch._registerView(_CASE_DEFAULT, new SwitchView(viewContainer, templateRef));
|
2015-01-09 09:16:12 +01:00
|
|
|
}
|
|
|
|
}
|