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