1 | import {
|
2 | assign
|
3 | } from 'min-dash';
|
4 |
|
5 | import { is } from './ModelUtil';
|
6 |
|
7 | import { isLabel } from 'diagram-js/lib/util/ModelUtil';
|
8 |
|
9 | export { isLabel } from 'diagram-js/lib/util/ModelUtil';
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | export var DEFAULT_LABEL_SIZE = {
|
20 | width: 90,
|
21 | height: 20
|
22 | };
|
23 |
|
24 | export var FLOW_LABEL_INDENT = 15;
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | export function isLabelExternal(semantic) {
|
35 | return is(semantic, 'bpmn:Event') ||
|
36 | is(semantic, 'bpmn:Gateway') ||
|
37 | is(semantic, 'bpmn:DataStoreReference') ||
|
38 | is(semantic, 'bpmn:DataObjectReference') ||
|
39 | is(semantic, 'bpmn:DataInput') ||
|
40 | is(semantic, 'bpmn:DataOutput') ||
|
41 | is(semantic, 'bpmn:SequenceFlow') ||
|
42 | is(semantic, 'bpmn:MessageFlow') ||
|
43 | is(semantic, 'bpmn:Group');
|
44 | }
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 | export function hasExternalLabel(element) {
|
54 | return isLabel(element.label);
|
55 | }
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 | export function getFlowLabelPosition(waypoints) {
|
65 |
|
66 |
|
67 | var mid = waypoints.length / 2 - 1;
|
68 |
|
69 | var first = waypoints[Math.floor(mid)];
|
70 | var second = waypoints[Math.ceil(mid + 0.01)];
|
71 |
|
72 |
|
73 | var position = getWaypointsMid(waypoints);
|
74 |
|
75 |
|
76 | var angle = Math.atan((second.y - first.y) / (second.x - first.x));
|
77 |
|
78 | var x = position.x,
|
79 | y = position.y;
|
80 |
|
81 | if (Math.abs(angle) < Math.PI / 2) {
|
82 | y -= FLOW_LABEL_INDENT;
|
83 | } else {
|
84 | x += FLOW_LABEL_INDENT;
|
85 | }
|
86 |
|
87 | return { x: x, y: y };
|
88 | }
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 | export function getWaypointsMid(waypoints) {
|
99 |
|
100 | var mid = waypoints.length / 2 - 1;
|
101 |
|
102 | var first = waypoints[Math.floor(mid)];
|
103 | var second = waypoints[Math.ceil(mid + 0.01)];
|
104 |
|
105 | return {
|
106 | x: first.x + (second.x - first.x) / 2,
|
107 | y: first.y + (second.y - first.y) / 2
|
108 | };
|
109 | }
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 | export function getExternalLabelMid(element) {
|
119 |
|
120 | if (element.waypoints) {
|
121 | return getFlowLabelPosition(element.waypoints);
|
122 | } else if (is(element, 'bpmn:Group')) {
|
123 | return {
|
124 | x: element.x + element.width / 2,
|
125 | y: element.y + DEFAULT_LABEL_SIZE.height / 2
|
126 | };
|
127 | } else {
|
128 | return {
|
129 | x: element.x + element.width / 2,
|
130 | y: element.y + element.height + DEFAULT_LABEL_SIZE.height / 2
|
131 | };
|
132 | }
|
133 | }
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 | export function getExternalLabelBounds(di, element) {
|
146 |
|
147 | var mid,
|
148 | size,
|
149 | bounds,
|
150 | label = di.label;
|
151 |
|
152 | if (label && label.bounds) {
|
153 | bounds = label.bounds;
|
154 |
|
155 | size = {
|
156 | width: Math.max(DEFAULT_LABEL_SIZE.width, bounds.width),
|
157 | height: bounds.height
|
158 | };
|
159 |
|
160 | mid = {
|
161 | x: bounds.x + bounds.width / 2,
|
162 | y: bounds.y + bounds.height / 2
|
163 | };
|
164 | } else {
|
165 |
|
166 | mid = getExternalLabelMid(element);
|
167 |
|
168 | size = DEFAULT_LABEL_SIZE;
|
169 | }
|
170 |
|
171 | return assign({
|
172 | x: mid.x - size.width / 2,
|
173 | y: mid.y - size.height / 2
|
174 | }, size);
|
175 | }
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 | function getLabelAttr(semantic) {
|
183 | if (
|
184 | is(semantic, 'bpmn:FlowElement') ||
|
185 | is(semantic, 'bpmn:Participant') ||
|
186 | is(semantic, 'bpmn:Lane') ||
|
187 | is(semantic, 'bpmn:SequenceFlow') ||
|
188 | is(semantic, 'bpmn:MessageFlow') ||
|
189 | is(semantic, 'bpmn:DataInput') ||
|
190 | is(semantic, 'bpmn:DataOutput')
|
191 | ) {
|
192 | return 'name';
|
193 | }
|
194 |
|
195 | if (is(semantic, 'bpmn:TextAnnotation')) {
|
196 | return 'text';
|
197 | }
|
198 |
|
199 | if (is(semantic, 'bpmn:Group')) {
|
200 | return 'categoryValueRef';
|
201 | }
|
202 | }
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 | function getCategoryValue(semantic) {
|
210 | var categoryValueRef = semantic['categoryValueRef'];
|
211 |
|
212 | if (!categoryValueRef) {
|
213 | return '';
|
214 | }
|
215 |
|
216 |
|
217 | return categoryValueRef.value || '';
|
218 | }
|
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 | export function getLabel(element) {
|
226 | var semantic = element.businessObject,
|
227 | attr = getLabelAttr(semantic);
|
228 |
|
229 | if (attr) {
|
230 |
|
231 | if (attr === 'categoryValueRef') {
|
232 |
|
233 | return getCategoryValue(semantic);
|
234 | }
|
235 |
|
236 | return semantic[attr] || '';
|
237 | }
|
238 | }
|
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 |
|
247 | export function setLabel(element, text) {
|
248 | var semantic = element.businessObject,
|
249 | attr = getLabelAttr(semantic);
|
250 |
|
251 | if (attr) {
|
252 |
|
253 | if (attr === 'categoryValueRef') {
|
254 | semantic['categoryValueRef'].value = text;
|
255 | } else {
|
256 | semantic[attr] = text;
|
257 | }
|
258 |
|
259 | }
|
260 |
|
261 | return element;
|
262 | }
|