DeleteLaneBehavior.js 2.5 KB

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