Path: blob/trunk/third_party/closure/goog/events/focushandler.js
2868 views
// Copyright 2006 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview This event handler allows you to catch focusin and focusout16* events on descendants. Unlike the "focus" and "blur" events which do not17* propagate consistently, and therefore must be added to the element that is18* focused, this allows you to attach one listener to an ancester and you will19* be notified when the focus state changes of ony of its descendants.20* @author [email protected] (Erik Arvidsson)21* @see ../demos/focushandler.html22*/2324goog.provide('goog.events.FocusHandler');25goog.provide('goog.events.FocusHandler.EventType');2627goog.require('goog.events');28goog.require('goog.events.BrowserEvent');29goog.require('goog.events.EventTarget');30goog.require('goog.userAgent');31323334/**35* This event handler allows you to catch focus events when descendants gain or36* loses focus.37* @param {Element|Document} element The node to listen on.38* @constructor39* @extends {goog.events.EventTarget}40* @final41*/42goog.events.FocusHandler = function(element) {43goog.events.EventTarget.call(this);4445/**46* This is the element that we will listen to the real focus events on.47* @type {Element|Document}48* @private49*/50this.element_ = element;5152// In IE we use focusin/focusout and in other browsers we use a capturing53// listner for focus/blur54var typeIn = goog.userAgent.IE ? 'focusin' : 'focus';55var typeOut = goog.userAgent.IE ? 'focusout' : 'blur';5657/**58* Store the listen key so it easier to unlisten in dispose.59* @private60* @type {goog.events.Key}61*/62this.listenKeyIn_ =63goog.events.listen(this.element_, typeIn, this, !goog.userAgent.IE);6465/**66* Store the listen key so it easier to unlisten in dispose.67* @private68* @type {goog.events.Key}69*/70this.listenKeyOut_ =71goog.events.listen(this.element_, typeOut, this, !goog.userAgent.IE);72};73goog.inherits(goog.events.FocusHandler, goog.events.EventTarget);747576/**77* Enum type for the events fired by the focus handler78* @enum {string}79*/80goog.events.FocusHandler.EventType = {81FOCUSIN: 'focusin',82FOCUSOUT: 'focusout'83};848586/**87* This handles the underlying events and dispatches a new event.88* @param {goog.events.BrowserEvent} e The underlying browser event.89*/90goog.events.FocusHandler.prototype.handleEvent = function(e) {91var be = e.getBrowserEvent();92var event = new goog.events.BrowserEvent(be);93event.type = e.type == 'focusin' || e.type == 'focus' ?94goog.events.FocusHandler.EventType.FOCUSIN :95goog.events.FocusHandler.EventType.FOCUSOUT;96this.dispatchEvent(event);97};9899100/** @override */101goog.events.FocusHandler.prototype.disposeInternal = function() {102goog.events.FocusHandler.superClass_.disposeInternal.call(this);103goog.events.unlistenByKey(this.listenKeyIn_);104goog.events.unlistenByKey(this.listenKeyOut_);105delete this.element_;106};107108109