SplitLaneHandler.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import {
  2. getChildLanes,
  3. LANE_INDENTATION
  4. } from '../util/LaneUtil';
  5. /**
  6. * A handler that splits a lane into a number of sub-lanes,
  7. * creating new sub lanes, if necessary.
  8. *
  9. * @param {Modeling} modeling
  10. */
  11. export default function SplitLaneHandler(modeling, translate) {
  12. this._modeling = modeling;
  13. this._translate = translate;
  14. }
  15. SplitLaneHandler.$inject = [
  16. 'modeling',
  17. 'translate'
  18. ];
  19. SplitLaneHandler.prototype.preExecute = function(context) {
  20. var modeling = this._modeling,
  21. translate = this._translate;
  22. var shape = context.shape,
  23. newLanesCount = context.count;
  24. var childLanes = getChildLanes(shape),
  25. existingLanesCount = childLanes.length;
  26. if (existingLanesCount > newLanesCount) {
  27. throw new Error(translate('more than {count} child lanes', { count: newLanesCount }));
  28. }
  29. var newLanesHeight = Math.round(shape.height / newLanesCount);
  30. // Iterate from top to bottom in child lane order,
  31. // resizing existing lanes and creating new ones
  32. // so that they split the parent proportionally.
  33. //
  34. // Due to rounding related errors, the bottom lane
  35. // needs to take up all the remaining space.
  36. var laneY,
  37. laneHeight,
  38. laneBounds,
  39. newLaneAttrs,
  40. idx;
  41. for (idx = 0; idx < newLanesCount; idx++) {
  42. laneY = shape.y + idx * newLanesHeight;
  43. // if bottom lane
  44. if (idx === newLanesCount - 1) {
  45. laneHeight = shape.height - (newLanesHeight * idx);
  46. } else {
  47. laneHeight = newLanesHeight;
  48. }
  49. laneBounds = {
  50. x: shape.x + LANE_INDENTATION,
  51. y: laneY,
  52. width: shape.width - LANE_INDENTATION,
  53. height: laneHeight
  54. };
  55. if (idx < existingLanesCount) {
  56. // resize existing lane
  57. modeling.resizeShape(childLanes[idx], laneBounds);
  58. } else {
  59. // create a new lane at position
  60. newLaneAttrs = {
  61. type: 'bpmn:Lane'
  62. };
  63. modeling.createShape(newLaneAttrs, laneBounds, shape);
  64. }
  65. }
  66. };