Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 library polymer_expression; | |
|
justinfagnani
2013/08/15 03:29:14
rename this file to polymer_expression?
| |
| 6 | |
| 7 import 'dart:async'; | |
| 8 import 'dart:html'; | |
| 9 | |
| 10 import 'package:observe/observe.dart'; | |
| 11 | |
| 12 import 'eval.dart'; | |
| 13 import 'expression.dart'; | |
| 14 import 'parser.dart'; | |
| 15 | |
| 16 // TODO(justin): Investigate XSS protection | |
| 17 Object _classAttributeConverter(v) => | |
| 18 (v is Map) ? v.keys.where((k) => v[k] == true).join(' ') : | |
| 19 (v is Iterable) ? v.join(' ') : | |
| 20 v; | |
| 21 | |
| 22 Object _styleAttributeConverter(v) => | |
| 23 (v is Map) ? v.keys.map((k) => '$k: ${v[k]}').join(';') : | |
| 24 (v is Iterable) ? v.join(';') : | |
| 25 v; | |
| 26 | |
| 27 class PolymerExpressions extends BindingDelegate { | |
| 28 | |
| 29 final Map<String, Object> globals; | |
| 30 | |
| 31 PolymerExpressions({Map<String, Object> globals}) | |
| 32 : globals = (globals == null) ? new Map<String, Object>() : globals; | |
| 33 | |
| 34 _Binding getBinding(model, String path, name, node) { | |
| 35 if (path == null) return null; | |
| 36 var expr = new Parser(path).parse(); | |
| 37 if (model is! Scope) { | |
| 38 model = new Scope(model: model, variables: globals); | |
| 39 } | |
| 40 if (node is Element && name == "class") { | |
| 41 return new _Binding(expr, model, _classAttributeConverter); | |
| 42 } | |
| 43 if (node is Element && name == "style") { | |
| 44 return new _Binding(expr, model, _styleAttributeConverter); | |
| 45 } | |
| 46 return new _Binding(expr, model); | |
| 47 } | |
| 48 | |
| 49 getInstanceModel(Element template, model) { | |
| 50 if (model is! Scope) { | |
| 51 var _scope = new Scope(model: model, variables: globals); | |
| 52 return _scope; | |
| 53 } | |
| 54 return model; | |
| 55 } | |
| 56 } | |
| 57 | |
| 58 class _Binding extends Object with ChangeNotifierMixin { | |
| 59 static const _VALUE = const Symbol('value'); | |
| 60 | |
| 61 final Scope _scope; | |
| 62 final ExpressionObserver _expr; | |
| 63 final _converter; | |
| 64 var _value; | |
| 65 | |
| 66 | |
| 67 _Binding(Expression expr, Scope scope, [this._converter]) | |
| 68 : _expr = observe(expr, scope), | |
| 69 _scope = scope { | |
| 70 _expr.onUpdate.listen(_setValue); | |
| 71 _setValue(_expr.currentValue); | |
| 72 } | |
| 73 | |
| 74 _setValue(v) { | |
| 75 if (v is Comprehension) { | |
| 76 // convert the Comprehension into a list of scopes with the loop | |
| 77 // variable added to the scope | |
| 78 _value = v.iterable.map((i) { | |
| 79 var vars = new Map(); | |
| 80 vars[v.identifier] = i; | |
| 81 Scope childScope = new Scope(parent: _scope, variables: vars); | |
| 82 return childScope; | |
| 83 }).toList(growable: false); | |
| 84 } else { | |
| 85 _value = (_converter == null) ? v : _converter(v); | |
| 86 } | |
| 87 notifyChange(new PropertyChangeRecord(_VALUE)); | |
| 88 } | |
| 89 | |
| 90 get value => _value; | |
| 91 | |
| 92 set value(v) { | |
| 93 try { | |
| 94 assign(_expr, v, _scope); | |
| 95 notifyChange(new PropertyChangeRecord(_VALUE)); | |
| 96 } on EvalException catch (e) { | |
| 97 // silently swallow binding errors | |
| 98 } | |
| 99 } | |
| 100 | |
| 101 getValueWorkaround(key) { | |
| 102 if (key == _VALUE) return value; | |
| 103 } | |
| 104 | |
| 105 setValueWorkaround(key, v) { | |
| 106 if (key == _VALUE) value = v; | |
| 107 } | |
| 108 | |
| 109 } | |
| OLD | NEW |