| 1 |
// Copyright 2005 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 A base class for event objects. |
| 17 |
* |
| 18 |
*/ |
| 19 |
|
| 20 |
|
| 21 |
goog.provide('goog.events.Event'); |
| 22 |
goog.provide('goog.events.EventLike'); |
| 23 |
|
| 24 |
/** |
| 25 |
* goog.events.Event no longer depends on goog.Disposable. Keep requiring |
| 26 |
* goog.Disposable here to not break projects which assume this dependency. |
| 27 |
* @suppress {extraRequire} |
| 28 |
*/ |
| 29 |
goog.require('goog.Disposable'); |
| 30 |
goog.require('goog.events.EventId'); |
| 31 |
|
| 32 |
|
| 33 |
/** |
| 34 |
* A typedef for event like objects that are dispatchable via the |
| 35 |
* goog.events.dispatchEvent function. strings are treated as the type for a |
| 36 |
* goog.events.Event. Objects are treated as an extension of a new |
| 37 |
* goog.events.Event with the type property of the object being used as the type |
| 38 |
* of the Event. |
| 39 |
* @typedef {string|Object|goog.events.Event|goog.events.EventId} |
| 40 |
*/ |
| 41 |
goog.events.EventLike; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
/** |
| 46 |
* A base class for event objects, so that they can support preventDefault and |
| 47 |
* stopPropagation. |
| 48 |
* |
| 49 |
* @suppress {underscore} Several properties on this class are technically |
| 50 |
* public, but referencing these properties outside this package is strongly |
| 51 |
* discouraged. |
| 52 |
* |
| 53 |
* @param {string|!goog.events.EventId} type Event Type. |
| 54 |
* @param {Object=} opt_target Reference to the object that is the target of |
| 55 |
* this event. It has to implement the {@code EventTarget} interface |
| 56 |
* declared at {@link http://developer.mozilla.org/en/DOM/EventTarget}. |
| 57 |
* @constructor |
| 58 |
*/ |
| 59 |
goog.events.Event = function(type, opt_target) { |
| 60 |
/** |
| 61 |
* Event type. |
| 62 |
* @type {string} |
| 63 |
*/ |
| 64 |
this.type = type instanceof goog.events.EventId ? String(type) : type; |
| 65 |
|
| 66 |
/** |
| 67 |
* TODO(tbreisacher): The type should probably be |
| 68 |
* EventTarget|goog.events.EventTarget. |
| 69 |
* |
| 70 |
* Target of the event. |
| 71 |
* @type {Object|undefined} |
| 72 |
*/ |
| 73 |
this.target = opt_target; |
| 74 |
|
| 75 |
/** |
| 76 |
* Object that had the listener attached. |
| 77 |
* @type {Object|undefined} |
| 78 |
*/ |
| 79 |
this.currentTarget = this.target; |
| 80 |
|
| 81 |
/** |
| 82 |
* Whether to cancel the event in internal capture/bubble processing for IE. |
| 83 |
* @type {boolean} |
| 84 |
* @public |
| 85 |
*/ |
| 86 |
this.propagationStopped_ = false; |
| 87 |
|
| 88 |
/** |
| 89 |
* Whether the default action has been prevented. |
| 90 |
* This is a property to match the W3C specification at |
| 91 |
* {@link http://www.w3.org/TR/DOM-Level-3-Events/ |
| 92 |
* #events-event-type-defaultPrevented}. |
| 93 |
* Must be treated as read-only outside the class. |
| 94 |
* @type {boolean} |
| 95 |
*/ |
| 96 |
this.defaultPrevented = false; |
| 97 |
|
| 98 |
/** |
| 99 |
* Return value for in internal capture/bubble processing for IE. |
| 100 |
* @type {boolean} |
| 101 |
* @public |
| 102 |
*/ |
| 103 |
this.returnValue_ = true; |
| 104 |
}; |
| 105 |
|
| 106 |
|
| 107 |
/** |
| 108 |
* Stops event propagation. |
| 109 |
*/ |
| 110 |
goog.events.Event.prototype.stopPropagation = function() { |
| 111 |
this.propagationStopped_ = true; |
| 112 |
}; |
| 113 |
|
| 114 |
|
| 115 |
/** |
| 116 |
* Prevents the default action, for example a link redirecting to a url. |
| 117 |
*/ |
| 118 |
goog.events.Event.prototype.preventDefault = function() { |
| 119 |
this.defaultPrevented = true; |
| 120 |
this.returnValue_ = false; |
| 121 |
}; |
| 122 |
|
| 123 |
|
| 124 |
/** |
| 125 |
* Stops the propagation of the event. It is equivalent to |
| 126 |
* {@code e.stopPropagation()}, but can be used as the callback argument of |
| 127 |
* {@link goog.events.listen} without declaring another function. |
| 128 |
* @param {!goog.events.Event} e An event. |
| 129 |
*/ |
| 130 |
goog.events.Event.stopPropagation = function(e) { |
| 131 |
e.stopPropagation(); |
| 132 |
}; |
| 133 |
|
| 134 |
|
| 135 |
/** |
| 136 |
* Prevents the default action. It is equivalent to |
| 137 |
* {@code e.preventDefault()}, but can be used as the callback argument of |
| 138 |
* {@link goog.events.listen} without declaring another function. |
| 139 |
* @param {!goog.events.Event} e An event. |
| 140 |
*/ |
| 141 |
goog.events.Event.preventDefault = function(e) { |
| 142 |
e.preventDefault(); |
| 143 |
}; |