utils.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // 创建监听器实例
  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. export function createFieldObject(option, prefix) {
  38. const { name, fieldType, string, expression } = option;
  39. const fieldConfig = fieldType === "string" ? { name, string } : { name, expression };
  40. return window.bpmnInstances.moddle.create(`${prefix}:Field`, fieldConfig);
  41. }
  42. // 创建脚本实例
  43. export function createScriptObject(options, prefix) {
  44. const { scriptType, scriptFormat, value, resource } = options;
  45. const scriptConfig = scriptType === "inlineScript" ? { scriptFormat, value } : { scriptFormat, resource };
  46. return window.bpmnInstances.moddle.create(`${prefix}:Script`, scriptConfig);
  47. }
  48. // 更新元素扩展属性
  49. export function updateElementExtensions(element, extensionList) {
  50. const extensions = window.bpmnInstances.moddle.create("bpmn:ExtensionElements", {
  51. values: extensionList
  52. });
  53. window.bpmnInstances.modeling.updateProperties(element, {
  54. extensionElements: extensions
  55. });
  56. }
  57. // 创建一个id
  58. export function uuid(length = 8, chars) {
  59. let result = "";
  60. let charsString = chars || "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  61. for (let i = length; i > 0; --i) {
  62. result += charsString[Math.floor(Math.random() * charsString.length)];
  63. }
  64. return result;
  65. }