UNPKG

tav-ui

Version:
147 lines (142 loc) 5.19 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var vue = require('vue'); var lodashEs = require('lodash-es'); require('../constants/index2.js'); var hooks = require('../constants/hooks2.js'); const CASCADE_PRO_CACHE = /* @__PURE__ */ new Map(); const handleSelectRecordsAfterGetFields = (records, selectRecord, idPathSplitResult, setSelectRecords, type) => { if (type === "init") { setSelectRecords([], "recover"); } else { const allParentRecords = []; for (let i = 0; i < idPathSplitResult.length - 1; i++) { const options = records[i]; const parentRecord = options.find((option) => option.id === idPathSplitResult[i]); parentRecord && allParentRecords.push(parentRecord); } if (allParentRecords.length > 0) { setSelectRecords([...allParentRecords, vue.unref(selectRecord)], "normal"); } else { setSelectRecords([vue.unref(selectRecord)], "normal"); } } }; function getFirstLetterSortedOptions(options) { return options.sort((a, b) => { const nameA = a.firstLetter?.toUpperCase(); const nameB = b.firstLetter?.toUpperCase(); if (nameA && nameB) { if (nameA < nameB) { return -1; } if (nameA > nameB) { return 1; } } return 0; }); } function handleOptionsWithFirstLetterVisible(options) { const handledFirstLetterSort = getFirstLetterSortedOptions(options); const firstLetters = [ ...new Set(handledFirstLetterSort.map((option) => option.firstLetter)) ]; const firstLettersMap = firstLetters.reduce((result, cur) => { result[cur] = 0; return result; }, {}); const groupByFirstLetterOptions = handledFirstLetterSort.map((option) => { const { firstLetter } = option; if (firstLettersMap[firstLetter] === 0) { firstLettersMap[firstLetter]++; return { ...option, firstLetterGroup: firstLetter }; } else { return { ...option, firstLetterGroup: "" }; } }); return groupByFirstLetterOptions; } function searchData(param) { const firstLetterVisible = vue.unref(param.firstLetterVisible); const { id, pid, fieldIndex, idPathSplitResult } = vue.unref(param.selectRecordFibers); const tree = vue.unref(param.options).tree; if (id === "" && pid === "" && fieldIndex === 0) { if (firstLetterVisible) { if (!CASCADE_PRO_CACHE.has(vue.unref(param.id))) { CASCADE_PRO_CACHE.set(vue.unref(param.id), { firstFieldFirstLetterResult: handleOptionsWithFirstLetterVisible(tree) }); } const cache = CASCADE_PRO_CACHE.get(vue.unref(param.id)); handleSelectRecordsAfterGetFields([cache["firstFieldFirstLetterResult"]], vue.unref(param.selectRecord), idPathSplitResult, param.setSelectRecords, "init"); return [cache["firstFieldFirstLetterResult"]]; } else { handleSelectRecordsAfterGetFields([tree], vue.unref(param.selectRecord), idPathSplitResult, param.setSelectRecords, "init"); return [tree]; } } else { const currentFieldLength = idPathSplitResult.length; const nextFieldLength = currentFieldLength + 1; const records = []; for (let i = 0; i < nextFieldLength; i++) { if (i === 0) { if (firstLetterVisible) { if (!CASCADE_PRO_CACHE.has(vue.unref(param.id))) { CASCADE_PRO_CACHE.set(vue.unref(param.id), { firstFieldFirstLetterResult: handleOptionsWithFirstLetterVisible(tree) }); } const cache = CASCADE_PRO_CACHE.get(vue.unref(param.id)); records.push(cache["firstFieldFirstLetterResult"]); } else { records.push(tree); } } else { const currentOptions = records[i - 1].find((option) => option.id === idPathSplitResult[i - 1])?.children; records.push(currentOptions); } } const response = records.filter(Boolean); handleSelectRecordsAfterGetFields(response, vue.unref(param.selectRecord), idPathSplitResult, param.setSelectRecords, "normal"); return response; } } function useFieldRequest(param) { const resultRef = vue.ref([]); const errorRef = vue.ref(""); const onClick = (selected) => { const selectRecord = selected; param.setSelectRecord(selectRecord); param.setLoading(true); const response = searchData({ firstLetterVisible: param.firstLetterVisible, selectRecord: param.selectRecord, setSelectRecords: param.setSelectRecords, selectRecordFibers: param.selectRecordFibers, options: param.options, id: param.id }); if (Array.isArray(response)) { resultRef.value = response; errorRef.value = ""; } else if (response === null) { } else { resultRef.value = []; errorRef.value = hooks.RequestErrorTip; } param.setLoading(false); }; if (param.immediate) { onClick(vue.unref(param.selectRecord)); } const result = vue.computed(() => vue.unref(resultRef)); const error = vue.computed(() => vue.unref(errorRef)); return { result, error, onClick: lodashEs.debounce(onClick, hooks.DebounceDely) }; } exports.useFieldRequest = useFieldRequest; //# sourceMappingURL=use-request2.js.map