UNPKG

yanzhen-design-vue

Version:

147 lines (146 loc) 5.15 kB
import { inject, computed, unref, provide, ref } from "vue"; import { isObject } from "@vue/shared"; import { get, set } from "lodash-es"; import { isEmptyObject } from "@yanzhen-libs/utils"; import "../constants/index.mjs"; import { OTHER_TABLES_RELEVANCE, OTHER_TABLE_TYPE } from "../constants/common.mjs"; import { otherTablesContextKey } from "../constants/other-table-config.mjs"; const RELEVANCE = OTHER_TABLES_RELEVANCE; const otherTableType = OTHER_TABLE_TYPE; function useOtherTablesConfig() { const config = inject(otherTablesContextKey); return computed(() => { return unref(config); }); } const provideOtherTablesConfig = (context) => { provide(otherTablesContextKey, context); }; const useOtherTable = (props, otherOptions) => { const primaryCodeKey = "CX1PrimaryCode"; const associationModalRef = ref(null); const { ctx, provideConfig } = otherOptions; const associationSubmit = (options) => { console.log("[options]", options); const isRadio = options.type === otherTableType.RADIO; isRadio ? stagingCx1Data(options) : stagingTableData(options); validateForm(); }; const stagingCx1Data = (options) => { const { selectRelBizObjectModelCode, selectTheBizObjRelationTableColumnCode, selectRecord, tableColumnCode } = options; let transData = {}; let middleData = {}; if (Object.keys(selectRecord).length) { transData = selectRecord["transData"] ?? {}; middleData = selectRecord["middleData"] ?? {}; } if (!isEmptyObject(props.model[primaryCodeKey])) { handleUpdateModel(primaryCodeKey, {}); } for (const custom of props.elements) { const { id, relBizObjectColumnCode, relBizObjectModelCode, relBizObjectStorageMode, theBizObjRelationTableColumnCode, theSourceColumnCode, sourceBizObjectModelCode, hidden } = custom.Attributes; let modelCode = relBizObjectModelCode; let ColumnCode = theBizObjRelationTableColumnCode; let isSource = false; if (sourceBizObjectModelCode && sourceBizObjectModelCode !== relBizObjectModelCode) { modelCode = sourceBizObjectModelCode; ColumnCode = theSourceColumnCode; isSource = true; } if (modelCode === selectRelBizObjectModelCode && ColumnCode === selectTheBizObjRelationTableColumnCode) { if (String(relBizObjectStorageMode) === RELEVANCE.TC) { handleUpdateModel(id, transData[relBizObjectColumnCode] ?? ""); } if (String(relBizObjectStorageMode) === RELEVANCE.CX1 && !hidden) { if (isSource) { handleUpdateModel(id, middleData[relBizObjectColumnCode] ?? ""); handleUpdateModel(`${primaryCodeKey}.${id}`, transData[relBizObjectColumnCode] ?? ""); } else { handleUpdateModel(id, selectRecord[relBizObjectColumnCode] ?? ""); handleUpdateModel(`${primaryCodeKey}.${id}`, selectRecord[tableColumnCode] ?? ""); } } } } }; const stagingTableData = (options) => { const { selectRecord, clickItemId } = options; props.elements.forEach((item) => { const { id } = item.Attributes; if (id === clickItemId) { handleUpdateModel(id, selectRecord.join(",")); } }); }; const openAssociationModal = (raw) => { var _a; const { sceneID, bizObjectModelCode } = props; const clickItemData = props.elements.find( (item) => item.Attributes.id === raw.columnCode ); const { id, relBizObjectStorageMode } = clickItemData.Attributes; const selectType = [RELEVANCE.CX2, RELEVANCE.KZ].includes(relBizObjectStorageMode) ? otherTableType.CHECKBOX : otherTableType.RADIO; let selectedRowKeys = ""; if ([RELEVANCE.CX1, RELEVANCE.CX2].includes(relBizObjectStorageMode)) { const CX1PrimaryCode = props.model.CX1PrimaryCode ?? {}; const transData = props.model ?? {}; selectedRowKeys = CX1PrimaryCode[id] || transData[id] || ""; } const option = { clickItemData: unref(clickItemData), sceneID, bizObjectModelCode, selectType, selectedRowKeys }; (_a = associationModalRef.value) == null ? void 0 : _a.openModal(option); }; function handleUpdateModel(key, value) { const model = unref(props.model); if (!isObject(model)) return; const oldValue = get(model, key); if (oldValue !== value) { set(model, key, value); } } const validateForm = async () => { const valid = await ctx.formCtx.beforeSubmit({ edit: true, isValid: true }); console.log("valid-valid", valid); }; provide("openAssociationModal", openAssociationModal); provideOtherTablesConfig({ component: provideConfig == null ? void 0 : provideConfig.component, primaryCode: computed(() => props.model.CX1PrimaryCode), stagingCx1Data, provideProps: props, associationModalRef }); return { associationModalRef, stagingCx1Data, stagingTableData, associationSubmit }; }; export { provideOtherTablesConfig, useOtherTable, useOtherTablesConfig };