# lib/goog/events/event.js

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 | * @param {string|!goog.events.EventId} type Event Type.
50 | * @param {Object=} opt_target Reference to the object that is the target of
51 | * this event. It has to implement the {@code EventTarget} interface
52 | * declared at {@link http://developer.mozilla.org/en/DOM/EventTarget}.
53 | * @constructor
54 | */
55 | goog.events.Event = function(type, opt_target) {
56 | /**
57 | * Event type.
58 | * @type {string}
59 | */
60 | this.type = type instanceof goog.events.EventId ? String(type) : type;
61 |
62 | /**
63 | * TODO(tbreisacher): The type should probably be
64 | * EventTarget|goog.events.EventTarget.
65 | *
66 | * Target of the event.
67 | * @type {Object|undefined}
68 | */
69 | this.target = opt_target;
70 |
71 | /**
72 | * Object that had the listener attached.
73 | * @type {Object|undefined}
74 | */
75 | this.currentTarget = this.target;
76 |
77 | /**
78 | * Whether to cancel the event in internal capture/bubble processing for IE.
79 | * @type {boolean}
80 | * @public
81 | * @suppress {underscore|visibility} Technically public, but referencing this
82 | * outside this package is strongly discouraged.
83 | */
84 | this.propagationStopped_ = false;
85 |
86 | /**
87 | * Whether the default action has been prevented.
88 | * This is a property to match the W3C specification at
89 | * {@link http://www.w3.org/TR/DOM-Level-3-Events/
90 | * #events-event-type-defaultPrevented}.
91 | * Must be treated as read-only outside the class.
92 | * @type {boolean}
93 | */
94 | this.defaultPrevented = false;
95 |
96 | /**
97 | * Return value for in internal capture/bubble processing for IE.
98 | * @type {boolean}
99 | * @public
100 | * @suppress {underscore|visibility} Technically public, but referencing this
101 | * outside this package is strongly discouraged.
102 | */
103 | this.returnValue_ = true;
104 | };
105 |
106 |
107 | /**
108 | * For backwards compatibility (goog.events.Event used to inherit
109 | * goog.Disposable).
110 | * @deprecated Events don't need to be disposed.
111 | */
112 | goog.events.Event.prototype.disposeInternal = function() {
113 | };
114 |
115 |
116 | /**
117 | * For backwards compatibility (goog.events.Event used to inherit
118 | * goog.Disposable).
119 | * @deprecated Events don't need to be disposed.
120 | */
121 | goog.events.Event.prototype.dispose = function() {
122 | };
123 |
124 |
125 | /**
126 | * Stops event propagation.
127 | */
128 | goog.events.Event.prototype.stopPropagation = function() {
129 | this.propagationStopped_ = true;
130 | };
131 |
132 |
133 | /**
134 | * Prevents the default action, for example a link redirecting to a url.
135 | */
136 | goog.events.Event.prototype.preventDefault = function() {
137 | this.defaultPrevented = true;
138 | this.returnValue_ = false;
139 | };
140 |
141 |
142 | /**
143 | * Stops the propagation of the event. It is equivalent to
144 | * {@code e.stopPropagation()}, but can be used as the callback argument of
145 | * {@link goog.events.listen} without declaring another function.
146 | * @param {!goog.events.Event} e An event.
147 | */
148 | goog.events.Event.stopPropagation = function(e) {
149 | e.stopPropagation();
150 | };
151 |
152 |
153 | /**
154 | * Prevents the default action. It is equivalent to
155 | * {@code e.preventDefault()}, but can be used as the callback argument of
156 | * {@link goog.events.listen} without declaring another function.
157 | * @param {!goog.events.Event} e An event.
158 | */
159 | goog.events.Event.preventDefault = function(e) {
160 | e.preventDefault();
161 | };