UNPKG

@aplus-frontend/utils

Version:

Utils for Aplus frontend team.

410 lines (409 loc) 11.8 kB
"use strict"; Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const dayjs = require("dayjs"); const lodashEs = require("lodash-es"); const index = require("../date/index"); const vue = require("vue"); const LOS_ANGELES_TIMEZONE = -7; const DEFAULT_TIMEZONE = 8; const DEFAULT_START_Date_KEY = "startDate"; const DEFAULT_END_Date_KEY = "endDate"; const zoneMap = /* @__PURE__ */ new Map(); zoneMap.set(0, "RJ"); zoneMap.set(1, "PR"); zoneMap.set(-1, "VD"); zoneMap.set(2, "CP"); zoneMap.set(-2, "BS"); zoneMap.set(3, "MS"); zoneMap.set(-3, "SP"); zoneMap.set(4, "DB"); zoneMap.set(-4, "NY"); zoneMap.set(5, "KR"); zoneMap.set(-5, "CG"); zoneMap.set(6, "TM"); zoneMap.set(-6, "DV"); zoneMap.set(7, "SU"); zoneMap.set(-7, "LA"); zoneMap.set(8, "SH"); zoneMap.set(-8, "ALS"); zoneMap.set(index.userTimezone, index.userTimezone); function computeTimezone(time, val) { if (val > 0) { return dayjs(time).add(val, "hours").valueOf(); } else if (val < 0) { return dayjs(time).subtract(val, "hours").valueOf(); } else { return time; } } function transformTableParams(values, timeConfigArr) { var _a, _b; const result = {}; const NUMBER = "[object Number]"; const STRING = "[object String]"; const ARRAY = "[object Array]"; const OBJECT = "[object Object]"; Object.keys(values).forEach((key) => { const type = Object.prototype.toString.call(values[key]); const val = values[key]; switch (type) { case NUMBER: result[key] = val; break; case STRING: result[key] = val == null ? void 0 : val.trim(); break; case ARRAY: { const targetObj = timeConfigArr == null ? void 0 : timeConfigArr.find((ele) => ele.key === key); let targetTimezone; let currentTimezone; if (targetObj && (val == null ? void 0 : val.length) === 2) { if (!lodashEs.isNil(targetObj.targetUtc)) { targetTimezone = zoneMap.get(targetObj.targetUtc) ?? (zoneMap.set( targetObj.targetUtc, targetObj.targetUtc ) && zoneMap.get(targetObj.targetUtc)); } else { targetTimezone = zoneMap.get(LOS_ANGELES_TIMEZONE); } if (targetObj.currentUtc) { currentTimezone = zoneMap.get(targetObj.currentUtc) ?? (zoneMap.set( targetObj.currentUtc, targetObj.currentUtc ) && zoneMap.get(targetObj.currentUtc)); } const startTime = dayjs(val[0]).startOf((targetObj == null ? void 0 : targetObj.timeOf) || "days"); const endTime = dayjs(val[1]).endOf((targetObj == null ? void 0 : targetObj.timeOf) || "days"); const startValue = index.getUtcTimestamp( startTime, targetTimezone, currentTimezone ); const endValue = index.getUtcTimestamp( endTime, targetTimezone, currentTimezone ); result[targetObj.startKey || DEFAULT_START_Date_KEY] = startValue; result[targetObj.endKey || DEFAULT_END_Date_KEY] = endValue; } else { result[key] = val; } break; } default: result[key] = val; } }); if (result.field && result.order) { result["orders"] = [ { column: result.field, asc: result.order !== "descend" } ]; delete result.field; delete result.order; } if (!lodashEs.isEmpty(result.sort) && Object.prototype.toString.call(result.sort) === OBJECT) { result["orders"] = (_b = (_a = Object.keys(result.sort)) == null ? void 0 : _a.filter((ele) => { var _a2; return (_a2 = result.sort) == null ? void 0 : _a2[ele]; })) == null ? void 0 : _b.map((key) => { return { column: key, asc: result.sort[key] !== "descend" }; }); delete result.sort; } if (lodashEs.isEmpty(result.sort)) { delete result.sort; } if (lodashEs.isEmpty(result.filter)) { delete result.filter; } if (result.current && !result.page) { result.page = result.current; delete result.current; } return result; } function transformTableParamsNoTimezone(values, timeConfigArr) { var _a, _b; const result = {}; const NUMBER = "[object Number]"; const STRING = "[object String]"; const ARRAY = "[object Array]"; const OBJECT = "[object Object]"; Object.keys(values).forEach((key) => { const type = Object.prototype.toString.call(values[key]); const val = values[key]; switch (type) { case NUMBER: result[key] = val; break; case STRING: result[key] = val == null ? void 0 : val.trim(); break; case ARRAY: { const targetObj = timeConfigArr == null ? void 0 : timeConfigArr.find((ele) => ele.key === key); if (targetObj && (val == null ? void 0 : val.length) === 2) { result[targetObj.startKey || DEFAULT_START_Date_KEY] = val[0]; result[targetObj.endKey || DEFAULT_END_Date_KEY] = val[1]; } else { result[key] = val; } break; } default: result[key] = val; } }); if (result.field && result.order) { result["orders"] = [ { column: result.field, asc: result.order !== "descend" } ]; delete result.field; delete result.order; } if (!lodashEs.isEmpty(result.sort) && Object.prototype.toString.call(result.sort) === OBJECT) { result["orders"] = (_b = (_a = Object.keys(result.sort)) == null ? void 0 : _a.filter((ele) => { var _a2; return (_a2 = result.sort) == null ? void 0 : _a2[ele]; })) == null ? void 0 : _b.map((key) => { return { column: key, asc: result.sort[key] !== "descend" }; }); delete result.sort; } if (lodashEs.isEmpty(result.sort)) { delete result.sort; } if (lodashEs.isEmpty(result.filter)) { delete result.filter; } if (result.current && !result.page) { result.page = result.current; delete result.current; } return result; } function setCellTenant(tenantName, tenantCode, Tooltip) { console.warn("[Deprecated] `setCellTenant` 已废弃,请使用UI组件代替。"); const ellipsis = { whiteSpace: "nowrap", overflow: "hidden", textOverflow: "ellipsis" }; const toolTitle = vue.h("div", {}, [ vue.h("div", {}, tenantName || "--"), vue.h("div", {}, tenantCode || "--") ]); return vue.h( "div", { class: "aplus-table-cellTenant", style: { ...ellipsis, lineHeight: "22px" } }, [ vue.h( Tooltip, { title: toolTitle, placement: "topLeft" }, [ vue.h( "span", { class: "aplus-table-cellTenant_name", style: ellipsis }, tenantName || "--" ), vue.h( "div", { class: "aplus-table-cellTenant_code", style: { ...ellipsis, color: "#999" } }, vue.h("span", {}, tenantCode || "--") ) ] ) ] ); } function generatorCellLineItem(config) { const data = config.data; const labelStyle = config.labelStyle; const valueStyle = config.valueStyle; const linesItemStyle = config.linesItemStyle; try { const isText = typeof data.value === "string"; const showEllipse = data.value && isText; const ellipseStyle = showEllipse ? { flexShrink: 1, minWidth: 0, whiteSpace: "nowrap", overflow: "hidden", textOverflow: "ellipsis" } : {}; return vue.h( "div", { style: { display: "flex", width: "100%", ...linesItemStyle } }, [ vue.h( "div", { style: labelStyle }, `${data.label}${data.label ? ":" : ""}` ), vue.h( "div", { style: { ...ellipseStyle, ...valueStyle } }, [data.value || "--"] ) ] ); } catch (error) { console.error(error); } } function generatorCellLinesWrapper(cellLines = [], linesWrapperStyle) { return vue.h( "div", { style: { width: "100%", minWidth: 0, display: "inline-flex", flexDirection: "column", justifyContent: "center", alignItems: "start", lineHeight: "22px", ...linesWrapperStyle } }, cellLines ); } function generatorCellLinesFinal(Tooltip, linesWrapperStyle, vNodes) { var _a; let props = null; let titleSlot = null; if (vue.isVNode(Tooltip)) { props = Tooltip.props || null; titleSlot = ((_a = Tooltip.children) == null ? void 0 : _a.title) || null; } if (!lodashEs.isNil(Tooltip) && typeof Tooltip !== "boolean") { return vue.h( Tooltip, { placement: "top", mouseEnterDelay: 0.5 }, { title: titleSlot || (props == null ? void 0 : props.title) || vNodes.tooltipText, default: generatorCellLinesWrapper(vNodes.cellLines, linesWrapperStyle) } ); } else { return generatorCellLinesWrapper(vNodes.cellLines, linesWrapperStyle); } } const setCellLinesToB = function(data, Tooltip, styleConfig) { console.warn("[Deprecated] `setCellLinesToB` 已废弃,请使用UI组件代替。"); const cellLines = []; const tooltipText = []; const { labelStyle, valueStyle, linesWrapperStyle, linesItemStyle } = styleConfig || {}; Reflect.ownKeys(data).forEach((key) => { const value = data[key]; const label = key; const itemTooltip = generatorCellLineItem({ data: { value, label }, labelStyle: { color: "#fff" }, valueStyle: { color: "#fff" } }); const itemBox = generatorCellLineItem({ data: { value, label }, labelStyle: { color: "#999", ...labelStyle }, valueStyle: { color: "#333", ...valueStyle }, linesItemStyle }); cellLines.push(itemBox); tooltipText.push(itemTooltip); }); return generatorCellLinesFinal(Tooltip || false, linesWrapperStyle || {}, { cellLines, tooltipText }); }; const setCellLinesToC = function(data, Tooltip, styleConfig) { console.warn("[Deprecated] `setCellLinesToC` 已废弃,请使用UI组件代替。"); const cellLines = []; const tooltipText = []; const { labelStyle, valueStyle, linesWrapperStyle, linesItemStyle } = styleConfig || {}; Reflect.ownKeys(data).forEach((key) => { const value = data[key]; const label = key; const itemTooltip = generatorCellLineItem({ data: { value, label }, labelStyle: { color: "#fff" }, valueStyle: { color: "#fff" } }); const itemBox = generatorCellLineItem({ data: { value, label }, labelStyle: { color: "#8896B0", ...labelStyle }, valueStyle: { ...valueStyle }, linesItemStyle }); cellLines.push(itemBox); tooltipText.push(itemTooltip); }); return generatorCellLinesFinal(Tooltip || false, linesWrapperStyle || {}, { cellLines, tooltipText }); }; exports.DEFAULT_END_Date_KEY = DEFAULT_END_Date_KEY; exports.DEFAULT_START_Date_KEY = DEFAULT_START_Date_KEY; exports.DEFAULT_TIMEZONE = DEFAULT_TIMEZONE; exports.LOS_ANGELES_TIMEZONE = LOS_ANGELES_TIMEZONE; exports.computeTimezone = computeTimezone; exports.setCellLinesToB = setCellLinesToB; exports.setCellLinesToC = setCellLinesToC; exports.setCellTenant = setCellTenant; exports.transformTableParams = transformTableParams; exports.transformTableParamsNoTimezone = transformTableParamsNoTimezone;