UNPKG

tav-ui

Version:
305 lines (302 loc) 10.9 kB
import { unref, computed } from 'vue'; import { isFunction, isObject } from '../../../../utils/is2.mjs'; import { PAGE_SIZE } from '../const2.mjs'; const DEF = { data: { result: [] } }; function getPropByPath(obj, path, strict = false) { let tempObj = obj; path = path.replace(/\[(\w+)\]/g, ".$1"); path = path.replace(/^\./, ""); const keyArr = path.split("."); let i = 0; for (let len = keyArr.length; i < len - 1; ++i) { if (!tempObj && !strict) break; const key = keyArr[i]; if (key in tempObj) { tempObj = tempObj[key]; } else { if (strict) { console.log(`please transfer a valid prop path to form item!`); } break; } } return { o: tempObj, k: keyArr[i], v: tempObj ? tempObj[keyArr[i]] : null }; } function deepSet(obj, path, val) { path = path.replaceAll("[", ".["); const keys = path.split("."); for (let i = 0; i < keys.length; i++) { let currentKey = keys[i]; let nextKey = keys[i + 1]; if (currentKey.includes("[")) { currentKey = parseInt(currentKey.substring(1, currentKey.length - 1)); } if (nextKey && nextKey.includes("[")) { nextKey = parseInt(nextKey.substring(1, nextKey.length - 1)); } if (typeof nextKey !== "undefined") { obj[currentKey] = obj[currentKey] ? obj[currentKey] : isNaN(nextKey) ? {} : []; } else { obj[currentKey] = val; } obj = obj[currentKey]; } } function handleExtendProxyConfig(tablePropsRef) { const { apiSetting, proxyConfig, api } = unref(tablePropsRef); const hasApi = api && isFunction(api); if (hasApi) { const hasApiSetting = Object.keys(apiSetting).length > 0; const hasProxyConfigProps = proxyConfig?.props && Object.keys(proxyConfig?.props).length > 0; if (hasApiSetting) { const props = { list: apiSetting.listField ? `data.${apiSetting.listField}` : "data", result: apiSetting.listField ? `data.${apiSetting.listField}` : "data", total: apiSetting.totalField ? `data.${apiSetting.totalField}` : "data" }; if (!hasProxyConfigProps) { unref(tablePropsRef).proxyConfig = { props }; } } } return tablePropsRef; } function handleExtendApi(tablePropsRef, tableRef, emit, tableEmitter) { const { api, beforeApi, afterApi, customActionConfig, apiSetting, permission, apiType } = unref(tablePropsRef); const permissionApi = permission?.apiPermissionData ?? void 0; const permissionApiParams = permission?.apiParams ?? void 0; const hasApi = permissionApi && isFunction(permissionApi) || api && isFunction(api); const hasExportAllApi = customActionConfig && customActionConfig.export && isObject(customActionConfig.export); if (hasApi) { unref(tablePropsRef).proxyConfig["ajax"] = { query: () => Promise.resolve({}) }; let params = { filter: permissionApiParams?.body?.filter ?? {}, model: permissionApiParams?.body?.model ?? {} }; let result = {}; if (hasExportAllApi) { unref(tablePropsRef).proxyConfig["ajax"]["queryAll"] = async (refParam) => { const keepedApiParamKeys = customActionConfig.export.keepedApiParamKeys ?? []; const isPermissionApi = permissionApi && permissionApiParams; let _api = api; if (isPermissionApi) { _api = permissionApi; params = { ...permissionApiParams, ...(params.body?.filter ?? params.filter) && (params.body?.model ?? params.model) ? { body: apiType === "list" ? { ...params.body?.filter ?? params.filter ?? {}, ...{ ...params.body?.model ?? params.model ?? {}, viewAll: true, modeType: refParam?.options?.modeType } } : { filter: params.body?.filter ?? params.filter ?? {}, model: { ...params.body?.model ?? params.model ?? {}, viewAll: true, modeType: refParam?.options?.modeType } } } : {} }; } else { if (apiType === "list") { params = { ...params, ...params.model ? { model: { ...params.model, viewAll: true, modeType: refParam?.options?.modeType } } : { viewAll: true, modeType: refParam?.options?.modeType } }; } else { params = { filter: params.filter ?? {}, model: { ...params.model ?? {}, viewAll: true, modeType: refParam?.options?.modeType } }; } } const allParamsFilter = {}; const isModeTypeAll = (params.body?.model ?? params.model ?? params)["modeType"] === "all"; if (isModeTypeAll) { keepedApiParamKeys.length && keepedApiParamKeys.forEach((key) => { const { v } = getPropByPath(params, key); if (v) { deepSet(allParamsFilter, key, v); } }); } let exportResult = {}; const apiResult = await _api?.(isModeTypeAll ? params.body ? { ...allParamsFilter, body: { filter: allParamsFilter.body?.filter ?? {}, model: params.body.model } } : params.filter || params.model ? { filter: allParamsFilter.filter ?? {}, model: params.model ?? {} } : { viewAll: true, modeType: refParam?.options?.modeType } : params); if (apiResult.data && apiResult.success) { exportResult = apiResult; if (customActionConfig.export.afterApi && isFunction(customActionConfig.export.afterApi)) { exportResult = await customActionConfig.export.afterApi(apiResult) || exportResult; } if (apiType === "list") { exportResult = { data: { [apiSetting.listField]: exportResult.data, [apiSetting.totalField]: exportResult.data.length } }; } } return exportResult; }; } unref(tablePropsRef).proxyConfig["autoLoad"] = false; unref(tablePropsRef).proxyConfig["beforeQuery"] = async (vxeOption, option) => { const { page: { currentPage = 1, pageSize = PAGE_SIZE } } = vxeOption; const model = { page: currentPage, limit: pageSize }; params = option ? option.model ? { ...option, ...{ model: { ...model, ...option.model } }, ...option.model } : { ...option, ...{ model } } : { ...params?.body ?? params, ...{ model } }; try { if (beforeApi && isFunction(beforeApi)) { params = await beforeApi(params) || params; } let _api = api; if (permissionApi && permissionApiParams) { _api = permissionApi; params = { ...permissionApiParams, ...(params.filter ?? {}) && (params.model ?? {}) ? { body: apiType === "list" ? { ...params.filter ?? params, ...params.model ?? params } : { filter: params.filter ?? {}, model: params.model ?? {} } } : {} }; } if (_api && isFunction(_api)) { console.log("hijack table pro api \u{1F602}"); const apiResult = await _api(params); if (apiResult.data && apiResult.success) { result = apiResult; if (afterApi && isFunction(afterApi)) { result = await afterApi(result) || result; } if (apiType === "list") { result = { data: { [apiSetting.listField]: result.data, [apiSetting.totalField]: result.data.length } }; } } else { result = DEF; } } else { result = DEF; } tableEmitter.emit("table-pro:api-success", { result }); emit("ApiSuccess", { ...result }); return Promise.resolve(result); } catch (error) { emit("ApiError", error); return Promise.resolve(DEF); } }; } return tablePropsRef; } function handleExtendProps(tablePropsRef, tableRef, emit, tableEmitter) { const handleExtendProxyConfigResult = handleExtendProxyConfig(tablePropsRef); const handleExtendApiResult = handleExtendApi(handleExtendProxyConfigResult, tableRef, emit, tableEmitter); return handleExtendApiResult; } function handleRowLineHeight(tablePropsRef) { const { fixedLineHeight } = unref(tablePropsRef); if (fixedLineHeight) { unref(tablePropsRef).showOverflow = "ellipsis"; unref(tablePropsRef).showHeaderOverflow = "ellipsis"; unref(tablePropsRef).showFooterOverflow = "ellipsis"; unref(tablePropsRef).scrollX.enabled = true; unref(tablePropsRef).scrollY.enabled = true; } else { unref(tablePropsRef).showOverflow = null; unref(tablePropsRef).showHeaderOverflow = null; unref(tablePropsRef).showFooterOverflow = null; unref(tablePropsRef).scrollX.enabled = false; unref(tablePropsRef).scrollY.enabled = false; } return tablePropsRef; } function mergePropsRef(defaultPropsRef, paramPropsRef, tableRef, emit, tableEmitter) { return computed(() => { for (const [key] of Object.entries(unref(paramPropsRef))) { if (unref(defaultPropsRef)[key]) { if (isObject(unref(defaultPropsRef)[key])) { unref(paramPropsRef)[key] = { ...unref(defaultPropsRef)[key], ...unref(paramPropsRef)[key] }; } } } const handledRowLineHeightTablePropsRef = handleRowLineHeight(paramPropsRef); const tablePropsRef = handleExtendProps(handledRowLineHeightTablePropsRef, tableRef, emit, tableEmitter); return { ...unref(tablePropsRef) }; }); } function createDefaultPropsRef(defaultProps) { const _defaultProps = {}; for (const [key, value] of Object.entries(defaultProps)) { const defaultValue = value.default; if (defaultValue) { _defaultProps[key] = typeof defaultValue === "function" ? defaultValue() : defaultValue; } } return computed(() => ({ ..._defaultProps })); } function useProps(tableProProps, paramPropsRef, tableRef, emit, tableEmitter) { const defaultPropsRef = createDefaultPropsRef(tableProProps); const tablePropsRef = mergePropsRef(defaultPropsRef, paramPropsRef, tableRef, emit, tableEmitter); return tablePropsRef; } export { useProps }; //# sourceMappingURL=useProps2.mjs.map