| OLD | NEW |
| 1 part of angular.directive; | 1 part of angular.directive; |
| 2 | 2 |
| 3 /** | 3 /** |
| 4 * The ngSwitch directive is used to conditionally swap DOM structure on your | 4 * The ngSwitch directive is used to conditionally swap DOM structure on your |
| 5 * template based on a scope expression. Elements within ngSwitch but without | 5 * template based on a scope expression. Elements within ngSwitch but without |
| 6 * ngSwitchWhen or ngSwitchDefault directives will be preserved at the location | 6 * ngSwitchWhen or ngSwitchDefault directives will be preserved at the location |
| 7 * as specified in the template. | 7 * as specified in the template. |
| 8 * | 8 * |
| 9 * The directive itself works similar to ngInclude, however, instead of | 9 * The directive itself works similar to ngInclude, however, instead of |
| 10 * downloading template code (or loading it from the template cache), ngSwitch | 10 * downloading template code (or loading it from the template cache), ngSwitch |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 * <div ng-switch-default>default</div> | 48 * <div ng-switch-default>default</div> |
| 49 * </div> | 49 * </div> |
| 50 * </div> | 50 * </div> |
| 51 */ | 51 */ |
| 52 @NgDirective( | 52 @NgDirective( |
| 53 selector: '[ng-switch]', | 53 selector: '[ng-switch]', |
| 54 map: const { | 54 map: const { |
| 55 'ng-switch': '=>value', | 55 'ng-switch': '=>value', |
| 56 'change': '&onChange' | 56 'change': '&onChange' |
| 57 }, | 57 }, |
| 58 visibility: NgDirective.DIRECT_CHILDREN_VISIBILITY | 58 visibility: NgDirective.DIRECT_CHILDREN_VISIBILITY) |
| 59 ) | |
| 60 class NgSwitchDirective { | 59 class NgSwitchDirective { |
| 61 Map<String, List<_Case>> cases = new Map<String, List<_Case>>(); | 60 Map<String, List<_Case>> cases = new Map<String, List<_Case>>(); |
| 62 List<_BlockScopePair> currentBlocks = <_BlockScopePair>[]; | 61 List<_BlockScopePair> currentBlocks = <_BlockScopePair>[]; |
| 63 Function onChange; | 62 Function onChange; |
| 64 Scope scope; | 63 final Scope scope; |
| 65 | 64 |
| 66 NgSwitchDirective(this.scope) { | 65 NgSwitchDirective(this.scope) { |
| 67 cases['?'] = <_Case>[]; | 66 cases['?'] = <_Case>[]; |
| 68 } | 67 } |
| 69 | 68 |
| 70 addCase(String value, BlockHole anchor, BoundBlockFactory blockFactory) { | 69 addCase(String value, BlockHole anchor, BoundBlockFactory blockFactory) { |
| 71 cases.putIfAbsent(value, () => <_Case>[]); | 70 cases.putIfAbsent(value, () => <_Case>[]); |
| 72 cases[value].add(new _Case(anchor, blockFactory)); | 71 cases[value].add(new _Case(anchor, blockFactory)); |
| 73 } | 72 } |
| 74 | 73 |
| 75 set value(val) { | 74 set value(val) { |
| 76 currentBlocks | 75 currentBlocks |
| 77 ..forEach((_BlockScopePair pair) { | 76 ..forEach((_BlockScopePair pair) { |
| 78 pair.block.remove(); | 77 pair.block.remove(); |
| 79 pair.scope.$destroy(); | 78 pair.scope.destroy(); |
| 80 }) | 79 }) |
| 81 ..clear(); | 80 ..clear(); |
| 82 | 81 |
| 83 val = '!$val'; | 82 val = '!$val'; |
| 84 (cases.containsKey(val) ? cases[val] : cases['?']) | 83 (cases.containsKey(val) ? cases[val] : cases['?']) |
| 85 .forEach((_Case caze) { | 84 .forEach((_Case caze) { |
| 86 Scope childScope = scope.$new(); | 85 Scope childScope = scope.createChild(new PrototypeMap(scope.context)); |
| 87 var block = caze.blockFactory(childScope) | 86 var block = caze.blockFactory(childScope)..insertAfter(caze.anchor); |
| 88 ..insertAfter(caze.anchor); | |
| 89 currentBlocks.add(new _BlockScopePair(block, childScope)); | 87 currentBlocks.add(new _BlockScopePair(block, childScope)); |
| 90 }); | 88 }); |
| 91 if (onChange != null) { | 89 if (onChange != null) { |
| 92 onChange(); | 90 onChange(); |
| 93 } | 91 } |
| 94 } | 92 } |
| 95 } | 93 } |
| 96 | 94 |
| 97 class _BlockScopePair { | 95 class _BlockScopePair { |
| 98 final Block block; | 96 final Block block; |
| 99 final Scope scope; | 97 final Scope scope; |
| 100 | 98 |
| 101 _BlockScopePair(this.block, this.scope); | 99 _BlockScopePair(this.block, this.scope); |
| 102 } | 100 } |
| 103 | 101 |
| 104 class _Case { | 102 class _Case { |
| 105 final BlockHole anchor; | 103 final BlockHole anchor; |
| 106 final BoundBlockFactory blockFactory; | 104 final BoundBlockFactory blockFactory; |
| 107 | 105 |
| 108 _Case(this.anchor, this.blockFactory); | 106 _Case(this.anchor, this.blockFactory); |
| 109 } | 107 } |
| 110 | 108 |
| 111 @NgDirective( | 109 @NgDirective( |
| 112 selector: '[ng-switch-when]', | 110 selector: '[ng-switch-when]', |
| 113 children: NgAnnotation.TRANSCLUDE_CHILDREN, | 111 children: NgAnnotation.TRANSCLUDE_CHILDREN, |
| 114 map: const { | 112 map: const {'.': '@value'}) |
| 115 '.': '@value' | |
| 116 } | |
| 117 ) | |
| 118 class NgSwitchWhenDirective { | 113 class NgSwitchWhenDirective { |
| 119 final NgSwitchDirective ngSwitch; | 114 final NgSwitchDirective ngSwitch; |
| 120 final BlockHole hole; | 115 final BlockHole hole; |
| 121 final BoundBlockFactory blockFactory; | 116 final BoundBlockFactory blockFactory; |
| 122 final Scope scope; | 117 final Scope scope; |
| 123 | 118 |
| 124 NgSwitchWhenDirective(this.ngSwitch, this.hole, this.blockFactory, this.scope)
; | 119 NgSwitchWhenDirective(this.ngSwitch, this.hole, this.blockFactory, this.scope)
; |
| 125 | 120 |
| 126 set value(String value) => | 121 set value(String value) => ngSwitch.addCase('!$value', hole, blockFactory); |
| 127 ngSwitch.addCase('!$value', hole, blockFactory); | |
| 128 } | 122 } |
| 129 | 123 |
| 130 | 124 |
| 131 @NgDirective( | 125 @NgDirective( |
| 132 children: NgAnnotation.TRANSCLUDE_CHILDREN, | 126 children: NgAnnotation.TRANSCLUDE_CHILDREN, |
| 133 selector: '[ng-switch-default]' | 127 selector: '[ng-switch-default]') |
| 134 ) | |
| 135 class NgSwitchDefaultDirective { | 128 class NgSwitchDefaultDirective { |
| 136 | 129 |
| 137 NgSwitchDefaultDirective(NgSwitchDirective ngSwitch, BlockHole hole, | 130 NgSwitchDefaultDirective(NgSwitchDirective ngSwitch, BlockHole hole, |
| 138 BoundBlockFactory blockFactory, Scope scope) { | 131 BoundBlockFactory blockFactory, Scope scope) { |
| 139 ngSwitch.addCase('?', hole, blockFactory); | 132 ngSwitch.addCase('?', hole, blockFactory); |
| 140 } | 133 } |
| 141 } | 134 } |
| OLD | NEW |