-
Notifications
You must be signed in to change notification settings - Fork 526
Expand file tree
/
Copy pathdom.js
More file actions
172 lines (152 loc) · 4.52 KB
/
Copy pathdom.js
File metadata and controls
172 lines (152 loc) · 4.52 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
exports.addReadyListener = function (callback) {
if (document.readyState === 'complete') {
setTimeout(callback, 1);
} else {
window.addEventListener('load', callback, false);
}
};
exports.getTouchEventName = function (eventName) {
var isIE11Touch = window.navigator.pointerEnabled;
var isIE10Touch = window.navigator.msPointerEnabled;
var isStandardTouch = !(isIE11Touch || isIE10Touch);
var key;
if (isIE11Touch) {
key = 'ie11';
} else if (isIE10Touch) {
key = 'ie10';
} else if (isStandardTouch) {
key = 'standard';
}
if (key && TOUCH_MAP[eventName]) {
return TOUCH_MAP[eventName][key];
}
};
/**
* Add an event listener
* @param {HTMLElement} element
* @param {string} eventName
* @param {function) handler
* @param {boolean} suppressTouchDefault - Should we preventDefault on touch events
*/
var addEvent = function (
element,
eventName,
handler,
suppressTouchDefault = true
) {
// Scope bound event map to this addEvent call - we only provide for unbinding
// what we bind right here.
var boundEvents = {};
var bindEvent = function (type, eventName, handler) {
element.addEventListener(eventName, handler, false);
boundEvents[type] = {name: eventName, handler: handler};
};
var unbindEvent = function (type) {
var eventInfo = boundEvents[type];
if (eventInfo) {
element.removeEventListener(eventInfo.name, eventInfo.handler);
delete boundEvents[type];
}
};
// Add click handler
bindEvent('click', eventName, handler);
// Optionally add touch handler
var touchEvent = exports.getTouchEventName(eventName);
if (touchEvent) {
bindEvent('touch', touchEvent, function (e) {
// Stop mouse events and suppress default event handler to prevent
// unintentional double-clicking
if (suppressTouchDefault) {
e.preventDefault();
}
// ---- Workaround for IE 11 (2019) ----
// Background: PreventDefault is not recognized in IE. In IE 11, a click
// event will fire the following events:
// MSPointerDown -> pointerdown -> MSPointerUp -> pointerup -> click
// This exact same sequence of events happens whether the event originated
// from a touch, a trackpad, or a mouse. mousedown, mouseup, and mousemove
// events behave similarly. Because of this behavior, we can (and should)
// remove the event handlers that are not specific to IE 11 as soon as an
// IE 11 event is fired. This will prevent duplicate events from happening
// such as double click.
let IEEvents = [
'pointerdown',
'MSPointerDown',
'pointermove',
'MSPointerMove',
'pointerup',
'MSPointerUp',
];
if (IEEvents.includes(touchEvent)) {
unbindEvent('click');
}
handler?.call(this, e);
});
}
// Return function that unbinds all handlers
return function () {
unbindEvent('click');
unbindEvent('touch');
};
};
exports.addMouseDownTouchEvent = function (element, handler) {
return addEvent(element, 'mousedown', handler);
};
exports.addMouseUpTouchEvent = function (
element,
handler,
suppressTouchDefault = true
) {
return addEvent(element, 'mouseup', handler, suppressTouchDefault);
};
exports.addMouseMoveTouchEvent = function (element, handler) {
return addEvent(element, 'mousemove', handler);
};
exports.addClickTouchEvent = function (element, handler) {
return addEvent(element, 'click', handler);
};
// A map from standard touch events to various aliases.
var TOUCH_MAP = {
// Incomplete list, add as needed.
click: {
standard: 'touchstart',
ie10: 'MSPointerDown',
ie11: 'pointerdown',
},
mousedown: {
standard: 'touchstart',
ie10: 'MSPointerDown',
ie11: 'pointerdown',
},
mouseup: {
standard: 'touchend',
ie10: 'MSPointerUp',
ie11: 'pointerup',
},
mousemove: {
standard: 'touchmove',
ie10: 'MSPointerMove',
ie11: 'pointermove',
},
};
exports.TOUCH_MAP = TOUCH_MAP;
exports.isMobile = function () {
var reg = /Mobile|iP(hone|od|ad)|Android|BlackBerry|IEMobile/;
return reg.test(window.navigator.userAgent);
};
exports.isWindowsTouch = function () {
var reg = /MSIE.*Touch/;
return reg.test(window.navigator.userAgent);
};
exports.isAndroid = function () {
var reg = /Android/;
return reg.test(window.navigator.userAgent);
};
exports.isIOS = function () {
var reg = /iP(hone|od|ad)/;
return reg.test(window.navigator.userAgent);
};
exports.isIPad = function () {
var reg = /iPad/i;
return reg.test(window.navigator.userAgent);
};