2015-04-29 15:07:55 -07:00
|
|
|
import {Decorator} from 'angular2/src/core/annotations_impl/annotations';
|
2015-04-27 09:26:55 -07:00
|
|
|
import {ViewContainerRef} from 'angular2/src/core/compiler/view_container_ref';
|
2015-04-29 15:07:55 -07:00
|
|
|
import {ProtoViewRef} from 'angular2/src/core/compiler/view_ref';
|
2015-02-12 11:54:22 +01:00
|
|
|
import {isPresent, isBlank, normalizeBlank} from 'angular2/src/facade/lang';
|
2015-02-05 13:08:05 -08:00
|
|
|
import {ListWrapper, List, MapWrapper, Map} from 'angular2/src/facade/collection';
|
2015-04-28 18:17:00 -07:00
|
|
|
import {Parent} from 'angular2/src/core/annotations_impl/visibility';
|
2015-01-09 09:16:12 +01:00
|
|
|
|
2015-04-29 15:07:55 -07:00
|
|
|
class SwitchView {
|
|
|
|
_viewContainerRef: ViewContainerRef;
|
|
|
|
_protoViewRef: ProtoViewRef;
|
|
|
|
|
|
|
|
constructor(viewContainerRef: ViewContainerRef, protoViewRef: ProtoViewRef) {
|
|
|
|
this._protoViewRef = protoViewRef;
|
|
|
|
this._viewContainerRef = viewContainerRef;
|
|
|
|
}
|
|
|
|
|
|
|
|
create() {
|
|
|
|
this._viewContainerRef.create(this._protoViewRef);
|
|
|
|
}
|
|
|
|
|
|
|
|
destroy() {
|
|
|
|
this._viewContainerRef.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-09 09:16:12 +01:00
|
|
|
/**
|
2015-02-04 22:27:31 +01:00
|
|
|
* The `Switch` 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-02-04 22:27:31 +01:00
|
|
|
* Elements within `Switch` but without `SwitchWhen` or `SwitchDefault` directives will be
|
2015-01-09 09:16:12 +01:00
|
|
|
* preserved at the location as specified in the template.
|
|
|
|
*
|
2015-02-04 22:27:31 +01:00
|
|
|
* `Switch` 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-02-04 22:27:31 +01:00
|
|
|
* (where you place the directive), place an expression on the **`[switch]="..."` attribute**),
|
|
|
|
* define any inner elements inside of the directive and place a `[switch-when]` attribute per
|
2015-01-09 09:16:12 +01:00
|
|
|
* element.
|
2015-02-04 22:27:31 +01:00
|
|
|
* The when attribute is used to inform Switch 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-04-06 11:47:38 +02:00
|
|
|
* # Example:
|
2015-01-09 09:16:12 +01:00
|
|
|
*
|
|
|
|
* ```
|
2015-02-04 22:27:31 +01:00
|
|
|
* <ANY [switch]="expression">
|
|
|
|
* <template [switch-when]="whenExpression1">...</template>
|
|
|
|
* <template [switch-when]="whenExpression1">...</template>
|
|
|
|
* <template [switch-default]>...</template>
|
2015-01-09 09:16:12 +01:00
|
|
|
* </ANY>
|
|
|
|
* ```
|
2015-04-06 11:47:38 +02:00
|
|
|
*
|
2015-04-10 12:45:02 +02:00
|
|
|
* @exportedAs angular2/directives
|
2015-01-09 09:16:12 +01:00
|
|
|
*/
|
|
|
|
@Decorator({
|
2015-02-04 22:27:31 +01:00
|
|
|
selector: '[switch]',
|
2015-04-09 21:20:11 +02:00
|
|
|
properties: {
|
2015-02-17 21:55:18 +01:00
|
|
|
'value': 'switch'
|
2015-01-09 09:16:12 +01:00
|
|
|
}
|
|
|
|
})
|
2015-02-04 22:27:31 +01:00
|
|
|
export class Switch {
|
2015-01-09 09:16:12 +01:00
|
|
|
_switchValue: any;
|
|
|
|
_useDefault: boolean;
|
2015-04-29 15:07:55 -07:00
|
|
|
_valueViews: Map;
|
|
|
|
_activeViews: List<SwitchView>;
|
2015-01-09 09:16:12 +01:00
|
|
|
|
|
|
|
constructor() {
|
2015-04-29 15:07:55 -07:00
|
|
|
this._valueViews = MapWrapper.create();
|
|
|
|
this._activeViews = ListWrapper.create();
|
2015-01-09 09:16:12 +01:00
|
|
|
this._useDefault = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
set value(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-04-29 15:07:55 -07:00
|
|
|
var views = MapWrapper.get(this._valueViews, value);
|
|
|
|
if (isBlank(views)) {
|
2015-01-09 09:16:12 +01:00
|
|
|
this._useDefault = true;
|
2015-04-29 15:07:55 -07:00
|
|
|
views = normalizeBlank(MapWrapper.get(this._valueViews, _whenDefault));
|
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-04-29 15:07:55 -07:00
|
|
|
_onWhenValueChanged(oldWhen, newWhen, view: SwitchView):void {
|
|
|
|
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();
|
|
|
|
ListWrapper.push(this._activeViews, 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-04-29 15:07:55 -07:00
|
|
|
this._activateViews(MapWrapper.get(this._valueViews, _whenDefault));
|
2015-01-09 09:16:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-29 15:07:55 -07:00
|
|
|
_emptyAllActiveViews():void {
|
|
|
|
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-04-29 15:07:55 -07:00
|
|
|
this._activeViews = ListWrapper.create();
|
2015-01-09 09:16:12 +01:00
|
|
|
}
|
|
|
|
|
2015-04-29 15:07:55 -07:00
|
|
|
_activateViews(views: List<SwitchView>):void {
|
|
|
|
// 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-04-29 15:07:55 -07:00
|
|
|
_registerView(value, view: SwitchView): void {
|
|
|
|
var views = MapWrapper.get(this._valueViews, value);
|
|
|
|
if (isBlank(views)) {
|
|
|
|
views = ListWrapper.create();
|
|
|
|
MapWrapper.set(this._valueViews, value, views);
|
2015-01-09 09:16:12 +01:00
|
|
|
}
|
2015-04-29 15:07:55 -07:00
|
|
|
ListWrapper.push(views, view);
|
2015-01-09 09:16:12 +01:00
|
|
|
}
|
|
|
|
|
2015-04-29 15:07:55 -07:00
|
|
|
_deregisterView(value, view: SwitchView):void {
|
2015-01-09 09:16:12 +01:00
|
|
|
// `_whenDefault` is used a marker for non-registered whens
|
|
|
|
if (value == _whenDefault) return;
|
2015-04-29 15:07:55 -07:00
|
|
|
var views = MapWrapper.get(this._valueViews, value);
|
|
|
|
if (views.length == 1) {
|
|
|
|
MapWrapper.delete(this._valueViews, 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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-29 15:07:55 -07:00
|
|
|
|
2015-01-09 09:16:12 +01:00
|
|
|
/**
|
|
|
|
* Defines a case statement as an expression.
|
|
|
|
*
|
2015-02-04 22:27:31 +01:00
|
|
|
* If multiple `SwitchWhen` match the `Switch` value, all of them are displayed.
|
2015-01-09 09:16:12 +01:00
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* // match against a context variable
|
2015-02-04 22:27:31 +01:00
|
|
|
* <template [switch-when]="contextVariable">...</template>
|
2015-01-09 09:16:12 +01:00
|
|
|
*
|
|
|
|
* // match against a constant string
|
2015-02-04 22:27:31 +01:00
|
|
|
* <template [switch-when]="'stringValue'">...</template>
|
2015-01-09 09:16:12 +01:00
|
|
|
* ```
|
2015-03-31 22:47:11 +00:00
|
|
|
*
|
2015-04-10 12:45:02 +02:00
|
|
|
* @exportedAs angular2/directives
|
2015-01-09 09:16:12 +01:00
|
|
|
*/
|
2015-04-29 15:07:55 -07:00
|
|
|
@Decorator({
|
2015-02-04 22:27:31 +01:00
|
|
|
selector: '[switch-when]',
|
2015-04-09 21:20:11 +02:00
|
|
|
properties: {
|
2015-02-17 21:55:18 +01:00
|
|
|
'when' : 'switch-when'
|
2015-01-09 09:16:12 +01:00
|
|
|
}
|
|
|
|
})
|
2015-02-04 22:27:31 +01:00
|
|
|
export class SwitchWhen {
|
2015-01-09 09:16:12 +01:00
|
|
|
_value: any;
|
2015-02-04 22:27:31 +01:00
|
|
|
_switch: Switch;
|
2015-04-29 15:07:55 -07:00
|
|
|
_view: SwitchView;
|
2015-01-09 09:16:12 +01:00
|
|
|
|
2015-04-29 15:07:55 -07:00
|
|
|
constructor(viewContainer: ViewContainerRef, protoViewRef: ProtoViewRef, @Parent() sswitch: Switch) {
|
2015-01-09 09:16:12 +01:00
|
|
|
// `_whenDefault` is used as a marker for a not yet initialized value
|
|
|
|
this._value = _whenDefault;
|
2015-02-04 22:27:31 +01:00
|
|
|
this._switch = sswitch;
|
2015-04-29 15:07:55 -07:00
|
|
|
this._view = new SwitchView(viewContainer, protoViewRef);
|
|
|
|
}
|
|
|
|
|
|
|
|
onDestroy() {
|
|
|
|
this._switch
|
2015-01-09 09:16:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
set when(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-02-04 22:27:31 +01:00
|
|
|
* Default case statements are displayed when no `SwitchWhen` match the `switch` value.
|
2015-01-09 09:16:12 +01:00
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
*
|
|
|
|
* ```
|
2015-02-04 22:27:31 +01:00
|
|
|
* <template [switch-default]>...</template>
|
2015-01-09 09:16:12 +01:00
|
|
|
* ```
|
2015-04-06 11:47:38 +02:00
|
|
|
*
|
2015-04-10 12:45:02 +02:00
|
|
|
* @exportedAs angular2/directives
|
2015-01-09 09:16:12 +01:00
|
|
|
*/
|
2015-04-29 15:07:55 -07:00
|
|
|
@Decorator({
|
2015-02-04 22:27:31 +01:00
|
|
|
selector: '[switch-default]'
|
2015-01-09 09:16:12 +01:00
|
|
|
})
|
2015-02-04 22:27:31 +01:00
|
|
|
export class SwitchDefault {
|
2015-04-29 15:07:55 -07:00
|
|
|
constructor(viewContainer: ViewContainerRef, protoViewRef: ProtoViewRef, @Parent() sswitch: Switch) {
|
|
|
|
sswitch._registerView(_whenDefault, new SwitchView(viewContainer, protoViewRef));
|
2015-01-09 09:16:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var _whenDefault = new Object();
|