@randevcx/ranui
Version:
UI Component library based on `Web Component`
542 lines (541 loc) • 15.3 kB
JavaScript
var __defProp = Object.defineProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __publicField = (obj, key, value) => {
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
return value;
};
const noop = () => {
};
const currentDevice = () => {
if (typeof window !== "undefined") {
const ua = navigator.userAgent.toLowerCase();
if (/ipad|ipod/.test(ua))
return "ipad";
if (/android/.test(ua))
return "android";
if (/iphone/.test(ua))
return "iphone";
return "pc";
}
return "pc";
};
const isClient = typeof window !== "undefined";
const isMobile = () => {
if (!isClient)
return false;
const ua = window.navigator.userAgent;
if (/Android|webOS|iPhone|iPod|iPad|BlackBerry/i.test(ua)) {
return true;
}
return false;
};
const K_sha2 = [
1116352408,
1899447441,
3049323471,
3921009573,
961987163,
1508970993,
2453635748,
2870763221,
3624381080,
310598401,
607225278,
1426881987,
1925078388,
2162078206,
2614888103,
3248222580,
3835390401,
4022224774,
264347078,
604807628,
770255983,
1249150122,
1555081692,
1996064986,
2554220882,
2821834349,
2952996808,
3210313671,
3336571891,
3584528711,
113926993,
338241895,
666307205,
773529912,
1294757372,
1396182291,
1695183700,
1986661051,
2177026350,
2456956037,
2730485921,
2820302411,
3259730800,
3345764771,
3516065817,
3600352804,
4094571909,
275423344,
430227734,
506948616,
659060556,
883997877,
958139571,
1322822218,
1537002063,
1747873779,
1955562222,
2024104815,
2227730452,
2361852424,
2428436474,
2756734187,
3204031479,
3329325298
];
class Int_64 {
constructor(msint_32, lsint_32) {
__publicField(this, "highOrder");
__publicField(this, "lowOrder");
this.highOrder = msint_32;
this.lowOrder = lsint_32;
}
}
[
new Int_64(K_sha2[0], 3609767458),
new Int_64(K_sha2[1], 602891725),
new Int_64(K_sha2[2], 3964484399),
new Int_64(K_sha2[3], 2173295548),
new Int_64(K_sha2[4], 4081628472),
new Int_64(K_sha2[5], 3053834265),
new Int_64(K_sha2[6], 2937671579),
new Int_64(K_sha2[7], 3664609560),
new Int_64(K_sha2[8], 2734883394),
new Int_64(K_sha2[9], 1164996542),
new Int_64(K_sha2[10], 1323610764),
new Int_64(K_sha2[11], 3590304994),
new Int_64(K_sha2[12], 4068182383),
new Int_64(K_sha2[13], 991336113),
new Int_64(K_sha2[14], 633803317),
new Int_64(K_sha2[15], 3479774868),
new Int_64(K_sha2[16], 2666613458),
new Int_64(K_sha2[17], 944711139),
new Int_64(K_sha2[18], 2341262773),
new Int_64(K_sha2[19], 2007800933),
new Int_64(K_sha2[20], 1495990901),
new Int_64(K_sha2[21], 1856431235),
new Int_64(K_sha2[22], 3175218132),
new Int_64(K_sha2[23], 2198950837),
new Int_64(K_sha2[24], 3999719339),
new Int_64(K_sha2[25], 766784016),
new Int_64(K_sha2[26], 2566594879),
new Int_64(K_sha2[27], 3203337956),
new Int_64(K_sha2[28], 1034457026),
new Int_64(K_sha2[29], 2466948901),
new Int_64(K_sha2[30], 3758326383),
new Int_64(K_sha2[31], 168717936),
new Int_64(K_sha2[32], 1188179964),
new Int_64(K_sha2[33], 1546045734),
new Int_64(K_sha2[34], 1522805485),
new Int_64(K_sha2[35], 2643833823),
new Int_64(K_sha2[36], 2343527390),
new Int_64(K_sha2[37], 1014477480),
new Int_64(K_sha2[38], 1206759142),
new Int_64(K_sha2[39], 344077627),
new Int_64(K_sha2[40], 1290863460),
new Int_64(K_sha2[41], 3158454273),
new Int_64(K_sha2[42], 3505952657),
new Int_64(K_sha2[43], 106217008),
new Int_64(K_sha2[44], 3606008344),
new Int_64(K_sha2[45], 1432725776),
new Int_64(K_sha2[46], 1467031594),
new Int_64(K_sha2[47], 851169720),
new Int_64(K_sha2[48], 3100823752),
new Int_64(K_sha2[49], 1363258195),
new Int_64(K_sha2[50], 3750685593),
new Int_64(K_sha2[51], 3785050280),
new Int_64(K_sha2[52], 3318307427),
new Int_64(K_sha2[53], 3812723403),
new Int_64(K_sha2[54], 2003034995),
new Int_64(K_sha2[55], 3602036899),
new Int_64(K_sha2[56], 1575990012),
new Int_64(K_sha2[57], 1125592928),
new Int_64(K_sha2[58], 2716904306),
new Int_64(K_sha2[59], 442776044),
new Int_64(K_sha2[60], 593698344),
new Int_64(K_sha2[61], 3733110249),
new Int_64(K_sha2[62], 2999351573),
new Int_64(K_sha2[63], 3815920427),
new Int_64(3391569614, 3928383900),
new Int_64(3515267271, 566280711),
new Int_64(3940187606, 3454069534),
new Int_64(4118630271, 4000239992),
new Int_64(116418474, 1914138554),
new Int_64(174292421, 2731055270),
new Int_64(289380356, 3203993006),
new Int_64(460393269, 320620315),
new Int_64(685471733, 587496836),
new Int_64(852142971, 1086792851),
new Int_64(1017036298, 365543100),
new Int_64(1126000580, 2618297676),
new Int_64(1288033470, 3409855158),
new Int_64(1501505948, 4234509866),
new Int_64(1607167915, 987167468),
new Int_64(1816402316, 1246189591)
];
[
new Int_64(0, 1),
new Int_64(0, 32898),
new Int_64(2147483648, 32906),
new Int_64(2147483648, 2147516416),
new Int_64(0, 32907),
new Int_64(0, 2147483649),
new Int_64(2147483648, 2147516545),
new Int_64(2147483648, 32777),
new Int_64(0, 138),
new Int_64(0, 136),
new Int_64(0, 2147516425),
new Int_64(0, 2147483658),
new Int_64(0, 2147516555),
new Int_64(2147483648, 139),
new Int_64(2147483648, 32905),
new Int_64(2147483648, 32771),
new Int_64(2147483648, 32770),
new Int_64(2147483648, 128),
new Int_64(0, 32778),
new Int_64(2147483648, 2147483658),
new Int_64(2147483648, 2147516545),
new Int_64(2147483648, 32896),
new Int_64(0, 2147483649),
new Int_64(2147483648, 2147516424)
];
function formatDuration(time) {
return time < 10 ? `0${time}` : time;
}
function timestampToTime(timestamp) {
let date = /* @__PURE__ */ new Date();
if (timestamp) {
date = new Date(timestamp);
}
date.format = (format = "YYYY-MM-DD HH:mm:ss") => {
const year = date.getFullYear();
const month = formatDuration(date.getMonth() + 1);
const day = formatDuration(date.getDate());
const hour = formatDuration(date.getHours());
const minute = formatDuration(date.getMinutes());
const second = formatDuration(date.getSeconds());
return format.replace(/Y+/gi, `${year}`).replace(/M+/g, `${month}`).replace(/D+/gi, `${day}`).replace(/H+/gi, `${hour}`).replace(/m+/g, `${minute}`).replace(/S+/gi, `${second}`);
};
return date;
}
const timeFormat = (time) => {
if (time === 0)
return "00:00";
if (!time)
return "";
const hour = Math.trunc(time / 3600);
const minute = Math.trunc(time % 3600 / 60);
const second = formatDuration(Math.trunc(time - hour * 3600 - minute * 60));
if (hour === 0) {
return `${formatDuration(minute)}:${second}`;
}
return `${formatDuration(hour)}:${formatDuration(minute)}:${second}`;
};
const getPixelRatio = (context) => {
const backingStore = context.backingStorePixelRatio || context.webkitBackingStorePixelRatio || context.mozBackingStorePixelRatio || context.msBackingStorePixelRatio || context.oBackingStorePixelRatio || 1;
return (isClient && window.devicePixelRatio || 1) / backingStore;
};
const addClassToElement = (element, addClass) => {
if (typeof document === "undefined")
return void 0;
const classList = element.classList;
if (!classList.contains(addClass)) {
classList.add(addClass);
}
};
const removeClassToElement = (element, removeClass) => {
if (typeof document === "undefined")
return void 0;
const classList = element.classList;
if (classList.contains(removeClass)) {
classList.remove(removeClass);
}
};
const createDocumentFragment = (list) => {
if (typeof document === "undefined")
return void 0;
const Fragment = document.createDocumentFragment();
list.forEach((item) => Fragment.appendChild(item));
return Fragment;
};
const generateThrottle = () => {
let timer;
return function(fn, wait = 300) {
return function(...args) {
if (timer) {
clearTimeout(timer);
timer = void 0;
}
const context = this;
if (!timer) {
timer = setTimeout(() => {
fn.apply(context, args);
clearTimeout(timer);
timer = void 0;
}, wait);
}
};
};
};
const codes = /* @__PURE__ */ new Map([
[100, "Continue"],
[101, "Switching Protocols"],
[102, "Processing"],
[103, "Early Hints"],
[200, "OK"],
[201, "Created"],
[202, "Accepted"],
[203, "Non-Authoritative Information"],
[204, "No Content"],
[205, "Reset Content"],
[206, "Partial Content"],
[207, "Multi-Status"],
[208, "Already Reported"],
[226, "IM Used"],
[300, "Multiple Choices"],
[301, "Moved Permanently"],
[302, "Found"],
[303, "See Other"],
[304, "Not Modified"],
[305, "Use Proxy"],
[307, "Temporary Redirect"],
[308, "Permanent Redirect"],
[400, "Bad Request"],
[401, "Unauthorized"],
[402, "Payment Required"],
[403, "Forbidden"],
[404, "Not Found"],
[405, "Method Not Allowed"],
[406, "Not Acceptable"],
[407, "Proxy Authentication Required"],
[408, "Request Timeout"],
[409, "Conflict"],
[410, "Gone"],
[411, "Length Required"],
[412, "Precondition Failed"],
[413, "Payload Too Large"],
[414, "URI Too Long"],
[415, "Unsupported Media Type"],
[416, "Range Not Satisfiable"],
[417, "Expectation Failed"],
[418, "I'm a Teapot"],
[421, "Misdirected Request"],
[422, "Unprocessable Entity"],
[423, "Locked"],
[424, "Failed Dependency"],
[425, "Too Early"],
[426, "Upgrade Required"],
[428, "Precondition Required"],
[429, "Too Many Requests"],
[431, "Request Header Fields Too Large"],
[451, "Unavailable For Legal Reasons"],
[500, "Internal Server Error"],
[501, "Not Implemented"],
[502, "Bad Gateway"],
[503, "Service Unavailable"],
[504, "Gateway Timeout"],
[505, "HTTP Version Not Supported"],
[506, "Variant Also Negotiates"],
[507, "Insufficient Storage"],
[508, "Loop Detected"],
[509, "Bandwidth Limit Exceeded"],
[510, "Not Extended"],
[511, "Network Authentication Required"]
]);
({
// status code to message map
message: codes,
// status message (lower-case) to code map
code: createMessageToStatusCodeMap(codes),
// array of status codes
codes: createStatusCodeList(codes),
// status codes for redirects
redirect: {
300: true,
301: true,
302: true,
303: true,
305: true,
307: true,
308: true
},
// status codes for empty bodies
empty: {
204: true,
205: true,
304: true
},
// status codes for when you should retry the request
retry: {
502: true,
503: true,
504: true
}
});
function createMessageToStatusCodeMap(codes2) {
const map = /* @__PURE__ */ new Map();
for (const [status2, message] of codes2) {
map.set(message.toLowerCase(), status2);
}
return map;
}
function createStatusCodeList(codes2) {
const codeList = [];
for (const [status2, _] of codes2) {
codeList.push(status2);
}
return codeList;
}
const perToNum = (str = "") => {
if (str.length === 0)
return 0;
if (str.endsWith("%")) {
const value = Number(str.replace("%", ""));
return value > 1 ? value / 100 : value;
} else {
return Number(str);
}
};
const range = (num, min = 0, max = 1) => {
return Math.min(max, Math.max(min, num));
};
const _Mathjs = class _Mathjs2 {
constructor() {
__publicField(this, "getDecimalLength", (n2) => {
const [_, decimal] = n2.toString().split(".");
return decimal ? decimal.length : 0;
});
__publicField(this, "amend", (n2, precision = 15) => parseFloat(Number(n2).toPrecision(precision)));
__publicField(this, "power", (a2, b2) => Math.pow(10, Math.max(this.getDecimalLength(a2), this.getDecimalLength(b2))));
}
};
__publicField(_Mathjs, "handleMethod", (l2, r2) => {
const mathjs2 = new _Mathjs();
const { power, amend } = mathjs2;
const pow = power(l2, r2);
const a2 = amend(l2 * pow);
const b2 = amend(r2 * pow);
return (type) => {
switch (type) {
case "+":
return (a2 + b2) / pow;
case "-":
return (a2 - b2) / pow;
case "*":
return a2 * b2 / (pow * pow);
case "/":
return a2 / b2;
}
};
});
__publicField(_Mathjs, "add", (a2, b2) => {
return _Mathjs.handleMethod(a2, b2)("+");
});
__publicField(_Mathjs, "divide", (a2, b2) => {
return _Mathjs.handleMethod(a2, b2)("/");
});
__publicField(_Mathjs, "multiply", (a2, b2) => {
return _Mathjs.handleMethod(a2, b2)("*");
});
__publicField(_Mathjs, "subtract", (a2, b2) => {
return _Mathjs.handleMethod(a2, b2)("-");
});
const NEW_LISTENER = "NEW_LISTENER";
class SyncHook {
constructor() {
__publicField(this, "_events");
__publicField(this, "tap", (eventName, eventItem) => {
if (this._events[eventName] && eventName !== Symbol.for(NEW_LISTENER)) {
this.call(Symbol.for(NEW_LISTENER), eventName);
}
const callbacks = this._events[eventName] || [];
if (typeof eventItem === "function") {
callbacks.push({
name: eventName,
callback: eventItem
});
} else {
callbacks.push(eventItem);
}
this._events[eventName] = callbacks;
});
__publicField(this, "call", (eventName, ...args) => {
const callbacks = this._events[eventName] || [];
callbacks.forEach((item) => {
const { callback } = item;
callback(...args);
});
});
__publicField(this, "callSync", async (eventName, ...args) => {
const callbacks = this._events[eventName] || [];
for (const item of callbacks) {
const { callback } = item;
await callback(...args);
}
});
__publicField(this, "once", (eventName, eventItem) => {
let one;
if (typeof eventItem === "function") {
one = {
name: eventName,
callback: (...args) => {
eventItem(...args);
this.off(eventName, one);
},
initialCallback: eventItem
};
} else {
const { callback } = eventItem;
one = {
name: eventName,
callback: (...args) => {
callback(...args);
this.off(eventName, one);
},
initialCallback: callback
};
}
this.tap(eventName, one);
});
__publicField(this, "off", (eventName, eventItem) => {
const callbacks = this._events[eventName] || [];
const newCallbacks = callbacks.filter((item) => {
if (typeof eventItem === "function") {
return item.callback !== eventItem && item.initialCallback !== eventItem;
} else {
const { callback } = eventItem;
return item.callback !== callback && item.initialCallback !== callback;
}
});
this._events[eventName] = newCallbacks;
});
this._events = {};
}
}
export {
SyncHook as S,
addClassToElement as a,
createDocumentFragment as b,
currentDevice as c,
generateThrottle as d,
range as e,
timestampToTime as f,
getPixelRatio as g,
isMobile as i,
noop as n,
perToNum as p,
removeClassToElement as r,
timeFormat as t
};