2021-11-06 09:42:36 +00:00
|
|
|
/* eslint-disable max-classes-per-file */
|
2021-07-28 13:15:52 +00:00
|
|
|
export function bytesToSize(bytes) {
|
|
|
|
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
|
|
|
|
if (bytes === 0) return 'n/a';
|
|
|
|
const i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)), 10);
|
|
|
|
if (i === 0) return `${bytes} ${sizes[i]}`;
|
|
|
|
return `${(bytes / (1024 ** i)).toFixed(1)} ${sizes[i]}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function diffMinutes(dt2, dt1) {
|
|
|
|
let diff = (dt2.getTime() - dt1.getTime()) / 1000;
|
|
|
|
diff /= 60;
|
|
|
|
return Math.abs(Math.round(diff));
|
|
|
|
}
|
|
|
|
|
2021-12-08 08:07:25 +00:00
|
|
|
export function isInSameDay(dt2, dt1) {
|
2021-12-09 13:31:39 +00:00
|
|
|
return (
|
|
|
|
dt2.getFullYear() === dt1.getFullYear()
|
|
|
|
&& dt2.getMonth() === dt1.getMonth()
|
|
|
|
&& dt2.getDate() === dt1.getDate()
|
|
|
|
);
|
2021-07-28 13:15:52 +00:00
|
|
|
}
|
2021-09-09 12:19:57 +00:00
|
|
|
|
2022-01-11 15:13:40 +00:00
|
|
|
/**
|
|
|
|
* @param {Event} ev
|
|
|
|
* @param {string} [targetSelector] element selector for Element.matches([selector])
|
|
|
|
*/
|
|
|
|
export function getEventCords(ev, targetSelector) {
|
|
|
|
let boxInfo;
|
|
|
|
|
|
|
|
const path = ev.nativeEvent.composedPath();
|
|
|
|
const target = targetSelector
|
|
|
|
? path.find((element) => element.matches?.(targetSelector))
|
|
|
|
: null;
|
|
|
|
if (target) {
|
|
|
|
boxInfo = target.getBoundingClientRect();
|
|
|
|
} else {
|
|
|
|
boxInfo = ev.target.getBoundingClientRect();
|
|
|
|
}
|
|
|
|
|
2021-09-09 12:19:57 +00:00
|
|
|
return {
|
|
|
|
x: boxInfo.x,
|
|
|
|
y: boxInfo.y,
|
2022-01-11 15:13:40 +00:00
|
|
|
width: boxInfo.width,
|
|
|
|
height: boxInfo.height,
|
2021-09-09 12:19:57 +00:00
|
|
|
detail: ev.detail,
|
|
|
|
};
|
|
|
|
}
|
2021-09-12 15:12:51 +00:00
|
|
|
|
|
|
|
export function abbreviateNumber(number) {
|
|
|
|
if (number > 99) return '99+';
|
|
|
|
return number;
|
|
|
|
}
|
2021-11-06 09:42:36 +00:00
|
|
|
|
|
|
|
export class Debounce {
|
|
|
|
constructor() {
|
|
|
|
this.timeoutId = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {function} func - callback function
|
|
|
|
* @param {number} wait - wait in milliseconds to call func
|
|
|
|
* @returns {func} debounceCallback - to pass arguments to func callback
|
|
|
|
*/
|
|
|
|
_(func, wait) {
|
|
|
|
const that = this;
|
|
|
|
return function debounceCallback(...args) {
|
|
|
|
clearTimeout(that.timeoutId);
|
|
|
|
that.timeoutId = setTimeout(() => {
|
|
|
|
func.apply(this, args);
|
|
|
|
that.timeoutId = null;
|
|
|
|
}, wait);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class Throttle {
|
|
|
|
constructor() {
|
|
|
|
this.timeoutId = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {function} func - callback function
|
|
|
|
* @param {number} wait - wait in milliseconds to call func
|
|
|
|
* @returns {function} throttleCallback - to pass arguments to func callback
|
|
|
|
*/
|
|
|
|
_(func, wait) {
|
|
|
|
const that = this;
|
|
|
|
return function throttleCallback(...args) {
|
|
|
|
if (that.timeoutId !== null) return;
|
|
|
|
that.timeoutId = setTimeout(() => {
|
|
|
|
func.apply(this, args);
|
|
|
|
that.timeoutId = null;
|
|
|
|
}, wait);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getUrlPrams(paramName) {
|
|
|
|
const queryString = window.location.search;
|
|
|
|
const urlParams = new URLSearchParams(queryString);
|
|
|
|
return urlParams.get(paramName);
|
|
|
|
}
|
2021-11-18 08:02:12 +00:00
|
|
|
|
|
|
|
export function getScrollInfo(target) {
|
|
|
|
const scroll = {};
|
|
|
|
scroll.top = Math.round(target.scrollTop);
|
|
|
|
scroll.height = Math.round(target.scrollHeight);
|
|
|
|
scroll.viewHeight = Math.round(target.offsetHeight);
|
|
|
|
scroll.isScrollable = scroll.height > scroll.viewHeight;
|
|
|
|
return scroll;
|
|
|
|
}
|
2022-02-15 11:48:25 +00:00
|
|
|
|
|
|
|
export function avatarInitials(text) {
|
|
|
|
return [...text][0];
|
|
|
|
}
|
2022-04-24 10:12:24 +00:00
|
|
|
|
|
|
|
export function copyToClipboard(text) {
|
|
|
|
if (navigator.clipboard) {
|
|
|
|
navigator.clipboard.writeText(text);
|
|
|
|
} else {
|
|
|
|
const host = document.body;
|
|
|
|
const copyInput = document.createElement('input');
|
|
|
|
copyInput.style.position = 'fixed';
|
|
|
|
copyInput.style.opacity = '0';
|
|
|
|
copyInput.value = text;
|
|
|
|
host.append(copyInput);
|
|
|
|
|
|
|
|
copyInput.select();
|
|
|
|
copyInput.setSelectionRange(0, 99999);
|
|
|
|
document.execCommand('Copy');
|
|
|
|
copyInput.remove();
|
|
|
|
}
|
|
|
|
}
|