utils.ts 2.7 KB

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