| 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. | |
| 29 static Event createEvent(String eventType) { | |
| 30 Event event = document.createEvent("HTMLEvents"); | |
| 31 event.initEvent(eventType, true, true); | |
| 32 return event; | |
| 33 } | |
| 34 | |
| 35 /** | 28 /** |
| 36 * Clear the keyboard focus of the currently focused element (if there is | 29 * 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 | 30 * 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. | 31 * nothing. For most browsers this will cause the keyboard to be dismissed. |
| 39 */ | 32 */ |
| 40 static void blurFocusedElement() { | 33 static void blurFocusedElement() { |
| 41 Element focusedEl = document.query("*:focus"); | 34 Element focusedEl = document.query("*:focus"); |
| 42 if (focusedEl != null) { | 35 if (focusedEl != null) { |
| 43 focusedEl.blur(); | 36 focusedEl.blur(); |
| 44 } | 37 } |
| 45 } | 38 } |
| 46 } | 39 } |
| OLD | NEW |