UNPKG

3.36 kBJavaScriptView Raw
1import {
2 assign
3} from 'min-dash';
4
5import { is } from './ModelUtil';
6
7
8export var DEFAULT_LABEL_SIZE = {
9 width: 90,
10 height: 20
11};
12
13export var FLOW_LABEL_INDENT = 15;
14
15
16/**
17 * Returns true if the given semantic has an external label
18 *
19 * @param {BpmnElement} semantic
20 * @return {Boolean} true if has label
21 */
22export 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 * Returns true if the given element has an external label
36 *
37 * @param {djs.model.shape} element
38 * @return {Boolean} true if has label
39 */
40export function hasExternalLabel(element) {
41 return isLabel(element.label);
42}
43
44/**
45 * Get the position for sequence flow labels
46 *
47 * @param {Array<Point>} waypoints
48 * @return {Point} the label position
49 */
50export function getFlowLabelPosition(waypoints) {
51
52 // get the waypoints mid
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 // get position
59 var position = getWaypointsMid(waypoints);
60
61 // calculate angle
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 * Get the middle of a number of waypoints
79 *
80 * @param {Array<Point>} waypoints
81 * @return {Point} the mid point
82 */
83export 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
97export 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 * Returns the bounds of an elements label, parsed from the elements DI or
117 * generated from its bounds.
118 *
119 * @param {BpmnElement} semantic
120 * @param {djs.model.Base} element
121 */
122export 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
155export function isLabel(element) {
156 return element && !!element.labelTarget;
157}