LabelUtil.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. }
  26. /**
  27. * Returns true if the given element has an external label
  28. *
  29. * @param {djs.model.shape} element
  30. * @return {Boolean} true if has label
  31. */
  32. export function hasExternalLabel(element) {
  33. return isLabel(element.label);
  34. }
  35. /**
  36. * Get the position for sequence flow labels
  37. *
  38. * @param {Array<Point>} waypoints
  39. * @return {Point} the label position
  40. */
  41. export function getFlowLabelPosition(waypoints) {
  42. // get the waypoints mid
  43. var mid = waypoints.length / 2 - 1;
  44. var first = waypoints[Math.floor(mid)];
  45. var second = waypoints[Math.ceil(mid + 0.01)];
  46. // get position
  47. var position = getWaypointsMid(waypoints);
  48. // calculate angle
  49. var angle = Math.atan((second.y - first.y) / (second.x - first.x));
  50. var x = position.x,
  51. y = position.y;
  52. if (Math.abs(angle) < Math.PI / 2) {
  53. y -= FLOW_LABEL_INDENT;
  54. } else {
  55. x += FLOW_LABEL_INDENT;
  56. }
  57. return { x: x, y: y };
  58. }
  59. /**
  60. * Get the middle of a number of waypoints
  61. *
  62. * @param {Array<Point>} waypoints
  63. * @return {Point} the mid point
  64. */
  65. export function getWaypointsMid(waypoints) {
  66. var mid = waypoints.length / 2 - 1;
  67. var first = waypoints[Math.floor(mid)];
  68. var second = waypoints[Math.ceil(mid + 0.01)];
  69. return {
  70. x: first.x + (second.x - first.x) / 2,
  71. y: first.y + (second.y - first.y) / 2
  72. };
  73. }
  74. export function getExternalLabelMid(element) {
  75. if (element.waypoints) {
  76. return getFlowLabelPosition(element.waypoints);
  77. } else {
  78. return {
  79. x: element.x + element.width / 2,
  80. y: element.y + element.height + DEFAULT_LABEL_SIZE.height / 2
  81. };
  82. }
  83. }
  84. /**
  85. * Returns the bounds of an elements label, parsed from the elements DI or
  86. * generated from its bounds.
  87. *
  88. * @param {BpmnElement} semantic
  89. * @param {djs.model.Base} element
  90. */
  91. export function getExternalLabelBounds(semantic, element) {
  92. var mid,
  93. size,
  94. bounds,
  95. di = semantic.di,
  96. label = di.label;
  97. if (label && label.bounds) {
  98. bounds = label.bounds;
  99. size = {
  100. width: Math.max(DEFAULT_LABEL_SIZE.width, bounds.width),
  101. height: bounds.height
  102. };
  103. mid = {
  104. x: bounds.x + bounds.width / 2,
  105. y: bounds.y + bounds.height / 2
  106. };
  107. } else {
  108. mid = getExternalLabelMid(element);
  109. size = DEFAULT_LABEL_SIZE;
  110. }
  111. return assign({
  112. x: mid.x - size.width / 2,
  113. y: mid.y - size.height / 2
  114. }, size);
  115. }
  116. export function isLabel(element) {
  117. return element && element.labelTarget;
  118. }