BpmnSnappingUtil.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import {
  2. getOrientation
  3. } from 'diagram-js/lib/layout/LayoutUtil';
  4. export function getBoundaryAttachment(position, targetBounds) {
  5. var orientation = getOrientation(position, targetBounds, -15);
  6. if (orientation !== 'intersect') {
  7. return orientation;
  8. } else {
  9. return null;
  10. }
  11. }
  12. // participant snapping box implementation //////////////////////
  13. import { is } from '../../util/ModelUtil';
  14. import {
  15. asTRBL
  16. } from 'diagram-js/lib/layout/LayoutUtil';
  17. import {
  18. collectLanes,
  19. getLanesRoot
  20. } from '../modeling/util/LaneUtil';
  21. var abs = Math.abs,
  22. min = Math.min,
  23. max = Math.max;
  24. function addToTrbl(trbl, attr, value, choice) {
  25. var current = trbl[attr];
  26. // make sure to set the value if it does not exist
  27. // or apply the correct value by comparing against
  28. // choice(value, currentValue)
  29. trbl[attr] = current === undefined ? value : choice(value, current);
  30. }
  31. function addMin(trbl, attr, value) {
  32. return addToTrbl(trbl, attr, value, min);
  33. }
  34. function addMax(trbl, attr, value) {
  35. return addToTrbl(trbl, attr, value, max);
  36. }
  37. var LANE_MIN_HEIGHT = 60,
  38. LANE_MIN_WIDTH = 300,
  39. LANE_RIGHT_PADDING = 20,
  40. LANE_LEFT_PADDING = 50,
  41. LANE_TOP_PADDING = 20,
  42. LANE_BOTTOM_PADDING = 20;
  43. export function getParticipantSizeConstraints(laneShape, resizeDirection, balanced) {
  44. var lanesRoot = getLanesRoot(laneShape);
  45. var isFirst = true,
  46. isLast = true;
  47. // max top/bottom size for lanes
  48. var allLanes = collectLanes(lanesRoot, [ lanesRoot ]);
  49. var laneTrbl = asTRBL(laneShape);
  50. var maxTrbl = {},
  51. minTrbl = {};
  52. if (/e/.test(resizeDirection)) {
  53. minTrbl.right = laneTrbl.left + LANE_MIN_WIDTH;
  54. } else
  55. if (/w/.test(resizeDirection)) {
  56. minTrbl.left = laneTrbl.right - LANE_MIN_WIDTH;
  57. }
  58. allLanes.forEach(function(other) {
  59. var otherTrbl = asTRBL(other);
  60. if (/n/.test(resizeDirection)) {
  61. if (otherTrbl.top < (laneTrbl.top - 10)) {
  62. isFirst = false;
  63. }
  64. // max top size (based on next element)
  65. if (balanced && abs(laneTrbl.top - otherTrbl.bottom) < 10) {
  66. addMax(maxTrbl, 'top', otherTrbl.top + LANE_MIN_HEIGHT);
  67. }
  68. // min top size (based on self or nested element)
  69. if (abs(laneTrbl.top - otherTrbl.top) < 5) {
  70. addMin(minTrbl, 'top', otherTrbl.bottom - LANE_MIN_HEIGHT);
  71. }
  72. }
  73. if (/s/.test(resizeDirection)) {
  74. if (otherTrbl.bottom > (laneTrbl.bottom + 10)) {
  75. isLast = false;
  76. }
  77. // max bottom size (based on previous element)
  78. if (balanced && abs(laneTrbl.bottom - otherTrbl.top) < 10) {
  79. addMin(maxTrbl, 'bottom', otherTrbl.bottom - LANE_MIN_HEIGHT);
  80. }
  81. // min bottom size (based on self or nested element)
  82. if (abs(laneTrbl.bottom - otherTrbl.bottom) < 5) {
  83. addMax(minTrbl, 'bottom', otherTrbl.top + LANE_MIN_HEIGHT);
  84. }
  85. }
  86. });
  87. // max top/bottom/left/right size based on flow nodes
  88. var flowElements = lanesRoot.children.filter(function(s) {
  89. return !s.hidden && !s.waypoints && (is(s, 'bpmn:FlowElement') || is(s, 'bpmn:Artifact'));
  90. });
  91. flowElements.forEach(function(flowElement) {
  92. var flowElementTrbl = asTRBL(flowElement);
  93. if (isFirst && /n/.test(resizeDirection)) {
  94. addMin(minTrbl, 'top', flowElementTrbl.top - LANE_TOP_PADDING);
  95. }
  96. if (/e/.test(resizeDirection)) {
  97. addMax(minTrbl, 'right', flowElementTrbl.right + LANE_RIGHT_PADDING);
  98. }
  99. if (isLast && /s/.test(resizeDirection)) {
  100. addMax(minTrbl, 'bottom', flowElementTrbl.bottom + LANE_BOTTOM_PADDING);
  101. }
  102. if (/w/.test(resizeDirection)) {
  103. addMin(minTrbl, 'left', flowElementTrbl.left - LANE_LEFT_PADDING);
  104. }
  105. });
  106. return {
  107. min: minTrbl,
  108. max: maxTrbl
  109. };
  110. }