Path: blob/trunk/third_party/closure/goog/events/actionhandler.js
2868 views
// Copyright 2007 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 file contains a class to provide a unified mechanism for16* CLICK and enter KEYDOWN events. This provides better accessibility by17* providing the given functionality to a keyboard user which is otherwise18* would be available only via a mouse click.19*20* If there is an existing CLICK listener or planning to be added as below -21*22* <code>this.eventHandler_.listen(el, CLICK, this.onClick_);<code>23*24* it can be replaced with an ACTION listener as follows:25*26* <code>this.eventHandler_.listen(27* new goog.events.ActionHandler(el),28* ACTION,29* this.onAction_);<code>30*31*/3233goog.provide('goog.events.ActionEvent');34goog.provide('goog.events.ActionHandler');35goog.provide('goog.events.ActionHandler.EventType');36goog.provide('goog.events.BeforeActionEvent');3738goog.require('goog.events');39goog.require('goog.events.BrowserEvent');40goog.require('goog.events.EventTarget');41goog.require('goog.events.EventType');42goog.require('goog.events.KeyCodes');43goog.require('goog.userAgent');44454647/**48* A wrapper around an element that you want to listen to ACTION events on.49* @param {Element|Document} element The element or document to listen on.50* @constructor51* @extends {goog.events.EventTarget}52* @final53*/54goog.events.ActionHandler = function(element) {55goog.events.EventTarget.call(this);5657/**58* This is the element that we will listen to events on.59* @type {Element|Document}60* @private61*/62this.element_ = element;6364goog.events.listen(65element, goog.events.ActionHandler.KEY_EVENT_TYPE_, this.handleKeyDown_,66false, this);67goog.events.listen(68element, goog.events.EventType.CLICK, this.handleClick_, false, this);69};70goog.inherits(goog.events.ActionHandler, goog.events.EventTarget);717273/**74* Enum type for the events fired by the action handler75* @enum {string}76*/77goog.events.ActionHandler.EventType = {78ACTION: 'action',79BEFOREACTION: 'beforeaction'80};818283/**84* Key event type to listen for.85* @type {string}86* @private87*/88goog.events.ActionHandler.KEY_EVENT_TYPE_ = goog.userAgent.GECKO ?89goog.events.EventType.KEYPRESS :90goog.events.EventType.KEYDOWN;919293/**94* Handles key press events.95* @param {!goog.events.BrowserEvent} e The key press event.96* @private97*/98goog.events.ActionHandler.prototype.handleKeyDown_ = function(e) {99if (e.keyCode == goog.events.KeyCodes.ENTER ||100goog.userAgent.WEBKIT && e.keyCode == goog.events.KeyCodes.MAC_ENTER) {101this.dispatchEvents_(e);102}103};104105106/**107* Handles mouse events.108* @param {!goog.events.BrowserEvent} e The click event.109* @private110*/111goog.events.ActionHandler.prototype.handleClick_ = function(e) {112this.dispatchEvents_(e);113};114115116/**117* Dispatches BeforeAction and Action events to the element118* @param {!goog.events.BrowserEvent} e The event causing dispatches.119* @private120*/121goog.events.ActionHandler.prototype.dispatchEvents_ = function(e) {122var beforeActionEvent = new goog.events.BeforeActionEvent(e);123124// Allow application specific logic here before the ACTION event.125// For example, Gmail uses this event to restore keyboard focus126if (!this.dispatchEvent(beforeActionEvent)) {127// If the listener swallowed the BEFOREACTION event, don't dispatch the128// ACTION event.129return;130}131132133// Wrap up original event and send it off134var actionEvent = new goog.events.ActionEvent(e);135try {136this.dispatchEvent(actionEvent);137} finally {138// Stop propagating the event139e.stopPropagation();140}141};142143144/** @override */145goog.events.ActionHandler.prototype.disposeInternal = function() {146goog.events.ActionHandler.superClass_.disposeInternal.call(this);147goog.events.unlisten(148this.element_, goog.events.ActionHandler.KEY_EVENT_TYPE_,149this.handleKeyDown_, false, this);150goog.events.unlisten(151this.element_, goog.events.EventType.CLICK, this.handleClick_, false,152this);153delete this.element_;154};155156157158/**159* This class is used for the goog.events.ActionHandler.EventType.ACTION event.160* @param {!goog.events.BrowserEvent} browserEvent Browser event object.161* @constructor162* @extends {goog.events.BrowserEvent}163* @final164*/165goog.events.ActionEvent = function(browserEvent) {166goog.events.BrowserEvent.call(this, browserEvent.getBrowserEvent());167this.type = goog.events.ActionHandler.EventType.ACTION;168};169goog.inherits(goog.events.ActionEvent, goog.events.BrowserEvent);170171172173/**174* This class is used for the goog.events.ActionHandler.EventType.BEFOREACTION175* event. BEFOREACTION gives a chance to the application so the keyboard focus176* can be restored back, if required.177* @param {!goog.events.BrowserEvent} browserEvent Browser event object.178* @constructor179* @extends {goog.events.BrowserEvent}180* @final181*/182goog.events.BeforeActionEvent = function(browserEvent) {183goog.events.BrowserEvent.call(this, browserEvent.getBrowserEvent());184this.type = goog.events.ActionHandler.EventType.BEFOREACTION;185};186goog.inherits(goog.events.BeforeActionEvent, goog.events.BrowserEvent);187188189