Commit | Line | Data |
---|---|---|
bd969ed4 AE |
1 | /** |
2 | * Provides basic details on the JavaScript environment. | |
3 | * | |
4 | * @author Alexander Ebert | |
50d96bd8 | 5 | * @copyright 2001-2017 WoltLab GmbH |
bd969ed4 | 6 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> |
58d7e8f8 | 7 | * @module WoltLabSuite/Core/Environment |
bd969ed4 AE |
8 | */ |
9 | define([], function() { | |
10 | "use strict"; | |
11 | ||
12 | var _browser = 'other'; | |
13 | var _editor = 'none'; | |
14 | var _platform = 'desktop'; | |
15 | var _touch = false; | |
16 | ||
17 | /** | |
58d7e8f8 | 18 | * @exports WoltLabSuite/Core/Enviroment |
bd969ed4 | 19 | */ |
431e4cb4 | 20 | return { |
bd969ed4 AE |
21 | /** |
22 | * Determines environment variables. | |
23 | */ | |
24 | setup: function() { | |
25 | if (typeof window.chrome === 'object') { | |
26 | // this detects Opera as well, we could check for window.opr if we need to | |
27 | _browser = 'chrome'; | |
28 | } | |
29 | else { | |
30 | var styles = window.getComputedStyle(document.documentElement); | |
31 | for (var i = 0, length = styles.length; i < length; i++) { | |
32 | var property = styles[i]; | |
33 | ||
34 | if (property.indexOf('-ms-') === 0) { | |
35 | // it is tempting to use 'msie', but it wouldn't really represent 'Edge' | |
36 | _browser = 'microsoft'; | |
37 | } | |
38 | else if (property.indexOf('-moz-') === 0) { | |
39 | _browser = 'firefox'; | |
40 | } | |
41 | else if (property.indexOf('-webkit-') === 0) { | |
42 | _browser = 'safari'; | |
43 | } | |
44 | } | |
45 | } | |
46 | ||
47 | var ua = window.navigator.userAgent.toLowerCase(); | |
48 | if (ua.indexOf('crios') !== -1) { | |
49 | _browser = 'chrome'; | |
50 | _platform = 'ios'; | |
51 | } | |
52 | else if (/(?:iphone|ipad|ipod)/.test(ua)) { | |
53 | _browser = 'safari'; | |
54 | _platform = 'ios'; | |
55 | } | |
56 | else if (ua.indexOf('android') !== -1) { | |
57 | _platform = 'android'; | |
58 | } | |
59 | else if (ua.indexOf('iemobile') !== -1) { | |
60 | _browser = 'microsoft'; | |
61 | _platform = 'windows'; | |
62 | } | |
63 | ||
64 | if (_platform === 'desktop' && (ua.indexOf('mobile') !== -1 || ua.indexOf('tablet') !== -1)) { | |
65 | _platform = 'mobile'; | |
66 | } | |
67 | ||
68 | _editor = 'redactor'; | |
69 | _touch = (!!('ontouchstart' in window) || (!!('msMaxTouchPoints' in window.navigator) && window.navigator.msMaxTouchPoints > 0) || window.DocumentTouch && document instanceof DocumentTouch); | |
70 | }, | |
71 | ||
72 | /** | |
73 | * Returns the lower-case browser identifier. | |
74 | * | |
75 | * Possible values: | |
76 | * - chrome: Chrome and Opera | |
77 | * - firefox | |
78 | * - microsoft: Internet Explorer and Microsoft Edge | |
79 | * - safari | |
80 | * | |
81 | * @return {string} browser identifier | |
82 | */ | |
83 | browser: function() { | |
84 | return _browser; | |
85 | }, | |
86 | ||
87 | /** | |
88 | * Returns the available editor's name or an empty string. | |
89 | * | |
90 | * @return {string} editor name | |
91 | */ | |
92 | editor: function() { | |
93 | return _editor; | |
94 | }, | |
95 | ||
96 | /** | |
97 | * Returns the browser platform. | |
98 | * | |
99 | * Possible values: | |
100 | * - desktop | |
101 | * - android | |
102 | * - ios: iPhone, iPad and iPod | |
103 | * - windows: Windows on phones/tablets | |
104 | * | |
105 | * @return {string} browser platform | |
106 | */ | |
107 | platform: function() { | |
108 | return _platform; | |
109 | }, | |
110 | ||
111 | /** | |
112 | * Returns true if browser is potentially used with a touchscreen. | |
113 | * | |
114 | * Warning: Detecting touch is unreliable and should be avoided at all cost. | |
115 | * | |
e71525e4 | 116 | * @deprecated 3.0 - exists for backward-compatibility only, will be removed in the future |
bd969ed4 AE |
117 | * |
118 | * @return {boolean} true if a touchscreen is present | |
119 | */ | |
120 | touch: function() { | |
121 | return _touch; | |
122 | } | |
123 | }; | |
bd969ed4 | 124 | }); |