LaneUtil.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import { is } from '../../../util/ModelUtil';
  2. import {
  3. getParent
  4. } from './ModelingUtil';
  5. import {
  6. asTRBL
  7. } from 'diagram-js/lib/layout/LayoutUtil';
  8. import {
  9. substractTRBL,
  10. resizeTRBL
  11. } from 'diagram-js/lib/features/resize/ResizeUtil';
  12. /**
  13. * @typedef {import('../../../model/Types').Shape} Shape
  14. *
  15. * @typedef {import('diagram-js/lib/util/Types').Rect} Rect
  16. */
  17. var abs = Math.abs;
  18. function getTRBLResize(oldBounds, newBounds) {
  19. return substractTRBL(asTRBL(newBounds), asTRBL(oldBounds));
  20. }
  21. var LANE_PARENTS = [
  22. 'bpmn:Participant',
  23. 'bpmn:Process',
  24. 'bpmn:SubProcess'
  25. ];
  26. export var LANE_INDENTATION = 30;
  27. /**
  28. * Return all lanes that are children of the given shape.
  29. *
  30. * @param {Shape} shape
  31. * @param {Shape[]} [collectedShapes]
  32. *
  33. * @return {Shape[]}
  34. */
  35. export function collectLanes(shape, collectedShapes) {
  36. collectedShapes = collectedShapes || [];
  37. shape.children.filter(function(s) {
  38. if (is(s, 'bpmn:Lane')) {
  39. collectLanes(s, collectedShapes);
  40. collectedShapes.push(s);
  41. }
  42. });
  43. return collectedShapes;
  44. }
  45. /**
  46. * Return all lanes that are direct children of the given shape.
  47. *
  48. * @param {Shape} shape
  49. *
  50. * @return {Shape[]}
  51. */
  52. export function getChildLanes(shape) {
  53. return shape.children.filter(function(c) {
  54. return is(c, 'bpmn:Lane');
  55. });
  56. }
  57. /**
  58. * Return the parent shape of the given lane.
  59. *
  60. * @param {Shape} shape
  61. *
  62. * @return {Shape}
  63. */
  64. export function getLanesRoot(shape) {
  65. return getParent(shape, LANE_PARENTS) || shape;
  66. }
  67. /**
  68. * Compute the required resize operations for lanes
  69. * adjacent to the given shape, assuming it will be
  70. * resized to the given new bounds.
  71. *
  72. * @param {Shape} shape
  73. * @param {Rect} newBounds
  74. *
  75. * @return { {
  76. * shape: Shape;
  77. * newBounds: Rect;
  78. * }[] }
  79. */
  80. export function computeLanesResize(shape, newBounds) {
  81. var rootElement = getLanesRoot(shape);
  82. var initialShapes = is(rootElement, 'bpmn:Process') ? [] : [ rootElement ];
  83. var allLanes = collectLanes(rootElement, initialShapes),
  84. shapeTrbl = asTRBL(shape),
  85. shapeNewTrbl = asTRBL(newBounds),
  86. trblResize = getTRBLResize(shape, newBounds),
  87. resizeNeeded = [];
  88. allLanes.forEach(function(other) {
  89. if (other === shape) {
  90. return;
  91. }
  92. var topResize = 0,
  93. rightResize = trblResize.right,
  94. bottomResize = 0,
  95. leftResize = trblResize.left;
  96. var otherTrbl = asTRBL(other);
  97. if (trblResize.top) {
  98. if (abs(otherTrbl.bottom - shapeTrbl.top) < 10) {
  99. bottomResize = shapeNewTrbl.top - otherTrbl.bottom;
  100. }
  101. if (abs(otherTrbl.top - shapeTrbl.top) < 5) {
  102. topResize = shapeNewTrbl.top - otherTrbl.top;
  103. }
  104. }
  105. if (trblResize.bottom) {
  106. if (abs(otherTrbl.top - shapeTrbl.bottom) < 10) {
  107. topResize = shapeNewTrbl.bottom - otherTrbl.top;
  108. }
  109. if (abs(otherTrbl.bottom - shapeTrbl.bottom) < 5) {
  110. bottomResize = shapeNewTrbl.bottom - otherTrbl.bottom;
  111. }
  112. }
  113. if (topResize || rightResize || bottomResize || leftResize) {
  114. resizeNeeded.push({
  115. shape: other,
  116. newBounds: resizeTRBL(other, {
  117. top: topResize,
  118. right: rightResize,
  119. bottom: bottomResize,
  120. left: leftResize
  121. })
  122. });
  123. }
  124. });
  125. return resizeNeeded;
  126. }