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

Side by Side Diff: lib/dom/templates/html/dartium/impl_EventTarget.darttemplate

Issue 10698049: Unfork html events generation in dart2js and dartium. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: . Created 8 years, 5 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
OLDNEW
(Empty)
1 // Copyright (c) 2012, 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 class _EventsImpl implements Events {
6 // TODO(podivilov): add type.
7 final _ptr;
8
9 final Map<String, EventListenerList> _listenerMap;
10
11 _EventsImpl(this._ptr) : _listenerMap = <EventListenerList>{};
12
13 EventListenerList operator [](String type) {
14 return _listenerMap.putIfAbsent(type,
15 () => new _EventListenerListImpl(_ptr, type));
16 }
17 }
18
19 class _EventListenerWrapper {
20 final EventListener raw;
21 final Function wrapped;
22 final bool useCapture;
23 _EventListenerWrapper(this.raw, this.wrapped, this.useCapture);
24 }
25
26 class _EventListenerListImpl implements EventListenerList {
27 // TODO(podivilov): add type.
28 final _ptr;
29 final String _type;
30 List<_EventListenerWrapper> _wrappers;
31
32 _EventListenerListImpl(this._ptr, this._type) :
33 // TODO(jacobr): switch to <_EventListenerWrapper>[] when the VM allow it.
34 _wrappers = new List<_EventListenerWrapper>();
35
36 EventListenerList add(EventListener listener, [bool useCapture = false]) {
37 _add(listener, useCapture);
38 return this;
39 }
40
41 EventListenerList remove(EventListener listener, [bool useCapture = false]) {
42 _remove(listener, useCapture);
43 return this;
44 }
45
46 bool dispatch(Event evt) {
47 // TODO(jacobr): what is the correct behavior here. We could alternately
48 // force the event to have the expected type.
49 assert(evt.type == _type);
50 return _ptr.$dom_dispatchEvent(evt);
51 }
52
53 void _add(EventListener listener, bool useCapture) {
54 _ptr.$dom_addEventListener(_type,
55 _findOrAddWrapper(listener, useCapture),
56 useCapture);
57 }
58
59 void _remove(EventListener listener, bool useCapture) {
60 Function wrapper = _removeWrapper(listener, useCapture);
61 if (wrapper !== null) {
62 _ptr.$dom_removeEventListener(_type, wrapper, useCapture);
63 }
64 }
65
66 Function _removeWrapper(EventListener listener, bool useCapture) {
67 if (_wrappers === null) {
68 return null;
69 }
70 for (int i = 0; i < _wrappers.length; i++) {
71 _EventListenerWrapper wrapper = _wrappers[i];
72 if (wrapper.raw === listener && wrapper.useCapture == useCapture) {
73 // Order doesn't matter so we swap with the last element instead of
74 // performing a more expensive remove from the middle of the list.
75 if (i + 1 != _wrappers.length) {
76 _wrappers[i] = _wrappers.removeLast();
77 } else {
78 _wrappers.removeLast();
79 }
80 return wrapper.wrapped;
81 }
82 }
83 return null;
84 }
85
86 Function _findOrAddWrapper(EventListener listener, bool useCapture) {
87 if (_wrappers === null) {
88 _wrappers = <_EventListenerWrapper>[];
89 } else {
90 for (_EventListenerWrapper wrapper in _wrappers) {
91 if (wrapper.raw === listener && wrapper.useCapture == useCapture) {
92 return wrapper.wrapped;
93 }
94 }
95 }
96 final wrapped = (e) { listener(e); };
97 _wrappers.add(new _EventListenerWrapper(listener, wrapped, useCapture));
98 return wrapped;
99 }
100 }
101
102 class $CLASSNAME$EXTENDS$IMPLEMENTS$NATIVESPEC {
103 /*
104 $!MEMBERS
105 */
106 }
OLDNEW
« no previous file with comments | « lib/dom/scripts/systemnative.py ('k') | lib/dom/templates/html/frog/impl_EventTarget.darttemplate » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698