-
Notifications
You must be signed in to change notification settings - Fork 7
/
toModel.js
279 lines (250 loc) · 7.62 KB
/
toModel.js
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import Node from '../components/flow/helpers/Node'
import Flow from '../components/flow/helpers/Flow'
import { createConditionNode } from '../components/flow/helpers/NodeUtils'
import {
getNodeType,
createFlowGraph,
convertParticipant
// convertTransfer,
} from '../utils/adapterHelpers'
import {
updateFieldsWhenCreate,
updateFieldsWithMerge
} from '../utils/fieldsHelpers'
import { deepCopy } from '../utils/index'
import { SPONSOR_PICKER_TYPE } from '../constants/CONDITION_ADDITION'
/**
* @typedef {import('../components/flow/helpers/FlowGraph').default} FlowGraph
* @typedef {{ id: string, type?: 'general'|'other_condition', name?: string|null, targetTaskId: string, sourceTaskId: string, conditions?: [] }} SequenceItem
*
* toModel 序列化服务端数据 ServeModel 到客户端数据 ClientModel
* @param {{taskList: TaskNode[], sequenceList: SequenceItem[]}} ServerDataModel 服务端返回的数据 model
* @param {{flowFields: [], conditionFields: []}} ClientDataModel 解析数据时需要的客户端本地数据
* @param {boolean} isBusinessFlow 是否是业务流程
* @returns {Node}
*/
export const toModel = (
{ taskList = [], sequenceList = [] } = {},
{ flowFields = [], conditionFields = [] } = {},
isBusinessFlow
) => {
if (taskList.length && sequenceList.length) {
/* 有数据/反填充的情况下,处理数据初始化 graph vertices & edges,返回第一个节点 */
const startNode = connectGraphEdges(
{ taskList, sequenceList },
{ flowFields, conditionFields },
isBusinessFlow
)
return startNode
} else {
/* 无数据/新建的情况下,返回默认的起始节点 */
return Flow.createNode({
type: Node.TYPE.SPONSOR_NODE,
nodeId: 'task-start',
formFieldList: flowFields
}).next(
Flow.createNode({
type: Node.TYPE.APPROVER_NODE,
formFieldList: updateFieldsWhenCreate(flowFields)(
Node.TYPE.APPROVER_NODE
)
})
)
}
}
/**
* connectGraphEdges
* @param {{taskList: TaskNode[], sequenceList: SequenceItem[]}} ServerDataModel 服务端返回的数据 model
* @param {{flowFields: [], conditionFields: []}} ClientDataModel 解析数据时需要的客户端本地数据
* @param {boolean} isBusinessFlow 是否是业务流程
* @returns {Node} startNode
*/
const connectGraphEdges = (
{ sequenceList, taskList },
{ flowFields, conditionFields },
isBusinessFlow
) => {
const graph = createFlowGraph()
const findTask = findTaskById(taskList)
let startNode = null
for (let i = 0; i < sequenceList.length; i++) {
const node = sequenceList[i]
const {
type: conditionType,
sourceTaskId,
targetTaskId,
conditionList
} = node
const sourceNode = Flow.createNode(
toNode(deepCopy(findTask(sourceTaskId)), { flowFields })
)
const targetNode = Flow.createNode(
toNode(deepCopy(findTask(targetTaskId)), { flowFields })
)
// fix: [审批流]条件字段不存在不能发布审批流程的(#1022122)
const conditions =
Array.isArray(conditionList) && conditionList.length
? filterConditions(conditionList, flowFields, isBusinessFlow)
: []
// 如果是条件分支,开始绘制各个条件
if (sourceNode.type === Node.TYPE.CONDITION_BRANCH) {
const conditionNode = createConditionNode({
conditions,
conditionType
})
conditionNode.read({
conditions,
fields: conditionFields
})
graph.addEdge(sourceNode, conditionNode)
graph.addEdge(conditionNode, targetNode)
} else {
if (sourceNode.type === Node.TYPE.PARALLEL_BRANCH) {
targetNode.alias = Node.TYPE.PARALLEL_NODE
}
graph.addEdge(sourceNode, targetNode)
}
if (!startNode) {
startNode = sourceNode
}
}
drawNodeLinesByGraph(graph)
return startNode
}
/**
* * drawNodeLinesByGraph BFS 绘制视图节点和连线
* @param {FlowGraph} graph
* @param {Node} startNode
* // @param {WeakMap<Node, Node>} conditionNodesMap
*/
export const drawNodeLinesByGraph = (graph) => {
if (process.env.NODE_ENV === 'development') {
console.log('graph: ', graph)
}
const commonNodeMap = {}
for (const [key, node] of graph.edges) {
// END_NODE or PARALLEL_BRANCH_END 无需连线
if (
node &&
node.type !== Node.TYPE.PARALLEL_BRANCH_END &&
node.type !== Node.TYPE.CONDITION_BRANCH_END &&
node.type !== Node.TYPE.END_NODE
) {
const prevNode = graph.getVertexByKey(key)
if (commonNodeMap[node.nodeId]) {
console.log('commonNodeMap: ', commonNodeMap)
continue
} else {
prevNode.next(node)
}
if (prevNode.oppositeTaskId && !commonNodeMap[prevNode.oppositeTaskId]) {
if (!prevNode.childNode) {
commonNodeMap[prevNode.oppositeTaskId] = prevNode.next(
graph.getVertexByKey(prevNode.oppositeTaskId)
)
}
}
}
}
}
/**
* @typedef ClientNodeModel
* @property {string} nodeId
* @property {string} type
* @property {{ title: string, content: string }} model
* @property {{ type: string, list: [], [fieldKey]: string }} [participant]
* @property {[]} [formFieldList]
*
* toNode 将 ServerTaskItem 转为 Node 构造函数参数
* @param {TaskNode} ServerTaskItem
* @param {{flowFields: [], conditionFields: []}} ClientDataModel 解析数据时需要的客户端本地数据
* @param {boolean} needConvert 是否转换数据结构到 ClientViewModel
* @returns {ClientNodeModel}
*/
export const toNode = (
{
taskNodeId = '',
taskNodeName = '',
taskType = '',
participant = {},
transfer,
formFieldList = [],
...props
} = {},
{ flowFields: fields = [] } = {},
needConvert = true
) => {
const nodeType = getNodeType(taskType)
let content = ''
if (needConvert) {
if (participant) {
content = convertParticipant(participant, fields)
}
// convertTransfer(transfer, nodeType)
}
return {
nodeId: taskNodeId,
type: nodeType,
model: {
title: taskNodeName,
content
},
participant,
transfer,
formFieldList: updateFieldsWithMerge(fields, formFieldList, nodeType),
...props
}
}
/**
* findTaskById
* @typedef {Object<string, any>} TaskNode
* @property {string} taskNodeId
* @property {string} taskNodeName
* @property {string} taskType
* @property {Object<string, any>} [participant]
*
* @param {TaskNode[]} tasks
* @callback FindTaskFn
* @param {string} id
* @returns {TaskNode}
* @returns {FindTaskFn}
*/
export const findTaskById = (tasks = []) => {
const cachedTasks = tasks.reduce((cache, task) => {
cache[task.taskNodeId] = task
return cache
}, {})
return (id) => {
return (
id &&
(cachedTasks[id]
? cachedTasks[id]
: (cachedTasks[id] = tasks.find((t) => t && t.taskNodeId === id)))
)
}
}
/**
* @param {field[]} flowFields
* @param {condition[]} conditionList
* @param {boolean} isBusinessFlow 是否是业务流程
*/
function filterConditions(conditionList, flowFields, isBusinessFlow) {
return conditionList.filter(Boolean).map((condition) => {
// 如果是默认的发起人类型
const isSponsor = SPONSOR_PICKER_TYPE[condition.fieldKey]
if (!isSponsor) {
const field = flowFields.find(
(item) => item.fieldKey === condition.fieldKey
)
if (!field) {
condition.fieldKey = ''
} else {
// fix: 如果是业务流程,且字段的 required 属性是 false,也需要清空该条件
if (isBusinessFlow && field.require !== undefined && !field.require) {
condition.fieldKey = ''
}
}
}
return condition
})
}