-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
platform.js
164 lines (143 loc) · 4.34 KB
/
platform.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
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
// detect whether passive events are supported by the browser
const detectPassiveEvents = () => {
let result = false;
try {
const opts = Object.defineProperty({}, 'passive', {
get: function () {
result = true;
return false;
}
});
window.addEventListener('testpassive', null, opts);
window.removeEventListener('testpassive', null, opts);
} catch (e) {}
return result;
};
const ua = (typeof navigator !== 'undefined') ? navigator.userAgent : '';
const environment = typeof window !== 'undefined' ? 'browser' :
typeof global !== 'undefined' ? 'node' : 'worker';
// detect platform
const platformName =
(/android/i.test(ua) ? 'android' :
(/ip([ao]d|hone)/i.test(ua) ? 'ios' :
(/windows/i.test(ua) ? 'windows' :
(/mac os/i.test(ua) ? 'osx' :
(/linux/i.test(ua) ? 'linux' :
(/cros/i.test(ua) ? 'cros' : null))))));
// detect browser
const browserName =
(environment !== 'browser') ? null :
(/(Chrome\/|Chromium\/|Edg.*\/)/.test(ua) ? 'chrome' : // chrome, chromium, edge
(/Safari\//.test(ua) ? 'safari' : // safari, ios chrome/firefox
(/Firefox\//.test(ua) ? 'firefox' :
'other')));
const xbox = /xbox/i.test(ua);
const touch = (environment === 'browser') && ('ontouchstart' in window || ('maxTouchPoints' in navigator && navigator.maxTouchPoints > 0));
const gamepads = (environment === 'browser') && (!!navigator.getGamepads || !!navigator.webkitGetGamepads);
const workers = (typeof Worker !== 'undefined');
const passiveEvents = detectPassiveEvents();
/**
* Global namespace that stores flags regarding platform environment and features support.
*
* @namespace
* @example
* if (pc.platform.touch) {
* // touch is supported
* }
*/
const platform = {
/**
* String identifying the current platform. Can be one of: android, ios, windows, osx, linux,
* cros or null.
*
* @type {'android' | 'ios' | 'windows' | 'osx' | 'linux' | 'cros' | null}
* @ignore
*/
name: platformName,
/**
* String identifying the current runtime environment. Either 'browser', 'node' or 'worker'.
*
* @type {'browser' | 'node' | 'worker'}
*/
environment: environment,
/**
* The global object. This will be the window object when running in a browser and the global
* object when running in nodejs and self when running in a worker.
*
* @type {object}
*/
global: (typeof globalThis !== 'undefined' && globalThis) ??
(environment === 'browser' && window) ??
(environment === 'node' && global) ??
(environment === 'worker' && self),
/**
* Convenience boolean indicating whether we're running in the browser.
*
* @type {boolean}
*/
browser: environment === 'browser',
/**
* True if running on a desktop or laptop device.
*
* @type {boolean}
*/
desktop: ['windows', 'osx', 'linux', 'cros'].includes(platformName),
/**
* True if running on a mobile or tablet device.
*
* @type {boolean}
*/
mobile: ['android', 'ios'].includes(platformName),
/**
* True if running on an iOS device.
*
* @type {boolean}
*/
ios: platformName === 'ios',
/**
* True if running on an Android device.
*
* @type {boolean}
*/
android: platformName === 'android',
/**
* True if running on an Xbox device.
*
* @type {boolean}
*/
xbox: xbox,
/**
* True if the platform supports gamepads.
*
* @type {boolean}
*/
gamepads: gamepads,
/**
* True if the supports touch input.
*
* @type {boolean}
*/
touch: touch,
/**
* True if the platform supports Web Workers.
*
* @type {boolean}
*/
workers: workers,
/**
* True if the platform supports an options object as the third parameter to
* `EventTarget.addEventListener()` and the passive property is supported.
*
* @type {boolean}
* @ignore
*/
passiveEvents: passiveEvents,
/**
* Get the browser name.
*
* @type {'chrome' | 'safari' | 'firefox' | 'other' | null}
* @ignore
*/
browserName: browserName
};
export { platform };