useNodeData.ts
4.5 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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'
import { useContentDataStoreWithOut } from '@/store/modules/contentData'
import { useMessage } from '@/hooks/web/useMessage'
import { MessageEnum } from '@/enums/messageEnum'
import type { DeviceItemType } from '@/api/device/model'
import { getDeviceInfo } from '@/api/device'
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 { createMessage } = useMessage()
const nodeData = ref<NodeDataType>()
const deviceInfo = ref<DeviceItemType>()
const contentDataStore = useContentDataStoreWithOut()
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 getDeviceDetail = async () => {
if (!unref(nodeData)?.dataSourceJson.deviceId) return
deviceInfo.value = await getDeviceInfo(unref(nodeData)!.dataSourceJson.deviceId)
}
const getNodeAllData = async () => {
const result = await doGetNodeBindData(basicNodeBindData(ActionType.GET))
nodeData.value = result || getDefaultNodeData()
await getDeviceDetail()
return result
}
const saveNodeAllData = async (data: Partial<SaveNodeDataParamsType>) => {
if (!contentDataStore.hasDesignAuth) {
createMessage.error(MessageEnum.NO_AUTH)
throw new Error(MessageEnum.NO_AUTH)
}
const result = await doSaveNodeAllData(Object.assign(data, basicNodeBindData(ActionType.SAVE)))
removeCellSourceAttribute(cell)
nodeData.value = result || getDefaultNodeData()
await getDeviceDetail()
return result
}
const saveNodeDataSourceData = async (data: NodeDataDataSourceJsonType) => {
if (!contentDataStore.hasDesignAuth) {
createMessage.error(MessageEnum.NO_AUTH)
throw new Error(MessageEnum.NO_AUTH)
}
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) => {
if (!contentDataStore.hasDesignAuth) {
createMessage.error(MessageEnum.NO_AUTH)
throw new Error(MessageEnum.NO_AUTH)
}
const result = await doSaveNodeEvent({ ...basicNodeBindData(ActionType.SAVE), data })
removeCellSourceAttribute(cell)
return result
}
const saveNodeActData = async (data: NodeDataActJsonType) => {
if (!contentDataStore.hasDesignAuth) {
createMessage.error(MessageEnum.NO_AUTH)
throw new Error(MessageEnum.NO_AUTH)
}
const result = await doSaveNodeAct({ ...basicNodeBindData(ActionType.SAVE), data })
removeCellSourceAttribute(cell)
return result
}
onMounted(() => {
immediate && getNodeAllData()
})
return {
getDeviceInfo: computed(() => unref(deviceInfo)),
getNodeData: computed(() => unref(nodeData)),
getCellInfo,
getNodeAllData,
saveNodeAllData,
saveNodeEventData,
saveNodeActData,
saveNodeDataSourceData,
}
}