Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(77)

Side by Side Diff: pkg/polymer/lib/src/events.dart

Issue 307793002: update polymer, nodebind, and templatebinding (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « pkg/polymer/lib/src/declaration.dart ('k') | pkg/polymer/lib/src/instance.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 BindingDelegate _delegate;
21
22 PolymerExpressions({Map<String, Object> globals})
23 : _delegate = new polymer_expressions.PolymerExpressions(
24 globals: globals);
25
26 prepareBinding(String path, name, node) {
27 if (_hasEventPrefix(name)) {
28 return prepareEventBinding(path, name, node);
29 }
30 return _delegate.prepareBinding(path, name, node);
31 }
32
33 prepareInstanceModel(Element template) =>
34 _delegate.prepareInstanceModel(template);
35
36 prepareInstancePositionChanged(Element template) =>
37 _delegate.prepareInstancePositionChanged(template);
38 }
39
40 /// A mixin for a [BindingDelegate] to add Polymer event support.
41 /// This is included in [PolymerExpressions].
42 abstract class PolymerEventBindings {
43 /// Finds the event controller for this node.
44 Element findController(Node node) {
45 while (node.parentNode != null) {
46 if (node is Polymer && node.eventController != null) {
47 return node.eventController;
48 }
49 node = node.parentNode;
50 }
51 return node is ShadowRoot ? node.host : null;
52 }
53
54 EventListener getEventHandler(controller, target, String method) => (e) {
55 if (controller == null || controller is! Polymer) {
56 controller = findController(target);
57 }
58
59 if (controller is Polymer) {
60 var args = [e, e.detail, e.currentTarget];
61 controller.dispatchMethod(controller, method, args);
62 } else {
63 throw new StateError('controller $controller is not a '
64 'Dart polymer-element.');
65 }
66 };
67
68 prepareEventBinding(String path, String name, Node node) {
69 if (!_hasEventPrefix(name)) return null;
70
71 var eventType = _removeEventPrefix(name);
72 var translated = _eventTranslations[eventType];
73 eventType = translated != null ? translated : eventType;
74
75 return (model, node, oneTime) {
76 var handler = getEventHandler(null, node, path);
77 node.addEventListener(eventType, handler);
78
79 if (oneTime) return null;
80 return new _EventBindable(node, eventType, handler, path);
81 };
82 }
83 }
84
85
86 class _EventBindable extends Bindable {
87 final Node _node;
88 final String _eventType;
89 final Function _handler;
90 final String _path;
91
92 _EventBindable(this._node, this._eventType, this._handler, this._path);
93
94 // TODO(rafaelw): This is really pointless work. Aside from the cost
95 // of these allocations, NodeBind is going to setAttribute back to its
96 // current value. Fixing this would mean changing the TemplateBinding
97 // binding delegate API.
98 get value => '{{ $_path }}';
99
100 open(callback) => value;
101
102 void close() => _node.removeEventListener(_eventType, _handler);
103 }
104
105
106 /// Attribute prefix used for declarative event handlers.
107 const _EVENT_PREFIX = 'on-';
108
109 /// Whether an attribute declares an event.
110 bool _hasEventPrefix(String attr) => attr.startsWith(_EVENT_PREFIX);
111
112 String _removeEventPrefix(String name) => name.substring(_EVENT_PREFIX.length);
113
114 // Dart note: polymer.js calls this mixedCaseEventTypes. But we have additional
115 // things that need translation due to renames.
116 final _eventTranslations = const {
117 'domfocusout': 'DOMFocusOut',
118 'domfocusin': 'DOMFocusIn',
119 'dommousescroll': 'DOMMouseScroll',
120
121 // Dart note: handle Dart-specific event names.
122 'animationend': 'webkitAnimationEnd',
123 'animationiteration': 'webkitAnimationIteration',
124 'animationstart': 'webkitAnimationStart',
125 'doubleclick': 'dblclick',
126 'fullscreenchange': 'webkitfullscreenchange',
127 'fullscreenerror': 'webkitfullscreenerror',
128 'keyadded': 'webkitkeyadded',
129 'keyerror': 'webkitkeyerror',
130 'keymessage': 'webkitkeymessage',
131 'needkey': 'webkitneedkey',
132 'speechchange': 'webkitSpeechChange',
133 };
134
135 final _reverseEventTranslations = () {
136 final map = new Map<String, String>();
137 _eventTranslations.forEach((onName, eventType) {
138 map[eventType] = onName;
139 });
140 return map;
141 }();
142
143 // Dart note: we need this function because we have additional renames JS does
144 // not have. The JS renames are simply case differences, whereas we have ones
145 // like doubleclick -> dblclick and stripping the webkit prefix.
146 String _eventNameFromType(String eventType) {
147 final result = _reverseEventTranslations[eventType];
148 return result != null ? result : eventType;
149 }
OLDNEW
« no previous file with comments | « pkg/polymer/lib/src/declaration.dart ('k') | pkg/polymer/lib/src/instance.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698