-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
91 lines (79 loc) · 3.07 KB
/
index.ts
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
import { DeviceType } from "../enums/DeviceType";
import { InteractionType } from "../enums/InteractionType";
import { OrientationType } from "../enums/OrientationType";
import { Ref, ref } from "vue";
export class AppDeviceDetect {
private p_interactionType: Ref<InteractionType.State>;
private p_orientationType: Ref<OrientationType.State>;
private p_deviceType: Ref<DeviceType.State>;
constructor() {
this.p_interactionType = ref(InteractionType.State.Unknown);
this.p_orientationType = ref(OrientationType.State.Unknown);
this.p_deviceType = ref(DeviceType.State.Unknown);
window.addEventListener("touchstart", () => this.activeTouchState(), { once: true });
window.addEventListener("mousemove", () => this.activeDeskState(), { once: true });
}
public get InteractionType() {
return this.p_interactionType.value;
}
public get OrientationType() {
return this.p_orientationType.value;
}
public get DeviceType() {
return this.p_deviceType.value;
}
public defineTouchscreen(): void {
if (window.PointerEvent && "maxTouchPoints" in navigator) {
// if Pointer Events are supported, just check maxTouchPoints
if (navigator.maxTouchPoints > 0) {
this.activeTouchState();
}
} else {
// no Pointer Events...
if (window.matchMedia && window.matchMedia("(any-pointer:coarse)").matches) {
// check for any-pointer:coarse which mostly means touchscreen
this.activeTouchState();
} else if (window.TouchEvent || "ontouchstart" in window) {
// last resort - check for exposed touch events API / event handler
this.activeTouchState();
}
}
}
private activeTouchState(): void {
if (this.p_interactionType.value === InteractionType.State.Mouse) {
this.p_interactionType.value = InteractionType.State.Both;
} else {
this.p_interactionType.value = InteractionType.State.Touch;
}
}
private activeDeskState(): void {
if (this.p_interactionType.value === InteractionType.State.Touch) {
this.p_interactionType.value = InteractionType.State.Both;
} else {
this.p_interactionType.value = InteractionType.State.Mouse;
}
}
private defineDeviceType(): void {
const ua = navigator.userAgent;
if (/(tablet|ipad|playbook|silk)|(android(?!.*mobi))/i.test(ua)) {
this.p_deviceType.value = DeviceType.State.Tab;
return;
}
if (
/Mobile|iP(hone|od)|Android|BlackBerry|IEMobile|Kindle|Silk-Accelerated|(hpw|web)OS|Opera M(obi|ini)/.test(
ua
)
) {
this.p_deviceType.value = DeviceType.State.Phone;
return;
}
this.p_deviceType.value = DeviceType.State.Desktop;
}
private defineOrientationType(): void {
if (window.matchMedia("(orientation: portrait)").matches) {
this.p_orientationType.value = OrientationType.State.Portrait;
} else if (window.matchMedia("(orientation: landscape)").matches) {
this.p_orientationType.value = OrientationType.State.Landscape;
}
}
}