DeleteConnectionHandler.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import {
  2. add as collectionAdd,
  3. indexOf as collectionIdx
  4. } from '../../../util/Collections';
  5. import { saveClear } from '../../../util/Removal';
  6. /**
  7. * A handler that implements reversible deletion of Connections.
  8. */
  9. export default function DeleteConnectionHandler(canvas, modeling) {
  10. this._canvas = canvas;
  11. this._modeling = modeling;
  12. }
  13. DeleteConnectionHandler.$inject = [
  14. 'canvas',
  15. 'modeling'
  16. ];
  17. /**
  18. * - Remove connections
  19. */
  20. DeleteConnectionHandler.prototype.preExecute = function(context) {
  21. var modeling = this._modeling;
  22. var connection = context.connection;
  23. // remove connections
  24. saveClear(connection.incoming, function(connection) {
  25. // To make sure that the connection isn't removed twice
  26. // For example if a container is removed
  27. modeling.removeConnection(connection, { nested: true });
  28. });
  29. saveClear(connection.outgoing, function(connection) {
  30. modeling.removeConnection(connection, { nested: true });
  31. });
  32. };
  33. DeleteConnectionHandler.prototype.execute = function(context) {
  34. var connection = context.connection,
  35. parent = connection.parent;
  36. context.parent = parent;
  37. // remember containment
  38. context.parentIndex = collectionIdx(parent.children, connection);
  39. context.source = connection.source;
  40. context.target = connection.target;
  41. this._canvas.removeConnection(connection);
  42. connection.source = null;
  43. connection.target = null;
  44. return connection;
  45. };
  46. /**
  47. * Command revert implementation.
  48. */
  49. DeleteConnectionHandler.prototype.revert = function(context) {
  50. var connection = context.connection,
  51. parent = context.parent,
  52. parentIndex = context.parentIndex;
  53. connection.source = context.source;
  54. connection.target = context.target;
  55. // restore containment
  56. collectionAdd(parent.children, connection, parentIndex);
  57. this._canvas.addConnection(connection, parent);
  58. return connection;
  59. };