ResizeLaneHandler.js 3.4 KB

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