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 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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 = getEventHandler(null, node, path); |
92 var sub = node.on[eventType].listen(handler); | 92 _PolymerGestures.callMethod( |
93 'addEventListener', [node, eventType, handler]); | |
Siggi Cherem (dart-lang)
2014/08/13 23:00:26
I think we need to bind handler to the current zon
jakemac
2014/08/14 18:00:49
Ah nice good catch, I was just working on a bug th
| |
93 | 94 |
94 if (oneTime) return null; | 95 if (oneTime) return null; |
Siggi Cherem (dart-lang)
2014/08/13 23:00:26
I know this is how it was before and how it in JS,
jakemac
2014/08/14 18:00:49
Would you be fine with us filing this as an invest
Siggi Cherem (dart-lang)
2014/08/14 20:51:24
sure thing
| |
95 return new _EventBindable(sub, path); | 96 return new _EventBindable(path, node, eventType, handler); |
96 }; | 97 }; |
97 } | 98 } |
98 } | 99 } |
99 | 100 |
100 | 101 |
101 class _EventBindable extends Bindable { | 102 class _EventBindable extends Bindable { |
102 StreamSubscription _sub; | |
103 final String _path; | 103 final String _path; |
104 final Node _node; | |
105 final String _eventType; | |
106 final EventListener _handler; | |
104 | 107 |
105 _EventBindable(this._sub, this._path); | 108 _EventBindable(this._path, this._node, this._eventType, this._handler); |
106 | 109 |
107 // TODO(rafaelw): This is really pointless work. Aside from the cost | 110 // TODO(rafaelw): This is really pointless work. Aside from the cost |
108 // of these allocations, NodeBind is going to setAttribute back to its | 111 // of these allocations, NodeBind is going to setAttribute back to its |
109 // current value. Fixing this would mean changing the TemplateBinding | 112 // current value. Fixing this would mean changing the TemplateBinding |
110 // binding delegate API. | 113 // binding delegate API. |
111 get value => '{{ $_path }}'; | 114 get value => '{{ $_path }}'; |
112 | 115 |
113 open(callback) => value; | 116 open(callback) => value; |
114 | 117 |
115 void close() { | 118 void close() { |
116 if (_sub != null) { | 119 _PolymerGestures.callMethod( |
117 _sub.cancel(); | 120 'removeEventListener', [_node, _eventType, _handler]); |
Siggi Cherem (dart-lang)
2014/08/13 23:00:26
mmm... I'm not sure if js-interop guarantees that
jakemac
2014/08/14 18:00:49
Could this be solved by jsifying the handler on ou
Siggi Cherem (dart-lang)
2014/08/14 20:51:24
I think that's possible, probably you can create a
| |
118 _sub = null; | |
119 } | |
120 } | 121 } |
121 } | 122 } |
122 | 123 |
123 | 124 |
124 /// Attribute prefix used for declarative event handlers. | 125 /// Attribute prefix used for declarative event handlers. |
125 const _EVENT_PREFIX = 'on-'; | 126 const _EVENT_PREFIX = 'on-'; |
126 | 127 |
127 /// Whether an attribute declares an event. | 128 /// Whether an attribute declares an event. |
128 bool _hasEventPrefix(String attr) => attr.startsWith(_EVENT_PREFIX); | 129 bool _hasEventPrefix(String attr) => attr.startsWith(_EVENT_PREFIX); |
129 | 130 |
(...skipping 28 matching lines...) Expand all Loading... | |
158 return map; | 159 return map; |
159 }(); | 160 }(); |
160 | 161 |
161 // Dart note: we need this function because we have additional renames JS does | 162 // 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 | 163 // not have. The JS renames are simply case differences, whereas we have ones |
163 // like doubleclick -> dblclick and stripping the webkit prefix. | 164 // like doubleclick -> dblclick and stripping the webkit prefix. |
164 String _eventNameFromType(String eventType) { | 165 String _eventNameFromType(String eventType) { |
165 final result = _reverseEventTranslations[eventType]; | 166 final result = _reverseEventTranslations[eventType]; |
166 return result != null ? result : eventType; | 167 return result != null ? result : eventType; |
167 } | 168 } |
OLD | NEW |