77 lines
2.7 KiB
TypeScript
Raw Normal View History

import { toRaw } from 'vue'
2023-01-28 09:53:43 +08:00
// 创建监听器实例
2023-01-28 15:30:52 +08:00
export function createListenerObject(options, isTask, prefix) {
2023-01-28 11:28:52 +08:00
const listenerObj = Object.create(null)
listenerObj.event = options.event
isTask && (listenerObj.id = options.id) // 任务监听器特有的 id 字段
2023-01-28 09:53:43 +08:00
switch (options.listenerType) {
2023-01-28 15:30:52 +08:00
case 'scriptListener':
2023-01-28 11:28:52 +08:00
listenerObj.script = createScriptObject(options, prefix)
break
2023-01-28 15:30:52 +08:00
case 'expressionListener':
2023-01-28 11:28:52 +08:00
listenerObj.expression = options.expression
break
2023-01-28 15:30:52 +08:00
case 'delegateExpressionListener':
2023-01-28 11:28:52 +08:00
listenerObj.delegateExpression = options.delegateExpression
break
2023-01-28 09:53:43 +08:00
default:
2023-01-28 11:28:52 +08:00
listenerObj.class = options.class
2023-01-28 09:53:43 +08:00
}
// 注入字段
if (options.fields) {
2023-01-28 15:30:52 +08:00
listenerObj.fields = options.fields.map((field) => {
2023-01-28 11:28:52 +08:00
return createFieldObject(field, prefix)
})
2023-01-28 09:53:43 +08:00
}
// 任务监听器的 定时器 设置
2023-01-28 15:30:52 +08:00
if (isTask && options.event === 'timeout' && !!options.eventDefinitionType) {
const timeDefinition = window.bpmnInstances.moddle.create('bpmn:FormalExpression', {
body: options.eventTimeDefinitions
})
const TimerEventDefinition = window.bpmnInstances.moddle.create('bpmn:TimerEventDefinition', {
2023-01-28 09:53:43 +08:00
id: `TimerEventDefinition_${uuid(8)}`,
2023-01-28 15:30:52 +08:00
[`time${options.eventDefinitionType.replace(/^\S/, (s) => s.toUpperCase())}`]: timeDefinition
2023-01-28 11:28:52 +08:00
})
listenerObj.eventDefinitions = [TimerEventDefinition]
2023-01-28 09:53:43 +08:00
}
2023-01-28 15:30:52 +08:00
return window.bpmnInstances.moddle.create(
`${prefix}:${isTask ? 'TaskListener' : 'ExecutionListener'}`,
listenerObj
)
2023-01-28 09:53:43 +08:00
}
// 创建 监听器的注入字段 实例
2023-01-28 15:30:52 +08:00
export function createFieldObject(option, prefix) {
2023-01-28 11:28:52 +08:00
const { name, fieldType, string, expression } = option
2023-01-28 15:30:52 +08:00
const fieldConfig = fieldType === 'string' ? { name, string } : { name, expression }
2023-01-28 11:28:52 +08:00
return window.bpmnInstances.moddle.create(`${prefix}:Field`, fieldConfig)
2023-01-28 09:53:43 +08:00
}
// 创建脚本实例
2023-01-28 15:30:52 +08:00
export function createScriptObject(options, prefix) {
2023-01-28 11:28:52 +08:00
const { scriptType, scriptFormat, value, resource } = options
2023-01-28 15:30:52 +08:00
const scriptConfig =
scriptType === 'inlineScript' ? { scriptFormat, value } : { scriptFormat, resource }
2023-01-28 11:28:52 +08:00
return window.bpmnInstances.moddle.create(`${prefix}:Script`, scriptConfig)
2023-01-28 09:53:43 +08:00
}
// 更新元素扩展属性
2023-01-28 15:30:52 +08:00
export function updateElementExtensions(element, extensionList) {
const extensions = window.bpmnInstances.moddle.create('bpmn:ExtensionElements', {
2023-01-28 09:53:43 +08:00
values: extensionList
2023-01-28 11:28:52 +08:00
})
window.bpmnInstances.modeling.updateProperties(toRaw(element), {
2023-01-29 14:12:16 +08:00
extensionElements: extensions
2023-01-28 11:28:52 +08:00
})
2023-01-28 09:53:43 +08:00
}
// 创建一个id
2023-01-28 15:30:52 +08:00
export function uuid(length = 8, chars) {
let result = ''
const charsString = chars || '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
2023-01-28 09:53:43 +08:00
for (let i = length; i > 0; --i) {
2023-01-28 11:28:52 +08:00
result += charsString[Math.floor(Math.random() * charsString.length)]
2023-01-28 09:53:43 +08:00
}
2023-01-28 11:28:52 +08:00
return result
2023-01-28 09:53:43 +08:00
}