UNPKG

5.8 kBJavaScriptView Raw
1import {
2 has,
3 some
4} from 'min-dash';
5
6import {
7 getDi
8} from '../util/ModelUtil';
9
10import {
11 componentsToPath
12} from 'diagram-js/lib/util/RenderUtil';
13
14
15/**
16 * @typedef {import('../model').ModdleElement} ModdleElement
17 * @typedef {import('../model').Element} Element
18 *
19 * @typedef {import('../model').ShapeLike} ShapeLike
20 *
21 * @typedef {import('diagram-js/lib/util/Types').Dimensions} Dimensions
22 * @typedef {import('diagram-js/lib/util/Types').Rect} Rect
23 */
24
25// re-export for compatibility
26export {
27 getDi,
28 getBusinessObject as getSemantic
29} from '../util/ModelUtil';
30
31
32export var black = 'hsl(225, 10%, 15%)';
33export var white = 'white';
34
35// element utils //////////////////////
36
37/**
38 * Checks if eventDefinition of the given element matches with semantic type.
39 *
40 * @param {ModdleElement} event
41 * @param {string} eventDefinitionType
42 *
43 * @return {boolean}
44 */
45export function isTypedEvent(event, eventDefinitionType) {
46 return some(event.eventDefinitions, function(definition) {
47 return definition.$type === eventDefinitionType;
48 });
49}
50
51/**
52 * Check if element is a throw event.
53 *
54 * @param {ModdleElement} event
55 *
56 * @return {boolean}
57 */
58export function isThrowEvent(event) {
59 return (event.$type === 'bpmn:IntermediateThrowEvent') || (event.$type === 'bpmn:EndEvent');
60}
61
62/**
63 * Check if element is a throw event.
64 *
65 * @param {ModdleElement} element
66 *
67 * @return {boolean}
68 */
69export function isCollection(element) {
70 var dataObject = element.dataObjectRef;
71
72 return element.isCollection || (dataObject && dataObject.isCollection);
73}
74
75
76// color access //////////////////////
77
78/**
79 * @param {Element} element
80 * @param {string} [defaultColor]
81 * @param {string} [overrideColor]
82 *
83 * @return {string}
84 */
85export function getFillColor(element, defaultColor, overrideColor) {
86 var di = getDi(element);
87
88 return overrideColor || di.get('color:background-color') || di.get('bioc:fill') || defaultColor || white;
89}
90
91/**
92 * @param {Element} element
93 * @param {string} [defaultColor]
94 * @param {string} [overrideColor]
95 *
96 * @return {string}
97 */
98export function getStrokeColor(element, defaultColor, overrideColor) {
99 var di = getDi(element);
100
101 return overrideColor || di.get('color:border-color') || di.get('bioc:stroke') || defaultColor || black;
102}
103
104/**
105 * @param {Element} element
106 * @param {string} [defaultColor]
107 * @param {string} [defaultStrokeColor]
108 * @param {string} [overrideColor]
109 *
110 * @return {string}
111 */
112export function getLabelColor(element, defaultColor, defaultStrokeColor, overrideColor) {
113 var di = getDi(element),
114 label = di.get('label');
115
116 return overrideColor || (label && label.get('color:color')) || defaultColor ||
117 getStrokeColor(element, defaultStrokeColor);
118}
119
120// cropping path customizations //////////////////////
121
122/**
123 * @param {ShapeLike} shape
124 *
125 * @return {string} path
126 */
127export function getCirclePath(shape) {
128
129 var cx = shape.x + shape.width / 2,
130 cy = shape.y + shape.height / 2,
131 radius = shape.width / 2;
132
133 var circlePath = [
134 [ 'M', cx, cy ],
135 [ 'm', 0, -radius ],
136 [ 'a', radius, radius, 0, 1, 1, 0, 2 * radius ],
137 [ 'a', radius, radius, 0, 1, 1, 0, -2 * radius ],
138 [ 'z' ]
139 ];
140
141 return componentsToPath(circlePath);
142}
143
144/**
145 * @param {ShapeLike} shape
146 * @param {number} [borderRadius]
147 *
148 * @return {string} path
149 */
150export function getRoundRectPath(shape, borderRadius) {
151
152 var x = shape.x,
153 y = shape.y,
154 width = shape.width,
155 height = shape.height;
156
157 var roundRectPath = [
158 [ 'M', x + borderRadius, y ],
159 [ 'l', width - borderRadius * 2, 0 ],
160 [ 'a', borderRadius, borderRadius, 0, 0, 1, borderRadius, borderRadius ],
161 [ 'l', 0, height - borderRadius * 2 ],
162 [ 'a', borderRadius, borderRadius, 0, 0, 1, -borderRadius, borderRadius ],
163 [ 'l', borderRadius * 2 - width, 0 ],
164 [ 'a', borderRadius, borderRadius, 0, 0, 1, -borderRadius, -borderRadius ],
165 [ 'l', 0, borderRadius * 2 - height ],
166 [ 'a', borderRadius, borderRadius, 0, 0, 1, borderRadius, -borderRadius ],
167 [ 'z' ]
168 ];
169
170 return componentsToPath(roundRectPath);
171}
172
173/**
174 * @param {ShapeLike} shape
175 *
176 * @return {string} path
177 */
178export function getDiamondPath(shape) {
179
180 var width = shape.width,
181 height = shape.height,
182 x = shape.x,
183 y = shape.y,
184 halfWidth = width / 2,
185 halfHeight = height / 2;
186
187 var diamondPath = [
188 [ 'M', x + halfWidth, y ],
189 [ 'l', halfWidth, halfHeight ],
190 [ 'l', -halfWidth, halfHeight ],
191 [ 'l', -halfWidth, -halfHeight ],
192 [ 'z' ]
193 ];
194
195 return componentsToPath(diamondPath);
196}
197
198/**
199 * @param {ShapeLike} shape
200 *
201 * @return {string} path
202 */
203export function getRectPath(shape) {
204 var x = shape.x,
205 y = shape.y,
206 width = shape.width,
207 height = shape.height;
208
209 var rectPath = [
210 [ 'M', x, y ],
211 [ 'l', width, 0 ],
212 [ 'l', 0, height ],
213 [ 'l', -width, 0 ],
214 [ 'z' ]
215 ];
216
217 return componentsToPath(rectPath);
218}
219
220/**
221 * Get width and height from element or overrides.
222 *
223 * @param {Dimensions|Rect|ShapeLike} bounds
224 * @param {Object} overrides
225 *
226 * @returns {Dimensions}
227 */
228export function getBounds(bounds, overrides = {}) {
229 return {
230 width: getWidth(bounds, overrides),
231 height: getHeight(bounds, overrides)
232 };
233}
234
235/**
236 * Get width from element or overrides.
237 *
238 * @param {Dimensions|Rect|ShapeLike} bounds
239 * @param {Object} overrides
240 *
241 * @returns {number}
242 */
243export function getWidth(bounds, overrides = {}) {
244 return has(overrides, 'width') ? overrides.width : bounds.width;
245}
246
247/**
248 * Get height from element or overrides.
249 *
250 * @param {Dimensions|Rect|ShapeLike} bounds
251 * @param {Object} overrides
252 *
253 * @returns {number}
254 */
255export function getHeight(bounds, overrides = {}) {
256 return has(overrides, 'height') ? overrides.height : bounds.height;
257}
\No newline at end of file