2020-09-23 02:09:55 +02:00
|
|
|
import { ORIENTATION_LANDSCAPE, ORIENTATION_PORTRAIT } from './constants.js';
|
|
|
|
|
2020-08-13 10:28:25 +02:00
|
|
|
export function getLocalStorage(key) {
|
2020-06-14 09:24:26 +02:00
|
|
|
try {
|
|
|
|
return localStorage.getItem(key);
|
|
|
|
} catch (e) {
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-08-13 10:28:25 +02:00
|
|
|
export function setLocalStorage(key, value) {
|
2020-06-14 09:24:26 +02:00
|
|
|
try {
|
2020-06-14 10:10:26 +02:00
|
|
|
if (value !== "" && value !== null) {
|
|
|
|
localStorage.setItem(key, value);
|
|
|
|
} else {
|
|
|
|
localStorage.removeItem(key);
|
|
|
|
}
|
2020-06-14 09:24:26 +02:00
|
|
|
return true;
|
|
|
|
} catch (e) {}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-08-13 10:28:25 +02:00
|
|
|
export function clearLocalStorage(key) {
|
2020-06-14 10:10:26 +02:00
|
|
|
localStorage.removeItem(key);
|
|
|
|
}
|
|
|
|
|
2020-06-18 09:06:10 +02:00
|
|
|
// jump down to the max height of a div, with a slight delay
|
2020-08-13 10:28:25 +02:00
|
|
|
export function jumpToBottom(element) {
|
2020-06-18 09:06:10 +02:00
|
|
|
if (!element) return;
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
element.scrollTo({
|
|
|
|
top: element.scrollHeight,
|
|
|
|
left: 0,
|
2020-09-22 05:11:09 +02:00
|
|
|
behavior: 'auto'
|
2020-06-18 09:06:10 +02:00
|
|
|
});
|
|
|
|
}, 50, element);
|
2020-06-14 09:24:26 +02:00
|
|
|
}
|
|
|
|
|
2020-06-18 09:06:10 +02:00
|
|
|
// convert newlines to <br>s
|
2020-08-13 10:28:25 +02:00
|
|
|
export function addNewlines(str) {
|
2020-06-18 09:06:10 +02:00
|
|
|
return str.replace(/(?:\r\n|\r|\n)/g, '<br />');
|
2020-06-15 06:14:42 +02:00
|
|
|
}
|
2020-06-15 22:43:41 +02:00
|
|
|
|
2020-08-13 10:28:25 +02:00
|
|
|
export function pluralize(string, count) {
|
2020-06-18 19:24:54 +02:00
|
|
|
if (count === 1) {
|
|
|
|
return string;
|
|
|
|
} else {
|
|
|
|
return string + "s";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-16 00:45:55 +02:00
|
|
|
// Trying to determine if browser is mobile/tablet.
|
|
|
|
// Source: https://developer.mozilla.org/en-US/docs/Web/HTTP/Browser_detection_using_the_user_agent
|
2020-08-13 10:28:25 +02:00
|
|
|
export function hasTouchScreen() {
|
2020-08-21 00:29:15 +02:00
|
|
|
let hasTouch = false;
|
2020-08-13 10:28:25 +02:00
|
|
|
if ("maxTouchPoints" in navigator) {
|
2020-08-21 00:29:15 +02:00
|
|
|
hasTouch = navigator.maxTouchPoints > 0;
|
2020-06-16 00:45:55 +02:00
|
|
|
} else if ("msMaxTouchPoints" in navigator) {
|
2020-08-21 00:29:15 +02:00
|
|
|
hasTouch = navigator.msMaxTouchPoints > 0;
|
2020-06-16 00:45:55 +02:00
|
|
|
} else {
|
|
|
|
var mQ = window.matchMedia && matchMedia("(pointer:coarse)");
|
|
|
|
if (mQ && mQ.media === "(pointer:coarse)") {
|
2020-08-21 00:29:15 +02:00
|
|
|
hasTouch = !!mQ.matches;
|
2020-06-16 00:45:55 +02:00
|
|
|
} else if ('orientation' in window) {
|
2020-08-21 00:29:15 +02:00
|
|
|
hasTouch = true; // deprecated, but good fallback
|
2020-06-16 00:45:55 +02:00
|
|
|
} else {
|
|
|
|
// Only as a last resort, fall back to user agent sniffing
|
|
|
|
var UA = navigator.userAgent;
|
2020-08-21 00:29:15 +02:00
|
|
|
hasTouch = (
|
2020-06-16 00:45:55 +02:00
|
|
|
/\b(BlackBerry|webOS|iPhone|IEMobile)\b/i.test(UA) ||
|
|
|
|
/\b(Android|Windows Phone|iPad|iPod)\b/i.test(UA)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2020-08-21 00:29:15 +02:00
|
|
|
return hasTouch;
|
2020-06-16 00:45:55 +02:00
|
|
|
}
|
2020-06-15 22:43:41 +02:00
|
|
|
|
2020-09-23 02:09:55 +02:00
|
|
|
|
|
|
|
export function getOrientation(forTouch = false) {
|
|
|
|
// chrome mobile gives misleading matchMedia result when keyboard is up
|
|
|
|
if (forTouch && window.screen && window.screen.orientation) {
|
|
|
|
return window.screen.orientation.type.match('portrait') ?
|
|
|
|
ORIENTATION_PORTRAIT :
|
|
|
|
ORIENTATION_LANDSCAPE;
|
|
|
|
} else {
|
|
|
|
// all other cases
|
|
|
|
return window.matchMedia("(orientation: portrait)").matches ?
|
|
|
|
ORIENTATION_PORTRAIT :
|
|
|
|
ORIENTATION_LANDSCAPE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-13 10:28:25 +02:00
|
|
|
export function generateUsername() {
|
2020-07-05 07:34:00 +02:00
|
|
|
return `User ${(Math.floor(Math.random() * 42) + 1)}`;
|
2020-07-05 10:08:22 +02:00
|
|
|
}
|
|
|
|
|
2020-10-29 12:46:54 +01:00
|
|
|
export function padLeft(text, pad, size) {
|
|
|
|
return String(pad.repeat(size) + text).slice(-size);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function parseSecondsToDurationString(seconds = 0) {
|
2020-07-19 00:06:54 +02:00
|
|
|
const finiteSeconds = Number.isFinite(+seconds) ? Math.abs(seconds) : 0;
|
|
|
|
|
2020-10-29 12:46:54 +01:00
|
|
|
const days = Math.floor(finiteSeconds / 86400);
|
|
|
|
const daysString = days > 0 ? `${days} day${days > 1 ? 's' : ''} ` : '';
|
|
|
|
|
|
|
|
const hours = Math.floor((finiteSeconds / 3600) % 24);
|
|
|
|
const hoursString = hours || days ? padLeft(`${hours}:`, '0', 3) : '';
|
2020-07-19 00:06:54 +02:00
|
|
|
|
|
|
|
const mins = Math.floor((finiteSeconds / 60) % 60);
|
2020-10-29 12:46:54 +01:00
|
|
|
const minString = padLeft(`${mins}:`, '0', 3);
|
2020-07-19 00:06:54 +02:00
|
|
|
|
|
|
|
const secs = Math.floor(finiteSeconds % 60);
|
2020-10-29 12:46:54 +01:00
|
|
|
const secsString = padLeft(`${secs}`, '0', 2);
|
2020-07-19 00:06:54 +02:00
|
|
|
|
2020-10-29 12:46:54 +01:00
|
|
|
return daysString + hoursString + minString + secsString;
|
2020-07-20 00:17:03 +02:00
|
|
|
}
|
|
|
|
|
2020-08-13 10:28:25 +02:00
|
|
|
export function setVHvar() {
|
2020-07-20 00:12:57 +02:00
|
|
|
var vh = window.innerHeight * 0.01;
|
|
|
|
// Then we set the value in the --vh custom property to the root of the document
|
|
|
|
document.documentElement.style.setProperty('--vh', `${vh}px`);
|
|
|
|
console.log("== new vh", vh)
|
|
|
|
}
|
2020-08-06 19:55:33 +02:00
|
|
|
|
2020-08-13 10:28:25 +02:00
|
|
|
export function doesObjectSupportFunction(object, functionName) {
|
2020-08-06 19:55:33 +02:00
|
|
|
return typeof object[functionName] === "function";
|
2020-08-13 10:28:25 +02:00
|
|
|
}
|
2020-08-22 08:44:10 +02:00
|
|
|
|
|
|
|
// return a string of css classes
|
|
|
|
export function classNames(json) {
|
|
|
|
const classes = [];
|
|
|
|
|
|
|
|
Object.entries(json).map(function(item) {
|
|
|
|
const [ key, value ] = item;
|
|
|
|
if (value) {
|
|
|
|
classes.push(key);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
return classes.join(' ');
|
|
|
|
}
|
2020-08-24 12:30:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
// taken from
|
|
|
|
// https://medium.com/@TCAS3/debounce-deep-dive-javascript-es6-e6f8d983b7a1
|
|
|
|
export function debounce(fn, time) {
|
|
|
|
let timeout;
|
|
|
|
|
|
|
|
return function() {
|
|
|
|
const functionCall = () => fn.apply(this, arguments);
|
|
|
|
|
|
|
|
clearTimeout(timeout);
|
|
|
|
timeout = setTimeout(functionCall, time);
|
|
|
|
}
|
|
|
|
}
|