all files / Github/bpmn-js/lib/features/modeling/cmd/ SplitLaneHandler.js

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