chore(space-tool): refactor participant minimum height function

This commit is contained in:
Maciej Barelkowski 2020-03-12 15:00:52 +01:00 committed by fake-join[bot]
parent dafa6f138c
commit 6388312fb5
1 changed files with 46 additions and 29 deletions

View File

@ -1,7 +1,4 @@
import {
forEach,
reduce
} from 'min-dash';
import { forEach } from 'min-dash';
import { is } from '../../../util/ModelUtil';
@ -59,6 +56,9 @@ SpaceToolBehavior.$inject = [ 'eventBus' ];
// helpers //////////
function isHorizontal(axis) {
return axis === 'x';
}
/**
* Get minimum height for participant taking lanes into account.
@ -69,39 +69,56 @@ SpaceToolBehavior.$inject = [ 'eventBus' ];
* @returns {Object}
*/
function getParticipantMinHeight(participant, start) {
var lanes = getChildLanes(participant);
var lanesMinHeight;
if (!lanes.length) {
if (!hasChildLanes(participant)) {
return PARTICIPANT_MIN_DIMENSIONS.height;
}
function getLanesMinHeight(element) {
return reduce(getChildLanes(element), function(minHeight, lane) {
if (start >= lane.y && start <= lane.y + lane.height) {
lanesMinHeight = getLanesMinHeight(participant, start);
// resizing lane
if (hasChildLanes(lane)) {
minHeight += getLanesMinHeight(lane);
} else {
minHeight += lane.y + LANE_MIN_DIMENSIONS.height - participant.y;
}
} else if (start <= lane.y) {
// lane after resizing lane
minHeight += lane.height;
}
return minHeight;
}, 0);
}
return max(PARTICIPANT_MIN_DIMENSIONS.height, getLanesMinHeight(participant));
return max(PARTICIPANT_MIN_DIMENSIONS.height, lanesMinHeight);
}
function hasChildLanes(element) {
return !!getChildLanes(element).length;
}
function isHorizontal(axis) {
return axis === 'x';
}
function getLanesMinHeight(participant, resizeStart) {
var lanes = getChildLanes(participant),
resizedLane;
// find the nested lane which is currently resized
resizedLane = findResizedLane(lanes, resizeStart);
// resized lane cannot shrink below the minimum height
// but remaining lanes' dimensions are kept intact
return participant.height - resizedLane.height + LANE_MIN_DIMENSIONS.height;
}
/**
* Find nested lane which is currently resized.
*
* @param {Array<djs.model.Shape>} lanes
* @param {number} resizeStart
*/
function findResizedLane(lanes, resizeStart) {
var i, lane, childLanes;
for (i = 0; i < lanes.length; i++) {
lane = lanes[i];
// resizing current lane or a lane nested
if (resizeStart >= lane.y && resizeStart <= lane.y + lane.height) {
childLanes = getChildLanes(lane);
// a nested lane is resized
if (childLanes.length) {
return findResizedLane(childLanes, resizeStart);
}
// current lane is the resized one
return lane;
}
}
}