1 | import {
|
2 | assign
|
3 | } from 'min-dash';
|
4 |
|
5 | import { is } from './ModelUtil';
|
6 |
|
7 |
|
8 | export var DEFAULT_LABEL_SIZE = {
|
9 | width: 90,
|
10 | height: 20
|
11 | };
|
12 |
|
13 | export var FLOW_LABEL_INDENT = 15;
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | export function isLabelExternal(semantic) {
|
23 | return is(semantic, 'bpmn:Event') ||
|
24 | is(semantic, 'bpmn:Gateway') ||
|
25 | is(semantic, 'bpmn:DataStoreReference') ||
|
26 | is(semantic, 'bpmn:DataObjectReference') ||
|
27 | is(semantic, 'bpmn:DataInput') ||
|
28 | is(semantic, 'bpmn:DataOutput') ||
|
29 | is(semantic, 'bpmn:SequenceFlow') ||
|
30 | is(semantic, 'bpmn:MessageFlow') ||
|
31 | is(semantic, 'bpmn:Group');
|
32 | }
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 | export function hasExternalLabel(element) {
|
41 | return isLabel(element.label);
|
42 | }
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 | export function getFlowLabelPosition(waypoints) {
|
51 |
|
52 |
|
53 | var mid = waypoints.length / 2 - 1;
|
54 |
|
55 | var first = waypoints[Math.floor(mid)];
|
56 | var second = waypoints[Math.ceil(mid + 0.01)];
|
57 |
|
58 |
|
59 | var position = getWaypointsMid(waypoints);
|
60 |
|
61 |
|
62 | var angle = Math.atan((second.y - first.y) / (second.x - first.x));
|
63 |
|
64 | var x = position.x,
|
65 | y = position.y;
|
66 |
|
67 | if (Math.abs(angle) < Math.PI / 2) {
|
68 | y -= FLOW_LABEL_INDENT;
|
69 | } else {
|
70 | x += FLOW_LABEL_INDENT;
|
71 | }
|
72 |
|
73 | return { x: x, y: y };
|
74 | }
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 | export function getWaypointsMid(waypoints) {
|
84 |
|
85 | var mid = waypoints.length / 2 - 1;
|
86 |
|
87 | var first = waypoints[Math.floor(mid)];
|
88 | var second = waypoints[Math.ceil(mid + 0.01)];
|
89 |
|
90 | return {
|
91 | x: first.x + (second.x - first.x) / 2,
|
92 | y: first.y + (second.y - first.y) / 2
|
93 | };
|
94 | }
|
95 |
|
96 |
|
97 | export function getExternalLabelMid(element) {
|
98 |
|
99 | if (element.waypoints) {
|
100 | return getFlowLabelPosition(element.waypoints);
|
101 | } else if (is(element, 'bpmn:Group')) {
|
102 | return {
|
103 | x: element.x + element.width / 2,
|
104 | y: element.y + DEFAULT_LABEL_SIZE.height / 2
|
105 | };
|
106 | } else {
|
107 | return {
|
108 | x: element.x + element.width / 2,
|
109 | y: element.y + element.height + DEFAULT_LABEL_SIZE.height / 2
|
110 | };
|
111 | }
|
112 | }
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 | export function getExternalLabelBounds(semantic, element) {
|
123 |
|
124 | var mid,
|
125 | size,
|
126 | bounds,
|
127 | di = semantic.di,
|
128 | label = di.label;
|
129 |
|
130 | if (label && label.bounds) {
|
131 | bounds = label.bounds;
|
132 |
|
133 | size = {
|
134 | width: Math.max(DEFAULT_LABEL_SIZE.width, bounds.width),
|
135 | height: bounds.height
|
136 | };
|
137 |
|
138 | mid = {
|
139 | x: bounds.x + bounds.width / 2,
|
140 | y: bounds.y + bounds.height / 2
|
141 | };
|
142 | } else {
|
143 |
|
144 | mid = getExternalLabelMid(element);
|
145 |
|
146 | size = DEFAULT_LABEL_SIZE;
|
147 | }
|
148 |
|
149 | return assign({
|
150 | x: mid.x - size.width / 2,
|
151 | y: mid.y - size.height / 2
|
152 | }, size);
|
153 | }
|
154 |
|
155 | export function isLabel(element) {
|
156 | return element && !!element.labelTarget;
|
157 | }
|