Chromium Code Reviews| 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 /// Code from declaration/events.js | 5 /// Code from declaration/events.js |
| 6 part of polymer; | 6 part of polymer; |
| 7 | 7 |
| 8 /// An extension of [polymer_expressions.PolymerExpressions] that adds support | 8 /// An extension of [polymer_expressions.PolymerExpressions] that adds support |
| 9 /// for binding events using `on-eventName` using [PolymerEventBindings]. | 9 /// for binding events using `on-eventName` using [PolymerEventBindings]. |
| 10 // TODO(jmesserly): the JS layering is a bit odd, with polymer-dev implementing | 10 // TODO(jmesserly): the JS layering is a bit odd, with polymer-dev implementing |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 81 }; | 81 }; |
| 82 | 82 |
| 83 prepareEventBinding(String path, String name, Node node) { | 83 prepareEventBinding(String path, String name, Node node) { |
| 84 if (!_hasEventPrefix(name)) return null; | 84 if (!_hasEventPrefix(name)) return null; |
| 85 | 85 |
| 86 var eventType = _removeEventPrefix(name); | 86 var eventType = _removeEventPrefix(name); |
| 87 var translated = _eventTranslations[eventType]; | 87 var translated = _eventTranslations[eventType]; |
| 88 eventType = translated != null ? translated : eventType; | 88 eventType = translated != null ? translated : eventType; |
| 89 | 89 |
| 90 return (model, node, oneTime) { | 90 return (model, node, oneTime) { |
| 91 var handler = getEventHandler(null, node, path); | 91 var handler = Zone.current.bindUnaryCallback( |
| 92 var sub = node.on[eventType].listen(handler); | 92 getEventHandler(null, node, path)); |
| 93 _PolymerGestures.callMethod( | |
| 94 'addEventListener', [node, eventType, handler]); | |
| 93 | 95 |
| 94 if (oneTime) return null; | 96 if (oneTime) return null; |
| 95 return new _EventBindable(sub, path); | 97 return new _EventBindable(path, node, eventType, handler); |
| 96 }; | 98 }; |
| 97 } | 99 } |
| 98 } | 100 } |
| 99 | 101 |
| 100 | 102 |
| 101 class _EventBindable extends Bindable { | 103 class _EventBindable extends Bindable { |
| 102 StreamSubscription _sub; | |
| 103 final String _path; | 104 final String _path; |
| 105 final Node _node; | |
| 106 final String _eventType; | |
| 107 final ZoneUnaryCallback _handler; | |
|
Siggi Cherem (dart-lang)
2014/08/14 20:51:24
we should leave the type as it was - I think the r
| |
| 104 | 108 |
| 105 _EventBindable(this._sub, this._path); | 109 _EventBindable(this._path, this._node, this._eventType, this._handler); |
| 106 | 110 |
| 107 // TODO(rafaelw): This is really pointless work. Aside from the cost | 111 // TODO(rafaelw): This is really pointless work. Aside from the cost |
| 108 // of these allocations, NodeBind is going to setAttribute back to its | 112 // of these allocations, NodeBind is going to setAttribute back to its |
| 109 // current value. Fixing this would mean changing the TemplateBinding | 113 // current value. Fixing this would mean changing the TemplateBinding |
| 110 // binding delegate API. | 114 // binding delegate API. |
| 111 get value => '{{ $_path }}'; | 115 get value => '{{ $_path }}'; |
| 112 | 116 |
| 113 open(callback) => value; | 117 open(callback) => value; |
| 114 | 118 |
| 115 void close() { | 119 void close() { |
| 116 if (_sub != null) { | 120 _PolymerGestures.callMethod( |
| 117 _sub.cancel(); | 121 'removeEventListener', |
| 118 _sub = null; | 122 [_node, _eventType, _handler]); |
|
Siggi Cherem (dart-lang)
2014/08/14 20:51:24
you might not need the line-wrap
| |
| 119 } | |
| 120 } | 123 } |
| 121 } | 124 } |
| 122 | 125 |
| 123 | 126 |
| 124 /// Attribute prefix used for declarative event handlers. | 127 /// Attribute prefix used for declarative event handlers. |
| 125 const _EVENT_PREFIX = 'on-'; | 128 const _EVENT_PREFIX = 'on-'; |
| 126 | 129 |
| 127 /// Whether an attribute declares an event. | 130 /// Whether an attribute declares an event. |
| 128 bool _hasEventPrefix(String attr) => attr.startsWith(_EVENT_PREFIX); | 131 bool _hasEventPrefix(String attr) => attr.startsWith(_EVENT_PREFIX); |
| 129 | 132 |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 158 return map; | 161 return map; |
| 159 }(); | 162 }(); |
| 160 | 163 |
| 161 // Dart note: we need this function because we have additional renames JS does | 164 // Dart note: we need this function because we have additional renames JS does |
| 162 // not have. The JS renames are simply case differences, whereas we have ones | 165 // not have. The JS renames are simply case differences, whereas we have ones |
| 163 // like doubleclick -> dblclick and stripping the webkit prefix. | 166 // like doubleclick -> dblclick and stripping the webkit prefix. |
| 164 String _eventNameFromType(String eventType) { | 167 String _eventNameFromType(String eventType) { |
| 165 final result = _reverseEventTranslations[eventType]; | 168 final result = _reverseEventTranslations[eventType]; |
| 166 return result != null ? result : eventType; | 169 return result != null ? result : eventType; |
| 167 } | 170 } |
| OLD | NEW |