Files
david/src/utils/Ut.js
T
2026-07-04 05:25:10 +03:00

155 lines
3.7 KiB
JavaScript

const stamp = Date.now();
export class Ut {
static EventsUID = `Events_${stamp}`;
static DataUID = `Data_${stamp}`;
static WidgetUID = `Widget_${stamp}`;
static evKey = 0;
static isSet = (val) => typeof val !== 'undefined';
static isFn = (val) => typeof val === 'function';
static isStr = (val) => typeof val === 'string';
static ucFirst = (str) => str.charAt(0).toUpperCase() + str.slice(1);
static lcFirst = (str) => str.charAt(0).toLowerCase() + str.slice(1);
static isEmptyObj = obj => !Object.keys(obj).length;
static objVal = obj => Object.values(obj)[0];
static trigger = (handler, ...args) => typeof handler === 'function' ? handler.call(this, ...args) : null;
static each = (obj, callback, context) => obj && Object.keys(obj).forEach(key => callback.call(context ?? this, obj[key], key) );
static tplString = (str, context) => new Function('return `' + str + '`;').call(context || {});
static nextKey = () => ++Ut.evKey;
static resetKey = () => Ut.evKey = 0;
static extendNode(name, value) {
if (!Object.getOwnPropertyDescriptor(Node.prototype, name)) {
Object.defineProperty(Node.prototype, name, {
value: value,
enumerable: false,
writable: true,
configurable: true
});
}
}
static uxRegister(...classRefs) {
classRefs.forEach(classRef => {
const clsName = `ux${classRef.name}`;
if (!Object.getOwnPropertyDescriptor(Node.prototype, clsName)) {
Object.defineProperty(Node.prototype, clsName, {
enumerable: false,
configurable: true,
writable: true,
value: function (...args) {
return new classRef(this, ...args);
}}
);
}
});
}
static device = {
touchCapable : ('ontouchstart' in window), // Is Mobile Device Flag
isMobile : /Mobi/.test(navigator.userAgent),
swipe_h_threshold : 50,
swipe_v_threshold : 50,
startevent : ('ontouchstart' in window) ? 'touchstart' : 'mousedown',
endevent : ('ontouchstart' in window) ? 'touchend' : 'mouseup',
moveevent : ('ontouchstart' in window) ? 'touchmove' : 'mousemove'
}
/**
* Redirect
*/
static url = (location) => window.location.href = location;
/**
* Decode HTML entity
*/
static unescape = str => ((typeof str === 'string') && (new RegExp(/&|<|>|"|'/).test(str)))
? str.replace(/&amp;/g, "&").replace(/&lt/g, "<").replace(/&gt;/g, ">").replace(/&quot;/g, "\"").replace(/&#39;/g, "'")
: str;
/**
* Encode HTML Entities
*/
htmlEntities = str => String(str).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
/**
* Convert RGB to Hex
*/
rgb2hex = rgb => '#' + rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/).slice(1).map(n => parseInt(n, 10).toString(16).padStart(2, '0')).join('');
/**
* Copy To Clipboard
*/
static copyToClipboard(target) {
if (!target) {
return;
}
const isTextField = target instanceof HTMLInputElement || target instanceof HTMLTextAreaElement;
if (navigator.clipboard) {
if (isTextField) {
target.select();
target.setSelectionRange(0, 99999);
target = target.value;
}
navigator.clipboard.writeText(target);
return;
}
let tval = null;
if (isTextField) {
if (target.dataset.notrusted) {
delete target.dataset.notrusted;
return;
}
tval = target.value;
}
const textarea = document.createElement('textarea');
textarea.className = 'hidden';
textarea.value = tval || target;
document.body.prepend(textarea);
textarea.select();
try {
document.execCommand('copy');
}
catch (error) {
console.error(error);
}
finally {
textarea.remove();
if (isTextField) {
target.dataset.notrusted = 1;
target.select();
target.setSelectionRange(0, 99999);
}
}
}
};