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 18 matching lines...) Expand all Loading... |
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 import 'package:logging/logging.dart'; |
35 | 35 |
36 import 'eval.dart'; | 36 import 'eval.dart'; |
37 import 'expression.dart'; | 37 import 'expression.dart'; |
38 import 'parser.dart'; | 38 import 'parser.dart'; |
| 39 import 'src/globals.dart'; |
39 | 40 |
40 final Logger _logger = new Logger('polymer_expressions'); | 41 final Logger _logger = new Logger('polymer_expressions'); |
41 | 42 |
42 // TODO(justin): Investigate XSS protection | 43 // TODO(justin): Investigate XSS protection |
43 Object _classAttributeConverter(v) => | 44 Object _classAttributeConverter(v) => |
44 (v is Map) ? v.keys.where((k) => v[k] == true).join(' ') : | 45 (v is Map) ? v.keys.where((k) => v[k] == true).join(' ') : |
45 (v is Iterable) ? v.join(' ') : | 46 (v is Iterable) ? v.join(' ') : |
46 v; | 47 v; |
47 | 48 |
48 Object _styleAttributeConverter(v) => | 49 Object _styleAttributeConverter(v) => |
49 (v is Map) ? v.keys.map((k) => '$k: ${v[k]}').join(';') : | 50 (v is Map) ? v.keys.map((k) => '$k: ${v[k]}').join(';') : |
50 (v is Iterable) ? v.join(';') : | 51 (v is Iterable) ? v.join(';') : |
51 v; | 52 v; |
52 | 53 |
53 class PolymerExpressions extends BindingDelegate { | 54 class PolymerExpressions extends BindingDelegate { |
| 55 /** The default [globals] to use for Polymer expressions. */ |
| 56 static const Map DEFAULT_GLOBALS = const { 'enumerate': enumerate }; |
54 | 57 |
55 final Map<String, Object> globals; | 58 final Map<String, Object> globals; |
56 | 59 |
| 60 /** |
| 61 * Creates a new binding delegate for Polymer expressions, with the provided |
| 62 * variables used as [globals]. If no globals are supplied, a copy of the |
| 63 * [DEFAULT_GLOBALS] will be used. |
| 64 */ |
57 PolymerExpressions({Map<String, Object> globals}) | 65 PolymerExpressions({Map<String, Object> globals}) |
58 : globals = (globals == null) ? new Map<String, Object>() : globals; | 66 : globals = (globals == null) ? |
| 67 new Map<String, Object>.from(DEFAULT_GLOBALS) : globals; |
59 | 68 |
60 _Binding getBinding(model, String path, name, node) { | 69 _Binding getBinding(model, String path, name, node) { |
61 if (path == null) return null; | 70 if (path == null) return null; |
62 var expr = new Parser(path).parse(); | 71 var expr = new Parser(path).parse(); |
63 if (model is! Scope) { | 72 if (model is! Scope) { |
64 model = new Scope(model: model, variables: globals); | 73 model = new Scope(model: model, variables: globals); |
65 } | 74 } |
66 if (node is Element && name == "class") { | 75 if (node is Element && name == "class") { |
67 return new _Binding(expr, model, _classAttributeConverter); | 76 return new _Binding(expr, model, _classAttributeConverter); |
68 } | 77 } |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 } | 139 } |
131 } | 140 } |
132 | 141 |
133 getValueWorkaround(key) { | 142 getValueWorkaround(key) { |
134 if (key == _VALUE) return value; | 143 if (key == _VALUE) return value; |
135 } | 144 } |
136 | 145 |
137 setValueWorkaround(key, v) { | 146 setValueWorkaround(key, v) { |
138 if (key == _VALUE) value = v; | 147 if (key == _VALUE) value = v; |
139 } | 148 } |
140 | |
141 } | 149 } |
OLD | NEW |