Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80529 views
1
/**
2
* Copyright 2013-2015, Facebook, Inc.
3
*
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
7
*
8
* http://www.apache.org/licenses/LICENSE-2.0
9
*
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
15
*
16
* @providesModule EventListener
17
* @typechecks
18
*/
19
20
var emptyFunction = require("./emptyFunction");
21
22
/**
23
* Upstream version of event listener. Does not take into account specific
24
* nature of platform.
25
*/
26
var EventListener = {
27
/**
28
* Listen to DOM events during the bubble phase.
29
*
30
* @param {DOMEventTarget} target DOM element to register listener on.
31
* @param {string} eventType Event type, e.g. 'click' or 'mouseover'.
32
* @param {function} callback Callback function.
33
* @return {object} Object with a `remove` method.
34
*/
35
listen: function(target, eventType, callback) {
36
if (target.addEventListener) {
37
target.addEventListener(eventType, callback, false);
38
return {
39
remove: function() {
40
target.removeEventListener(eventType, callback, false);
41
}
42
};
43
} else if (target.attachEvent) {
44
target.attachEvent('on' + eventType, callback);
45
return {
46
remove: function() {
47
target.detachEvent('on' + eventType, callback);
48
}
49
};
50
}
51
},
52
53
/**
54
* Listen to DOM events during the capture phase.
55
*
56
* @param {DOMEventTarget} target DOM element to register listener on.
57
* @param {string} eventType Event type, e.g. 'click' or 'mouseover'.
58
* @param {function} callback Callback function.
59
* @return {object} Object with a `remove` method.
60
*/
61
capture: function(target, eventType, callback) {
62
if (!target.addEventListener) {
63
if ("production" !== process.env.NODE_ENV) {
64
console.error(
65
'Attempted to listen to events during the capture phase on a ' +
66
'browser that does not support the capture phase. Your application ' +
67
'will not receive some events.'
68
);
69
}
70
return {
71
remove: emptyFunction
72
};
73
} else {
74
target.addEventListener(eventType, callback, true);
75
return {
76
remove: function() {
77
target.removeEventListener(eventType, callback, true);
78
}
79
};
80
}
81
},
82
83
registerDefault: function() {}
84
};
85
86
module.exports = EventListener;
87
88