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

Side by Side Diff: lib/dom/src/native_EventsImplementation.dart

Issue 10447032: Remove unused code and templates. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 7 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 | « lib/dom/src/DOMStringMap.dart ('k') | lib/dom/src/native_FactoryProviders.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) 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 EventsImplementation implements html.Events {
6
7 final EventTarget _ptr;
8
9 final Map<String, html.EventListenerList> _listenerMap;
10
11 EventsImplementation(this._ptr) : _listenerMap = <html.EventListenerList>{};
12
13 html.EventListenerList operator [](String type) {
14 return _listenerMap.putIfAbsent(type,
15 () => new EventListenerListImplementation(_ptr, type));
16 }
17 }
18
19 class EventListenerWrapper {
20 final html.EventListener raw;
21 final Function wrapped;
22 final bool useCapture;
23 EventListenerWrapper(this.raw, this.wrapped, this.useCapture);
24 }
25
26 class EventListenerListImplementation implements html.EventListenerList {
27 final EventTarget _ptr;
28 final String _type;
29 List<EventListenerWrapper> _wrappers;
30
31 EventListenerListImplementation(this._ptr, this._type)
32 : _wrappers = <EventListenerWrapper>[];
33
34 html.EventListenerList add(html.EventListener listener, [bool useCapture = fal se]) {
35 _add(listener, useCapture);
36 return this;
37 }
38
39 html.EventListenerList remove(html.EventListener listener, [bool useCapture = false]) {
40 _remove(listener, useCapture);
41 return this;
42 }
43
44 bool dispatch(html.Event evt) {
45 // TODO(jacobr): what is the correct behavior here. We could alternately
46 // force the event to have the expected type.
47 assert(evt.type == _type);
48 return _ptr.dispatchEvent(html.unwrap_internal(evt));
49 }
50
51 void _add(html.EventListener listener, bool useCapture) {
52 _ptr.addEventListener(_type,
53 _findOrAddWrapper(listener, useCapture),
54 useCapture);
55 }
56
57 void _remove(html.EventListener listener, bool useCapture) {
58 Function wrapper = _removeWrapper(listener, useCapture);
59 if (wrapper !== null) {
60 _ptr.removeEventListener(_type, wrapper, useCapture);
61 }
62 }
63
64 Function _removeWrapper(html.EventListener listener, bool useCapture) {
65 if (_wrappers === null) {
66 return null;
67 }
68 for (int i = 0; i < _wrappers.length; i++) {
69 EventListenerWrapper wrapper = _wrappers[i];
70 if (wrapper.raw === listener && wrapper.useCapture == useCapture) {
71 // Order doesn't matter so we swap with the last element instead of
72 // performing a more expensive remove from the middle of the list.
73 if (i + 1 != _wrappers.length) {
74 _wrappers[i] = _wrappers.removeLast();
75 } else {
76 _wrappers.removeLast();
77 }
78 return wrapper.wrapped;
79 }
80 }
81 return null;
82 }
83
84 Function _findOrAddWrapper(html.EventListener listener, bool useCapture) {
85 if (_wrappers === null) {
86 _wrappers = <EventListenerWrapper>[];
87 } else {
88 for (EventListenerWrapper wrapper in _wrappers) {
89 if (wrapper.raw === listener && wrapper.useCapture == useCapture) {
90 return wrapper.wrapped;
91 }
92 }
93 }
94 final wrapped = (e) { listener(html.wrap_internal(e)); };
95 _wrappers.add(new EventListenerWrapper(listener, wrapped, useCapture));
96 return wrapped;
97 }
98 }
OLDNEW
« no previous file with comments | « lib/dom/src/DOMStringMap.dart ('k') | lib/dom/src/native_FactoryProviders.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698