SpaceToolBehavior.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import { forEach } from 'min-dash';
  2. import { is } from '../../../util/ModelUtil';
  3. import { isExpanded } from '../../../util/DiUtil';
  4. import {
  5. LANE_MIN_DIMENSIONS,
  6. PARTICIPANT_MIN_DIMENSIONS,
  7. SUB_PROCESS_MIN_DIMENSIONS,
  8. TEXT_ANNOTATION_MIN_DIMENSIONS
  9. } from './ResizeBehavior';
  10. import { getChildLanes } from '../util/LaneUtil';
  11. var max = Math.max;
  12. export default function SpaceToolBehavior(eventBus) {
  13. eventBus.on('spaceTool.getMinDimensions', function(context) {
  14. var shapes = context.shapes,
  15. axis = context.axis,
  16. start = context.start,
  17. minDimensions = {};
  18. forEach(shapes, function(shape) {
  19. var id = shape.id;
  20. if (is(shape, 'bpmn:Participant')) {
  21. if (isHorizontal(axis)) {
  22. minDimensions[ id ] = PARTICIPANT_MIN_DIMENSIONS;
  23. } else {
  24. minDimensions[ id ] = {
  25. width: PARTICIPANT_MIN_DIMENSIONS.width,
  26. height: getParticipantMinHeight(shape, start)
  27. };
  28. }
  29. }
  30. if (is(shape, 'bpmn:SubProcess') && isExpanded(shape)) {
  31. minDimensions[ id ] = SUB_PROCESS_MIN_DIMENSIONS;
  32. }
  33. if (is(shape, 'bpmn:TextAnnotation')) {
  34. minDimensions[ id ] = TEXT_ANNOTATION_MIN_DIMENSIONS;
  35. }
  36. });
  37. return minDimensions;
  38. });
  39. }
  40. SpaceToolBehavior.$inject = [ 'eventBus' ];
  41. // helpers //////////
  42. function isHorizontal(axis) {
  43. return axis === 'x';
  44. }
  45. /**
  46. * Get minimum height for participant taking lanes into account.
  47. *
  48. * @param {<djs.model.Shape>} participant
  49. * @param {number} start
  50. *
  51. * @returns {Object}
  52. */
  53. function getParticipantMinHeight(participant, start) {
  54. var lanesMinHeight;
  55. if (!hasChildLanes(participant)) {
  56. return PARTICIPANT_MIN_DIMENSIONS.height;
  57. }
  58. lanesMinHeight = getLanesMinHeight(participant, start);
  59. return max(PARTICIPANT_MIN_DIMENSIONS.height, lanesMinHeight);
  60. }
  61. function hasChildLanes(element) {
  62. return !!getChildLanes(element).length;
  63. }
  64. function getLanesMinHeight(participant, resizeStart) {
  65. var lanes = getChildLanes(participant),
  66. resizedLane;
  67. // find the nested lane which is currently resized
  68. resizedLane = findResizedLane(lanes, resizeStart);
  69. // resized lane cannot shrink below the minimum height
  70. // but remaining lanes' dimensions are kept intact
  71. return participant.height - resizedLane.height + LANE_MIN_DIMENSIONS.height;
  72. }
  73. /**
  74. * Find nested lane which is currently resized.
  75. *
  76. * @param {Array<djs.model.Shape>} lanes
  77. * @param {number} resizeStart
  78. */
  79. function findResizedLane(lanes, resizeStart) {
  80. var i, lane, childLanes;
  81. for (i = 0; i < lanes.length; i++) {
  82. lane = lanes[i];
  83. // resizing current lane or a lane nested
  84. if (resizeStart >= lane.y && resizeStart <= lane.y + lane.height) {
  85. childLanes = getChildLanes(lane);
  86. // a nested lane is resized
  87. if (childLanes.length) {
  88. return findResizedLane(childLanes, resizeStart);
  89. }
  90. // current lane is the resized one
  91. return lane;
  92. }
  93. }
  94. }