UNPKG

2.57 kBJavaScriptView Raw
1'use strict';
2
3const getConfirmation = (win, message, callback) => (callback(win.confirm(message)));
4const isModifiedEvent = (ev) => (ev.metaKey || ev.altKey || ev.ctrlKey || ev.shiftKey);
5/**
6 * Returns true if the HTML5 history API is supported. Taken from Modernizr.
7 *
8 * https://github.com/Modernizr/Modernizr/blob/master/LICENSE
9 * https://github.com/Modernizr/Modernizr/blob/master/feature-detects/history.js
10 * changed to avoid false negatives for Windows Phones: https://github.com/reactjs/react-router/issues/586
11 */
12const supportsHistory = (win) => {
13 const ua = win.navigator.userAgent;
14 if ((ua.indexOf('Android 2.') !== -1 || ua.indexOf('Android 4.0') !== -1) &&
15 ua.indexOf('Mobile Safari') !== -1 &&
16 ua.indexOf('Chrome') === -1 &&
17 ua.indexOf('Windows Phone') === -1) {
18 return false;
19 }
20 return win.history && 'pushState' in win.history;
21};
22/**
23 * Returns true if browser fires popstate on hash change.
24 * IE10 and IE11 do not.
25 */
26const supportsPopStateOnHashChange = (nav) => (nav.userAgent.indexOf('Trident') === -1);
27/**
28 * Returns false if using go(n) with hash history causes a full page reload.
29 */
30const supportsGoWithoutReloadUsingHash = (nav) => (nav.userAgent.indexOf('Firefox') === -1);
31const isExtraneousPopstateEvent = (nav, event) => (event.state === undefined &&
32 nav.userAgent.indexOf('CriOS') === -1);
33const storageAvailable = (win, type) => {
34 const storage = win[type];
35 const x = '__storage_test__';
36 try {
37 storage.setItem(x, x);
38 storage.removeItem(x);
39 return true;
40 }
41 catch (e) {
42 return e instanceof DOMException && (
43 // everything except Firefox
44 e.code === 22 ||
45 // Firefox
46 e.code === 1014 ||
47 // test name field too, because code might not be present
48 // everything except Firefox
49 e.name === 'QuotaExceededError' ||
50 // Firefox
51 e.name === 'NS_ERROR_DOM_QUOTA_REACHED') &&
52 // acknowledge QuotaExceededError only if there's something already stored
53 storage.length !== 0;
54 }
55};
56
57exports.getConfirmation = getConfirmation;
58exports.isExtraneousPopstateEvent = isExtraneousPopstateEvent;
59exports.isModifiedEvent = isModifiedEvent;
60exports.storageAvailable = storageAvailable;
61exports.supportsGoWithoutReloadUsingHash = supportsGoWithoutReloadUsingHash;
62exports.supportsHistory = supportsHistory;
63exports.supportsPopStateOnHashChange = supportsPopStateOnHashChange;