AddLaneHandler.js 2.2 KB

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