LaneUtil.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. var abs = Math.abs;
  13. function getTRBLResize(oldBounds, newBounds) {
  14. return substractTRBL(asTRBL(newBounds), asTRBL(oldBounds));
  15. }
  16. var LANE_PARENTS = [
  17. 'bpmn:Participant',
  18. 'bpmn:Process',
  19. 'bpmn:SubProcess'
  20. ];
  21. export var LANE_INDENTATION = 30;
  22. /**
  23. * Collect all lane shapes in the given paren
  24. *
  25. * @param {djs.model.Shape} shape
  26. * @param {Array<djs.model.Base>} [collectedShapes]
  27. *
  28. * @return {Array<djs.model.Base>}
  29. */
  30. export function collectLanes(shape, collectedShapes) {
  31. collectedShapes = collectedShapes || [];
  32. shape.children.filter(function(s) {
  33. if (is(s, 'bpmn:Lane')) {
  34. collectLanes(s, collectedShapes);
  35. collectedShapes.push(s);
  36. }
  37. });
  38. return collectedShapes;
  39. }
  40. /**
  41. * Return the lane children of the given element.
  42. *
  43. * @param {djs.model.Shape} shape
  44. *
  45. * @return {Array<djs.model.Shape>}
  46. */
  47. export function getChildLanes(shape) {
  48. return shape.children.filter(function(c) {
  49. return is(c, 'bpmn:Lane');
  50. });
  51. }
  52. /**
  53. * Return the root element containing the given lane shape
  54. *
  55. * @param {djs.model.Shape} shape
  56. *
  57. * @return {djs.model.Shape}
  58. */
  59. export function getLanesRoot(shape) {
  60. return getParent(shape, LANE_PARENTS) || shape;
  61. }
  62. /**
  63. * Compute the required resize operations for lanes
  64. * adjacent to the given shape, assuming it will be
  65. * resized to the given new bounds.
  66. *
  67. * @param {djs.model.Shape} shape
  68. * @param {Bounds} newBounds
  69. *
  70. * @return {Array<Object>}
  71. */
  72. export function computeLanesResize(shape, newBounds) {
  73. var rootElement = getLanesRoot(shape);
  74. var initialShapes = is(rootElement, 'bpmn:Process') ? [] : [ rootElement ];
  75. var allLanes = collectLanes(rootElement, initialShapes),
  76. shapeTrbl = asTRBL(shape),
  77. shapeNewTrbl = asTRBL(newBounds),
  78. trblResize = getTRBLResize(shape, newBounds),
  79. resizeNeeded = [];
  80. allLanes.forEach(function(other) {
  81. if (other === shape) {
  82. return;
  83. }
  84. var topResize = 0,
  85. rightResize = trblResize.right,
  86. bottomResize = 0,
  87. leftResize = trblResize.left;
  88. var otherTrbl = asTRBL(other);
  89. if (trblResize.top) {
  90. if (abs(otherTrbl.bottom - shapeTrbl.top) < 10) {
  91. bottomResize = shapeNewTrbl.top - otherTrbl.bottom;
  92. }
  93. if (abs(otherTrbl.top - shapeTrbl.top) < 5) {
  94. topResize = shapeNewTrbl.top - otherTrbl.top;
  95. }
  96. }
  97. if (trblResize.bottom) {
  98. if (abs(otherTrbl.top - shapeTrbl.bottom) < 10) {
  99. topResize = shapeNewTrbl.bottom - otherTrbl.top;
  100. }
  101. if (abs(otherTrbl.bottom - shapeTrbl.bottom) < 5) {
  102. bottomResize = shapeNewTrbl.bottom - otherTrbl.bottom;
  103. }
  104. }
  105. if (topResize || rightResize || bottomResize || leftResize) {
  106. resizeNeeded.push({
  107. shape: other,
  108. newBounds: resizeTRBL(other, {
  109. top: topResize,
  110. right: rightResize,
  111. bottom: bottomResize,
  112. left: leftResize
  113. })
  114. });
  115. }
  116. });
  117. return resizeNeeded;
  118. }