SpaceUtil.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import { forEach } from 'min-dash';
  2. /**
  3. * Return direction given axis and delta.
  4. *
  5. * @param {string} axis
  6. * @param {number} delta
  7. *
  8. * @return {string}
  9. */
  10. export function getDirection(axis, delta) {
  11. if (axis === 'x') {
  12. if (delta > 0) {
  13. return 'e';
  14. }
  15. if (delta < 0) {
  16. return 'w';
  17. }
  18. }
  19. if (axis === 'y') {
  20. if (delta > 0) {
  21. return 's';
  22. }
  23. if (delta < 0) {
  24. return 'n';
  25. }
  26. }
  27. return null;
  28. }
  29. /**
  30. * Returns connections whose waypoints are to be updated. Waypoints are to be updated if start
  31. * or end is to be moved or resized.
  32. *
  33. * @param {Array<djs.model.Shape} movingShapes
  34. * @param {Array<djs.model.Shape} resizingShapes
  35. *
  36. * @returns {Array<djs.model.Connection>}
  37. */
  38. export function getWaypointsUpdatingConnections(movingShapes, resizingShapes) {
  39. var waypointsUpdatingConnections = [];
  40. forEach(movingShapes.concat(resizingShapes), function(shape) {
  41. var incoming = shape.incoming,
  42. outgoing = shape.outgoing;
  43. forEach(incoming.concat(outgoing), function(connection) {
  44. var source = connection.source,
  45. target = connection.target;
  46. if (includes(movingShapes, source) ||
  47. includes(movingShapes, target) ||
  48. includes(resizingShapes, source) ||
  49. includes(resizingShapes, target)) {
  50. if (!includes(waypointsUpdatingConnections, connection)) {
  51. waypointsUpdatingConnections.push(connection);
  52. }
  53. }
  54. });
  55. });
  56. return waypointsUpdatingConnections;
  57. }
  58. function includes(array, item) {
  59. return array.indexOf(item) !== -1;
  60. }
  61. /**
  62. * Resize bounds.
  63. *
  64. * @param {Object} bounds
  65. * @param {number} bounds.x
  66. * @param {number} bounds.y
  67. * @param {number} bounds.width
  68. * @param {number} bounds.height
  69. * @param {string} direction
  70. * @param {Object} delta
  71. * @param {number} delta.x
  72. * @param {number} delta.y
  73. *
  74. * @return {Object}
  75. */
  76. export function resizeBounds(bounds, direction, delta) {
  77. var x = bounds.x,
  78. y = bounds.y,
  79. width = bounds.width,
  80. height = bounds.height,
  81. dx = delta.x,
  82. dy = delta.y;
  83. switch (direction) {
  84. case 'n':
  85. return {
  86. x: x,
  87. y: y + dy,
  88. width: width,
  89. height: height - dy
  90. };
  91. case 's':
  92. return {
  93. x: x,
  94. y: y,
  95. width: width,
  96. height: height + dy
  97. };
  98. case 'w':
  99. return {
  100. x: x + dx,
  101. y: y,
  102. width: width - dx,
  103. height: height
  104. };
  105. case 'e':
  106. return {
  107. x: x,
  108. y: y,
  109. width: width + dx,
  110. height: height
  111. };
  112. default:
  113. throw new Error('unknown direction: ' + direction);
  114. }
  115. }