utils.ts 2.8 KB

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