| 1 |
// Copyright 2006 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
/** |
| 16 |
* @fileoverview This event handler allows you to catch focusin and focusout |
| 17 |
* events on descendants. Unlike the "focus" and "blur" events which do not |
| 18 |
* propagate consistently, and therefore must be added to the element that is |
| 19 |
* focused, this allows you to attach one listener to an ancester and you will |
| 20 |
* be notified when the focus state changes of ony of its descendants. |
| 21 |
* @author [email protected] (Erik Arvidsson) |
| 22 |
* @see ../demos/focushandler.html |
| 23 |
*/ |
| 24 |
|
| 25 |
goog.provide('goog.events.FocusHandler'); |
| 26 |
goog.provide('goog.events.FocusHandler.EventType'); |
| 27 |
|
| 28 |
goog.require('goog.events'); |
| 29 |
goog.require('goog.events.BrowserEvent'); |
| 30 |
goog.require('goog.events.EventTarget'); |
| 31 |
goog.require('goog.userAgent'); |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
/** |
| 36 |
* This event handler allows you to catch focus events when descendants gain or |
| 37 |
* loses focus. |
| 38 |
* @param {Element|Document} element The node to listen on. |
| 39 |
* @constructor |
| 40 |
* @extends {goog.events.EventTarget} |
| 41 |
* @final |
| 42 |
*/ |
| 43 |
goog.events.FocusHandler = function(element) { |
| 44 |
goog.events.EventTarget.call(this); |
| 45 |
|
| 46 |
/** |
| 47 |
* This is the element that we will listen to the real focus events on. |
| 48 |
* @type {Element|Document} |
| 49 |
* @private |
| 50 |
*/ |
| 51 |
this.element_ = element; |
| 52 |
|
| 53 |
// In IE we use focusin/focusout and in other browsers we use a capturing |
| 54 |
// listner for focus/blur |
| 55 |
var typeIn = goog.userAgent.IE ? 'focusin' : 'focus'; |
| 56 |
var typeOut = goog.userAgent.IE ? 'focusout' : 'blur'; |
| 57 |
|
| 58 |
/** |
| 59 |
* Store the listen key so it easier to unlisten in dispose. |
| 60 |
* @private |
| 61 |
* @type {goog.events.Key} |
| 62 |
*/ |
| 63 |
this.listenKeyIn_ = |
| 64 |
goog.events.listen(this.element_, typeIn, this, !goog.userAgent.IE); |
| 65 |
|
| 66 |
/** |
| 67 |
* Store the listen key so it easier to unlisten in dispose. |
| 68 |
* @private |
| 69 |
* @type {goog.events.Key} |
| 70 |
*/ |
| 71 |
this.listenKeyOut_ = |
| 72 |
goog.events.listen(this.element_, typeOut, this, !goog.userAgent.IE); |
| 73 |
}; |
| 74 |
goog.inherits(goog.events.FocusHandler, goog.events.EventTarget); |
| 75 |
|
| 76 |
|
| 77 |
/** |
| 78 |
* Enum type for the events fired by the focus handler |
| 79 |
* @enum {string} |
| 80 |
*/ |
| 81 |
goog.events.FocusHandler.EventType = { |
| 82 |
FOCUSIN: 'focusin', |
| 83 |
FOCUSOUT: 'focusout' |
| 84 |
}; |
| 85 |
|
| 86 |
|
| 87 |
/** |
| 88 |
* This handles the underlying events and dispatches a new event. |
| 89 |
* @param {goog.events.BrowserEvent} e The underlying browser event. |
| 90 |
*/ |
| 91 |
goog.events.FocusHandler.prototype.handleEvent = function(e) { |
| 92 |
var be = e.getBrowserEvent(); |
| 93 |
var event = new goog.events.BrowserEvent(be); |
| 94 |
event.type = e.type == 'focusin' || e.type == 'focus' ? |
| 95 |
goog.events.FocusHandler.EventType.FOCUSIN : |
| 96 |
goog.events.FocusHandler.EventType.FOCUSOUT; |
| 97 |
this.dispatchEvent(event); |
| 98 |
}; |
| 99 |
|
| 100 |
|
| 101 |
/** @override */ |
| 102 |
goog.events.FocusHandler.prototype.disposeInternal = function() { |
| 103 |
goog.events.FocusHandler.superClass_.disposeInternal.call(this); |
| 104 |
goog.events.unlistenByKey(this.listenKeyIn_); |
| 105 |
goog.events.unlistenByKey(this.listenKeyOut_); |
| 106 |
delete this.element_; |
| 107 |
}; |