ResizeLaneHandler.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import { is } from '../../../util/ModelUtil';
  2. import {
  3. getLanesRoot,
  4. computeLanesResize
  5. } from '../util/LaneUtil';
  6. import {
  7. eachElement
  8. } from 'diagram-js/lib/util/Elements';
  9. import {
  10. asTRBL
  11. } from 'diagram-js/lib/layout/LayoutUtil';
  12. import {
  13. substractTRBL
  14. } from 'diagram-js/lib/features/resize/ResizeUtil';
  15. /**
  16. * A handler that resizes a lane.
  17. *
  18. * @param {Modeling} modeling
  19. */
  20. export default function ResizeLaneHandler(modeling, spaceTool) {
  21. this._modeling = modeling;
  22. this._spaceTool = spaceTool;
  23. }
  24. ResizeLaneHandler.$inject = [
  25. 'modeling',
  26. 'spaceTool'
  27. ];
  28. ResizeLaneHandler.prototype.preExecute = function(context) {
  29. var shape = context.shape,
  30. newBounds = context.newBounds,
  31. balanced = context.balanced;
  32. if (balanced !== false) {
  33. this.resizeBalanced(shape, newBounds);
  34. } else {
  35. this.resizeSpace(shape, newBounds);
  36. }
  37. };
  38. /**
  39. * Resize balanced, adjusting next / previous lane sizes.
  40. *
  41. * @param {djs.model.Shape} shape
  42. * @param {Bounds} newBounds
  43. */
  44. ResizeLaneHandler.prototype.resizeBalanced = function(shape, newBounds) {
  45. var modeling = this._modeling;
  46. var resizeNeeded = computeLanesResize(shape, newBounds);
  47. // resize the lane
  48. modeling.resizeShape(shape, newBounds);
  49. // resize other lanes as needed
  50. resizeNeeded.forEach(function(r) {
  51. modeling.resizeShape(r.shape, r.newBounds);
  52. });
  53. };
  54. /**
  55. * Resize, making actual space and moving below / above elements.
  56. *
  57. * @param {djs.model.Shape} shape
  58. * @param {Bounds} newBounds
  59. */
  60. ResizeLaneHandler.prototype.resizeSpace = function(shape, newBounds) {
  61. var spaceTool = this._spaceTool;
  62. var shapeTrbl = asTRBL(shape),
  63. newTrbl = asTRBL(newBounds);
  64. var trblDiff = substractTRBL(newTrbl, shapeTrbl);
  65. var lanesRoot = getLanesRoot(shape);
  66. var allAffected = [],
  67. allLanes = [];
  68. eachElement(lanesRoot, function(element) {
  69. allAffected.push(element);
  70. if (is(element, 'bpmn:Lane') || is(element, 'bpmn:Participant')) {
  71. allLanes.push(element);
  72. }
  73. return element.children;
  74. });
  75. var change,
  76. spacePos,
  77. direction,
  78. offset,
  79. adjustments;
  80. if (trblDiff.bottom || trblDiff.top) {
  81. change = trblDiff.bottom || trblDiff.top;
  82. spacePos = shape.y + (trblDiff.bottom ? shape.height : 0) + (trblDiff.bottom ? -10 : 10);
  83. direction = trblDiff.bottom ? 's' : 'n';
  84. offset = trblDiff.top > 0 || trblDiff.bottom < 0 ? -change : change;
  85. adjustments = spaceTool.calculateAdjustments(allAffected, 'y', offset, spacePos);
  86. spaceTool.makeSpace(adjustments.movingShapes, adjustments.resizingShapes, { x: 0, y: change }, direction);
  87. }
  88. if (trblDiff.left || trblDiff.right) {
  89. change = trblDiff.right || trblDiff.left;
  90. spacePos = shape.x + (trblDiff.right ? shape.width : 0) + (trblDiff.right ? -10 : 100);
  91. direction = trblDiff.right ? 'e' : 'w';
  92. offset = trblDiff.left > 0 || trblDiff.right < 0 ? -change : change;
  93. adjustments = spaceTool.calculateAdjustments(allLanes, 'x', offset, spacePos);
  94. spaceTool.makeSpace(adjustments.movingShapes, adjustments.resizingShapes, { x: change, y: 0 }, direction);
  95. }
  96. };