tav-ui
Version:
309 lines (304 loc) • 11.1 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var vue = require('vue');
var is = require('../../../../utils/is2.js');
var _const = require('../const2.js');
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 } = vue.unref(tablePropsRef);
const hasApi = api && is.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) {
vue.unref(tablePropsRef).proxyConfig = { props };
}
}
}
return tablePropsRef;
}
function handleExtendApi(tablePropsRef, tableRef, emit, tableEmitter) {
const { api, beforeApi, afterApi, customActionConfig, apiSetting, permission, apiType } = vue.unref(tablePropsRef);
const permissionApi = permission?.apiPermissionData ?? void 0;
const permissionApiParams = permission?.apiParams ?? void 0;
const hasApi = permissionApi && is.isFunction(permissionApi) || api && is.isFunction(api);
const hasExportAllApi = customActionConfig && customActionConfig.export && is.isObject(customActionConfig.export);
if (hasApi) {
vue.unref(tablePropsRef).proxyConfig["ajax"] = {
query: () => Promise.resolve({})
};
let params = {
filter: permissionApiParams?.body?.filter ?? {},
model: permissionApiParams?.body?.model ?? {}
};
let result = {};
if (hasExportAllApi) {
vue.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 && is.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;
};
}
vue.unref(tablePropsRef).proxyConfig["autoLoad"] = false;
vue.unref(tablePropsRef).proxyConfig["beforeQuery"] = async (vxeOption, option) => {
const {
page: { currentPage = 1, pageSize = _const.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 && is.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 && is.isFunction(_api)) {
console.log("hijack table pro api \u{1F602}");
const apiResult = await _api(params);
if (apiResult.data && apiResult.success) {
result = apiResult;
if (afterApi && is.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 } = vue.unref(tablePropsRef);
if (fixedLineHeight) {
vue.unref(tablePropsRef).showOverflow = "ellipsis";
vue.unref(tablePropsRef).showHeaderOverflow = "ellipsis";
vue.unref(tablePropsRef).showFooterOverflow = "ellipsis";
vue.unref(tablePropsRef).scrollX.enabled = true;
vue.unref(tablePropsRef).scrollY.enabled = true;
} else {
vue.unref(tablePropsRef).showOverflow = null;
vue.unref(tablePropsRef).showHeaderOverflow = null;
vue.unref(tablePropsRef).showFooterOverflow = null;
vue.unref(tablePropsRef).scrollX.enabled = false;
vue.unref(tablePropsRef).scrollY.enabled = false;
}
return tablePropsRef;
}
function mergePropsRef(defaultPropsRef, paramPropsRef, tableRef, emit, tableEmitter) {
return vue.computed(() => {
for (const [key] of Object.entries(vue.unref(paramPropsRef))) {
if (vue.unref(defaultPropsRef)[key]) {
if (is.isObject(vue.unref(defaultPropsRef)[key])) {
vue.unref(paramPropsRef)[key] = {
...vue.unref(defaultPropsRef)[key],
...vue.unref(paramPropsRef)[key]
};
}
}
}
const handledRowLineHeightTablePropsRef = handleRowLineHeight(paramPropsRef);
const tablePropsRef = handleExtendProps(handledRowLineHeightTablePropsRef, tableRef, emit, tableEmitter);
return { ...vue.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 vue.computed(() => ({ ..._defaultProps }));
}
function useProps(tableProProps, paramPropsRef, tableRef, emit, tableEmitter) {
const defaultPropsRef = createDefaultPropsRef(tableProProps);
const tablePropsRef = mergePropsRef(defaultPropsRef, paramPropsRef, tableRef, emit, tableEmitter);
return tablePropsRef;
}
exports.useProps = useProps;
//# sourceMappingURL=useProps2.js.map