utils.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import { toRaw } from 'vue'
  2. const bpmnInstances = () => (window as any)?.bpmnInstances
  3. // 创建监听器实例
  4. export function createListenerObject(options, isTask, prefix) {
  5. debugger
  6. const listenerObj = Object.create(null)
  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. export function createFieldObject(option, prefix) {
  46. const { name, fieldType, string, expression } = option
  47. const fieldConfig = fieldType === 'string' ? { name, string } : { name, expression }
  48. return bpmnInstances().moddle.create(`${prefix}:Field`, fieldConfig)
  49. }
  50. // 创建脚本实例
  51. export function createScriptObject(options, prefix) {
  52. const { scriptType, scriptFormat, value, resource } = options
  53. const scriptConfig =
  54. scriptType === 'inlineScript' ? { scriptFormat, value } : { scriptFormat, resource }
  55. return bpmnInstances().moddle.create(`${prefix}:Script`, scriptConfig)
  56. }
  57. // 更新元素扩展属性
  58. export function updateElementExtensions(element, extensionList) {
  59. const extensions = bpmnInstances().moddle.create('bpmn:ExtensionElements', {
  60. values: extensionList
  61. })
  62. bpmnInstances().modeling.updateProperties(toRaw(element), {
  63. extensionElements: extensions
  64. })
  65. }
  66. // 创建一个id
  67. export function uuid(length = 8, chars?) {
  68. let result = ''
  69. const charsString = chars || '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
  70. for (let i = length; i > 0; --i) {
  71. result += charsString[Math.floor(Math.random() * charsString.length)]
  72. }
  73. return result
  74. }