From 6388312fb545a1f3c974f56eb693dd121f14d360 Mon Sep 17 00:00:00 2001 From: Maciej Barelkowski Date: Thu, 12 Mar 2020 15:00:52 +0100 Subject: [PATCH] chore(space-tool): refactor participant minimum height function --- .../modeling/behavior/SpaceToolBehavior.js | 75 ++++++++++++------- 1 file changed, 46 insertions(+), 29 deletions(-) diff --git a/lib/features/modeling/behavior/SpaceToolBehavior.js b/lib/features/modeling/behavior/SpaceToolBehavior.js index 593fcef5..4949f5bb 100644 --- a/lib/features/modeling/behavior/SpaceToolBehavior.js +++ b/lib/features/modeling/behavior/SpaceToolBehavior.js @@ -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'; -} \ No newline at end of file +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} 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; + } + } +}