BpmnRenderUtil.js 5.7 KB

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