OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 /** | |
6 * Common events related helpers. | |
7 */ | |
8 class EventUtil { | |
9 | |
10 /** | |
11 * Add an event listener to an element. | |
12 * The event callback is specified by [handler]. | |
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 | |
15 * focus event, and added back on blur events. | |
16 */ | |
17 static void observe(Element element, | |
18 EventListenerList listenerList, Function handler, | |
19 [bool capture = false, bool removeHandlerOnFocus = false])
{ | |
20 listenerList.add(handler, capture); | |
21 // TODO(jacobr): this remove on focus behavior seems really ugly. | |
22 if (removeHandlerOnFocus) { | |
23 element.on.focus.add((e) { listenerList.remove(handler, capture); }); | |
24 element.on.blur.add((e) { listenerList.add(handler, capture); }); | |
25 } | |
26 } | |
27 | |
28 /** | |
29 * Clear the keyboard focus of the currently focused element (if there is | |
30 * one). If there is no currently focused element then this function will do | |
31 * nothing. For most browsers this will cause the keyboard to be dismissed. | |
32 */ | |
33 static void blurFocusedElement() { | |
34 Element focusedEl = document.query("*:focus"); | |
35 if (focusedEl != null) { | |
36 focusedEl.blur(); | |
37 } | |
38 } | |
39 } | |
OLD | NEW |