| 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 /** | 5 /** |
| 6 * A binding delegate used with Polymer elements that | 6 * A binding delegate used with Polymer elements that |
| 7 * allows for complex binding expressions, including | 7 * allows for complex binding expressions, including |
| 8 * property access, function invocation, | 8 * property access, function invocation, |
| 9 * list/map indexing, and two-way filtering. | 9 * list/map indexing, and two-way filtering. |
| 10 * | 10 * |
| 11 * When you install polymer.dart, | 11 * When you install polymer.dart, |
| 12 * polymer_expressions is automatically installed as well. | 12 * polymer_expressions is automatically installed as well. |
| 13 * | 13 * |
| 14 * Polymer expressions are part of the Polymer.dart project. | 14 * Polymer expressions are part of the Polymer.dart project. |
| 15 * Refer to the | 15 * Refer to the |
| 16 * [Polymer.dart](http://www.dartlang.org/polymer-dart/) | 16 * [Polymer.dart](http://www.dartlang.org/polymer-dart/) |
| 17 * homepage for example code, project status, and | 17 * homepage for example code, project status, and |
| 18 * information about how to get started using Polymer.dart in your apps. | 18 * information about how to get started using Polymer.dart in your apps. |
| 19 * | 19 * |
| 20 * ## Other resources | 20 * ## Other resources |
| 21 * | 21 * |
| 22 * The | 22 * The |
| 23 * [Polymer expressions](http://pub.dartlang.org/packages/polymer_expressions) | 23 * [Polymer expressions](http://pub.dartlang.org/packages/polymer_expressions) |
| 24 * pub repository contains detailed documentation about using polymer | 24 * pub repository contains detailed documentation about using polymer |
| 25 * expressions. | 25 * expressions. |
| 26 */ | 26 */ |
| 27 | 27 |
| 28 library polymer_expressions; | 28 library polymer_expressions; |
| 29 | 29 |
| 30 import 'dart:async'; | 30 import 'dart:async'; |
| 31 import 'dart:html'; | 31 import 'dart:html'; |
| 32 | 32 |
| 33 import 'package:observe/observe.dart'; | 33 import 'package:observe/observe.dart'; |
| 34 import 'package:logging/logging.dart'; |
| 34 | 35 |
| 35 import 'eval.dart'; | 36 import 'eval.dart'; |
| 36 import 'expression.dart'; | 37 import 'expression.dart'; |
| 37 import 'parser.dart'; | 38 import 'parser.dart'; |
| 38 | 39 |
| 40 final Logger _logger = new Logger('polymer_expressions'); |
| 41 |
| 39 // TODO(justin): Investigate XSS protection | 42 // TODO(justin): Investigate XSS protection |
| 40 Object _classAttributeConverter(v) => | 43 Object _classAttributeConverter(v) => |
| 41 (v is Map) ? v.keys.where((k) => v[k] == true).join(' ') : | 44 (v is Map) ? v.keys.where((k) => v[k] == true).join(' ') : |
| 42 (v is Iterable) ? v.join(' ') : | 45 (v is Iterable) ? v.join(' ') : |
| 43 v; | 46 v; |
| 44 | 47 |
| 45 Object _styleAttributeConverter(v) => | 48 Object _styleAttributeConverter(v) => |
| 46 (v is Map) ? v.keys.map((k) => '$k: ${v[k]}').join(';') : | 49 (v is Map) ? v.keys.map((k) => '$k: ${v[k]}').join(';') : |
| 47 (v is Iterable) ? v.join(';') : | 50 (v is Iterable) ? v.join(';') : |
| 48 v; | 51 v; |
| (...skipping 30 matching lines...) Expand all Loading... |
| 79 } | 82 } |
| 80 | 83 |
| 81 class _Binding extends Object with ChangeNotifierMixin { | 84 class _Binding extends Object with ChangeNotifierMixin { |
| 82 static const _VALUE = const Symbol('value'); | 85 static const _VALUE = const Symbol('value'); |
| 83 | 86 |
| 84 final Scope _scope; | 87 final Scope _scope; |
| 85 final ExpressionObserver _expr; | 88 final ExpressionObserver _expr; |
| 86 final _converter; | 89 final _converter; |
| 87 var _value; | 90 var _value; |
| 88 | 91 |
| 89 | |
| 90 _Binding(Expression expr, Scope scope, [this._converter]) | 92 _Binding(Expression expr, Scope scope, [this._converter]) |
| 91 : _expr = observe(expr, scope), | 93 : _expr = observe(expr, scope), |
| 92 _scope = scope { | 94 _scope = scope { |
| 93 _expr.onUpdate.listen(_setValue); | 95 _expr.onUpdate.listen(_setValue).onError((e) { |
| 94 _setValue(_expr.currentValue); | 96 _logger.warning("Error evaluating expression '$_expr': ${e.message}"); |
| 97 }); |
| 98 try { |
| 99 update(_expr, _scope); |
| 100 _setValue(_expr.currentValue); |
| 101 } on EvalException catch (e) { |
| 102 _logger.warning("Error evaluating expression '$_expr': ${e.message}"); |
| 103 } |
| 95 } | 104 } |
| 96 | 105 |
| 97 _setValue(v) { | 106 _setValue(v) { |
| 98 if (v is Comprehension) { | 107 if (v is Comprehension) { |
| 99 // convert the Comprehension into a list of scopes with the loop | 108 // convert the Comprehension into a list of scopes with the loop |
| 100 // variable added to the scope | 109 // variable added to the scope |
| 101 _value = v.iterable.map((i) { | 110 _value = v.iterable.map((i) { |
| 102 var vars = new Map(); | 111 var vars = new Map(); |
| 103 vars[v.identifier] = i; | 112 vars[v.identifier] = i; |
| 104 Scope childScope = new Scope(parent: _scope, variables: vars); | 113 Scope childScope = new Scope(parent: _scope, variables: vars); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 123 | 132 |
| 124 getValueWorkaround(key) { | 133 getValueWorkaround(key) { |
| 125 if (key == _VALUE) return value; | 134 if (key == _VALUE) return value; |
| 126 } | 135 } |
| 127 | 136 |
| 128 setValueWorkaround(key, v) { | 137 setValueWorkaround(key, v) { |
| 129 if (key == _VALUE) value = v; | 138 if (key == _VALUE) value = v; |
| 130 } | 139 } |
| 131 | 140 |
| 132 } | 141 } |
| OLD | NEW |