| 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 /// Code from declaration/events.js | |
| 6 part of polymer; | |
| 7 | |
| 8 /// An extension of [polymer_expressions.PolymerExpressions] that adds support | |
| 9 /// for binding events using `on-eventName` using [PolymerEventBindings]. | |
| 10 // TODO(jmesserly): the JS layering is a bit odd, with polymer-dev implementing | |
| 11 // events and polymer-expressions implementing everything else. I don't think | |
| 12 // this separation is right in the long term, so we're using the same class name | |
| 13 // until we can sort it out. | |
| 14 class PolymerExpressions extends BindingDelegate with PolymerEventBindings { | |
| 15 | |
| 16 /// A wrapper around polymer_expressions used to implement forwarding. | |
| 17 /// Ideally we would inherit from it, but mixins can't be applied to a type | |
| 18 /// that forwards to a superclass with a constructor that has optional or | |
| 19 /// named arguments. | |
| 20 final polymer_expressions.PolymerExpressions _delegate; | |
| 21 | |
| 22 Map<String, Object> get globals => _delegate.globals; | |
| 23 | |
| 24 PolymerExpressions({Map<String, Object> globals}) | |
| 25 : _delegate = new polymer_expressions.PolymerExpressions( | |
| 26 globals: globals); | |
| 27 | |
| 28 prepareBinding(String path, name, node) { | |
| 29 if (_hasEventPrefix(name)) { | |
| 30 return prepareEventBinding(path, name, node); | |
| 31 } | |
| 32 return _delegate.prepareBinding(path, name, node); | |
| 33 } | |
| 34 | |
| 35 prepareInstanceModel(Element template) => | |
| 36 _delegate.prepareInstanceModel(template); | |
| 37 | |
| 38 prepareInstancePositionChanged(Element template) => | |
| 39 _delegate.prepareInstancePositionChanged(template); | |
| 40 | |
| 41 static final getExpression = | |
| 42 polymer_expressions.PolymerExpressions.getExpression; | |
| 43 static final getBinding = polymer_expressions.PolymerExpressions.getBinding; | |
| 44 | |
| 45 } | |
| 46 | |
| 47 /// A mixin for a [BindingDelegate] to add Polymer event support. | |
| 48 /// This is included in [PolymerExpressions]. | |
| 49 abstract class PolymerEventBindings { | |
| 50 /// Finds the event controller for this node. | |
| 51 Element findController(Node node) { | |
| 52 while (node.parentNode != null) { | |
| 53 if (node is Polymer && node.eventController != null) { | |
| 54 return node.eventController; | |
| 55 } else if (node is Element) { | |
| 56 // If it is a normal element, js polymer element, or dart wrapper to a | |
| 57 // js polymer element, then we try js interop. | |
| 58 var eventController = | |
| 59 new JsObject.fromBrowserObject(node)['eventController']; | |
| 60 if (eventController != null) return eventController; | |
| 61 } | |
| 62 node = node.parentNode; | |
| 63 } | |
| 64 return node is ShadowRoot ? node.host : null; | |
| 65 } | |
| 66 | |
| 67 EventListener getEventHandler(controller, target, String method) => (e) { | |
| 68 if (controller == null || controller is! Polymer) { | |
| 69 controller = findController(target); | |
| 70 } | |
| 71 | |
| 72 if (controller is Polymer) { | |
| 73 var detail = null; | |
| 74 if (e is CustomEvent) { | |
| 75 detail = e.detail; | |
| 76 // TODO(sigmund): this shouldn't be necessary. See issue 19315. | |
| 77 if (detail == null) { | |
| 78 detail = new JsObject.fromBrowserObject(e)['detail']; | |
| 79 } | |
| 80 } | |
| 81 var args = [e, detail, e.currentTarget]; | |
| 82 controller.dispatchMethod(controller, method, args); | |
| 83 } else { | |
| 84 throw new StateError('controller $controller is not a ' | |
| 85 'Dart polymer-element.'); | |
| 86 } | |
| 87 }; | |
| 88 | |
| 89 prepareEventBinding(String path, String name, Node node) { | |
| 90 if (!_hasEventPrefix(name)) return null; | |
| 91 | |
| 92 var eventType = _removeEventPrefix(name); | |
| 93 var translated = _eventTranslations[eventType]; | |
| 94 eventType = translated != null ? translated : eventType; | |
| 95 | |
| 96 return (model, node, oneTime) { | |
| 97 var eventHandler = | |
| 98 Zone.current.bindUnaryCallback(getEventHandler(null, node, path)); | |
| 99 // TODO(jakemac): Remove this indirection if/when JsFunction gets a | |
| 100 // simpler constructor that doesn't pass this, http://dartbug.com/20545. | |
| 101 var handler = new JsFunction.withThis((_, e) => eventHandler(e)); | |
| 102 _PolymerGestures.callMethod( | |
| 103 'addEventListener', [node, eventType, handler]); | |
| 104 | |
| 105 if (oneTime) return null; | |
| 106 return new _EventBindable(path, node, eventType, handler); | |
| 107 }; | |
| 108 } | |
| 109 } | |
| 110 | |
| 111 | |
| 112 class _EventBindable extends Bindable { | |
| 113 final String _path; | |
| 114 final Node _node; | |
| 115 final String _eventType; | |
| 116 final JsFunction _handler; | |
| 117 | |
| 118 _EventBindable(this._path, this._node, this._eventType, this._handler); | |
| 119 | |
| 120 // TODO(rafaelw): This is really pointless work. Aside from the cost | |
| 121 // of these allocations, NodeBind is going to setAttribute back to its | |
| 122 // current value. Fixing this would mean changing the TemplateBinding | |
| 123 // binding delegate API. | |
| 124 get value => '{{ $_path }}'; | |
| 125 | |
| 126 open(callback) => value; | |
| 127 | |
| 128 void close() { | |
| 129 _PolymerGestures.callMethod( | |
| 130 'removeEventListener', [_node, _eventType, _handler]); | |
| 131 } | |
| 132 } | |
| 133 | |
| 134 | |
| 135 /// Attribute prefix used for declarative event handlers. | |
| 136 const _EVENT_PREFIX = 'on-'; | |
| 137 | |
| 138 /// Whether an attribute declares an event. | |
| 139 bool _hasEventPrefix(String attr) => attr.startsWith(_EVENT_PREFIX); | |
| 140 | |
| 141 String _removeEventPrefix(String name) => name.substring(_EVENT_PREFIX.length); | |
| 142 | |
| 143 // Dart note: polymer.js calls this mixedCaseEventTypes. But we have additional | |
| 144 // things that need translation due to renames. | |
| 145 final _eventTranslations = const { | |
| 146 'domfocusout': 'DOMFocusOut', | |
| 147 'domfocusin': 'DOMFocusIn', | |
| 148 'dommousescroll': 'DOMMouseScroll', | |
| 149 | |
| 150 // Dart note: handle Dart-specific event names. | |
| 151 'animationend': 'webkitAnimationEnd', | |
| 152 'animationiteration': 'webkitAnimationIteration', | |
| 153 'animationstart': 'webkitAnimationStart', | |
| 154 'doubleclick': 'dblclick', | |
| 155 'fullscreenchange': 'webkitfullscreenchange', | |
| 156 'fullscreenerror': 'webkitfullscreenerror', | |
| 157 'keyadded': 'webkitkeyadded', | |
| 158 'keyerror': 'webkitkeyerror', | |
| 159 'keymessage': 'webkitkeymessage', | |
| 160 'needkey': 'webkitneedkey', | |
| 161 'speechchange': 'webkitSpeechChange', | |
| 162 }; | |
| 163 | |
| 164 final _reverseEventTranslations = () { | |
| 165 final map = new Map<String, String>(); | |
| 166 _eventTranslations.forEach((onName, eventType) { | |
| 167 map[eventType] = onName; | |
| 168 }); | |
| 169 return map; | |
| 170 }(); | |
| 171 | |
| 172 // Dart note: we need this function because we have additional renames JS does | |
| 173 // not have. The JS renames are simply case differences, whereas we have ones | |
| 174 // like doubleclick -> dblclick and stripping the webkit prefix. | |
| 175 String _eventNameFromType(String eventType) { | |
| 176 final result = _reverseEventTranslations[eventType]; | |
| 177 return result != null ? result : eventType; | |
| 178 } | |
| OLD | NEW |