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
21goog.provide('goog.events.Event');
22goog.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 */
29goog.require('goog.Disposable');
30goog.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 */
41goog.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 */
55goog.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 * Stops event propagation.
109 */
110goog.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 */
118goog.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 */
130goog.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 */
141goog.events.Event.preventDefault = function(e) {
142 e.preventDefault();
143};