Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81155 views
1
/**
2
* Copyright 2013-2014, Facebook, Inc.
3
* All rights reserved.
4
*
5
* This source code is licensed under the BSD-style license found in the
6
* LICENSE file in the root directory of this source tree. An additional grant
7
* of patent rights can be found in the PATENTS file in the same directory.
8
*
9
* @providesModule SyntheticEvent
10
* @typechecks static-only
11
*/
12
13
"use strict";
14
15
var PooledClass = require('PooledClass');
16
17
var assign = require('Object.assign');
18
var emptyFunction = require('emptyFunction');
19
var getEventTarget = require('getEventTarget');
20
21
/**
22
* @interface Event
23
* @see http://www.w3.org/TR/DOM-Level-3-Events/
24
*/
25
var EventInterface = {
26
type: null,
27
target: getEventTarget,
28
// currentTarget is set when dispatching; no use in copying it here
29
currentTarget: emptyFunction.thatReturnsNull,
30
eventPhase: null,
31
bubbles: null,
32
cancelable: null,
33
timeStamp: function(event) {
34
return event.timeStamp || Date.now();
35
},
36
defaultPrevented: null,
37
isTrusted: null
38
};
39
40
/**
41
* Synthetic events are dispatched by event plugins, typically in response to a
42
* top-level event delegation handler.
43
*
44
* These systems should generally use pooling to reduce the frequency of garbage
45
* collection. The system should check `isPersistent` to determine whether the
46
* event should be released into the pool after being dispatched. Users that
47
* need a persisted event should invoke `persist`.
48
*
49
* Synthetic events (and subclasses) implement the DOM Level 3 Events API by
50
* normalizing browser quirks. Subclasses do not necessarily have to implement a
51
* DOM interface; custom application-specific events can also subclass this.
52
*
53
* @param {object} dispatchConfig Configuration used to dispatch this event.
54
* @param {string} dispatchMarker Marker identifying the event target.
55
* @param {object} nativeEvent Native browser event.
56
*/
57
function SyntheticEvent(dispatchConfig, dispatchMarker, nativeEvent) {
58
this.dispatchConfig = dispatchConfig;
59
this.dispatchMarker = dispatchMarker;
60
this.nativeEvent = nativeEvent;
61
62
var Interface = this.constructor.Interface;
63
for (var propName in Interface) {
64
if (!Interface.hasOwnProperty(propName)) {
65
continue;
66
}
67
var normalize = Interface[propName];
68
if (normalize) {
69
this[propName] = normalize(nativeEvent);
70
} else {
71
this[propName] = nativeEvent[propName];
72
}
73
}
74
75
var defaultPrevented = nativeEvent.defaultPrevented != null ?
76
nativeEvent.defaultPrevented :
77
nativeEvent.returnValue === false;
78
if (defaultPrevented) {
79
this.isDefaultPrevented = emptyFunction.thatReturnsTrue;
80
} else {
81
this.isDefaultPrevented = emptyFunction.thatReturnsFalse;
82
}
83
this.isPropagationStopped = emptyFunction.thatReturnsFalse;
84
}
85
86
assign(SyntheticEvent.prototype, {
87
88
preventDefault: function() {
89
this.defaultPrevented = true;
90
var event = this.nativeEvent;
91
event.preventDefault ? event.preventDefault() : event.returnValue = false;
92
this.isDefaultPrevented = emptyFunction.thatReturnsTrue;
93
},
94
95
stopPropagation: function() {
96
var event = this.nativeEvent;
97
event.stopPropagation ? event.stopPropagation() : event.cancelBubble = true;
98
this.isPropagationStopped = emptyFunction.thatReturnsTrue;
99
},
100
101
/**
102
* We release all dispatched `SyntheticEvent`s after each event loop, adding
103
* them back into the pool. This allows a way to hold onto a reference that
104
* won't be added back into the pool.
105
*/
106
persist: function() {
107
this.isPersistent = emptyFunction.thatReturnsTrue;
108
},
109
110
/**
111
* Checks if this event should be released back into the pool.
112
*
113
* @return {boolean} True if this should not be released, false otherwise.
114
*/
115
isPersistent: emptyFunction.thatReturnsFalse,
116
117
/**
118
* `PooledClass` looks for `destructor` on each instance it releases.
119
*/
120
destructor: function() {
121
var Interface = this.constructor.Interface;
122
for (var propName in Interface) {
123
this[propName] = null;
124
}
125
this.dispatchConfig = null;
126
this.dispatchMarker = null;
127
this.nativeEvent = null;
128
}
129
130
});
131
132
SyntheticEvent.Interface = EventInterface;
133
134
/**
135
* Helper to reduce boilerplate when creating subclasses.
136
*
137
* @param {function} Class
138
* @param {?object} Interface
139
*/
140
SyntheticEvent.augmentClass = function(Class, Interface) {
141
var Super = this;
142
143
var prototype = Object.create(Super.prototype);
144
assign(prototype, Class.prototype);
145
Class.prototype = prototype;
146
Class.prototype.constructor = Class;
147
148
Class.Interface = assign({}, Super.Interface, Interface);
149
Class.augmentClass = Super.augmentClass;
150
151
PooledClass.addPoolingTo(Class, PooledClass.threeArgumentPooler);
152
};
153
154
PooledClass.addPoolingTo(SyntheticEvent, PooledClass.threeArgumentPooler);
155
156
module.exports = SyntheticEvent;
157
158