DeleteLaneBehavior.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import inherits from 'inherits';
  2. import CommandInterceptor from 'diagram-js/lib/command/CommandInterceptor';
  3. import { is } from '../../../util/ModelUtil';
  4. import {
  5. getChildLanes
  6. } from '../util/LaneUtil';
  7. import {
  8. eachElement
  9. } from 'diagram-js/lib/util/Elements';
  10. var LOW_PRIORITY = 500;
  11. /**
  12. * BPMN specific delete lane behavior
  13. */
  14. export default function DeleteLaneBehavior(eventBus, modeling, spaceTool) {
  15. CommandInterceptor.call(this, eventBus);
  16. function compensateLaneDelete(shape, oldParent) {
  17. var siblings = getChildLanes(oldParent);
  18. var topAffected = [];
  19. var bottomAffected = [];
  20. eachElement(siblings, function(element) {
  21. if (element.y > shape.y) {
  22. bottomAffected.push(element);
  23. } else {
  24. topAffected.push(element);
  25. }
  26. return element.children;
  27. });
  28. if (!siblings.length) {
  29. return;
  30. }
  31. var offset;
  32. if (bottomAffected.length && topAffected.length) {
  33. offset = shape.height / 2;
  34. } else {
  35. offset = shape.height;
  36. }
  37. var topAdjustments,
  38. bottomAdjustments;
  39. if (topAffected.length) {
  40. topAdjustments = spaceTool.calculateAdjustments(
  41. topAffected, 'y', offset, shape.y - 10);
  42. spaceTool.makeSpace(
  43. topAdjustments.movingShapes,
  44. topAdjustments.resizingShapes,
  45. { x: 0, y: offset }, 's');
  46. }
  47. if (bottomAffected.length) {
  48. bottomAdjustments = spaceTool.calculateAdjustments(
  49. bottomAffected, 'y', -offset, shape.y + shape.height + 10);
  50. spaceTool.makeSpace(
  51. bottomAdjustments.movingShapes,
  52. bottomAdjustments.resizingShapes,
  53. { x: 0, y: -offset }, 'n');
  54. }
  55. }
  56. /**
  57. * Adjust sizes of other lanes after lane deletion
  58. */
  59. this.postExecuted('shape.delete', LOW_PRIORITY, function(event) {
  60. var context = event.context,
  61. hints = context.hints,
  62. shape = context.shape,
  63. oldParent = context.oldParent;
  64. // only compensate lane deletes
  65. if (!is(shape, 'bpmn:Lane')) {
  66. return;
  67. }
  68. // compensate root deletes only
  69. if (hints && hints.nested) {
  70. return;
  71. }
  72. compensateLaneDelete(shape, oldParent);
  73. });
  74. }
  75. DeleteLaneBehavior.$inject = [
  76. 'eventBus',
  77. 'modeling',
  78. 'spaceTool'
  79. ];
  80. inherits(DeleteLaneBehavior, CommandInterceptor);