dm-vue3-ui
Version:
This Components Library will help get you started developing in Vue 3.
290 lines (289 loc) • 9.42 kB
JavaScript
import { useSlots, reactive, computed } from "vue";
import dayjs from "dayjs";
function dateFormat(value = Date.now(), format = "YYYY-MM-DD HH:mm:ss") {
let date;
if (typeof value === "number" || typeof value === "string") {
date = new Date(value);
} else {
date = value;
}
function fixedTwo(value2) {
return value2 < 10 ? "0" + value2 : String(value2);
}
let showTime = format;
if (showTime.includes("SSS")) {
const S = date.getMilliseconds();
showTime = showTime.replace("SSS", "0".repeat(3 - String(S).length) + S);
}
if (showTime.includes("YY")) {
const Y = date.getFullYear();
showTime = showTime.includes("YYYY") ? showTime.replace("YYYY", String(Y)) : showTime.replace("YY", String(Y).slice(2, 4));
}
if (showTime.includes("M")) {
const M = date.getMonth() + 1;
showTime = showTime.includes("MM") ? showTime.replace("MM", fixedTwo(M)) : showTime.replace("M", String(M));
}
if (showTime.includes("D")) {
const D = date.getDate();
showTime = showTime.includes("DD") ? showTime.replace("DD", fixedTwo(D)) : showTime.replace("D", String(D));
}
if (showTime.includes("H")) {
const H = date.getHours();
showTime = showTime.includes("HH") ? showTime.replace("HH", fixedTwo(H)) : showTime.replace("H", String(H));
}
if (showTime.includes("m")) {
const m = date.getMinutes();
showTime = showTime.includes("mm") ? showTime.replace("mm", fixedTwo(m)) : showTime.replace("m", String(m));
}
if (showTime.includes("s")) {
const s = date.getSeconds();
showTime = showTime.includes("ss") ? showTime.replace("ss", fixedTwo(s)) : showTime.replace("s", String(s));
}
return showTime;
}
const requestAnimationFrame = typeof window !== "undefined" ? window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame : () => {
};
const cancelAnimationFrame = typeof window !== "undefined" ? window.cancelAnimationFrame : () => {
};
function rafTimeout(fn, delay = 0, interval = false) {
const requestAnimationFrame2 = typeof window !== "undefined" ? window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame : () => {
};
let start = null;
function timeElapse(timestamp) {
if (!start) {
start = timestamp;
}
const elapsed = timestamp - start;
if (elapsed >= delay) {
fn();
if (interval) {
start = null;
raf.id = requestAnimationFrame2(timeElapse);
}
} else {
raf.id = requestAnimationFrame2(timeElapse);
}
}
const raf = {
// 引用类型保存,方便获取 requestAnimationFrame()方法返回的 ID.
id: requestAnimationFrame2(timeElapse)
};
return raf;
}
function cancelRaf(raf) {
const cancelAnimationFrame2 = typeof window !== "undefined" ? window.cancelAnimationFrame : () => {
};
if (raf && raf.id) {
cancelAnimationFrame2(raf.id);
}
}
function throttle(fn, delay = 300) {
let valid = true;
return function() {
if (valid) {
valid = false;
rafTimeout(() => {
fn();
valid = true;
}, delay);
}
return false;
};
}
function debounce(fn, delay = 300) {
let timer = null;
return function() {
if (timer) {
cancelRaf(timer);
}
timer = rafTimeout(fn, delay);
};
}
function add(num1, num2) {
const num1DeciStr = String(num1).split(".")[1];
const num2DeciStr = String(num2).split(".")[1];
const maxLen = Math.max((num1DeciStr == null ? void 0 : num1DeciStr.length) || 0, (num2DeciStr == null ? void 0 : num2DeciStr.length) || 0);
const num1Str = num1.toFixed(maxLen);
const num2Str = num2.toFixed(maxLen);
const result = +num1Str.replace(".", "") + +num2Str.replace(".", "");
return result / Math.pow(10, maxLen);
}
function downloadFile(url, name) {
let fileName = "";
if (name) {
fileName = name;
} else {
const res = url.split("?")[0].split("/");
fileName = res[res.length - 1];
}
const xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
xhr.responseType = "blob";
xhr.onload = function() {
if (xhr.status === 200) {
const blob = xhr.response;
const link = document.createElement("a");
const body = document.querySelector("body");
link.href = typeof window !== "undefined" ? window.URL.createObjectURL(blob) : "";
link.download = fileName;
link.style.display = "none";
body == null ? void 0 : body.appendChild(link);
link.click();
body == null ? void 0 : body.removeChild(link);
typeof window !== "undefined" && window.URL.revokeObjectURL(link.href);
}
};
xhr.send();
}
function moneyFormat(value, decimal = 2, split = ",") {
function thousandFormat(numStr) {
const len = numStr.length;
return len <= 3 ? numStr : thousandFormat(numStr.slice(0, len - 3)) + split + numStr.slice(len - 3, len);
}
const money = String(value);
if (isFinite(parseFloat(money))) {
if (parseFloat(money) === 0) {
return parseFloat(money).toFixed(decimal);
} else {
let res = "";
const dotIndex = money.indexOf(".");
if (dotIndex === -1) {
if (decimal === 0) {
res = thousandFormat(money);
} else {
res = thousandFormat(money) + "." + "0".repeat(decimal);
}
} else {
const numStr = String(
(Math.round(parseFloat(money) * Math.pow(10, decimal)) / Math.pow(10, decimal)).toFixed(
decimal
)
);
const decimals = numStr.slice(dotIndex, dotIndex + decimal + 1);
res = thousandFormat(numStr.slice(0, dotIndex)) + decimals;
}
return res;
}
} else {
return "--";
}
}
const SEARCH_TEMPLATE_KEY = "di-alert-search-template";
const addTemplate = (moduleKey, value) => {
const templateInfo = store(SEARCH_TEMPLATE_KEY) && JSON.parse(store(SEARCH_TEMPLATE_KEY)) || {};
const currentModuleInfo = templateInfo[moduleKey] || [];
const index = currentModuleInfo.findIndex((v) => v.name === value.name);
index === -1 || currentModuleInfo.splice(index, 1);
currentModuleInfo.push(value);
templateInfo[moduleKey] = currentModuleInfo;
store(SEARCH_TEMPLATE_KEY, JSON.stringify(templateInfo));
};
const getTemplatesByModuleKey = (moduleKey) => {
const templateInfo = store(SEARCH_TEMPLATE_KEY) && JSON.parse(store(SEARCH_TEMPLATE_KEY)) || {};
const currentModuleTemplates = templateInfo[moduleKey] || [];
return currentModuleTemplates;
};
const deleteTemplate1 = (moduleKey, templateName) => {
const templateInfo = store(SEARCH_TEMPLATE_KEY) && JSON.parse(store(SEARCH_TEMPLATE_KEY)) || {};
const currentModuleInfo = templateInfo[moduleKey] || [];
const index = currentModuleInfo.findIndex((v) => v.name === templateName);
index === -1 || currentModuleInfo.splice(index, 1);
templateInfo[moduleKey] = currentModuleInfo;
store(SEARCH_TEMPLATE_KEY, JSON.stringify(templateInfo));
};
const validateRangeDateValue = (value) => {
if (Array.isArray(value) && value.length === 2) {
return value.every((v) => v && dayjs(v).isValid());
} else {
return false;
}
};
function store(key, value) {
function _parse(a) {
try {
return JSON.parse(a);
} catch (b) {
return a;
}
}
if (typeof window !== "undefined" && (window == null ? void 0 : window.localStorage)) {
try {
if (void 0 === value) {
value = localStorage.getItem(key);
return value && _parse(value);
} else {
localStorage.setItem(key, JSON.stringify(value));
}
} catch (e) {
console.log("not support localstorage");
}
}
}
function removeStore(key) {
if (typeof window !== "undefined") {
try {
localStorage.removeItem(key);
} catch (e) {
console.log("not support localstorage");
}
}
}
function useSlotsExist(slotsName = "default") {
const slots = useSlots();
const checkSlotsExist = (slotsName2) => {
var _a;
const slotsContent = (_a = slots[slotsName2]) == null ? void 0 : _a.call(slots);
const checkExist = (slotContent) => {
if (typeof slotContent.children === "string") {
if (slotContent.children === "v-if") {
return false;
}
return slotContent.children.trim() !== "";
} else {
if (slotContent.children === null) {
if (slotContent.type === "img" || typeof slotContent.type !== "string") {
return true;
}
} else {
return Boolean(slotContent.children);
}
}
};
if (slotsContent && (slotsContent == null ? void 0 : slotsContent.length)) {
const result = slotsContent.some((slotContent) => {
return checkExist(slotContent);
});
return result;
}
return false;
};
if (Array.isArray(slotsName)) {
const slotsExist = reactive({});
slotsName.forEach((item) => {
const exist = computed(() => checkSlotsExist(item));
slotsExist[item] = exist;
});
return slotsExist;
} else {
return computed(() => checkSlotsExist(slotsName));
}
}
export {
add,
addTemplate,
cancelAnimationFrame,
cancelRaf,
dateFormat,
debounce,
deleteTemplate1,
downloadFile,
getTemplatesByModuleKey,
moneyFormat,
rafTimeout,
removeStore,
requestAnimationFrame,
store,
throttle,
useSlotsExist,
validateRangeDateValue
};