tav-ui
Version:
148 lines (145 loc) • 4.96 kB
JavaScript
import { computed } from 'vue';
const useHandlerInOuter = () => {
let outerHandler;
const register = (handler) => {
outerHandler = handler;
};
const getHandler = () => {
if (!outerHandler) {
console.error("no register, handler it's undefined\ntry onMounted & nextTick");
}
return outerHandler;
};
const backfill = () => getHandler()?.backfill();
const clearResponse = () => getHandler()?.clearResponse();
return { register, backfill, getHandler, clearResponse };
};
const useFileTypeCode = (fileTypeCode) => {
const getOptionsByTypeCodes = (typeCodeArray) => computed(() => {
const options = [];
const typeCodeArraySet = [...new Set(typeCodeArray)];
for (const key in fileTypeCode) {
options.push(...fileTypeCode[key].filter((el) => typeCodeArraySet.includes(el.value)));
}
return options;
});
const getOptionsByModuleCodePrefix = (prefix) => {
if (!Array.isArray(prefix)) {
prefix = [prefix];
}
const onlyPrefix = [...new Set(prefix)];
const moduleCodes = [];
onlyPrefix.forEach((prefix2) => {
moduleCodes.push(...Object.keys(fileTypeCode).filter((el) => el.startsWith(prefix2)));
});
return getOptionsByModuleCode(moduleCodes);
};
const getOptionsByModuleCode = (moduleCode) => Array.isArray(moduleCode) ? [...new Set(moduleCode)].map((el) => fileTypeCode[el]).reduce((x, y) => x.concat(y), []) : fileTypeCode[moduleCode];
const mergeOptions = (moduleCode, typeCodeArray, moduleCodePrefix) => {
const options = (moduleCode && getOptionsByModuleCode(moduleCode) || []).concat(typeCodeArray !== void 0 ? getOptionsByTypeCodes(typeCodeArray).value : []).concat(moduleCodePrefix !== void 0 ? getOptionsByModuleCodePrefix(moduleCodePrefix) : []);
if (!(options && options.length)) {
return [];
}
const result = [];
options.forEach((el) => {
if (el && !result.some((resItem) => resItem.value === el.value && resItem.label === el.label)) {
result.push(el);
}
});
return result;
};
return {
mergeOptions,
getOptionsByTypeCodes,
getOptionsByModuleCode,
getOptionsByModuleCodePrefix
};
};
function getActionColumnMaxWidth(arr, {
margin = 17,
fontSize = 12,
appendWidth = 10
} = {}) {
let l = 0;
arr.sort((x, y) => y.length - x.length);
const _arr = arr.splice(0, 3);
for (const str of _arr) {
const [text, dots] = str.split("..");
l += text.length * fontSize;
if (dots) {
l += 2 * 4;
}
l += margin;
}
l += appendWidth * 2;
return l;
}
function useFileFormatter({ fileVersionCount = "newest" } = {}) {
const versionRecord = {};
function upadteVersion(file) {
const existFile = versionRecord[file.actualId]?.find((el) => el.id === file.id);
if (existFile) {
if (existFile.name !== file.name || existFile.address !== file.address) {
const fileArr = versionRecord[existFile.actualId];
if (fileArr) {
const lastIndex = fileArr.length - 1;
fileArr[lastIndex] = file;
}
}
return;
}
if (fileVersionCount === "newest") {
if (versionRecord[file.actualId]) {
if (file.version === 0 && getBasicFileByActualId(file.actualId).version === 1 && !(getBasicFileByActualId(file.actualId).businessId || getBasicFileByActualId(file.actualId).businessKey)) {
file.version = 1;
} else {
file.version = versionRecord[file.actualId][0].version + 1;
}
versionRecord[file.actualId][1] = file;
} else {
versionRecord[file.actualId] = [file];
}
return;
}
if (versionRecord[file.actualId]) {
if (versionRecord[file.actualId]?.some((el) => file.id === el.id))
return;
versionRecord[file.actualId].push(file);
} else {
versionRecord[file.actualId] = [file];
}
}
function formatToApi(files) {
const currentFileActualIdsSet = /* @__PURE__ */ new Set();
for (const file of files) {
upadteVersion(file);
currentFileActualIdsSet.add(file.actualId);
}
for (const actualId in versionRecord) {
currentFileActualIdsSet.has(actualId) || Reflect.deleteProperty(versionRecord, actualId);
}
return Object.keys(versionRecord).map((k) => ({
actualId: k,
moduleCode: versionRecord[k][0].moduleCode,
versionList: [versionRecord[k][1] || versionRecord[k][0]]
}));
}
function getFilesByActualId(actualId) {
return versionRecord[actualId];
}
function getBasicFileByActualId(actualId) {
return getFilesByActualId(actualId)?.[0];
}
function getNewestFileByActualId(actualId) {
return getFilesByActualId(actualId)?.[1];
}
return {
formatToApi,
upadteVersion,
getFilesByActualId,
getBasicFileByActualId,
getNewestFileByActualId
};
}
export { getActionColumnMaxWidth, useFileFormatter, useFileTypeCode, useHandlerInOuter };
//# sourceMappingURL=hooks2.mjs.map