Chromium Code Reviews| OLD | NEW |
|---|---|
| (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) { | |
|
Anton Muhin
2012/05/15 14:54:57
nit: => syntax?
podivilov
2012/05/16 12:32:24
Done.
| |
| 14 return _get(type); | |
| 15 } | |
| 16 | |
| 17 html.EventListenerList _get(String type) { | |
|
Anton Muhin
2012/05/15 14:54:57
why auxiliary _get?
podivilov
2012/05/16 12:32:24
It is used by subclasses.
Anton Muhin
2012/05/16 13:17:56
Cannot you use this[name] or super[name] to achiev
podivilov
2012/05/16 15:19:44
Done.
| |
| 18 return _listenerMap.putIfAbsent(type, | |
| 19 () => new EventListenerListImplementation(_ptr, type)); | |
| 20 } | |
| 21 } | |
| 22 | |
| 23 class EventListenerWrapper { | |
| 24 final html.EventListener raw; | |
| 25 final Function wrapped; | |
| 26 final bool useCapture; | |
| 27 EventListenerWrapper(this.raw, this.wrapped, this.useCapture); | |
| 28 } | |
| 29 | |
| 30 class EventListenerListImplementation implements html.EventListenerList { | |
| 31 final EventTarget _ptr; | |
| 32 final String _type; | |
| 33 List<EventListenerWrapper> _wrappers; | |
| 34 | |
| 35 EventListenerListImplementation(this._ptr, this._type) : | |
| 36 // TODO(jacobr): switch to <EventListenerWrapper>[] when the VM allow it. | |
|
Anton Muhin
2012/05/15 14:54:57
nit: at least in the rest of Dart code : is usuall
podivilov
2012/05/16 12:32:24
Done.
Anton Muhin
2012/05/16 13:17:56
Sorry, no, but I believe we used it a lot previous
podivilov
2012/05/16 15:19:44
Done.
| |
| 37 _wrappers = new List<EventListenerWrapper>(); | |
| 38 | |
| 39 html.EventListenerList add(html.EventListener listener, [bool useCapture = fal se]) { | |
| 40 _add(listener, useCapture); | |
| 41 return this; | |
| 42 } | |
| 43 | |
| 44 html.EventListenerList remove(html.EventListener listener, [bool useCapture = false]) { | |
| 45 _remove(listener, useCapture); | |
| 46 return this; | |
| 47 } | |
| 48 | |
| 49 bool dispatch(html.Event evt) { | |
| 50 // TODO(jacobr): what is the correct behavior here. We could alternately | |
| 51 // force the event to have the expected type. | |
| 52 assert(evt.type == _type); | |
| 53 return _ptr.dispatchEvent(html.unwrap_internal(evt)); | |
| 54 } | |
| 55 | |
| 56 void _add(html.EventListener listener, bool useCapture) { | |
| 57 _ptr.addEventListener(_type, | |
| 58 _findOrAddWrapper(listener, useCapture), | |
| 59 useCapture); | |
| 60 } | |
| 61 | |
| 62 void _remove(html.EventListener listener, bool useCapture) { | |
| 63 Function wrapper = _removeWrapper(listener, useCapture); | |
| 64 if (wrapper !== null) { | |
| 65 _ptr.removeEventListener(_type, wrapper, useCapture); | |
| 66 } | |
| 67 } | |
| 68 | |
| 69 Function _removeWrapper(html.EventListener listener, bool useCapture) { | |
| 70 if (_wrappers === null) { | |
| 71 return null; | |
| 72 } | |
| 73 for (int i = 0; i < _wrappers.length; i++) { | |
| 74 EventListenerWrapper wrapper = _wrappers[i]; | |
| 75 if (wrapper.raw === listener && wrapper.useCapture == useCapture) { | |
| 76 // Order doesn't matter so we swap with the last element instead of | |
| 77 // performing a more expensive remove from the middle of the list. | |
| 78 if (i + 1 != _wrappers.length) { | |
| 79 _wrappers[i] = _wrappers.removeLast(); | |
| 80 } else { | |
| 81 _wrappers.removeLast(); | |
| 82 } | |
| 83 return wrapper.wrapped; | |
| 84 } | |
| 85 } | |
| 86 return null; | |
| 87 } | |
| 88 | |
| 89 Function _findOrAddWrapper(html.EventListener listener, bool useCapture) { | |
| 90 if (_wrappers === null) { | |
| 91 _wrappers = <EventListenerWrapper>[]; | |
| 92 } else { | |
| 93 for (EventListenerWrapper wrapper in _wrappers) { | |
| 94 if (wrapper.raw === listener && wrapper.useCapture == useCapture) { | |
| 95 return wrapper.wrapped; | |
| 96 } | |
| 97 } | |
| 98 } | |
| 99 final wrapped = (e) { listener(html.wrap_internal(e)); }; | |
| 100 _wrappers.add(new EventListenerWrapper(listener, wrapped, useCapture)); | |
| 101 return wrapped; | |
| 102 } | |
| 103 } | |
| OLD | NEW |