ToggleCollapseConnectionBehaviour.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import inherits from 'inherits-browser';
  2. import CommandInterceptor from 'diagram-js/lib/command/CommandInterceptor';
  3. import {
  4. forEach
  5. } from 'min-dash';
  6. import { getMid } from 'diagram-js/lib/layout/LayoutUtil';
  7. import { selfAndAllChildren } from 'diagram-js/lib/util/Elements';
  8. import { isExpanded } from '../../../util/DiUtil';
  9. /**
  10. * @typedef {import('diagram-js/lib/core/EventBus').default} EventBus
  11. * @typedef {import('../Modeling').default} Modeling
  12. *
  13. * @typedef {import('../../../model/Types').Element} Element
  14. * @typedef {import('../../../model/Types').Shape} Shape
  15. *
  16. * @typedef {import('diagram-js/lib/util/Types').DirectionTRBL} DirectionTRBL
  17. */
  18. /**
  19. * @param {EventBus} eventBus
  20. * @param {Modeling} modeling
  21. */
  22. export default function ToggleCollapseConnectionBehaviour(
  23. eventBus, modeling
  24. ) {
  25. CommandInterceptor.call(this, eventBus);
  26. this.postExecuted('shape.toggleCollapse', 1500, function(context) {
  27. // var shape = context.shape;
  28. var shape = context.shape;
  29. // only change connections when collapsing
  30. if (isExpanded(shape)) {
  31. return;
  32. }
  33. var allChildren = selfAndAllChildren(shape);
  34. allChildren.forEach(function(child) {
  35. // Ensure that the connection array is not modified during iteration
  36. var incomingConnections = child.incoming.slice(),
  37. outgoingConnections = child.outgoing.slice();
  38. forEach(incomingConnections, function(c) {
  39. handleConnection(c, true);
  40. });
  41. forEach(outgoingConnections, function(c) {
  42. handleConnection(c, false);
  43. });
  44. });
  45. function handleConnection(c, incoming) {
  46. if (allChildren.indexOf(c.source) !== -1 && allChildren.indexOf(c.target) !== -1) {
  47. return;
  48. }
  49. if (incoming) {
  50. modeling.reconnectEnd(c, shape, getMid(shape));
  51. } else {
  52. modeling.reconnectStart(c, shape, getMid(shape));
  53. }
  54. }
  55. }, true);
  56. }
  57. inherits(ToggleCollapseConnectionBehaviour, CommandInterceptor);
  58. ToggleCollapseConnectionBehaviour.$inject = [
  59. 'eventBus',
  60. 'modeling',
  61. ];