useNodeData.ts
3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import { computed, onMounted, ref, unref } from 'vue'
import { doGetNodeBindData, doSaveNodeAct, doSaveNodeAllData, doSaveNodeDataSource, doSaveNodeEvent } from '@/api/node'
import { useParseParams } from '@/core/LoadData'
import type { MxCell } from '@/fitCore/types'
import type { BasicNodeBindType, NodeDataActJsonType, NodeDataDataSourceJsonType, NodeDataEventJsonType, NodeDataType, SaveNodeDataParamsType } from '@/api/node/model'
import { CellAttributeKeyEnum } from '@/enums/cellAttributeEnum'
interface UseNodeDataParamsType {
cell: MxCell
immediate?: boolean
}
const getDefaultNodeData = (): Partial<NodeDataType> => {
return {
dataSourceJson: {} as any,
eventJson: {} as any,
actJson: {} as any,
}
}
enum ActionType {
SAVE = 'SAVE',
GET = 'GET',
}
function removeCellSourceAttribute(cell: MxCell) {
if (cell.hasAttribute(CellAttributeKeyEnum.COPY_SOURCE))
cell.getValue()?.removeAttribute?.(CellAttributeKeyEnum.COPY_SOURCE)
}
export function useNodeData({ cell, immediate = true }: UseNodeDataParamsType) {
const nodeData = ref<NodeDataType>()
// const contentId = window.DrawApp.configurationContentId!
const { configurationId } = useParseParams()
const getCellID = () => {
return cell.getId()
}
const getSourceCellID = () => {
const sourceNode = cell.getAttribute(CellAttributeKeyEnum.COPY_SOURCE)
return sourceNode
}
const basicNodeBindData = (type: ActionType): BasicNodeBindType => {
return {
id: type === ActionType.GET ? getSourceCellID () || getCellID () : getCellID(),
contentId: window.DrawApp.currentPage.getId(),
configurationId,
}
}
const getCellInfo = computed(() => {
return {
[CellAttributeKeyEnum.CATEGORY]: cell.getAttribute(CellAttributeKeyEnum.CATEGORY),
[CellAttributeKeyEnum.COMPONENT_KEY]: cell.getAttribute(CellAttributeKeyEnum.COMPONENT_KEY),
}
})
const getNodeAllData = async () => {
const result = await doGetNodeBindData(basicNodeBindData(ActionType.GET))
nodeData.value = result || getDefaultNodeData()
return result
}
const saveNodeAllData = async (data: Partial<SaveNodeDataParamsType>) => {
const result = await doSaveNodeAllData(Object.assign(data, basicNodeBindData(ActionType.SAVE)))
removeCellSourceAttribute(cell)
nodeData.value = result || getDefaultNodeData()
return result
}
const saveNodeDataSourceData = async (data: NodeDataDataSourceJsonType) => {
const result = await doSaveNodeDataSource({ ...basicNodeBindData(ActionType.SAVE), data })
removeCellSourceAttribute(cell)
if (unref(nodeData)?.dataSourceJson)
nodeData.value!.dataSourceJson = result
return result
}
const saveNodeEventData = async (data: NodeDataEventJsonType) => {
const result = await doSaveNodeEvent({ ...basicNodeBindData(ActionType.SAVE), data })
removeCellSourceAttribute(cell)
return result
}
const saveNodeActData = async (data: NodeDataActJsonType) => {
const result = await doSaveNodeAct({ ...basicNodeBindData(ActionType.SAVE), data })
removeCellSourceAttribute(cell)
return result
}
onMounted(() => {
immediate && getNodeAllData()
})
return {
getNodeData: computed(() => unref(nodeData)),
getCellInfo,
getNodeAllData,
saveNodeAllData,
saveNodeEventData,
saveNodeActData,
saveNodeDataSourceData,
}
}