Path: blob/trunk/third_party/closure/goog/events/event.js
2868 views
// Copyright 2005 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 A base class for event objects.16*17*/181920goog.provide('goog.events.Event');21goog.provide('goog.events.EventLike');2223/**24* goog.events.Event no longer depends on goog.Disposable. Keep requiring25* goog.Disposable here to not break projects which assume this dependency.26* @suppress {extraRequire}27*/28goog.require('goog.Disposable');29goog.require('goog.events.EventId');303132/**33* A typedef for event like objects that are dispatchable via the34* goog.events.dispatchEvent function. strings are treated as the type for a35* goog.events.Event. Objects are treated as an extension of a new36* goog.events.Event with the type property of the object being used as the type37* of the Event.38* @typedef {string|Object|goog.events.Event|goog.events.EventId}39*/40goog.events.EventLike;41424344/**45* A base class for event objects, so that they can support preventDefault and46* stopPropagation.47*48* @suppress {underscore} Several properties on this class are technically49* public, but referencing these properties outside this package is strongly50* discouraged.51*52* @param {string|!goog.events.EventId} type Event Type.53* @param {Object=} opt_target Reference to the object that is the target of54* this event. It has to implement the {@code EventTarget} interface55* declared at {@link http://developer.mozilla.org/en/DOM/EventTarget}.56* @constructor57*/58goog.events.Event = function(type, opt_target) {59/**60* Event type.61* @type {string}62*/63this.type = type instanceof goog.events.EventId ? String(type) : type;6465/**66* TODO(tbreisacher): The type should probably be67* EventTarget|goog.events.EventTarget.68*69* Target of the event.70* @type {Object|undefined}71*/72this.target = opt_target;7374/**75* Object that had the listener attached.76* @type {Object|undefined}77*/78this.currentTarget = this.target;7980/**81* Whether to cancel the event in internal capture/bubble processing for IE.82* @type {boolean}83* @public84*/85this.propagationStopped_ = false;8687/**88* Whether the default action has been prevented.89* This is a property to match the W3C specification at90* {@link http://www.w3.org/TR/DOM-Level-3-Events/91* #events-event-type-defaultPrevented}.92* Must be treated as read-only outside the class.93* @type {boolean}94*/95this.defaultPrevented = false;9697/**98* Return value for in internal capture/bubble processing for IE.99* @type {boolean}100* @public101*/102this.returnValue_ = true;103};104105106/**107* Stops event propagation.108*/109goog.events.Event.prototype.stopPropagation = function() {110this.propagationStopped_ = true;111};112113114/**115* Prevents the default action, for example a link redirecting to a url.116*/117goog.events.Event.prototype.preventDefault = function() {118this.defaultPrevented = true;119this.returnValue_ = false;120};121122123/**124* Stops the propagation of the event. It is equivalent to125* {@code e.stopPropagation()}, but can be used as the callback argument of126* {@link goog.events.listen} without declaring another function.127* @param {!goog.events.Event} e An event.128*/129goog.events.Event.stopPropagation = function(e) {130e.stopPropagation();131};132133134/**135* Prevents the default action. It is equivalent to136* {@code e.preventDefault()}, but can be used as the callback argument of137* {@link goog.events.listen} without declaring another function.138* @param {!goog.events.Event} e An event.139*/140goog.events.Event.preventDefault = function(e) {141e.preventDefault();142};143144145