LabelUtil.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import {
  2. assign
  3. } from 'min-dash';
  4. import { is } from './ModelUtil';
  5. export var DEFAULT_LABEL_SIZE = {
  6. width: 90,
  7. height: 20
  8. };
  9. export var FLOW_LABEL_INDENT = 15;
  10. /**
  11. * Returns true if the given semantic has an external label
  12. *
  13. * @param {BpmnElement} semantic
  14. * @return {boolean} true if has label
  15. */
  16. export function isLabelExternal(semantic) {
  17. return is(semantic, 'bpmn:Event') ||
  18. is(semantic, 'bpmn:Gateway') ||
  19. is(semantic, 'bpmn:DataStoreReference') ||
  20. is(semantic, 'bpmn:DataObjectReference') ||
  21. is(semantic, 'bpmn:DataInput') ||
  22. is(semantic, 'bpmn:DataOutput') ||
  23. is(semantic, 'bpmn:SequenceFlow') ||
  24. is(semantic, 'bpmn:MessageFlow') ||
  25. is(semantic, 'bpmn:Group');
  26. }
  27. /**
  28. * Returns true if the given element has an external label
  29. *
  30. * @param {djs.model.shape} element
  31. * @return {boolean} true if has label
  32. */
  33. export function hasExternalLabel(element) {
  34. return isLabel(element.label);
  35. }
  36. /**
  37. * Get the position for sequence flow labels
  38. *
  39. * @param {Array<Point>} waypoints
  40. * @return {Point} the label position
  41. */
  42. export function getFlowLabelPosition(waypoints) {
  43. // get the waypoints mid
  44. var mid = waypoints.length / 2 - 1;
  45. var first = waypoints[Math.floor(mid)];
  46. var second = waypoints[Math.ceil(mid + 0.01)];
  47. // get position
  48. var position = getWaypointsMid(waypoints);
  49. // calculate angle
  50. var angle = Math.atan((second.y - first.y) / (second.x - first.x));
  51. var x = position.x,
  52. y = position.y;
  53. if (Math.abs(angle) < Math.PI / 2) {
  54. y -= FLOW_LABEL_INDENT;
  55. } else {
  56. x += FLOW_LABEL_INDENT;
  57. }
  58. return { x: x, y: y };
  59. }
  60. /**
  61. * Get the middle of a number of waypoints
  62. *
  63. * @param {Array<Point>} waypoints
  64. * @return {Point} the mid point
  65. */
  66. export function getWaypointsMid(waypoints) {
  67. var mid = waypoints.length / 2 - 1;
  68. var first = waypoints[Math.floor(mid)];
  69. var second = waypoints[Math.ceil(mid + 0.01)];
  70. return {
  71. x: first.x + (second.x - first.x) / 2,
  72. y: first.y + (second.y - first.y) / 2
  73. };
  74. }
  75. export function getExternalLabelMid(element) {
  76. if (element.waypoints) {
  77. return getFlowLabelPosition(element.waypoints);
  78. } else if (is(element, 'bpmn:Group')) {
  79. return {
  80. x: element.x + element.width / 2,
  81. y: element.y + DEFAULT_LABEL_SIZE.height / 2
  82. };
  83. } else {
  84. return {
  85. x: element.x + element.width / 2,
  86. y: element.y + element.height + DEFAULT_LABEL_SIZE.height / 2
  87. };
  88. }
  89. }
  90. /**
  91. * Returns the bounds of an elements label, parsed from the elements DI or
  92. * generated from its bounds.
  93. *
  94. * @param {BpmnElement} semantic
  95. * @param {djs.model.Base} element
  96. */
  97. export function getExternalLabelBounds(semantic, element) {
  98. var mid,
  99. size,
  100. bounds,
  101. di = semantic.di,
  102. label = di.label;
  103. if (label && label.bounds) {
  104. bounds = label.bounds;
  105. size = {
  106. width: Math.max(DEFAULT_LABEL_SIZE.width, bounds.width),
  107. height: bounds.height
  108. };
  109. mid = {
  110. x: bounds.x + bounds.width / 2,
  111. y: bounds.y + bounds.height / 2
  112. };
  113. } else {
  114. mid = getExternalLabelMid(element);
  115. size = DEFAULT_LABEL_SIZE;
  116. }
  117. return assign({
  118. x: mid.x - size.width / 2,
  119. y: mid.y - size.height / 2
  120. }, size);
  121. }
  122. export function isLabel(element) {
  123. return element && !!element.labelTarget;
  124. }