-
-
Notifications
You must be signed in to change notification settings - Fork 5.9k
/
Browser.js
68 lines (57 loc) · 2.62 KB
/
Browser.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
/*
* @namespace Browser
* @aka L.Browser
*
* A namespace with static properties for browser/feature detection used by Leaflet internally.
*
* @example
*
* ```js
* if (L.Browser.chrome) {
* alert('You are running Chrome!');
* }
* ```
*/
// @property chrome: Boolean; `true` for the Chrome browser.
const chrome = userAgentContains('chrome');
// @property safari: Boolean; `true` for the Safari browser.
const safari = !chrome && userAgentContains('safari');
// @property mobile: Boolean; `true` for all browsers running in a mobile device.
const mobile = typeof orientation !== 'undefined' || userAgentContains('mobile');
// @property pointer: Boolean
// `true` for all browsers supporting [pointer events](https://msdn.microsoft.com/en-us/library/dn433244%28v=vs.85%29.aspx).
const pointer = typeof window === 'undefined' ? false : !!window.PointerEvent;
// @property touchNative: Boolean
// `true` for all browsers supporting [touch events](https://developer.mozilla.org/docs/Web/API/Touch_events).
// **This does not necessarily mean** that the browser is running in a computer with
// a touchscreen, it only means that the browser is capable of understanding
// touch events.
const touchNative = typeof window === 'undefined' ? false : 'ontouchstart' in window || !!(window.TouchEvent);
// @property touch: Boolean
// `true` for all browsers supporting either [touch](#browser-touch) or [pointer](#browser-pointer) events.
// Note: pointer events will be preferred (if available), and processed for all `touch*` listeners.
const touch = touchNative || pointer;
// @property retina: Boolean
// `true` for browsers on a high-resolution "retina" screen or on any screen when browser's display zoom is more than 100%.
const retina = typeof window === 'undefined' || typeof window.devicePixelRatio === 'undefined' ? false : window.devicePixelRatio > 1;
// @property mac: Boolean; `true` when the browser is running in a Mac platform
const mac = typeof navigator === 'undefined' || typeof navigator.platform === 'undefined' ? false : navigator.platform.startsWith('Mac');
// @property mac: Boolean; `true` when the browser is running in a Linux platform
const linux = typeof navigator === 'undefined' || typeof navigator.platform === 'undefined' ? false : navigator.platform.startsWith('Linux');
function userAgentContains(str) {
if (typeof navigator === 'undefined' || typeof navigator.userAgent === 'undefined') {
return false;
}
return navigator.userAgent.toLowerCase().includes(str);
}
export default {
chrome,
safari,
mobile,
pointer,
touch,
touchNative,
retina,
mac,
linux
};