dm-vue3-ui
Version:
This Components Library will help get you started developing in Vue 3.
240 lines (239 loc) • 7.16 kB
JavaScript
import dayjs from "dayjs";
import { presetValues, realTimeValues, timeUnits } from "./config";
import { $t } from "../../i18n/index";
function getTimeType(startTime, endTime, values) {
const types = ["preset", "real", "relative", "absolute"];
let result = { isMatch: false, activeIndex: -1, type: "" };
for (let i = 0; i < types.length; i++) {
const type = types[i];
switch (type) {
case "preset":
result = isMatchPreset(startTime, endTime, values);
break;
case "real":
result = isMatchRealTime(startTime, endTime);
break;
case "relative":
result = isMatchRelativeTime(startTime, endTime);
break;
case "absolute":
result = isMatchAbsoluteTime(startTime, endTime);
break;
}
result.type = type;
if (result.isMatch) {
break;
}
}
return result;
}
function isMatchPreset(startTime, endTime, values) {
let activeIndex = "";
if (values.length > 0) {
activeIndex = values.findIndex(
(item) => item.startTime == startTime && item.endTime == endTime
);
} else {
activeIndex = presetValues.findIndex(
(item) => item.startTime == startTime && item.endTime == endTime
);
}
const isMatch = activeIndex == -1 ? false : true;
return {
isMatch,
activeIndex
};
}
function isMatchRealTime(startTime, endTime) {
const matchStartTime = startTime.match(/^rt-(\d+)(s|m|h|d|w|mon|q|y)$/);
const matchEndTime = endTime.match(/^rtnow$/);
const isMatch = matchStartTime && matchEndTime ? true : false;
let activeIndex = -1;
if (isMatch) {
activeIndex = realTimeValues.findIndex(
(item) => item.value == matchStartTime[1] && item.unit == matchStartTime[2]
);
}
return {
isMatch,
activeIndex
};
}
function isMatchRelativeTime(startTime, endTime) {
const matchStartTime = startTime.match(/^-(\d+)(s|m|h|d|w|mon|q|y)$/);
const matchEndTime = endTime.match(/^-(\d+)(s|m|h|d|w|mon|q|y)$/);
const isMatch = matchStartTime && matchEndTime || matchStartTime && endTime == "now" ? true : false;
return {
isMatch,
activeIndex: -1
//相对时间不存在快捷菜单
};
}
function isMatchAbsoluteTime(startTime, endTime) {
const matchStartTime = startTime.match(/^\d{10}$/);
const matchEndTime = endTime.match(/^\d{10}$/);
let isMatch = false;
if (matchStartTime && endTime == "now") {
isMatch = true;
} else if (matchEndTime && startTime == "") {
isMatch = true;
} else if (matchStartTime && matchEndTime) {
isMatch = true;
} else {
isMatch = false;
}
return {
isMatch,
activeIndex: -1
//绝对时间不存在快捷菜单
};
}
function getLabelText(type, startTime, endTime, values, format = "YYYY-MM-DD HH:mm:ss") {
let labelText = "";
switch (type) {
case "preset":
labelText = getPresetLabelText(startTime, endTime, values);
break;
case "real":
labelText = getRealLabelText(startTime, endTime);
break;
case "relative":
labelText = getRelativeLabelText(startTime, endTime);
break;
case "absolute":
labelText = getAbsoluteLabelText(startTime, endTime, format);
break;
}
return labelText;
}
function getPresetLabelText(startTime, endTime, values) {
let obj = {};
if (values.length > 0) {
obj = values.find((item) => {
return item.startTime == startTime && item.endTime == endTime;
});
} else {
obj = presetValues.find((item) => {
return item.startTime == startTime && item.endTime == endTime;
});
}
if (obj) {
return obj.label;
} else {
return "";
}
}
function getRealLabelText(startTime, endTime) {
let textStr = "";
const matchStartTime = startTime.match(/^rt-(\d+)(s|m|h|d|w|mon|q|y)$/);
if (matchStartTime && endTime == "rtnow") {
const value = matchStartTime[1];
const unit = matchStartTime[2];
const timeUnitLabel = getTimeUnitLabel(unit);
textStr = `${value}${timeUnitLabel} ${$t("datePicker.relative.to")} ${$t(
"datePicker.immediately"
)}`;
}
return textStr;
}
function getRelativeLabelText(startTime, endTime) {
let textStr = "";
const matchStartTime = startTime.match(/^-(\d+)(s|m|h|d|w|mon|q|y)$/);
const matchEndTime = endTime.match(/^-(\d+)(s|m|h|d|w|mon|q|y)$/);
if (matchStartTime && matchEndTime) {
const startValue = matchStartTime[1];
const startUnit = matchStartTime[2];
const startUnitLabel = getTimeUnitLabel(startUnit);
const endValue = matchEndTime[1];
const endUnit = matchEndTime[2];
const endUnitLabel = getTimeUnitLabel(endUnit);
textStr = `${startValue}${startUnitLabel} ${$t(
"datePicker.relative.to"
)} ${endValue}${endUnitLabel}`;
} else if (matchStartTime && endTime == "now") {
const startValue = matchStartTime[1];
const startUnit = matchStartTime[2];
const startUnitLabel = getTimeUnitLabel(startUnit);
textStr = `${startValue}${startUnitLabel} ${$t("datePicker.relative.to")} ${$t(
"datePicker.now"
)}`;
}
return textStr;
}
function getAbsoluteLabelText(startTime, endTime, format = "YYYY-MM-DD HH:mm:ss") {
const dateFormat = "YYYY-MM-DD HH:mm:ss";
let textStr = "";
if (format === "unix") {
if (endTime == "") {
textStr = `${dayjs.unix(startTime).format(dateFormat)} ${$t("datePicker.after")}`;
} else if (startTime == "") {
textStr = `${dayjs.unix(endTime).format(dateFormat)} ${$t("datePicker.before")}`;
} else {
textStr = `${dayjs.unix(startTime).format(dateFormat)} - ${dayjs.unix(endTime).format(dateFormat)}`;
}
} else {
if (endTime == "") {
textStr = `${startTime} ${$t("datePicker.after")}`;
} else if (startTime == "") {
textStr = `${endTime} ${$t("datePicker.before")}`;
} else {
textStr = `${startTime} - ${endTime}`;
}
}
return textStr;
}
function getTimeUnitLabel(unit) {
const timeUnit = timeUnits.find((item) => item.value == unit);
if (timeUnit) {
return timeUnit.label;
} else {
return "";
}
}
function isTimeNumber(value) {
const num = Number(value);
if (Number.isInteger(num) && num > 0) {
return true;
} else {
return false;
}
}
function compareDate(startValue, startUnit, endValue, endUnit) {
const startMomentUnit = getMomentUnit(startUnit);
const startMoment = dayjs().subtract(startValue, startMomentUnit);
let endMoment = dayjs();
if (endValue !== "now") {
const endMomentUnit = getMomentUnit(endUnit);
endMoment = dayjs().subtract(Number(endValue), endMomentUnit);
}
if (endMoment.isBefore(startMoment)) {
return {
isValid: false,
msg: $t("datePicker.validateMsg")[3]
};
} else if (startMoment.isSame(endMoment)) {
return {
isValid: false,
msg: $t("datePicker.validateMsg")[1]
};
} else {
return {
isValid: true,
msg: $t("datePicker.validateMsg")[4]
};
}
}
function getMomentUnit(unit) {
const timeUnit = timeUnits.find((item) => item.value == unit);
if (timeUnit) {
return timeUnit.momentUnit;
} else {
return "";
}
}
export {
compareDate,
getLabelText,
getTimeType,
isTimeNumber
};