-
Notifications
You must be signed in to change notification settings - Fork 0
/
mouse_or_touch.js
39 lines (37 loc) · 1.58 KB
/
mouse_or_touch.js
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
"use strict";
/**
Created by Complynx on 22.03.2019,
http://complynx.net
<[email protected]> Daniel Drizhuk
*/
let cache;
/**
* if first event is mouse event, it's mouse, if first event is touch event, it's touch.
* @returns {Promise<string>} "mouse"|"touch"
*/
export function detect() {
return new Promise((resolve)=>{
if(cache) return resolve(cache);
let mouse_detector, remove_mouse_detector = () => {
window.removeEventListener("mousemove", mouse_detector, {passive: true});
window.removeEventListener("mousedown", mouse_detector, {passive: true});
window.removeEventListener("mouseover", mouse_detector, {passive: true});
window.removeEventListener("touchstart", mouse_detector, {once: false});
window.removeEventListener("touchmove", mouse_detector, {once: false});
};
mouse_detector = ev => {
if (ev.type.startsWith('mouse') && ev.buttons === 0) {
resolve(cache = "mouse");
remove_mouse_detector();
} else if (ev.type.startsWith('touch')) {
resolve(cache = "touch");
remove_mouse_detector();
}
};
window.addEventListener("mousemove", mouse_detector, {passive: true});
window.addEventListener("mousedown", mouse_detector, {passive: true});
window.addEventListener("mouseover", mouse_detector, {passive: true});
window.addEventListener("touchstart", mouse_detector, {once: false});
window.addEventListener("touchmove", mouse_detector, {once: false});
});
}