|
| 1 | +import {Decorator, Template} from 'core/annotations/annotations'; |
| 2 | +import {ViewPort} from 'core/compiler/viewport'; |
| 3 | +import {NgElement} from 'core/dom/element'; |
| 4 | +import {DOM} from 'facade/dom'; |
| 5 | +import {isPresent, isBlank} from 'facade/lang'; |
| 6 | +import {ListWrapper, List, MapWrapper, Map} from 'facade/collection'; |
| 7 | +import {Parent} from 'core/annotations/visibility'; |
| 8 | + |
| 9 | +/** |
| 10 | + * The `ngSwitch` directive is used to conditionally swap DOM structure on your template based on a |
| 11 | + * scope expression. |
| 12 | + * Elements within `ngSwitch` but without `ngSwitchWhen` or `ngSwitchDefault` directives will be |
| 13 | + * preserved at the location as specified in the template. |
| 14 | + * |
| 15 | + * `ngSwitch` simply chooses nested elements and makes them visible based on which element matches |
| 16 | + * the value obtained from the evaluated expression. In other words, you define a container element |
| 17 | + * (where you place the directive), place an expression on the **`[ng-switch]="..."` attribute**), |
| 18 | + * define any inner elements inside of the directive and place a `[ng-switch-when]` attribute per |
| 19 | + * element. |
| 20 | + * The when attribute is used to inform ngSwitch which element to display when the expression is |
| 21 | + * evaluated. If a matching expression is not found via a when attribute then an element with the |
| 22 | + * default attribute is displayed. |
| 23 | + * |
| 24 | + * Example: |
| 25 | + * |
| 26 | + * ``` |
| 27 | + * <ANY [ng-switch]="expression"> |
| 28 | + * <template [ng-switch-when]="whenExpression1">...</template> |
| 29 | + * <template [ng-switch-when]="whenExpression1">...</template> |
| 30 | + * <template [ng-switch-default]>...</template> |
| 31 | + * </ANY> |
| 32 | + * ``` |
| 33 | + */ |
| 34 | +@Decorator({ |
| 35 | + selector: '[ng-switch]', |
| 36 | + bind: { |
| 37 | + 'ng-switch': 'value' |
| 38 | + } |
| 39 | +}) |
| 40 | +export class NgSwitch { |
| 41 | + _switchValue: any; |
| 42 | + _useDefault: boolean; |
| 43 | + _valueViewPorts: Map; |
| 44 | + _activeViewPorts: List; |
| 45 | + |
| 46 | + constructor() { |
| 47 | + this._valueViewPorts = MapWrapper.create(); |
| 48 | + this._activeViewPorts = ListWrapper.create(); |
| 49 | + this._useDefault = false; |
| 50 | + } |
| 51 | + |
| 52 | + set value(value) { |
| 53 | + // Remove the currently active viewports |
| 54 | + this._removeAllActiveViewPorts(); |
| 55 | + |
| 56 | + // Add the viewports matching the value (with a fallback to default) |
| 57 | + this._useDefault = false; |
| 58 | + var viewPorts = MapWrapper.get(this._valueViewPorts, value); |
| 59 | + if (isBlank(viewPorts)) { |
| 60 | + this._useDefault = true; |
| 61 | + viewPorts = MapWrapper.get(this._valueViewPorts, _whenDefault); |
| 62 | + } |
| 63 | + this._activateViewPorts(viewPorts); |
| 64 | + |
| 65 | + this._switchValue = value; |
| 66 | + } |
| 67 | + |
| 68 | + _onWhenValueChanged(oldWhen, newWhen, viewPort: ViewPort) { |
| 69 | + this._deregisterViewPort(oldWhen, viewPort); |
| 70 | + this._registerViewPort(newWhen, viewPort); |
| 71 | + |
| 72 | + if (oldWhen === this._switchValue) { |
| 73 | + viewPort.remove(); |
| 74 | + ListWrapper.remove(this._activeViewPorts, viewPort); |
| 75 | + } else if (newWhen === this._switchValue) { |
| 76 | + if (this._useDefault) { |
| 77 | + this._useDefault = false; |
| 78 | + this._removeAllActiveViewPorts(); |
| 79 | + } |
| 80 | + viewPort.create(); |
| 81 | + ListWrapper.push(this._activeViewPorts, viewPort); |
| 82 | + } |
| 83 | + |
| 84 | + // Switch to default when there is no more active viewports |
| 85 | + if (this._activeViewPorts.length === 0 && !this._useDefault) { |
| 86 | + this._useDefault = true; |
| 87 | + this._activateViewPorts(MapWrapper.get(this._valueViewPorts, _whenDefault)); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + _removeAllActiveViewPorts() { |
| 92 | + var activeViewPorts = this._activeViewPorts; |
| 93 | + for (var i = 0; i < activeViewPorts.length; i++) { |
| 94 | + activeViewPorts[i].remove(); |
| 95 | + } |
| 96 | + this._activeViewPorts = ListWrapper.create(); |
| 97 | + } |
| 98 | + |
| 99 | + _activateViewPorts(viewPorts) { |
| 100 | + // TODO(vicb): assert(this._activeViewPorts.length === 0); |
| 101 | + if (isPresent(viewPorts)) { |
| 102 | + for (var i = 0; i < viewPorts.length; i++) { |
| 103 | + viewPorts[i].create(); |
| 104 | + } |
| 105 | + this._activeViewPorts = viewPorts; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + _registerViewPort(value, viewPort: ViewPort) { |
| 110 | + var viewPorts = MapWrapper.get(this._valueViewPorts, value); |
| 111 | + if (isBlank(viewPorts)) { |
| 112 | + viewPorts = ListWrapper.create(); |
| 113 | + MapWrapper.set(this._valueViewPorts, value, viewPorts); |
| 114 | + } |
| 115 | + ListWrapper.push(viewPorts, viewPort); |
| 116 | + } |
| 117 | + |
| 118 | + _deregisterViewPort(value, viewPort: ViewPort) { |
| 119 | + // `_whenDefault` is used a marker for non-registered whens |
| 120 | + if (value == _whenDefault) return; |
| 121 | + var viewPorts = MapWrapper.get(this._valueViewPorts, value); |
| 122 | + if (viewPorts.length == 1) { |
| 123 | + MapWrapper.delete(this._valueViewPorts, value); |
| 124 | + } else { |
| 125 | + ListWrapper.remove(viewPorts, viewPort); |
| 126 | + } |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +/** |
| 131 | + * Defines a case statement as an expression. |
| 132 | + * |
| 133 | + * If multiple `ngSwitchWhen` match the `ngSwitch` value, all of them are displayed. |
| 134 | + * |
| 135 | + * Example: |
| 136 | + * |
| 137 | + * ``` |
| 138 | + * // match against a context variable |
| 139 | + * <template [ng-switch-when]="contextVariable">...</template> |
| 140 | + * |
| 141 | + * // match against a constant string |
| 142 | + * <template [ng-switch-when]="'stringValue'">...</template> |
| 143 | + * ``` |
| 144 | + */ |
| 145 | +@Template({ |
| 146 | + selector: '[ng-switch-when]', |
| 147 | + bind: { |
| 148 | + 'ng-switch-when' : 'when' |
| 149 | + } |
| 150 | +}) |
| 151 | +export class NgSwitchWhen { |
| 152 | + _value: any; |
| 153 | + _ngSwitch: NgSwitch; |
| 154 | + _viewPort: ViewPort; |
| 155 | + |
| 156 | + constructor(el: NgElement, viewPort: ViewPort, @Parent() ngSwitch: NgSwitch) { |
| 157 | + // `_whenDefault` is used as a marker for a not yet initialized value |
| 158 | + this._value = _whenDefault; |
| 159 | + this._ngSwitch = ngSwitch; |
| 160 | + this._viewPort = viewPort; |
| 161 | + } |
| 162 | + |
| 163 | + set when(value) { |
| 164 | + this._ngSwitch._onWhenValueChanged(this._value, value, this._viewPort); |
| 165 | + this._value = value; |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | + |
| 170 | +/** |
| 171 | + * Defines a default case statement. |
| 172 | + * |
| 173 | + * Default case statements are displayed when no `NgSwitchWhen` match the `ngSwitch` value. |
| 174 | + * |
| 175 | + * Example: |
| 176 | + * |
| 177 | + * ``` |
| 178 | + * <template [ng-switch-default]>...</template> |
| 179 | + * ``` |
| 180 | + */ |
| 181 | +@Template({ |
| 182 | + selector: '[ng-switch-default]' |
| 183 | +}) |
| 184 | +export class NgSwitchDefault { |
| 185 | + constructor(viewPort: ViewPort, @Parent() ngSwitch: NgSwitch) { |
| 186 | + ngSwitch._registerViewPort(_whenDefault, viewPort); |
| 187 | + } |
| 188 | +} |
| 189 | + |
| 190 | +var _whenDefault = new Object(); |
0 commit comments