| 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 * |
| (...skipping 12 matching lines...) Expand all Loading... |
| 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:logging/logging.dart'; | |
| 34 import 'package:observe/observe.dart'; | 33 import 'package:observe/observe.dart'; |
| 35 import 'package:template_binding/template_binding.dart'; | 34 import 'package:template_binding/template_binding.dart'; |
| 36 | 35 |
| 37 import 'eval.dart'; | 36 import 'eval.dart'; |
| 38 import 'expression.dart'; | 37 import 'expression.dart'; |
| 39 import 'parser.dart'; | 38 import 'parser.dart'; |
| 40 import 'src/globals.dart'; | 39 import 'src/globals.dart'; |
| 41 | 40 |
| 42 final Logger _logger = new Logger('polymer_expressions'); | |
| 43 | |
| 44 // TODO(justin): Investigate XSS protection | 41 // TODO(justin): Investigate XSS protection |
| 45 Object _classAttributeConverter(v) => | 42 Object _classAttributeConverter(v) => |
| 46 (v is Map) ? v.keys.where((k) => v[k] == true).join(' ') : | 43 (v is Map) ? v.keys.where((k) => v[k] == true).join(' ') : |
| 47 (v is Iterable) ? v.join(' ') : | 44 (v is Iterable) ? v.join(' ') : |
| 48 v; | 45 v; |
| 49 | 46 |
| 50 Object _styleAttributeConverter(v) => | 47 Object _styleAttributeConverter(v) => |
| 51 (v is Map) ? v.keys.map((k) => '$k: ${v[k]}').join(';') : | 48 (v is Map) ? v.keys.map((k) => '$k: ${v[k]}').join(';') : |
| 52 (v is Iterable) ? v.join(';') : | 49 (v is Iterable) ? v.join(';') : |
| 53 v; | 50 v; |
| 54 | 51 |
| 55 class PolymerExpressions extends BindingDelegate { | 52 class PolymerExpressions extends BindingDelegate { |
| 56 /** The default [globals] to use for Polymer expressions. */ | 53 /** The default [globals] to use for Polymer expressions. */ |
| 57 static const Map DEFAULT_GLOBALS = const { 'enumerate': enumerate }; | 54 static const Map DEFAULT_GLOBALS = const { 'enumerate': enumerate }; |
| 58 | 55 |
| 59 final Map<String, Object> globals; | 56 final Map<String, Object> globals; |
| 60 | 57 |
| 61 /** | 58 /** |
| 62 * Creates a new binding delegate for Polymer expressions, with the provided | 59 * Creates a new binding delegate for Polymer expressions, with the provided |
| 63 * variables used as [globals]. If no globals are supplied, a copy of the | 60 * variables used as [globals]. If no globals are supplied, a copy of the |
| 64 * [DEFAULT_GLOBALS] will be used. | 61 * [DEFAULT_GLOBALS] will be used. |
| 65 */ | 62 */ |
| 66 PolymerExpressions({Map<String, Object> globals}) | 63 PolymerExpressions({Map<String, Object> globals}) |
| 67 : globals = (globals == null) ? | 64 : globals = globals == null ? |
| 68 new Map<String, Object>.from(DEFAULT_GLOBALS) : globals; | 65 new Map<String, Object>.from(DEFAULT_GLOBALS) : globals; |
| 69 | 66 |
| 70 prepareBinding(String path, name, node) { | 67 prepareBinding(String path, name, node) { |
| 71 if (path == null) return null; | 68 if (path == null) return null; |
| 72 var expr = new Parser(path).parse(); | 69 var expr = new Parser(path).parse(); |
| 73 | 70 |
| 74 // For template bind/repeat to an empty path, just pass through the model. | 71 // For template bind/repeat to an empty path, just pass through the model. |
| 75 // We don't want to unwrap the Scope. | 72 // We don't want to unwrap the Scope. |
| 76 // TODO(jmesserly): a custom element extending <template> could notice this | 73 // TODO(jmesserly): a custom element extending <template> could notice this |
| 77 // behavior. An alternative is to associate the Scope with the node via an | 74 // behavior. An alternative is to associate the Scope with the node via an |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 Expression _expr; | 108 Expression _expr; |
| 112 Function _callback; | 109 Function _callback; |
| 113 StreamSubscription _sub; | 110 StreamSubscription _sub; |
| 114 var _value; | 111 var _value; |
| 115 | 112 |
| 116 _Binding(this._expr, this._scope, [this._converter]); | 113 _Binding(this._expr, this._scope, [this._converter]); |
| 117 | 114 |
| 118 static _oneTime(Expression expr, Scope scope, [converter]) { | 115 static _oneTime(Expression expr, Scope scope, [converter]) { |
| 119 try { | 116 try { |
| 120 return _convertValue(eval(expr, scope), scope, converter); | 117 return _convertValue(eval(expr, scope), scope, converter); |
| 121 } on EvalException catch (e) { | 118 } catch (e, s) { |
| 122 _logger.warning("Error evaluating expression '$expr': ${e.message}"); | 119 new Completer().completeError( |
| 123 return null; | 120 "Error evaluating expression '$expr': $e", s); |
| 124 } | 121 } |
| 122 return null; |
| 125 } | 123 } |
| 126 | 124 |
| 127 _setValue(v) { | 125 _setValue(v) { |
| 128 _value = _convertValue(v, _scope, _converter); | 126 _value = _convertValue(v, _scope, _converter); |
| 129 if (_callback != null) _callback(_value); | 127 if (_callback != null) _callback(_value); |
| 130 } | 128 } |
| 131 | 129 |
| 132 static _convertValue(v, scope, converter) { | 130 static _convertValue(v, scope, converter) { |
| 133 if (v is Comprehension) { | 131 if (v is Comprehension) { |
| 134 // convert the Comprehension into a list of scopes with the loop | 132 // convert the Comprehension into a list of scopes with the loop |
| 135 // variable added to the scope | 133 // variable added to the scope |
| 136 return v.iterable.map((i) { | 134 return v.iterable.map((i) => scope.childScope(v.identifier, i)) |
| 137 var vars = new Map(); | 135 .toList(growable: false); |
| 138 vars[v.identifier] = i; | |
| 139 Scope childScope = new Scope(parent: scope, variables: vars); | |
| 140 return childScope; | |
| 141 }).toList(growable: false); | |
| 142 } else { | 136 } else { |
| 143 return converter == null ? v : converter(v); | 137 return converter == null ? v : converter(v); |
| 144 } | 138 } |
| 145 } | 139 } |
| 146 | 140 |
| 147 get value { | 141 get value { |
| 148 if (_callback != null) return _value; | 142 if (_callback != null) return _value; |
| 149 return _oneTime(_expr, _scope, _converter); | 143 return _oneTime(_expr, _scope, _converter); |
| 150 } | 144 } |
| 151 | 145 |
| 152 set value(v) { | 146 set value(v) { |
| 153 try { | 147 try { |
| 154 assign(_expr, v, _scope); | 148 assign(_expr, v, _scope); |
| 155 } on EvalException catch (e) { | 149 } catch (e, s) { |
| 156 _logger.warning("Error evaluating expression '$_expr': ${e.message}"); | 150 new Completer().completeError( |
| 151 "Error evaluating expression '$_expr': $e", s); |
| 157 } | 152 } |
| 158 } | 153 } |
| 159 | 154 |
| 160 open(callback(value)) { | 155 open(callback(value)) { |
| 161 if (_callback != null) throw new StateError('already open'); | 156 if (_callback != null) throw new StateError('already open'); |
| 162 | 157 |
| 163 _callback = callback; | 158 _callback = callback; |
| 164 final expr = observe(_expr, _scope); | 159 final expr = observe(_expr, _scope); |
| 165 _expr = expr; | 160 _expr = expr; |
| 166 _sub = expr.onUpdate.listen(_setValue)..onError((e) { | 161 _sub = expr.onUpdate.listen(_setValue)..onError((e, s) { |
| 167 _logger.warning("Error evaluating expression '$_expr': ${e.message}"); | 162 new Completer().completeError( |
| 163 "Error evaluating expression '$expr': $e", s); |
| 168 }); | 164 }); |
| 169 try { | 165 try { |
| 170 update(expr, _scope); | 166 update(expr, _scope); |
| 171 _value = _convertValue(expr.currentValue, _scope, _converter); | 167 _value = _convertValue(expr.currentValue, _scope, _converter); |
| 172 } on EvalException catch (e) { | 168 } catch (e, s) { |
| 173 _logger.warning("Error evaluating expression '$_expr': ${e.message}"); | 169 new Completer().completeError( |
| 170 "Error evaluating expression '$expr': $e", s); |
| 174 } | 171 } |
| 175 return _value; | 172 return _value; |
| 176 } | 173 } |
| 177 | 174 |
| 178 void close() { | 175 void close() { |
| 179 if (_callback == null) return; | 176 if (_callback == null) return; |
| 180 | 177 |
| 181 _sub.cancel(); | 178 _sub.cancel(); |
| 182 _sub = null; | 179 _sub = null; |
| 183 _expr = (_expr as ExpressionObserver).expression; | 180 _expr = (_expr as ExpressionObserver).expression; |
| 184 _callback = null; | 181 _callback = null; |
| 185 } | 182 } |
| 186 } | 183 } |
| OLD | NEW |