2017-08-31 16:43:36 +00:00
|
|
|
const b64 = require('base64-js');
|
|
|
|
|
|
|
|
function arrayToB64(array) {
|
|
|
|
return b64
|
|
|
|
.fromByteArray(array)
|
|
|
|
.replace(/\+/g, '-')
|
|
|
|
.replace(/\//g, '_')
|
|
|
|
.replace(/=/g, '');
|
2017-06-02 03:59:27 +00:00
|
|
|
}
|
|
|
|
|
2017-08-31 16:43:36 +00:00
|
|
|
function b64ToArray(str) {
|
2018-03-29 05:03:46 +00:00
|
|
|
return b64.toByteArray(str + '==='.slice((str.length + 3) % 4));
|
2017-06-02 03:59:27 +00:00
|
|
|
}
|
|
|
|
|
2017-08-24 21:54:02 +00:00
|
|
|
function loadShim(polyfill) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const shim = document.createElement('script');
|
|
|
|
shim.src = polyfill;
|
|
|
|
shim.addEventListener('load', () => resolve(true));
|
|
|
|
shim.addEventListener('error', () => resolve(false));
|
|
|
|
document.head.appendChild(shim);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-07-20 23:06:06 +00:00
|
|
|
function isFile(id) {
|
2017-08-02 18:17:23 +00:00
|
|
|
return /^[0-9a-fA-F]{10}$/.test(id);
|
2017-07-20 23:06:06 +00:00
|
|
|
}
|
|
|
|
|
2017-08-04 03:13:17 +00:00
|
|
|
function copyToClipboard(str) {
|
|
|
|
const aux = document.createElement('input');
|
|
|
|
aux.setAttribute('value', str);
|
|
|
|
aux.contentEditable = true;
|
|
|
|
aux.readOnly = true;
|
|
|
|
document.body.appendChild(aux);
|
|
|
|
if (navigator.userAgent.match(/iphone|ipad|ipod/i)) {
|
|
|
|
const range = document.createRange();
|
|
|
|
range.selectNodeContents(aux);
|
2018-07-06 22:49:50 +00:00
|
|
|
const sel = getSelection();
|
2017-08-04 03:13:17 +00:00
|
|
|
sel.removeAllRanges();
|
|
|
|
sel.addRange(range);
|
|
|
|
aux.setSelectionRange(0, str.length);
|
2017-08-04 21:44:11 +00:00
|
|
|
} else {
|
2017-08-04 03:13:17 +00:00
|
|
|
aux.select();
|
|
|
|
}
|
2017-08-04 21:44:11 +00:00
|
|
|
const result = document.execCommand('copy');
|
2017-08-04 03:13:17 +00:00
|
|
|
document.body.removeChild(aux);
|
2017-08-04 21:44:11 +00:00
|
|
|
return result;
|
2017-08-04 03:13:17 +00:00
|
|
|
}
|
|
|
|
|
2017-08-06 01:06:43 +00:00
|
|
|
const LOCALIZE_NUMBERS = !!(
|
|
|
|
typeof Intl === 'object' &&
|
|
|
|
Intl &&
|
2017-08-24 21:54:02 +00:00
|
|
|
typeof Intl.NumberFormat === 'function' &&
|
|
|
|
typeof navigator === 'object'
|
2017-08-06 01:06:43 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
const UNITS = ['B', 'kB', 'MB', 'GB'];
|
|
|
|
function bytes(num) {
|
2017-08-31 16:43:36 +00:00
|
|
|
if (num < 1) {
|
|
|
|
return '0B';
|
|
|
|
}
|
2017-08-06 01:06:43 +00:00
|
|
|
const exponent = Math.min(Math.floor(Math.log10(num) / 3), UNITS.length - 1);
|
|
|
|
const n = Number(num / Math.pow(1000, exponent));
|
2017-11-09 21:58:20 +00:00
|
|
|
let nStr = n.toFixed(1);
|
|
|
|
if (LOCALIZE_NUMBERS) {
|
|
|
|
try {
|
2017-11-07 23:54:42 +00:00
|
|
|
const locale = document.querySelector('html').lang;
|
|
|
|
nStr = n.toLocaleString(locale, {
|
2017-08-06 01:06:43 +00:00
|
|
|
minimumFractionDigits: 1,
|
|
|
|
maximumFractionDigits: 1
|
2017-11-09 21:58:20 +00:00
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
2017-08-06 01:06:43 +00:00
|
|
|
return `${nStr}${UNITS[exponent]}`;
|
|
|
|
}
|
|
|
|
|
2017-08-09 02:46:15 +00:00
|
|
|
function percent(ratio) {
|
2017-11-09 21:58:20 +00:00
|
|
|
if (LOCALIZE_NUMBERS) {
|
|
|
|
try {
|
2017-11-07 23:54:42 +00:00
|
|
|
const locale = document.querySelector('html').lang;
|
|
|
|
return ratio.toLocaleString(locale, { style: 'percent' });
|
2017-11-09 21:58:20 +00:00
|
|
|
} catch (e) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return `${Math.floor(ratio * 100)}%`;
|
2017-08-09 02:46:15 +00:00
|
|
|
}
|
|
|
|
|
2018-02-08 03:46:18 +00:00
|
|
|
function number(n) {
|
|
|
|
if (LOCALIZE_NUMBERS) {
|
|
|
|
const locale = document.querySelector('html').lang;
|
|
|
|
return n.toLocaleString(locale);
|
|
|
|
}
|
|
|
|
return n.toString();
|
|
|
|
}
|
|
|
|
|
2017-08-11 02:01:39 +00:00
|
|
|
function allowedCopy() {
|
|
|
|
const support = !!document.queryCommandSupported;
|
|
|
|
return support ? document.queryCommandSupported('copy') : false;
|
|
|
|
}
|
|
|
|
|
2017-08-24 21:54:02 +00:00
|
|
|
function delay(delay = 100) {
|
|
|
|
return new Promise(resolve => setTimeout(resolve, delay));
|
|
|
|
}
|
|
|
|
|
2018-02-13 19:32:59 +00:00
|
|
|
function fadeOut(selector) {
|
|
|
|
const classes = document.querySelector(selector).classList;
|
|
|
|
classes.remove('effect--fadeIn');
|
|
|
|
classes.add('effect--fadeOut');
|
2017-08-24 21:54:02 +00:00
|
|
|
return delay(300);
|
|
|
|
}
|
|
|
|
|
2018-01-24 18:23:13 +00:00
|
|
|
function openLinksInNewTab(links, should = true) {
|
|
|
|
links = links || Array.from(document.querySelectorAll('a:not([target])'));
|
|
|
|
if (should) {
|
|
|
|
links.forEach(l => {
|
|
|
|
l.setAttribute('target', '_blank');
|
|
|
|
l.setAttribute('rel', 'noopener noreferrer');
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
links.forEach(l => {
|
|
|
|
l.removeAttribute('target');
|
|
|
|
l.removeAttribute('rel');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return links;
|
|
|
|
}
|
2017-07-20 23:06:06 +00:00
|
|
|
|
2018-08-03 19:24:41 +00:00
|
|
|
function browserName() {
|
|
|
|
try {
|
|
|
|
if (/firefox/i.test(navigator.userAgent)) {
|
|
|
|
return 'firefox';
|
2018-07-31 18:09:18 +00:00
|
|
|
}
|
2018-08-03 19:24:41 +00:00
|
|
|
if (/edge/i.test(navigator.userAgent)) {
|
|
|
|
return 'edge';
|
|
|
|
}
|
|
|
|
if (/trident/i.test(navigator.userAgent)) {
|
|
|
|
return 'ie';
|
|
|
|
}
|
|
|
|
if (/chrome/i.test(navigator.userAgent)) {
|
|
|
|
return 'chrome';
|
|
|
|
}
|
|
|
|
if (/safari/i.test(navigator.userAgent)) {
|
|
|
|
return 'safari';
|
|
|
|
}
|
|
|
|
return 'other';
|
|
|
|
} catch (e) {
|
|
|
|
return 'unknown';
|
2018-07-31 18:09:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-24 21:54:02 +00:00
|
|
|
module.exports = {
|
|
|
|
fadeOut,
|
|
|
|
delay,
|
2017-08-11 02:01:39 +00:00
|
|
|
allowedCopy,
|
2017-08-06 01:06:43 +00:00
|
|
|
bytes,
|
2017-08-09 02:46:15 +00:00
|
|
|
percent,
|
2018-02-08 03:46:18 +00:00
|
|
|
number,
|
2017-08-04 03:13:17 +00:00
|
|
|
copyToClipboard,
|
2017-08-31 16:43:36 +00:00
|
|
|
arrayToB64,
|
|
|
|
b64ToArray,
|
2018-03-01 03:24:41 +00:00
|
|
|
loadShim,
|
2017-07-20 23:06:06 +00:00
|
|
|
isFile,
|
2018-07-31 18:09:18 +00:00
|
|
|
openLinksInNewTab,
|
2018-08-03 19:24:41 +00:00
|
|
|
browserName
|
2017-06-02 03:59:27 +00:00
|
|
|
};
|