Files
Angular-Docs-Cn/modules/angular2/src/directives/switch.js
T

190 lines
5.9 KiB
JavaScript
Raw Normal View History

import {Decorator, Viewport} from 'angular2/src/core/annotations/annotations';
import {ViewContainer} from 'angular2/src/core/compiler/view_container';
import {NgElement} from 'angular2/src/core/dom/element';
import {isPresent, isBlank, normalizeBlank} from 'angular2/src/facade/lang';
import {ListWrapper, List, MapWrapper, Map} from 'angular2/src/facade/collection';
import {Parent} from 'angular2/src/core/annotations/visibility';
2015-01-09 09:16:12 +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.
* 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.
*
* `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
* (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.
* 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.
*
* Example:
*
* ```
* <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>
* ```
*/
@Decorator({
selector: '[switch]',
2015-01-09 09:16:12 +01:00
bind: {
'switch': 'value'
2015-01-09 09:16:12 +01:00
}
})
export class Switch {
2015-01-09 09:16:12 +01:00
_switchValue: any;
_useDefault: boolean;
_valueViewContainers: Map;
_activeViewContainers: List<ViewContainer>;
2015-01-09 09:16:12 +01:00
constructor() {
this._valueViewContainers = MapWrapper.create();
this._activeViewContainers = ListWrapper.create();
2015-01-09 09:16:12 +01:00
this._useDefault = false;
}
set value(value) {
// Empty the currently active ViewContainers
this._emptyAllActiveViewContainers();
2015-01-09 09:16:12 +01:00
// Add the ViewContainers matching the value (with a fallback to default)
2015-01-09 09:16:12 +01:00
this._useDefault = false;
var containers = MapWrapper.get(this._valueViewContainers, value);
if (isBlank(containers)) {
2015-01-09 09:16:12 +01:00
this._useDefault = true;
containers = normalizeBlank(MapWrapper.get(this._valueViewContainers, _whenDefault));
2015-01-09 09:16:12 +01:00
}
this._activateViewContainers(containers);
2015-01-09 09:16:12 +01:00
this._switchValue = value;
}
_onWhenValueChanged(oldWhen, newWhen, viewContainer: ViewContainer) {
this._deregisterViewContainer(oldWhen, viewContainer);
this._registerViewContainer(newWhen, viewContainer);
2015-01-09 09:16:12 +01:00
if (oldWhen === this._switchValue) {
viewContainer.remove();
ListWrapper.remove(this._activeViewContainers, viewContainer);
2015-01-09 09:16:12 +01:00
} else if (newWhen === this._switchValue) {
if (this._useDefault) {
this._useDefault = false;
this._emptyAllActiveViewContainers();
2015-01-09 09:16:12 +01:00
}
viewContainer.create();
ListWrapper.push(this._activeViewContainers, viewContainer);
2015-01-09 09:16:12 +01:00
}
// Switch to default when there is no more active ViewContainers
if (this._activeViewContainers.length === 0 && !this._useDefault) {
2015-01-09 09:16:12 +01:00
this._useDefault = true;
this._activateViewContainers(MapWrapper.get(this._valueViewContainers, _whenDefault));
2015-01-09 09:16:12 +01:00
}
}
_emptyAllActiveViewContainers() {
var activeContainers = this._activeViewContainers;
for (var i = 0; i < activeContainers.length; i++) {
activeContainers[i].remove();
2015-01-09 09:16:12 +01:00
}
this._activeViewContainers = ListWrapper.create();
2015-01-09 09:16:12 +01:00
}
_activateViewContainers(containers: List<ViewContainer>) {
// TODO(vicb): assert(this._activeViewContainers.length === 0);
if (isPresent(containers)) {
for (var i = 0; i < containers.length; i++) {
containers[i].create();
2015-01-09 09:16:12 +01:00
}
this._activeViewContainers = containers;
2015-01-09 09:16:12 +01:00
}
}
_registerViewContainer(value, container: ViewContainer) {
var containers = MapWrapper.get(this._valueViewContainers, value);
if (isBlank(containers)) {
containers = ListWrapper.create();
MapWrapper.set(this._valueViewContainers, value, containers);
2015-01-09 09:16:12 +01:00
}
ListWrapper.push(containers, container);
2015-01-09 09:16:12 +01:00
}
_deregisterViewContainer(value, container: ViewContainer) {
2015-01-09 09:16:12 +01:00
// `_whenDefault` is used a marker for non-registered whens
if (value == _whenDefault) return;
var containers = MapWrapper.get(this._valueViewContainers, value);
if (containers.length == 1) {
MapWrapper.delete(this._valueViewContainers, value);
2015-01-09 09:16:12 +01:00
} else {
ListWrapper.remove(containers, container);
2015-01-09 09:16:12 +01:00
}
}
}
/**
* Defines a case statement as an expression.
*
* 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
* <template [switch-when]="contextVariable">...</template>
2015-01-09 09:16:12 +01:00
*
* // match against a constant string
* <template [switch-when]="'stringValue'">...</template>
2015-01-09 09:16:12 +01:00
* ```
*/
@Viewport({
selector: '[switch-when]',
2015-01-09 09:16:12 +01:00
bind: {
'switch-when' : 'when'
2015-01-09 09:16:12 +01:00
}
})
export class SwitchWhen {
2015-01-09 09:16:12 +01:00
_value: any;
_switch: Switch;
_viewContainer: ViewContainer;
2015-01-09 09:16:12 +01:00
constructor(el: NgElement, viewContainer: ViewContainer, @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;
this._switch = sswitch;
this._viewContainer = viewContainer;
2015-01-09 09:16:12 +01:00
}
set when(value) {
this._switch._onWhenValueChanged(this._value, value, this._viewContainer);
2015-01-09 09:16:12 +01:00
this._value = value;
}
}
/**
* Defines a default case statement.
*
* Default case statements are displayed when no `SwitchWhen` match the `switch` value.
2015-01-09 09:16:12 +01:00
*
* Example:
*
* ```
* <template [switch-default]>...</template>
2015-01-09 09:16:12 +01:00
* ```
*/
@Viewport({
selector: '[switch-default]'
2015-01-09 09:16:12 +01:00
})
export class SwitchDefault {
constructor(viewContainer: ViewContainer, @Parent() sswitch: Switch) {
sswitch._registerViewContainer(_whenDefault, viewContainer);
2015-01-09 09:16:12 +01:00
}
}
var _whenDefault = new Object();