first commit
This commit is contained in:
+148
@@ -0,0 +1,148 @@
|
||||
const stamp = Date.now();
|
||||
|
||||
export class Ut {
|
||||
|
||||
static EventsUID = 'Events' + stamp;
|
||||
static DataUID = 'Data' + stamp;
|
||||
|
||||
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) {
|
||||
for (let key in obj) {
|
||||
callback.call(context || this, obj[key], key);
|
||||
}
|
||||
}
|
||||
|
||||
static tplString = (str, context) => new Function('return `' + str + '`;').call(context || {});
|
||||
|
||||
static extendNode(name, value) {
|
||||
|
||||
if (!Object.getOwnPropertyDescriptor(Node.prototype, name)) {
|
||||
Object.defineProperty(Node.prototype, name, {
|
||||
value: value,
|
||||
enumerable: false,
|
||||
writable: true,
|
||||
configurable: true
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
static extendNodeUx(...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(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, "\"").replace(/'/g, "'")
|
||||
: str;
|
||||
|
||||
/**
|
||||
* Encode HTML Entities
|
||||
*/
|
||||
htmlEntities = str => String(str).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
|
||||
|
||||
/**
|
||||
* 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 (navigator.clipboard) {
|
||||
|
||||
if (target instanceof Node) {
|
||||
|
||||
target.select();
|
||||
target.setSelectionRange(0, 99999);
|
||||
|
||||
target = target.value;
|
||||
}
|
||||
|
||||
navigator.clipboard.writeText(target);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
let tval = null;
|
||||
|
||||
if (target instanceof Node) {
|
||||
|
||||
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 (target instanceof Node) {
|
||||
|
||||
target.dataset.notrusted = 1;
|
||||
|
||||
target.select();
|
||||
target.setSelectionRange(0, 99999);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user