2015-09-03 22:01:36 -07:00
|
|
|
import {Directive} from 'angular2/src/core/metadata';
|
|
|
|
import {Host} from 'angular2/src/core/di';
|
2015-10-02 07:37:23 -07:00
|
|
|
import {ViewContainerRef, TemplateRef} from 'angular2/src/core/linker';
|
2015-11-06 17:34:07 -08:00
|
|
|
import {isPresent, isBlank, normalizeBlank, CONST_EXPR} from 'angular2/src/facade/lang';
|
|
|
|
import {ListWrapper, Map} from 'angular2/src/facade/collection';
|
2015-01-09 09:16:12 +01:00
|
|
|
|
2015-08-13 13:18:41 -07:00
|
|
|
const _WHEN_DEFAULT = CONST_EXPR(new Object());
|
2015-04-29 15:07:55 -07:00
|
|
|
|
2015-08-13 13:18:41 -07:00
|
|
|
export class SwitchView {
|
|
|
|
constructor(private _viewContainerRef: ViewContainerRef, private _templateRef: TemplateRef) {}
|
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-05-11 16:07:23 -07:00
|
|
|
* The `NgSwitch` directive is used to conditionally swap DOM structure on your template based on a
|
2015-01-09 09:16:12 +01:00
|
|
|
* scope expression.
|
2015-05-11 16:07:23 -07:00
|
|
|
* Elements within `NgSwitch` but without `NgSwitchWhen` or `NgSwitchDefault` directives will be
|
2015-01-09 09:16:12 +01:00
|
|
|
* preserved at the location as specified in the template.
|
|
|
|
*
|
2015-05-11 16:07:23 -07:00
|
|
|
* `NgSwitch` simply chooses nested elements and makes them visible based on which element matches
|
2015-01-09 09:16:12 +01:00
|
|
|
* the value obtained from the evaluated expression. In other words, you define a container element
|
2015-05-11 16:07:23 -07:00
|
|
|
* (where you place the directive), place an expression on the **`[ng-switch]="..."` attribute**),
|
|
|
|
* define any inner elements inside of the directive and place a `[ng-switch-when]` attribute per
|
2015-01-09 09:16:12 +01:00
|
|
|
* element.
|
2015-05-11 16:07:23 -07:00
|
|
|
* The when attribute is used to inform NgSwitch which element to display when the expression is
|
2015-01-09 09:16:12 +01:00
|
|
|
* evaluated. If a matching expression is not found via a when attribute then an element with the
|
|
|
|
* default attribute is displayed.
|
|
|
|
*
|
2015-10-19 15:37:32 +01:00
|
|
|
* ### Example
|
2015-01-09 09:16:12 +01:00
|
|
|
*
|
|
|
|
* ```
|
2015-05-11 16:07:23 -07:00
|
|
|
* <ANY [ng-switch]="expression">
|
|
|
|
* <template [ng-switch-when]="whenExpression1">...</template>
|
|
|
|
* <template [ng-switch-when]="whenExpression1">...</template>
|
2015-06-21 12:15:30 +02:00
|
|
|
* <template ng-switch-default>...</template>
|
2015-01-09 09:16:12 +01:00
|
|
|
* </ANY>
|
|
|
|
* ```
|
|
|
|
*/
|
2015-09-30 20:59:23 -07:00
|
|
|
@Directive({selector: '[ng-switch]', 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
|
|
|
|
2015-05-11 16:07:23 -07:00
|
|
|
set ngSwitch(value) {
|
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;
|
2015-08-13 13:18:41 -07:00
|
|
|
views = normalizeBlank(this._valueViews.get(_WHEN_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 */
|
2015-05-20 17:19:46 -07:00
|
|
|
_onWhenValueChanged(oldWhen, newWhen, view: SwitchView): void {
|
2015-04-29 15:07:55 -07:00
|
|
|
this._deregisterView(oldWhen, view);
|
|
|
|
this._registerView(newWhen, view);
|
2015-01-09 09:16:12 +01:00
|
|
|
|
|
|
|
if (oldWhen === this._switchValue) {
|
2015-04-29 15:07:55 -07:00
|
|
|
view.destroy();
|
|
|
|
ListWrapper.remove(this._activeViews, view);
|
2015-01-09 09:16:12 +01:00
|
|
|
} else if (newWhen === this._switchValue) {
|
|
|
|
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;
|
2015-08-13 13:18:41 -07:00
|
|
|
this._activateViews(this._valueViews.get(_WHEN_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 */
|
2015-04-29 15:07:55 -07:00
|
|
|
_registerView(value, 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 */
|
2015-05-20 17:19:46 -07:00
|
|
|
_deregisterView(value, view: SwitchView): void {
|
2015-08-13 13:18:41 -07:00
|
|
|
// `_WHEN_DEFAULT` is used a marker for non-registered whens
|
|
|
|
if (value === _WHEN_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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Defines a case statement as an expression.
|
|
|
|
*
|
2015-05-11 16:07:23 -07:00
|
|
|
* If multiple `NgSwitchWhen` match the `NgSwitch` value, all of them are displayed.
|
2015-01-09 09:16:12 +01:00
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* // match against a context variable
|
2015-05-11 16:07:23 -07:00
|
|
|
* <template [ng-switch-when]="contextVariable">...</template>
|
2015-01-09 09:16:12 +01:00
|
|
|
*
|
|
|
|
* // match against a constant string
|
2015-06-21 12:15:30 +02:00
|
|
|
* <template ng-switch-when="stringValue">...</template>
|
2015-01-09 09:16:12 +01:00
|
|
|
* ```
|
|
|
|
*/
|
2015-09-30 20:59:23 -07:00
|
|
|
@Directive({selector: '[ng-switch-when]', inputs: ['ngSwitchWhen']})
|
2015-05-11 16:07:23 -07:00
|
|
|
export class NgSwitchWhen {
|
2015-08-13 13:18:41 -07:00
|
|
|
// `_WHEN_DEFAULT` is used as a marker for a not yet initialized value
|
2015-10-09 17:21:25 -07:00
|
|
|
/** @internal */
|
2015-08-13 13:18:41 -07:00
|
|
|
_value: any = _WHEN_DEFAULT;
|
2015-10-09 17:21:25 -07:00
|
|
|
/** @internal */
|
2015-04-29 15:07:55 -07:00
|
|
|
_view: SwitchView;
|
2015-01-09 09:16:12 +01:00
|
|
|
|
2015-07-17 08:03:40 -07:00
|
|
|
constructor(viewContainer: ViewContainerRef, templateRef: TemplateRef,
|
2015-08-13 13:18:41 -07:00
|
|
|
@Host() private _switch: NgSwitch) {
|
2015-07-17 08:03:40 -07:00
|
|
|
this._view = new SwitchView(viewContainer, templateRef);
|
2015-04-29 15:07:55 -07:00
|
|
|
}
|
|
|
|
|
2015-05-11 16:07:23 -07:00
|
|
|
set ngSwitchWhen(value) {
|
2015-04-29 15:07:55 -07:00
|
|
|
this._switch._onWhenValueChanged(this._value, value, this._view);
|
2015-01-09 09:16:12 +01:00
|
|
|
this._value = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Defines a default case statement.
|
|
|
|
*
|
2015-06-02 08:55:58 -07:00
|
|
|
* Default case statements are displayed when no `NgSwitchWhen` match the `ng-switch` value.
|
2015-01-09 09:16:12 +01:00
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
*
|
|
|
|
* ```
|
2015-06-21 12:15:30 +02:00
|
|
|
* <template ng-switch-default>...</template>
|
2015-01-09 09:16:12 +01:00
|
|
|
* ```
|
|
|
|
*/
|
2015-05-20 17:19:46 -07:00
|
|
|
@Directive({selector: '[ng-switch-default]'})
|
2015-05-11 16:07:23 -07:00
|
|
|
export class NgSwitchDefault {
|
2015-07-17 08:03:40 -07:00
|
|
|
constructor(viewContainer: ViewContainerRef, templateRef: TemplateRef,
|
2015-07-29 11:26:09 -07:00
|
|
|
@Host() sswitch: NgSwitch) {
|
2015-08-13 13:18:41 -07:00
|
|
|
sswitch._registerView(_WHEN_DEFAULT, new SwitchView(viewContainer, templateRef));
|
2015-01-09 09:16:12 +01:00
|
|
|
}
|
|
|
|
}
|