zhangwencui
2 天以前 a2002ba0e8aa2a0e4eee61b5205748d8f6e454fc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
const bpmnInstances = () => (window as any)?.bpmnInstances;
 
interface ListenerFieldOptions {
  expression?: string;
  fieldType: string;
  name: string;
  string?: string;
}
 
interface ListenerOptions {
  class?: string;
  delegateExpression?: string;
  event?: string;
  eventDefinitionType?: string;
  eventTimeDefinitions?: string;
  expression?: string;
  fields?: ListenerFieldOptions[];
  id?: string;
  listenerType?: string;
  resource?: string;
  scriptFormat?: string;
  scriptType?: string;
  value?: string;
}
 
// 创建监听器实例
export function createListenerObject(
  options: ListenerOptions,
  isTask: boolean,
  prefix: string,
) {
  const listenerObj: Record<string, any> = Object.create(null);
  listenerObj.event = options.event;
  isTask && (listenerObj.id = options.id); // 任务监听器特有的 id 字段
  switch (options.listenerType) {
    case 'delegateExpressionListener': {
      listenerObj.delegateExpression = options.delegateExpression;
      break;
    }
    case 'expressionListener': {
      listenerObj.expression = options.expression;
      break;
    }
    case 'scriptListener': {
      listenerObj.script = createScriptObject(options, prefix);
      break;
    }
    default: {
      listenerObj.class = options.class;
    }
  }
  // 注入字段
  if (options.fields) {
    listenerObj.fields = options.fields.map((field) => {
      return createFieldObject(field, prefix);
    });
  }
  // 任务监听器的 定时器 设置
  if (isTask && options.event === 'timeout' && !!options.eventDefinitionType) {
    const timeDefinition = bpmnInstances().moddle.create(
      'bpmn:FormalExpression',
      {
        body: options.eventTimeDefinitions,
      },
    );
    const TimerEventDefinition = bpmnInstances().moddle.create(
      'bpmn:TimerEventDefinition',
      {
        id: `TimerEventDefinition_${uuid(8)}`,
        [`time${options.eventDefinitionType.replace(/^\S/, (s) => s.toUpperCase())}`]:
          timeDefinition,
      },
    );
    listenerObj.eventDefinitions = [TimerEventDefinition];
  }
  return bpmnInstances().moddle.create(
    `${prefix}:${isTask ? 'TaskListener' : 'ExecutionListener'}`,
    listenerObj,
  );
}
 
// 创建 监听器的注入字段 实例
export function createFieldObject(
  option: ListenerFieldOptions,
  prefix: string,
) {
  const { name, fieldType, string, expression } = option;
  const fieldConfig =
    fieldType === 'string' ? { name, string } : { name, expression };
  return bpmnInstances().moddle.create(`${prefix}:Field`, fieldConfig);
}
 
// 创建脚本实例
export function createScriptObject(options: ListenerOptions, prefix: string) {
  const { scriptType, scriptFormat, value, resource } = options;
  const scriptConfig =
    scriptType === 'inlineScript'
      ? { scriptFormat, value }
      : { scriptFormat, resource };
  return bpmnInstances().moddle.create(`${prefix}:Script`, scriptConfig);
}
 
// 更新元素扩展属性
export function updateElementExtensions(element: any, extensionList: any[]) {
  const extensions = bpmnInstances().moddle.create('bpmn:ExtensionElements', {
    values: extensionList,
  });
  // 直接使用原始元素对象,不需要toRaw包装
  bpmnInstances().modeling.updateProperties(element, {
    extensionElements: extensions,
  });
}
 
// 创建一个id
export function uuid(
  length = 8,
  charsString = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
) {
  let result = '';
 
  for (let i = length; i > 0; --i) {
    result += charsString[Math.floor(Math.random() * charsString.length)];
  }
  return result;
}