|
| 1 | +library angular2.src.compiler.change_definition_factory; |
| 2 | + |
| 3 | +import "package:angular2/src/facade/collection.dart" |
| 4 | + show ListWrapper, StringMapWrapper; |
| 5 | +import "package:angular2/src/facade/lang.dart" show isPresent, isBlank; |
| 6 | +import "package:angular2/src/core/reflection/reflection.dart" show reflector; |
| 7 | +import "package:angular2/src/core/change_detection/change_detection.dart" |
| 8 | + show |
| 9 | + DirectiveIndex, |
| 10 | + BindingRecord, |
| 11 | + DirectiveRecord, |
| 12 | + ChangeDetectionStrategy, |
| 13 | + ChangeDetectorDefinition, |
| 14 | + ChangeDetectorGenConfig, |
| 15 | + ASTWithSource; |
| 16 | +import "directive_metadata.dart" |
| 17 | + show CompileDirectiveMetadata, CompileTypeMetadata; |
| 18 | +import "template_ast.dart" |
| 19 | + show |
| 20 | + TemplateAst, |
| 21 | + ElementAst, |
| 22 | + BoundTextAst, |
| 23 | + PropertyBindingType, |
| 24 | + DirectiveAst, |
| 25 | + TemplateAstVisitor, |
| 26 | + templateVisitAll, |
| 27 | + NgContentAst, |
| 28 | + EmbeddedTemplateAst, |
| 29 | + VariableAst, |
| 30 | + BoundElementPropertyAst, |
| 31 | + BoundEventAst, |
| 32 | + BoundDirectivePropertyAst, |
| 33 | + AttrAst, |
| 34 | + TextAst; |
| 35 | +import "package:angular2/src/core/linker/interfaces.dart" show LifecycleHooks; |
| 36 | + |
| 37 | +List<ChangeDetectorDefinition> createChangeDetectorDefinitions( |
| 38 | + CompileTypeMetadata componentType, |
| 39 | + ChangeDetectionStrategy componentStrategy, |
| 40 | + ChangeDetectorGenConfig genConfig, |
| 41 | + List<TemplateAst> parsedTemplate) { |
| 42 | + var pvVisitors = []; |
| 43 | + var visitor = new ProtoViewVisitor(null, pvVisitors, componentStrategy); |
| 44 | + templateVisitAll(visitor, parsedTemplate); |
| 45 | + return createChangeDefinitions(pvVisitors, componentType, genConfig); |
| 46 | +} |
| 47 | + |
| 48 | +class ProtoViewVisitor implements TemplateAstVisitor { |
| 49 | + ProtoViewVisitor parent; |
| 50 | + List<ProtoViewVisitor> allVisitors; |
| 51 | + ChangeDetectionStrategy strategy; |
| 52 | + num viewIndex; |
| 53 | + num nodeCount = 0; |
| 54 | + num boundElementCount = 0; |
| 55 | + List<String> variableNames = []; |
| 56 | + List<BindingRecord> bindingRecords = []; |
| 57 | + List<BindingRecord> eventRecords = []; |
| 58 | + List<DirectiveRecord> directiveRecords = []; |
| 59 | + ProtoViewVisitor(this.parent, this.allVisitors, this.strategy) { |
| 60 | + this.viewIndex = allVisitors.length; |
| 61 | + allVisitors.add(this); |
| 62 | + } |
| 63 | + dynamic visitEmbeddedTemplate(EmbeddedTemplateAst ast, dynamic context) { |
| 64 | + this.nodeCount++; |
| 65 | + this.boundElementCount++; |
| 66 | + templateVisitAll(this, ast.outputs); |
| 67 | + for (var i = 0; i < ast.directives.length; i++) { |
| 68 | + ast.directives[i].visit(this, i); |
| 69 | + } |
| 70 | + var childVisitor = new ProtoViewVisitor( |
| 71 | + this, this.allVisitors, ChangeDetectionStrategy.Default); |
| 72 | + // Attention: variables present on an embedded template count towards |
| 73 | + |
| 74 | + // the embedded template and not the template anchor! |
| 75 | + templateVisitAll(childVisitor, ast.vars); |
| 76 | + templateVisitAll(childVisitor, ast.children); |
| 77 | + return null; |
| 78 | + } |
| 79 | + |
| 80 | + dynamic visitElement(ElementAst ast, dynamic context) { |
| 81 | + this.nodeCount++; |
| 82 | + if (ast.isBound()) { |
| 83 | + this.boundElementCount++; |
| 84 | + } |
| 85 | + templateVisitAll(this, ast.inputs, null); |
| 86 | + templateVisitAll(this, ast.outputs); |
| 87 | + templateVisitAll(this, ast.exportAsVars); |
| 88 | + for (var i = 0; i < ast.directives.length; i++) { |
| 89 | + ast.directives[i].visit(this, i); |
| 90 | + } |
| 91 | + templateVisitAll(this, ast.children); |
| 92 | + return null; |
| 93 | + } |
| 94 | + |
| 95 | + dynamic visitNgContent(NgContentAst ast, dynamic context) { |
| 96 | + return null; |
| 97 | + } |
| 98 | + |
| 99 | + dynamic visitVariable(VariableAst ast, dynamic context) { |
| 100 | + this.variableNames.add(ast.name); |
| 101 | + return null; |
| 102 | + } |
| 103 | + |
| 104 | + dynamic visitEvent(BoundEventAst ast, DirectiveRecord directiveRecord) { |
| 105 | + var bindingRecord = isPresent(directiveRecord) |
| 106 | + ? BindingRecord.createForHostEvent( |
| 107 | + ast.handler, ast.fullName, directiveRecord) |
| 108 | + : BindingRecord.createForEvent( |
| 109 | + ast.handler, ast.fullName, this.boundElementCount - 1); |
| 110 | + this.eventRecords.add(bindingRecord); |
| 111 | + return null; |
| 112 | + } |
| 113 | + |
| 114 | + dynamic visitElementProperty( |
| 115 | + BoundElementPropertyAst ast, DirectiveRecord directiveRecord) { |
| 116 | + var boundElementIndex = this.boundElementCount - 1; |
| 117 | + var dirIndex = |
| 118 | + isPresent(directiveRecord) ? directiveRecord.directiveIndex : null; |
| 119 | + var bindingRecord; |
| 120 | + if (identical(ast.type, PropertyBindingType.Property)) { |
| 121 | + bindingRecord = isPresent(dirIndex) |
| 122 | + ? BindingRecord.createForHostProperty(dirIndex, ast.value, ast.name) |
| 123 | + : BindingRecord.createForElementProperty( |
| 124 | + ast.value, boundElementIndex, ast.name); |
| 125 | + } else if (identical(ast.type, PropertyBindingType.Attribute)) { |
| 126 | + bindingRecord = isPresent(dirIndex) |
| 127 | + ? BindingRecord.createForHostAttribute(dirIndex, ast.value, ast.name) |
| 128 | + : BindingRecord.createForElementAttribute( |
| 129 | + ast.value, boundElementIndex, ast.name); |
| 130 | + } else if (identical(ast.type, PropertyBindingType.Class)) { |
| 131 | + bindingRecord = isPresent(dirIndex) |
| 132 | + ? BindingRecord.createForHostClass(dirIndex, ast.value, ast.name) |
| 133 | + : BindingRecord.createForElementClass( |
| 134 | + ast.value, boundElementIndex, ast.name); |
| 135 | + } else if (identical(ast.type, PropertyBindingType.Style)) { |
| 136 | + bindingRecord = isPresent(dirIndex) |
| 137 | + ? BindingRecord.createForHostStyle( |
| 138 | + dirIndex, ast.value, ast.name, ast.unit) |
| 139 | + : BindingRecord.createForElementStyle( |
| 140 | + ast.value, boundElementIndex, ast.name, ast.unit); |
| 141 | + } |
| 142 | + this.bindingRecords.add(bindingRecord); |
| 143 | + return null; |
| 144 | + } |
| 145 | + |
| 146 | + dynamic visitAttr(AttrAst ast, dynamic context) { |
| 147 | + return null; |
| 148 | + } |
| 149 | + |
| 150 | + dynamic visitBoundText(BoundTextAst ast, dynamic context) { |
| 151 | + var nodeIndex = this.nodeCount++; |
| 152 | + this |
| 153 | + .bindingRecords |
| 154 | + .add(BindingRecord.createForTextNode(ast.value, nodeIndex)); |
| 155 | + return null; |
| 156 | + } |
| 157 | + |
| 158 | + dynamic visitText(TextAst ast, dynamic context) { |
| 159 | + this.nodeCount++; |
| 160 | + return null; |
| 161 | + } |
| 162 | + |
| 163 | + dynamic visitDirective(DirectiveAst ast, num directiveIndexAsNumber) { |
| 164 | + var directiveIndex = |
| 165 | + new DirectiveIndex(this.boundElementCount - 1, directiveIndexAsNumber); |
| 166 | + var directiveMetadata = ast.directive; |
| 167 | + var outputsArray = []; |
| 168 | + StringMapWrapper.forEach( |
| 169 | + ast.directive.outputs, |
| 170 | + (String eventName, String dirProperty) => |
| 171 | + outputsArray.add([dirProperty, eventName])); |
| 172 | + var directiveRecord = new DirectiveRecord( |
| 173 | + directiveIndex: directiveIndex, |
| 174 | + callAfterContentInit: !identical( |
| 175 | + directiveMetadata.lifecycleHooks |
| 176 | + .indexOf(LifecycleHooks.AfterContentInit), |
| 177 | + -1), |
| 178 | + callAfterContentChecked: !identical( |
| 179 | + directiveMetadata.lifecycleHooks |
| 180 | + .indexOf(LifecycleHooks.AfterContentChecked), |
| 181 | + -1), |
| 182 | + callAfterViewInit: !identical( |
| 183 | + directiveMetadata.lifecycleHooks |
| 184 | + .indexOf(LifecycleHooks.AfterViewInit), |
| 185 | + -1), |
| 186 | + callAfterViewChecked: !identical( |
| 187 | + directiveMetadata.lifecycleHooks |
| 188 | + .indexOf(LifecycleHooks.AfterViewChecked), |
| 189 | + -1), |
| 190 | + callOnChanges: !identical( |
| 191 | + directiveMetadata.lifecycleHooks.indexOf(LifecycleHooks.OnChanges), |
| 192 | + -1), |
| 193 | + callDoCheck: !identical( |
| 194 | + directiveMetadata.lifecycleHooks.indexOf(LifecycleHooks.DoCheck), |
| 195 | + -1), |
| 196 | + callOnInit: !identical( |
| 197 | + directiveMetadata.lifecycleHooks.indexOf(LifecycleHooks.OnInit), |
| 198 | + -1), |
| 199 | + callOnDestroy: !identical( |
| 200 | + directiveMetadata.lifecycleHooks.indexOf(LifecycleHooks.OnDestroy), |
| 201 | + -1), |
| 202 | + changeDetection: directiveMetadata.changeDetection, |
| 203 | + outputs: outputsArray); |
| 204 | + this.directiveRecords.add(directiveRecord); |
| 205 | + templateVisitAll(this, ast.inputs, directiveRecord); |
| 206 | + var bindingRecords = this.bindingRecords; |
| 207 | + if (directiveRecord.callOnChanges) { |
| 208 | + bindingRecords |
| 209 | + .add(BindingRecord.createDirectiveOnChanges(directiveRecord)); |
| 210 | + } |
| 211 | + if (directiveRecord.callOnInit) { |
| 212 | + bindingRecords.add(BindingRecord.createDirectiveOnInit(directiveRecord)); |
| 213 | + } |
| 214 | + if (directiveRecord.callDoCheck) { |
| 215 | + bindingRecords.add(BindingRecord.createDirectiveDoCheck(directiveRecord)); |
| 216 | + } |
| 217 | + templateVisitAll(this, ast.hostProperties, directiveRecord); |
| 218 | + templateVisitAll(this, ast.hostEvents, directiveRecord); |
| 219 | + templateVisitAll(this, ast.exportAsVars); |
| 220 | + return null; |
| 221 | + } |
| 222 | + |
| 223 | + dynamic visitDirectiveProperty( |
| 224 | + BoundDirectivePropertyAst ast, DirectiveRecord directiveRecord) { |
| 225 | + // TODO: these setters should eventually be created by change detection, to make |
| 226 | + |
| 227 | + // it monomorphic! |
| 228 | + var setter = reflector.setter(ast.directiveName); |
| 229 | + this.bindingRecords.add(BindingRecord.createForDirective( |
| 230 | + ast.value, ast.directiveName, setter, directiveRecord)); |
| 231 | + return null; |
| 232 | + } |
| 233 | +} |
| 234 | + |
| 235 | +List<ChangeDetectorDefinition> createChangeDefinitions( |
| 236 | + List<ProtoViewVisitor> pvVisitors, |
| 237 | + CompileTypeMetadata componentType, |
| 238 | + ChangeDetectorGenConfig genConfig) { |
| 239 | + var pvVariableNames = _collectNestedProtoViewsVariableNames(pvVisitors); |
| 240 | + return pvVisitors.map((pvVisitor) { |
| 241 | + var id = '''${ componentType . name}_${ pvVisitor . viewIndex}'''; |
| 242 | + return new ChangeDetectorDefinition( |
| 243 | + id, |
| 244 | + pvVisitor.strategy, |
| 245 | + pvVariableNames[pvVisitor.viewIndex], |
| 246 | + pvVisitor.bindingRecords, |
| 247 | + pvVisitor.eventRecords, |
| 248 | + pvVisitor.directiveRecords, |
| 249 | + genConfig); |
| 250 | + }).toList(); |
| 251 | +} |
| 252 | + |
| 253 | +List<List<String>> _collectNestedProtoViewsVariableNames( |
| 254 | + List<ProtoViewVisitor> pvVisitors) { |
| 255 | + List<List<String>> nestedPvVariableNames = |
| 256 | + ListWrapper.createFixedSize(pvVisitors.length); |
| 257 | + pvVisitors.forEach((pv) { |
| 258 | + List<String> parentVariableNames = |
| 259 | + isPresent(pv.parent) ? nestedPvVariableNames[pv.parent.viewIndex] : []; |
| 260 | + nestedPvVariableNames[pv.viewIndex] = |
| 261 | + (new List.from(parentVariableNames)..addAll(pv.variableNames)); |
| 262 | + }); |
| 263 | + return nestedPvVariableNames; |
| 264 | +} |
0 commit comments