AddLaneHandler.js 2.4 KB

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