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 eventHandler = |
92 var sub = node.on[eventType].listen(handler); | 92 Zone.current.bindUnaryCallback(getEventHandler(null, node, path)); |
| 93 // TODO(jakemac): Remove this indirection if/when JsFunction gets a |
| 94 // simpler constructor that doesn't pass this, http://dartbug.com/20545. |
| 95 var handler = new JsFunction.withThis((_, e) => eventHandler(e)); |
| 96 _PolymerGestures.callMethod( |
| 97 'addEventListener', [node, eventType, handler]); |
93 | 98 |
94 if (oneTime) return null; | 99 if (oneTime) return null; |
95 return new _EventBindable(sub, path); | 100 return new _EventBindable(path, node, eventType, handler); |
96 }; | 101 }; |
97 } | 102 } |
98 } | 103 } |
99 | 104 |
100 | 105 |
101 class _EventBindable extends Bindable { | 106 class _EventBindable extends Bindable { |
102 StreamSubscription _sub; | |
103 final String _path; | 107 final String _path; |
| 108 final Node _node; |
| 109 final String _eventType; |
| 110 final JsFunction _handler; |
104 | 111 |
105 _EventBindable(this._sub, this._path); | 112 _EventBindable(this._path, this._node, this._eventType, this._handler); |
106 | 113 |
107 // TODO(rafaelw): This is really pointless work. Aside from the cost | 114 // TODO(rafaelw): This is really pointless work. Aside from the cost |
108 // of these allocations, NodeBind is going to setAttribute back to its | 115 // of these allocations, NodeBind is going to setAttribute back to its |
109 // current value. Fixing this would mean changing the TemplateBinding | 116 // current value. Fixing this would mean changing the TemplateBinding |
110 // binding delegate API. | 117 // binding delegate API. |
111 get value => '{{ $_path }}'; | 118 get value => '{{ $_path }}'; |
112 | 119 |
113 open(callback) => value; | 120 open(callback) => value; |
114 | 121 |
115 void close() { | 122 void close() { |
116 if (_sub != null) { | 123 _PolymerGestures.callMethod( |
117 _sub.cancel(); | 124 'removeEventListener', [_node, _eventType, _handler]); |
118 _sub = null; | |
119 } | |
120 } | 125 } |
121 } | 126 } |
122 | 127 |
123 | 128 |
124 /// Attribute prefix used for declarative event handlers. | 129 /// Attribute prefix used for declarative event handlers. |
125 const _EVENT_PREFIX = 'on-'; | 130 const _EVENT_PREFIX = 'on-'; |
126 | 131 |
127 /// Whether an attribute declares an event. | 132 /// Whether an attribute declares an event. |
128 bool _hasEventPrefix(String attr) => attr.startsWith(_EVENT_PREFIX); | 133 bool _hasEventPrefix(String attr) => attr.startsWith(_EVENT_PREFIX); |
129 | 134 |
(...skipping 28 matching lines...) Expand all Loading... |
158 return map; | 163 return map; |
159 }(); | 164 }(); |
160 | 165 |
161 // Dart note: we need this function because we have additional renames JS does | 166 // 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 | 167 // not have. The JS renames are simply case differences, whereas we have ones |
163 // like doubleclick -> dblclick and stripping the webkit prefix. | 168 // like doubleclick -> dblclick and stripping the webkit prefix. |
164 String _eventNameFromType(String eventType) { | 169 String _eventNameFromType(String eventType) { |
165 final result = _reverseEventTranslations[eventType]; | 170 final result = _reverseEventTranslations[eventType]; |
166 return result != null ? result : eventType; | 171 return result != null ? result : eventType; |
167 } | 172 } |
OLD | NEW |