OLD | NEW |
---|---|
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 library polymer.src.micro.properties; | 4 library polymer.src.micro.properties; |
5 | 5 |
6 import 'dart:js'; | 6 import 'dart:js'; |
7 import 'package:reflectable/reflectable.dart'; | 7 import 'package:reflectable/reflectable.dart'; |
8 import 'behavior.dart'; | |
8 import 'declarations.dart'; | 9 import 'declarations.dart'; |
9 import 'js_proxy.dart'; | 10 import 'js_proxy.dart'; |
10 import 'property.dart'; | 11 import 'property.dart'; |
11 import 'event_handler.dart'; | 12 import 'event_handler.dart'; |
12 import 'listen.dart'; | 13 import 'listen.dart'; |
13 import 'observe.dart'; | 14 import 'observe.dart'; |
14 import 'polymer_register.dart'; | 15 import 'polymer_register.dart'; |
15 | 16 |
16 /// Creates a javascript object which can be passed to polymer js to register | 17 /// Creates a javascript object which can be passed to polymer js to register |
17 /// an element, given a dart [Type] and a [PolymerRegister] annotation. | 18 /// an element, given a dart [Type] and a [PolymerRegister] annotation. |
18 JsObject createPolymerDescriptor(Type type, PolymerRegister annotation) { | 19 JsObject createPolymerDescriptor(Type type, PolymerRegister annotation) { |
19 var object = { | 20 var object = { |
20 'is': annotation.tagName, | 21 'is': annotation.tagName, |
21 'extends': annotation.extendsTag, | 22 'extends': annotation.extendsTag, |
22 'hostAttributes': annotation.hostAttributes, | 23 'hostAttributes': annotation.hostAttributes, |
23 'properties': buildPropertiesObject(type), | 24 'properties': _buildPropertiesObject(type), |
24 'observers': _buildObserversObject(type), | 25 'observers': _buildObserversObject(type), |
25 'listeners': _buildListenersObject(type), | 26 'listeners': _buildListenersObject(type), |
27 'behaviors': _buildBehaviorsList(type), | |
26 '__isPolymerDart__': true, | 28 '__isPolymerDart__': true, |
27 }; | 29 }; |
28 _setupLifecycleMethods(type, object); | 30 _setupLifecycleMethods(type, object); |
29 _setupEventHandlerMethods(type, object); | 31 _setupEventHandlerMethods(type, object); |
30 | 32 |
31 return new JsObject.jsify(object); | 33 return new JsObject.jsify(object); |
32 } | 34 } |
33 | 35 |
34 /// Custom js object containing some helper methods for dart. | 36 /// Custom js object containing some helper methods for dart. |
35 final JsObject _polymerDart = context['Polymer']['Dart']; | 37 final JsObject _polymerDart = context['Polymer']['Dart']; |
36 | 38 |
37 /// Returns a list of [DeclarationMirror]s for all fields annotated as a | 39 /// Returns a list of [DeclarationMirror]s for all fields annotated as a |
38 /// [Property]. | 40 /// [Property]. |
39 Map<String, DeclarationMirror> propertyDeclarationsFor(Type type) { | 41 Map<String, DeclarationMirror> propertyDeclarationsFor(Type type) { |
40 return declarationsFor(type, jsProxyReflectable, where: (name, declaration) { | 42 return declarationsFor(type, jsProxyReflectable, where: (name, declaration) { |
41 if (isRegularMethod(declaration) || isSetter(declaration)) return false; | 43 if (isRegularMethod(declaration) || isSetter(declaration)) return false; |
42 return declaration.metadata.any((d) => d is Property); | 44 return declaration.metadata.any((d) => d is Property); |
43 }); | 45 }); |
44 } | 46 } |
45 | 47 |
46 // Set up the `properties` descriptor object. | 48 // Set up the `properties` descriptor object. |
47 Map buildPropertiesObject(Type type) { | 49 Map _buildPropertiesObject(Type type) { |
48 var declarations = propertyDeclarationsFor(type); | 50 var declarations = propertyDeclarationsFor(type); |
49 var properties = {}; | 51 var properties = {}; |
50 declarations.forEach((String name, DeclarationMirror declaration) { | 52 declarations.forEach((String name, DeclarationMirror declaration) { |
51 // Build a properties object for this property. | 53 // Build a properties object for this property. |
52 properties[name] = _getPropertyInfoForType(type, declaration); | 54 properties[name] = _getPropertyInfoForType(type, declaration); |
53 }); | 55 }); |
54 return properties; | 56 return properties; |
55 } | 57 } |
56 | 58 |
57 /// All @Observe annotated methods. | 59 /// All @Observe annotated methods. |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
180 var instanceMirror = jsProxyReflectable.reflect(dartInstance); | 182 var instanceMirror = jsProxyReflectable.reflect(dartInstance); |
181 return jsValue(instanceMirror.invokeGetter(declaration.simpleName)); | 183 return jsValue(instanceMirror.invokeGetter(declaration.simpleName)); |
182 }), | 184 }), |
183 }; | 185 }; |
184 if (isFinal) { | 186 if (isFinal) { |
185 property['readOnly'] = true; | 187 property['readOnly'] = true; |
186 } | 188 } |
187 return property; | 189 return property; |
188 } | 190 } |
189 | 191 |
192 /// List of [JsObjects]s representing the behaviors for an element. | |
193 List<JsObject> _buildBehaviorsList(Type type) { | |
194 var behaviors = <JsObject>[]; | |
195 | |
196 isBehavior(instance) => instance is BehaviorInterface; | |
197 | |
198 var interfaces = mixinsFor(type, jsProxyReflectable, | |
Siggi Cherem (dart-lang)
2015/08/12 22:43:15
Seems like we are visiting each behavior annotatio
jakemac
2015/08/13 17:50:45
I didn't add mixinForEach, but I did modify it to
| |
199 where: (ClassMirror mixin) => mixin.metadata.any(isBehavior)); | |
200 for (var interface in interfaces) { | |
201 behaviors.add( | |
202 (interface.metadata.firstWhere(isBehavior) as BehaviorInterface) | |
203 .getBehavior(interface.reflectedType)); | |
204 } | |
205 | |
206 return behaviors.isEmpty ? null : behaviors; | |
207 } | |
208 | |
190 /// Given a [Type] return the [JsObject] representation of that type. | 209 /// Given a [Type] return the [JsObject] representation of that type. |
191 /// TODO(jakemac): Make this more robust, specifically around Lists. | 210 /// TODO(jakemac): Make this more robust, specifically around Lists. |
192 dynamic jsType(Type type) { | 211 dynamic jsType(Type type) { |
193 var typeString = '$type'; | 212 var typeString = '$type'; |
194 if (typeString.startsWith('JsArray<')) typeString = 'List'; | 213 if (typeString.startsWith('JsArray<')) typeString = 'List'; |
195 if (typeString.startsWith('List<')) typeString = 'List'; | 214 if (typeString.startsWith('List<')) typeString = 'List'; |
196 if (typeString.startsWith('Map<')) typeString = 'Map'; | 215 if (typeString.startsWith('Map<')) typeString = 'Map'; |
197 switch (typeString) { | 216 switch (typeString) { |
198 case 'int': | 217 case 'int': |
199 case 'double': | 218 case 'double': |
200 case 'num': | 219 case 'num': |
201 return context['Number']; | 220 return context['Number']; |
202 case 'bool': | 221 case 'bool': |
203 return context['Boolean']; | 222 return context['Boolean']; |
204 case 'List': | 223 case 'List': |
205 case 'JsArray': | 224 case 'JsArray': |
206 return context['Array']; | 225 return context['Array']; |
207 case 'DateTime': | 226 case 'DateTime': |
208 return context['Date']; | 227 return context['Date']; |
209 case 'String': | 228 case 'String': |
210 return context['String']; | 229 return context['String']; |
211 case 'Map': | 230 case 'Map': |
212 case 'JsObject': | 231 case 'JsObject': |
213 return context['Object']; | 232 return context['Object']; |
214 default: | 233 default: |
215 // Just return the Dart type | 234 // Just return the Dart type |
216 return type; | 235 return type; |
217 } | 236 } |
218 } | 237 } |
OLD | NEW |