-
Notifications
You must be signed in to change notification settings - Fork 90
/
browserCheck.js
302 lines (272 loc) · 8.99 KB
/
browserCheck.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/**
* smallest native js domready ever
*/
function ready() {
if (/in/.test(document.readyState)) {
setTimeout(ready, 9);
} else {
populateCheckInfo();
setContactInfo();
}
}
/**
* Set up Modernizr and wait for it to finish before running the checks
*/
function initCheck() {
if (Modernizr && Modernizr.indexeddb === undefined) {
Modernizr.on('indexeddb', initCheck);
} else {
runBrowserCheck();
}
}
/**
* Appends test results to DOM
* @param {element} el
* @param {string} msg
* @param {boolean} test
* @return {boolean}
*/
function append(el, msg, test) {
var li = document.createElement('li');
if (typeof (platform) != 'undefined' && platform.name == 'IE' && parseFloat(platform.version) < 8) {
li.innerHTML = (test ? '/' : 'X') + ' ' + msg;
} else {
li.innerHTML = msg;
li.className = test ? 'js-found' : 'js-missing';
}
el.appendChild(li);
return test;
}
/**
* runs the browser check
*/
function runBrowserCheck() {
var baseUrl = '/';
var currentLocation = window.location.toString();
if (!checkCompat() && !checkVersion()) {
if (currentLocation.indexOf('/' + baseUrl) == -1) {
// old.html is in the version directory, so prepend if we have a version string
// version string will be replaced by the build. if not, clear it out so it doesn't affect the target url.
var version = '@version@'.replace(/@\w+@/, '');
window.location = version && currentLocation.indexOf(version) == -1 ? (version + baseUrl) : baseUrl;
} else {
ready();
}
} else if (currentLocation.indexOf('/' + baseUrl) != -1) {
ready();
}
}
/**
* removes test results and reruns browser check
* @param {boolean} doSpin
*/
function populateCheckInfo(doSpin) {
showBrowserWaitSpin(false);
var el = document.getElementsByClassName('js-full-list')[0];
var html = document.getElementsByTagName('html')[0];
if (el && html) {
var result = getModernizrValues();
el.innerHTML = result != '' ? result : 'no capabilities found';
toggleFullList(true);
}
var ul = document.getElementsByClassName('js-checks')[0];
if (ul) { // do each check and add the result
append(ul, 'Canvas render - draws graphics like the map',
typeof (Modernizr) != 'undefined' && Modernizr.canvas);
append(ul, 'Cross-Origin Resource Sharing - allows retrieving resources from other domains',
typeof (Modernizr) != 'undefined' && Modernizr.cors);
append(ul, 'Flexbox - used for layout and making things pretty',
typeof (Modernizr) != 'undefined' && Modernizr.flexbox);
append(ul, 'IndexedDB - allows data to be stored in local browser database',
typeof (Modernizr) != 'undefined' && Modernizr.indexeddb == true);
append(ul, 'Local storage - stores settings & caches images',
typeof (Modernizr) != 'undefined' && Modernizr.localstorage);
append(ul, 'SVG render - draws 2D vectors like the legend and timeline',
typeof (Modernizr) != 'undefined' && Modernizr.svg);
append(ul, 'Webworkers - allows tasks to run in the background',
typeof (Modernizr) != 'undefined' && Modernizr.webworkers);
}
var result = document.getElementsByClassName('js-report')[0];
if (result) {
var report = 'Unsupported Browser!';
if (checkVersion()) {
report = 'Supported Browser!';
if (!checkCompat()) {
report += ' <small><strong>(required features may be disabled!)</strong></small>';
}
}
result.innerHTML = report;
}
}
/**
* checks compatibility
* @return {boolean}
*/
function checkCompat() {
return typeof (Modernizr) != 'undefined' && Modernizr.canvas && Modernizr.localstorage && Modernizr.svg &&
Modernizr.webworkers && Modernizr.flexbox && Modernizr.cors;
}
/**
* checks minimum version
* @return {boolean}
*/
function checkVersion() {
return typeof (platform) != 'undefined' && ((platform.name == 'Chrome' && parseFloat(platform.version) >= 35) ||
(platform.name == 'Firefox' && parseFloat(platform.version) >= 38));
}
/**
* removes test results and reruns browser check
* @return {string}
*/
function getModernizrValues() {
var result = '';
for (var key in Modernizr) {
if (key.indexOf('_') === 0 ||
typeof Modernizr[key] === 'function') {
continue;
} else if (Modernizr[key] instanceof Boolean) {
result += key + ':' + Modernizr[key].valueOf() + '\n';
} else if (typeof Modernizr[key] === 'object') {
continue;
}
result += key + ':' + Modernizr[key] + '\n';
}
return result;
}
/**
* extracts all info from config
* @return {string}
*/
function getConfig() {
var request = new XMLHttpRequest();
var parsed;
request.open('GET', 'config/settings.json', false);
request.onload = function() {
if (request.readyState === 4 && request.status === 200) {
parsed = JSON.parse(request.responseText);
}
};
request.send(null);
return parsed;
}
/**
* extracts contact info from config and put it in the DOM
*/
function setContactInfo() {
var parsed = getConfig();
var browserPage = '';
if (parsed) {
var contactEl = document.getElementsByClassName('js-contact-info')[0];
if (parsed && parsed['admin'] && contactEl) {
var a = document.createElement('a');
var strong = document.createElement('strong');
strong.innerText = 'Help: ';
var link = parsed['admin']['supportWebsite'];
var text = parsed['admin']['supportWebsiteText'];
if (link && text) {
contactEl.appendChild(strong);
contactEl.appendChild(document.createElement('br'));
a.setAttribute('href', link);
a.className = 'btn btn-info';
a.innerHTML = text;
contactEl.appendChild(a);
contactEl.appendChild(document.createElement('p'));
}
}
}
if (!checkCompat() && checkVersion()) {
var warn = '<em>Your browser has been configured to disable features required by this application.</em> ' +
'<p>Please enable those features or contact your IT department for support.</p>';
if (platform.name == 'Firefox' && (!Modernizr.localstorage || !Modernizr.indexeddb == true)) {
var parsed = getConfig();
if (parsed && parsed['admin'] && parsed['admin']['firefoxCompatibleVersionLocalStorageOrIndexedDBErrorLink']) {
warn += '<p>For local storage or indexedDB related issues, see <a href="' +
parsed['admin']['firefoxCompatibleVersionLocalStorageOrIndexedDBErrorLink'] + '">this article</a></p>';
}
}
var link = '';
if (browserPage) {
link = '<a href="' + browserPage + '" class="btn btn-danger">Browser Download</a> <br> <br>';
}
setWarn(warn, link);
} else if (checkVersion()) {
setWarn('');
} else {
var minSupportInfo = '<strong>Recommended Browsers:</strong>' +
'<ul class="u-bullet">' +
'<li>Google Chrome version 60+</li>' +
'<li>Mozilla Firefox version 57+</li>' +
'</ul>' +
'<p>If you do not have one of these browsers installed, contact your local IT department for help.</p>';
if (browserPage) {
minSupportInfo += '<a href="' + browserPage + '" class="btn btn-danger">Browser Download</a> <br> <br>';
}
setWarn(minSupportInfo);
}
var browserInfo = document.getElementsByClassName('js-browser-info')[0];
if (browserInfo && typeof (platform) != 'undefined') {
browserInfo.innerHTML = '<ul><li class="' + (checkVersion() ? 'js-found' : 'js-missing') + '">' +
platform.name + ' Version ' + platform.version + ' detected</li></ul>';
}
}
/**
* updates the warning message
* @param {string} msg1
* @param {string=} opt_msg2
*/
function setWarn(msg1, opt_msg2) {
var minBrowserEl = document.getElementsByClassName('js-min-browser-msg')[0];
if (minBrowserEl) {
while (minBrowserEl.hasChildNodes()) {
minBrowserEl.removeChild(minBrowserEl.firstChild);
}
if (opt_msg2) {
var p = document.createElement('p');
p.innerHTML = msg1;
var p2 = document.createElement('p');
p2.innerHTML = opt_msg2;
minBrowserEl.appendChild(p);
minBrowserEl.appendChild(p2);
} else {
minBrowserEl.innerHTML = msg1;
}
}
}
/**
* removes test results and reruns browser check
* @param {boolean} doSpin
*/
function showBrowserWaitSpin(doSpin) {
var spin = document.getElementsByClassName('js-browser-check-wait')[0];
if (spin) {
spin.style.display = doSpin ? '' : 'none';
}
}
/**
* removes test results and reruns browser check
*/
function retryBrowserCheck() {
var ul = document.getElementsByClassName('js-checks')[0];
if (ul) {
while (ul.hasChildNodes()) {
ul.removeChild(ul.firstChild);
}
}
showBrowserWaitSpin(true);
var result = document.getElementsByClassName('js-report')[0];
if (result) {
result.innerHTML = '';
}
window.setTimeout(populateCheckInfo, 500);
}
/**
* toggles full list of detected capabilities
* @param {boolean=} opt_hide
*/
function toggleFullList(opt_hide) {
var el = document.getElementsByClassName('js-full-list')[0];
if (el) {
el.style.display = opt_hide ? 'none' : el.style.display == 'none' ? '' : 'none';
}
}
initCheck();