Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * Common events related helpers. | 6 * Common events related helpers. |
| 7 */ | 7 */ |
| 8 class EventUtil { | 8 class EventUtil { |
| 9 | 9 |
| 10 /** | 10 /** |
| 11 * Add an event listener to an element. | 11 * Add an event listener to an element. |
| 12 * The event callback is specified by [handler]. | 12 * The event callback is specified by [handler]. |
| 13 * If [capture] is true, the listener gets events on the capture phase. | 13 * If [capture] is true, the listener gets events on the capture phase. |
| 14 * If [removeHandlerOnFocus] is true the handler is removed when there is any | 14 * If [removeHandlerOnFocus] is true the handler is removed when there is any |
| 15 * focus event, and added back on blur events. | 15 * focus event, and added back on blur events. |
| 16 */ | 16 */ |
| 17 static void observe(Element element, | 17 static void observe(Element element, |
| 18 EventListenerList listenerList, Function handler, | 18 EventListenerList listenerList, Function handler, |
| 19 [bool capture = false, bool removeHandlerOnFocus = false]) { | 19 [bool capture = false, bool removeHandlerOnFocus = false]) { |
| 20 listenerList.add(handler, capture); | 20 listenerList.add(handler, capture); |
| 21 // TODO(jacobr): this remove on focus behavior seems really ugly. | 21 // TODO(jacobr): this remove on focus behavior seems really ugly. |
| 22 if (removeHandlerOnFocus) { | 22 if (removeHandlerOnFocus) { |
| 23 element.on.focus.add((e) { listenerList.remove(handler, capture); }); | 23 element.on.focus.add((e) { listenerList.remove(handler, capture); }); |
| 24 element.on.blur.add((e) { listenerList.add(handler, capture); }); | 24 element.on.blur.add((e) { listenerList.add(handler, capture); }); |
| 25 } | 25 } |
| 26 } | 26 } |
| 27 | 27 |
| 28 // TODO(jacobr): add a comment or remove this method. | 28 // TODO(jacobr): add a comment or remove this method. |
| 29 static Event createEvent(String eventType) { | 29 static Event createEvent(String eventType) => |
|
Jacob
2011/10/27 23:16:05
take this a step further and kill off this now use
nweiz
2011/10/27 23:47:23
Done.
| |
| 30 Event event = document.createEvent("HTMLEvents"); | 30 new Event(eventType, canBubble: true, cancelable: true); |
| 31 event.initEvent(eventType, true, true); | |
| 32 return event; | |
| 33 } | |
| 34 | 31 |
| 35 /** | 32 /** |
| 36 * Clear the keyboard focus of the currently focused element (if there is | 33 * Clear the keyboard focus of the currently focused element (if there is |
| 37 * one). If there is no currently focused element then this function will do | 34 * one). If there is no currently focused element then this function will do |
| 38 * nothing. For most browsers this will cause the keyboard to be dismissed. | 35 * nothing. For most browsers this will cause the keyboard to be dismissed. |
| 39 */ | 36 */ |
| 40 static void blurFocusedElement() { | 37 static void blurFocusedElement() { |
| 41 Element focusedEl = document.query("*:focus"); | 38 Element focusedEl = document.query("*:focus"); |
| 42 if (focusedEl != null) { | 39 if (focusedEl != null) { |
| 43 focusedEl.blur(); | 40 focusedEl.blur(); |
| 44 } | 41 } |
| 45 } | 42 } |
| 46 } | 43 } |
| OLD | NEW |