AddLaneHandler.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import {
  2. filter
  3. } from 'min-dash';
  4. import {
  5. eachElement
  6. } from 'diagram-js/lib/util/Elements';
  7. import {
  8. getLanesRoot,
  9. getChildLanes,
  10. LANE_INDENTATION
  11. } from '../util/LaneUtil';
  12. /**
  13. * @typedef {import('diagram-js/lib/command/CommandHandler').default} CommandHandler
  14. *
  15. * @typedef {import('../Modeling').default} Modeling
  16. * @typedef {import('../../space-tool/BpmnSpaceTool').default} SpaceTool
  17. */
  18. /**
  19. * A handler that allows us to add a new lane
  20. * above or below an existing one.
  21. *
  22. * @implements {CommandHandler}
  23. *
  24. * @param {Modeling} modeling
  25. * @param {SpaceTool} spaceTool
  26. */
  27. export default function AddLaneHandler(modeling, spaceTool) {
  28. this._modeling = modeling;
  29. this._spaceTool = spaceTool;
  30. }
  31. AddLaneHandler.$inject = [
  32. 'modeling',
  33. 'spaceTool'
  34. ];
  35. AddLaneHandler.prototype.preExecute = function(context) {
  36. var spaceTool = this._spaceTool,
  37. modeling = this._modeling;
  38. var shape = context.shape,
  39. location = context.location;
  40. var lanesRoot = getLanesRoot(shape);
  41. var isRoot = lanesRoot === shape,
  42. laneParent = isRoot ? shape : shape.parent;
  43. var existingChildLanes = getChildLanes(laneParent);
  44. // (0) add a lane if we currently got none and are adding to root
  45. if (!existingChildLanes.length) {
  46. modeling.createShape({ type: 'bpmn:Lane' }, {
  47. x: shape.x + LANE_INDENTATION,
  48. y: shape.y,
  49. width: shape.width - LANE_INDENTATION,
  50. height: shape.height
  51. }, laneParent);
  52. }
  53. // (1) collect affected elements to create necessary space
  54. var allAffected = [];
  55. eachElement(lanesRoot, function(element) {
  56. allAffected.push(element);
  57. // handle element labels in the diagram root
  58. if (element.label) {
  59. allAffected.push(element.label);
  60. }
  61. if (element === shape) {
  62. return [];
  63. }
  64. return filter(element.children, function(c) {
  65. return c !== shape;
  66. });
  67. });
  68. var offset = location === 'top' ? -120 : 120,
  69. lanePosition = location === 'top' ? shape.y : shape.y + shape.height,
  70. spacePos = lanePosition + (location === 'top' ? 10 : -10),
  71. direction = location === 'top' ? 'n' : 's';
  72. var adjustments = spaceTool.calculateAdjustments(allAffected, 'y', offset, spacePos);
  73. spaceTool.makeSpace(
  74. adjustments.movingShapes,
  75. adjustments.resizingShapes,
  76. { x: 0, y: offset },
  77. direction,
  78. spacePos
  79. );
  80. // (2) create new lane at open space
  81. context.newLane = modeling.createShape({ type: 'bpmn:Lane' }, {
  82. x: shape.x + (isRoot ? LANE_INDENTATION : 0),
  83. y: lanePosition - (location === 'top' ? 120 : 0),
  84. width: shape.width - (isRoot ? LANE_INDENTATION : 0),
  85. height: 120
  86. }, laneParent);
  87. };