提交 | 用户 | 时间
|
820397
|
1 |
import { toRaw } from 'vue' |
H |
2 |
const bpmnInstances = () => (window as any)?.bpmnInstances |
|
3 |
// 创建监听器实例 |
|
4 |
export function createListenerObject(options, isTask, prefix) { |
3e359e
|
5 |
debugger |
820397
|
6 |
const listenerObj = Object.create(null) |
H |
7 |
listenerObj.event = options.event |
|
8 |
isTask && (listenerObj.id = options.id) // 任务监听器特有的 id 字段 |
|
9 |
switch (options.listenerType) { |
|
10 |
case 'scriptListener': |
|
11 |
listenerObj.script = createScriptObject(options, prefix) |
|
12 |
break |
|
13 |
case 'expressionListener': |
|
14 |
listenerObj.expression = options.expression |
|
15 |
break |
|
16 |
case 'delegateExpressionListener': |
|
17 |
listenerObj.delegateExpression = options.delegateExpression |
|
18 |
break |
|
19 |
default: |
|
20 |
listenerObj.class = options.class |
|
21 |
} |
|
22 |
// 注入字段 |
|
23 |
if (options.fields) { |
|
24 |
listenerObj.fields = options.fields.map((field) => { |
|
25 |
return createFieldObject(field, prefix) |
|
26 |
}) |
|
27 |
} |
|
28 |
// 任务监听器的 定时器 设置 |
|
29 |
if (isTask && options.event === 'timeout' && !!options.eventDefinitionType) { |
|
30 |
const timeDefinition = bpmnInstances().moddle.create('bpmn:FormalExpression', { |
|
31 |
body: options.eventTimeDefinitions |
|
32 |
}) |
|
33 |
const TimerEventDefinition = bpmnInstances().moddle.create('bpmn:TimerEventDefinition', { |
|
34 |
id: `TimerEventDefinition_${uuid(8)}`, |
|
35 |
[`time${options.eventDefinitionType.replace(/^\S/, (s) => s.toUpperCase())}`]: timeDefinition |
|
36 |
}) |
|
37 |
listenerObj.eventDefinitions = [TimerEventDefinition] |
|
38 |
} |
|
39 |
return bpmnInstances().moddle.create( |
|
40 |
`${prefix}:${isTask ? 'TaskListener' : 'ExecutionListener'}`, |
|
41 |
listenerObj |
|
42 |
) |
|
43 |
} |
|
44 |
|
|
45 |
// 创建 监听器的注入字段 实例 |
|
46 |
export function createFieldObject(option, prefix) { |
|
47 |
const { name, fieldType, string, expression } = option |
|
48 |
const fieldConfig = fieldType === 'string' ? { name, string } : { name, expression } |
|
49 |
return bpmnInstances().moddle.create(`${prefix}:Field`, fieldConfig) |
|
50 |
} |
|
51 |
|
|
52 |
// 创建脚本实例 |
|
53 |
export function createScriptObject(options, prefix) { |
|
54 |
const { scriptType, scriptFormat, value, resource } = options |
|
55 |
const scriptConfig = |
|
56 |
scriptType === 'inlineScript' ? { scriptFormat, value } : { scriptFormat, resource } |
|
57 |
return bpmnInstances().moddle.create(`${prefix}:Script`, scriptConfig) |
|
58 |
} |
|
59 |
|
|
60 |
// 更新元素扩展属性 |
|
61 |
export function updateElementExtensions(element, extensionList) { |
|
62 |
const extensions = bpmnInstances().moddle.create('bpmn:ExtensionElements', { |
|
63 |
values: extensionList |
|
64 |
}) |
|
65 |
bpmnInstances().modeling.updateProperties(toRaw(element), { |
|
66 |
extensionElements: extensions |
|
67 |
}) |
|
68 |
} |
|
69 |
|
|
70 |
// 创建一个id |
|
71 |
export function uuid(length = 8, chars?) { |
|
72 |
let result = '' |
|
73 |
const charsString = chars || '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' |
|
74 |
for (let i = length; i > 0; --i) { |
|
75 |
result += charsString[Math.floor(Math.random() * charsString.length)] |
|
76 |
} |
|
77 |
return result |
|
78 |
} |