ProcessViewer.vue
6.18 KB
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
<template>
<div class="my-process-designer">
<div class="my-process-designer__container">
<div class="my-process-designer__canvas" ref="canvas"></div>
</div>
</div>
</template>
<script>
import BpmnViewer from "bpmn-js/lib/Viewer";
import DefaultEmptyXML from "./plugins/defaultEmpty";
export default {
name: "MyProcessViewer",
componentName: "MyProcessViewer",
props: {
value: String, // xml 字符串
prefix: {
type: String,
default: "camunda"
},
taskData: {
type: Array,
default: () => []
}
},
data() {
return {
xml: '',
tasks: [],
};
},
mounted() {
this.xml = this.value;
this.tasks = this.taskData;
// 初始化
this.initBpmnModeler();
this.createNewDiagram(this.xml);
this.$once("hook:beforeDestroy", () => {
if (this.bpmnModeler) this.bpmnModeler.destroy();
this.$emit("destroy", this.bpmnModeler);
this.bpmnModeler = null;
});
},
watch: {
value: function (newValue) { // 在 xmlString 发生变化时,重新创建,从而绘制流程图
this.xml = newValue;
this.createNewDiagram(this.xml);
},
taskData: function (newTaskData) {
this.tasks = newTaskData;
this.createNewDiagram(this.xml);
}
},
methods: {
initBpmnModeler() {
if (this.bpmnModeler) return;
this.bpmnModeler = new BpmnViewer({
container: this.$refs["canvas"],
bpmnRenderer: {
}
})
},
/* 创建新的流程图 */
async createNewDiagram(xml) {
// 将字符串转换成图显示出来
let newId = `Process_${new Date().getTime()}`;
let newName = `业务流程_${new Date().getTime()}`;
let xmlString = xml || DefaultEmptyXML(newId, newName, this.prefix);
try {
// console.log(this.bpmnModeler.importXML);
let { warnings } = await this.bpmnModeler.importXML(xmlString);
if (warnings && warnings.length) {
warnings.forEach(warn => console.warn(warn));
}
// 高亮流程图
await this.highlightDiagram();
} catch (e) {
console.error(e);
// console.error(`[Process Designer Warn]: ${e?.message || e}`);
}
},
/* 高亮流程图 */
async highlightDiagram() {
if (this.tasks.length === 0) {
return;
}
if (!this.bpmnModeler.getDefinitions().rootElements[0].flowElements) {
return;
}
let canvas = this.bpmnModeler.get('canvas');
this.bpmnModeler.getDefinitions().rootElements[0].flowElements.forEach(n => {
if (n.$type === 'bpmn:UserTask') {
let completeTask = this.tasks.find(m => m.definitionKey === n.id)
let todoTask = this.tasks.find(m => !m.endTime)
let endTask = this.tasks[this.tasks.length - 1]
if (completeTask) {
canvas.addMarker(n.id, completeTask.endTime ? 'highlight' : 'highlight-todo');
console.log(n.id + ' : ' + (completeTask.endTime ? 'highlight' : 'highlight-todo'));
// n.outgoing.forEach(nn => {
// let targetTask = this.tasks.find(m => m.definitionKey === nn.targetRef.id)
// if (targetTask) {
// canvas.addMarker(nn.id, targetTask.endTime ? 'highlight' : 'highlight-todo');
// } else if (nn.targetRef.$type === 'bpmn:ExclusiveGateway') {
// // canvas.addMarker(nn.id, 'highlight');
// canvas.addMarker(nn.id, completeTask.endTime ? 'highlight' : 'highlight-todo');
// canvas.addMarker(nn.targetRef.id, completeTask.endTime ? 'highlight' : 'highlight-todo');
// } else if (nn.targetRef.$type === 'bpmn:EndEvent') {
// if (!todoTask && endTask.definitionKey === n.id) {
// canvas.addMarker(nn.id, 'highlight');
// canvas.addMarker(nn.targetRef.id, 'highlight');
// }
// if (!completeTask.endTime) {
// canvas.addMarker(nn.id, 'highlight-todo');
// canvas.addMarker(nn.targetRef.id, 'highlight-todo');
// }
// }
// });
}
} else if (n.$type === 'bpmn:ExclusiveGateway') {
// n.outgoing.forEach(nn => {
// let targetTask = this.tasks.find(m => m.definitionKey === nn.targetRef.id)
// if (targetTask) {
// canvas.addMarker(nn.id, targetTask.endTime ? 'highlight' : 'highlight-todo');
// }
// })
}
if (n.$type === 'bpmn:StartEvent') {
canvas.addMarker(n.id, 'highlight-todo');
// n.outgoing.forEach(nn => {
// let completeTask = this.tasks.find(m => m.definitionKey === nn.targetRef.id)
// if (completeTask) {
// canvas.addMarker(nn.id, 'highlight');
// canvas.addMarker(n.id, 'highlight');
// return
// }
// });
}
})
}
}
};
</script>
<style>
.containers {
position: absolute;
background-color: #ffffff;
top:0;
left: 0;
width: 100%;
height: 100%;
}
.canvas {
width: 100%;
height: 100%;
}
/*/deep/.highlight-todo {*/
/* background-color: red;*/
/* fill: green !important;*/
/* stroke: green !important;*/
/*}*/
/deep/.highlight.djs-shape .djs-visual > :nth-child(1) {
fill: green !important;
stroke: green !important;
fill-opacity: 0.2 !important;
}
/deep/.highlight.djs-shape .djs-visual > :nth-child(2) {
fill: green !important;
}
/deep/.highlight.djs-shape .djs-visual > path {
fill: green !important;
fill-opacity: 0.2 !important;
stroke: green !important;
}
/deep/.highlight.djs-connection > .djs-visual > path {
stroke: green !important;
}
/deep/.highlight-todo.djs-connection > .djs-visual > path {
stroke: orange !important;
stroke-dasharray: 4px !important;
fill-opacity: 0.2 !important;
marker-end: url(#sequenceflow-end-_E7DFDF-_E7DFDF-803g1kf6zwzmcig1y2ulm5egr);
}
/deep/.highlight-todo.djs-shape .djs-visual > :nth-child(1) {
fill: orange !important;
stroke: orange !important;
stroke-dasharray: 4px !important;
fill-opacity: 0.2 !important;
}
/deep/.overlays-div {
font-size: 10px;
color: red;
width: 100px;
top: -20px !important;
}
</style>