forked from yuristrelets/react-toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevents.js
More file actions
70 lines (61 loc) · 1.62 KB
/
events.js
File metadata and controls
70 lines (61 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
export default {
getMousePosition (event) {
return {
x: event.pageX - window.scrollX,
y: event.pageY - window.scrollY
};
},
getTouchPosition (event) {
return {
x: event.touches[0].pageX - window.scrollX,
y: event.touches[0].pageY - window.scrollY
};
},
pauseEvent (event) {
event.stopPropagation();
event.preventDefault();
},
addEventsToDocument (eventMap) {
for (const key in eventMap) {
document.addEventListener(key, eventMap[key], false);
}
},
removeEventsFromDocument (eventMap) {
for (const key in eventMap) {
document.removeEventListener(key, eventMap[key], false);
}
},
targetIsDescendant (event, parent) {
let node = event.target;
while (node !== null) {
if (node === parent) return true;
node = node.parentNode;
}
return false;
},
addEventListenerOnTransitionEnded (element, fn) {
const eventName = transitionEventNamesFor(element);
if (!eventName) return false;
element.addEventListener(eventName, fn);
return true;
},
removeEventListenerOnTransitionEnded (element) {
const eventName = transitionEventNamesFor(element);
if (!eventName) return false;
element.removeEventListener(eventName);
return true;
}
};
const TRANSITIONS = {
'transition': 'transitionend',
'OTransition': 'oTransitionEnd',
'MozTransition': 'transitionend',
'WebkitTransition': 'webkitTransitionEnd'
};
function transitionEventNamesFor (element) {
for (const transition in TRANSITIONS) {
if (element.style[transition] !== undefined) {
return TRANSITIONS[transition];
}
}
}