gaoluyang
2026-06-29 27cd042df9aca0383a49f3514bc21958dd890912
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<script lang="ts" setup>
import { nextTick, onBeforeUnmount, ref, toRaw, watch } from 'vue';
 
import { Form, FormItem, Input, Select, Textarea } from 'ant-design-vue';
 
defineOptions({ name: 'FlowCondition' });
 
const props = defineProps({
  businessObject: {
    type: Object,
    default: () => ({}),
  },
  type: {
    type: String,
    default: '',
  },
});
 
const flowConditionForm = ref<any>({});
const bpmnElement = ref();
const bpmnElementSource = ref();
const bpmnElementSourceRef = ref();
const flowConditionRef = ref();
const bpmnInstances = () => (window as any)?.bpmnInstances;
 
const resetFlowCondition = () => {
  bpmnElement.value = bpmnInstances().bpmnElement;
  bpmnElementSource.value = bpmnElement.value.source;
  bpmnElementSourceRef.value = bpmnElement.value.businessObject.sourceRef;
  // 初始化默认type为default
  flowConditionForm.value = { type: 'default' };
  if (
    bpmnElementSourceRef.value &&
    bpmnElementSourceRef.value.default &&
    bpmnElementSourceRef.value.default.id === bpmnElement.value.id
  ) {
    flowConditionForm.value = { type: 'default' };
  } else if (bpmnElement.value.businessObject.conditionExpression) {
    // 带条件
    const conditionExpression =
      bpmnElement.value.businessObject.conditionExpression;
    flowConditionForm.value = { ...conditionExpression, type: 'condition' };
    // resource 可直接标识 是否是外部资源脚本
    if (flowConditionForm.value.resource) {
      // this.$set(this.flowConditionForm, "conditionType", "script");
      // this.$set(this.flowConditionForm, "scriptType", "externalScript");
      flowConditionForm.value.conditionType = 'script';
      flowConditionForm.value.scriptType = 'externalScript';
      return;
    }
    if (conditionExpression.language) {
      // this.$set(this.flowConditionForm, "conditionType", "script");
      // this.$set(this.flowConditionForm, "scriptType", "inlineScript");
      flowConditionForm.value.conditionType = 'script';
      flowConditionForm.value.scriptType = 'inlineScript';
 
      return;
    }
    // this.$set(this.flowConditionForm, "conditionType", "expression");
    flowConditionForm.value.conditionType = 'expression';
  } else {
    // 普通
    flowConditionForm.value = { type: 'normal' };
  }
};
 
const updateFlowType = (flowType: any) => {
  // 正常条件类
  if (flowType === 'condition') {
    flowConditionRef.value = bpmnInstances().moddle.create(
      'bpmn:FormalExpression',
    );
    bpmnInstances().modeling.updateProperties(toRaw(bpmnElement.value), {
      conditionExpression: flowConditionRef.value,
    });
    return;
  }
  // 默认路径
  if (flowType === 'default') {
    bpmnInstances().modeling.updateProperties(toRaw(bpmnElement.value), {
      conditionExpression: null,
    });
    bpmnInstances().modeling.updateProperties(toRaw(bpmnElementSource.value), {
      default: toRaw(bpmnElement.value),
    });
    return;
  }
  // 正常路径,如果来源节点的默认路径是当前连线时,清除父元素的默认路径配置
  if (
    bpmnElementSourceRef.value.default &&
    bpmnElementSourceRef.value.default.id === bpmnElement.value.id
  ) {
    bpmnInstances().modeling.updateProperties(toRaw(bpmnElementSource.value), {
      default: null,
    });
  }
  bpmnInstances().modeling.updateProperties(toRaw(bpmnElement.value), {
    conditionExpression: null,
  });
};
 
const updateFlowCondition = () => {
  const { conditionType, scriptType, body, resource, language } =
    flowConditionForm.value;
  let condition;
  if (conditionType === 'expression') {
    condition = bpmnInstances().moddle.create('bpmn:FormalExpression', {
      body,
    });
  } else {
    if (scriptType === 'inlineScript') {
      condition = bpmnInstances().moddle.create('bpmn:FormalExpression', {
        body,
        language,
      });
      // this.$set(this.flowConditionForm, "resource", "");
      flowConditionForm.value.resource = '';
    } else {
      // this.$set(this.flowConditionForm, "body", "");
      flowConditionForm.value.body = '';
      condition = bpmnInstances().moddle.create('bpmn:FormalExpression', {
        resource,
        language,
      });
    }
  }
  bpmnInstances().modeling.updateProperties(toRaw(bpmnElement.value), {
    conditionExpression: condition,
  });
};
 
onBeforeUnmount(() => {
  bpmnElement.value = null;
  bpmnElementSource.value = null;
  bpmnElementSourceRef.value = null;
});
 
watch(
  () => props.businessObject,
  (_) => {
    // console.log(val, 'val');
    nextTick(() => {
      resetFlowCondition();
    });
  },
  {
    immediate: true,
  },
);
</script>
 
<template>
  <div class="panel-tab__content">
    <Form
      :model="flowConditionForm"
      :label-col="{ span: 6 }"
      :wrapper-col="{ span: 18 }"
    >
      <FormItem label="流转类型">
        <Select v-model:value="flowConditionForm.type" @change="updateFlowType">
          <Select.Option value="normal">普通流转路径</Select.Option>
          <Select.Option value="default">默认流转路径</Select.Option>
          <Select.Option value="condition">条件流转路径</Select.Option>
        </Select>
      </FormItem>
      <FormItem
        label="条件格式"
        v-if="flowConditionForm.type === 'condition'"
        key="condition"
      >
        <Select v-model:value="flowConditionForm.conditionType">
          <Select.Option value="expression">表达式</Select.Option>
          <Select.Option value="script">脚本</Select.Option>
        </Select>
      </FormItem>
      <FormItem
        label="表达式"
        v-if="
          flowConditionForm.conditionType &&
          flowConditionForm.conditionType === 'expression'
        "
        key="express"
      >
        <Textarea
          v-model:value="flowConditionForm.body"
          :auto-size="{ minRows: 2, maxRows: 6 }"
          allow-clear
          @change="updateFlowCondition"
        />
      </FormItem>
      <template
        v-if="
          flowConditionForm.conditionType &&
          flowConditionForm.conditionType === 'script'
        "
      >
        <FormItem label="脚本语言" key="language">
          <Input
            v-model:value="flowConditionForm.language"
            allow-clear
            @change="updateFlowCondition"
          />
        </FormItem>
        <FormItem label="脚本类型" key="scriptType">
          <Select v-model:value="flowConditionForm.scriptType">
            <Select.Option value="inlineScript">内联脚本</Select.Option>
            <Select.Option value="externalScript">外部脚本</Select.Option>
          </Select>
        </FormItem>
        <FormItem
          label="脚本"
          v-if="flowConditionForm.scriptType === 'inlineScript'"
          key="body"
        >
          <Textarea
            v-model:value="flowConditionForm.body"
            :auto-size="{ minRows: 2, maxRows: 6 }"
            allow-clear
            @change="updateFlowCondition"
          />
        </FormItem>
        <FormItem
          label="资源地址"
          v-if="flowConditionForm.scriptType === 'externalScript'"
          key="resource"
        >
          <Input
            v-model:value="flowConditionForm.resource"
            allow-clear
            @change="updateFlowCondition"
          />
        </FormItem>
      </template>
    </Form>
  </div>
</template>