OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 | 4 |
5 part of polymer; | 5 part of polymer; |
6 | 6 |
7 /** | 7 /** |
8 * Use this annotation to publish a field as an attribute. For example: | 8 * Use this annotation to publish a field as an attribute. For example: |
9 * | 9 * |
10 * class MyPlaybackElement extends PolymerElement { | 10 * class MyPlaybackElement extends PolymerElement { |
(...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
335 // TODO(jmesserly): this should probably take a ClassMirror instead of | 335 // TODO(jmesserly): this should probably take a ClassMirror instead of |
336 // TypeMirror, but it is currently impossible to get from a TypeMirror to a | 336 // TypeMirror, but it is currently impossible to get from a TypeMirror to a |
337 // ClassMirror. | 337 // ClassMirror. |
338 Object deserializeValue(String value, Object defaultValue, TypeMirror type) => | 338 Object deserializeValue(String value, Object defaultValue, TypeMirror type) => |
339 deserialize.deserializeValue(value, defaultValue, type); | 339 deserialize.deserializeValue(value, defaultValue, type); |
340 | 340 |
341 String serializeValue(Object value, TypeMirror inferredType) { | 341 String serializeValue(Object value, TypeMirror inferredType) { |
342 if (value == null) return null; | 342 if (value == null) return null; |
343 | 343 |
344 final type = inferredType.qualifiedName; | 344 final type = inferredType.qualifiedName; |
345 if (type == const Symbol('dart.core.bool')) { | 345 if (type == #dart.core.bool) { |
346 return _toBoolean(value) ? '' : null; | 346 return _toBoolean(value) ? '' : null; |
347 } else if (type == const Symbol('dart.core.String') | 347 } else if (type == #dart.core.String |
348 || type == const Symbol('dart.core.int') | 348 || type == #dart.core.int |
349 || type == const Symbol('dart.core.double')) { | 349 || type == #dart.core.double) { |
350 return '$value'; | 350 return '$value'; |
351 } | 351 } |
352 return null; | 352 return null; |
353 } | 353 } |
354 | 354 |
355 void reflectPropertyToAttribute(String name) { | 355 void reflectPropertyToAttribute(String name) { |
356 // TODO(sjmiles): consider memoizing this | 356 // TODO(sjmiles): consider memoizing this |
357 final self = reflect(this); | 357 final self = reflect(this); |
358 // try to intelligently serialize property value | 358 // try to intelligently serialize property value |
359 // TODO(jmesserly): cache symbol? | 359 // TODO(jmesserly): cache symbol? |
360 final propValue = self.getField(new Symbol(name)).reflectee; | 360 final propValue = self.getField(new Symbol(name)).reflectee; |
361 final property = _declaration._publish[name]; | 361 final property = _declaration._publish[name]; |
362 var inferredType = _inferPropertyType(propValue, property); | 362 var inferredType = _inferPropertyType(propValue, property); |
363 final serializedValue = serializeValue(propValue, inferredType); | 363 final serializedValue = serializeValue(propValue, inferredType); |
364 // boolean properties must reflect as boolean attributes | 364 // boolean properties must reflect as boolean attributes |
365 if (serializedValue != null) { | 365 if (serializedValue != null) { |
366 attributes[name] = serializedValue; | 366 attributes[name] = serializedValue; |
367 // TODO(sorvell): we should remove attr for all properties | 367 // TODO(sorvell): we should remove attr for all properties |
368 // that have undefined serialization; however, we will need to | 368 // that have undefined serialization; however, we will need to |
369 // refine the attr reflection system to achieve this; pica, for example, | 369 // refine the attr reflection system to achieve this; pica, for example, |
370 // relies on having inferredType object properties not removed as | 370 // relies on having inferredType object properties not removed as |
371 // attrs. | 371 // attrs. |
372 } else if (inferredType.qualifiedName == const Symbol('dart.core.bool')) { | 372 } else if (inferredType.qualifiedName == #dart.core.bool) { |
373 attributes.remove(name); | 373 attributes.remove(name); |
374 } | 374 } |
375 } | 375 } |
376 | 376 |
377 /** | 377 /** |
378 * Creates the document fragment to use for each instance of the custom | 378 * Creates the document fragment to use for each instance of the custom |
379 * element, given the `<template>` node. By default this is equivalent to: | 379 * element, given the `<template>` node. By default this is equivalent to: |
380 * | 380 * |
381 * template.createInstance(this, polymerSyntax); | 381 * template.createInstance(this, polymerSyntax); |
382 * | 382 * |
(...skipping 528 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
911 | 911 |
912 bool _toBoolean(value) => null != value && false != value; | 912 bool _toBoolean(value) => null != value && false != value; |
913 | 913 |
914 TypeMirror _propertyType(DeclarationMirror property) => | 914 TypeMirror _propertyType(DeclarationMirror property) => |
915 property is VariableMirror | 915 property is VariableMirror |
916 ? (property as VariableMirror).type | 916 ? (property as VariableMirror).type |
917 : (property as MethodMirror).returnType; | 917 : (property as MethodMirror).returnType; |
918 | 918 |
919 TypeMirror _inferPropertyType(Object value, DeclarationMirror property) { | 919 TypeMirror _inferPropertyType(Object value, DeclarationMirror property) { |
920 var type = _propertyType(property); | 920 var type = _propertyType(property); |
921 if (type.qualifiedName == const Symbol('dart.core.Object') || | 921 if (type.qualifiedName == #dart.core.Object || |
922 type.qualifiedName == const Symbol('dynamic')) { | 922 type.qualifiedName == #dynamic) { |
923 // Attempt to infer field type from the default value. | 923 // Attempt to infer field type from the default value. |
924 if (value != null) { | 924 if (value != null) { |
925 type = reflect(value).type; | 925 type = reflect(value).type; |
926 } | 926 } |
927 } | 927 } |
928 return type; | 928 return type; |
929 } | 929 } |
930 | 930 |
931 final Logger _observeLog = new Logger('polymer.observe'); | 931 final Logger _observeLog = new Logger('polymer.observe'); |
932 final Logger _eventsLog = new Logger('polymer.events'); | 932 final Logger _eventsLog = new Logger('polymer.events'); |
933 final Logger _unbindLog = new Logger('polymer.unbind'); | 933 final Logger _unbindLog = new Logger('polymer.unbind'); |
934 final Logger _bindLog = new Logger('polymer.bind'); | 934 final Logger _bindLog = new Logger('polymer.bind'); |
935 | 935 |
936 final Expando _shadowHost = new Expando<Element>(); | 936 final Expando _shadowHost = new Expando<Element>(); |
937 | 937 |
938 final Expando _eventHandledTable = new Expando<Set<Node>>(); | 938 final Expando _eventHandledTable = new Expando<Set<Node>>(); |
OLD | NEW |