DeleteShapeHandler.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 shapes.
  8. *
  9. */
  10. export default function DeleteShapeHandler(canvas, modeling) {
  11. this._canvas = canvas;
  12. this._modeling = modeling;
  13. }
  14. DeleteShapeHandler.$inject = [ 'canvas', 'modeling' ];
  15. /**
  16. * - Remove connections
  17. * - Remove all direct children
  18. */
  19. DeleteShapeHandler.prototype.preExecute = function(context) {
  20. var modeling = this._modeling;
  21. var shape = context.shape;
  22. // remove connections
  23. saveClear(shape.incoming, function(connection) {
  24. // To make sure that the connection isn't removed twice
  25. // For example if a container is removed
  26. modeling.removeConnection(connection, { nested: true });
  27. });
  28. saveClear(shape.outgoing, function(connection) {
  29. modeling.removeConnection(connection, { nested: true });
  30. });
  31. // remove child shapes and connections
  32. saveClear(shape.children, function(child) {
  33. if (isConnection(child)) {
  34. modeling.removeConnection(child, { nested: true });
  35. } else {
  36. modeling.removeShape(child, { nested: true });
  37. }
  38. });
  39. };
  40. /**
  41. * Remove shape and remember the parent
  42. */
  43. DeleteShapeHandler.prototype.execute = function(context) {
  44. var canvas = this._canvas;
  45. var shape = context.shape,
  46. oldParent = shape.parent;
  47. context.oldParent = oldParent;
  48. // remove containment
  49. context.oldParentIndex = collectionIdx(oldParent.children, shape);
  50. // remove shape
  51. canvas.removeShape(shape);
  52. return shape;
  53. };
  54. /**
  55. * Command revert implementation
  56. */
  57. DeleteShapeHandler.prototype.revert = function(context) {
  58. var canvas = this._canvas;
  59. var shape = context.shape,
  60. oldParent = context.oldParent,
  61. oldParentIndex = context.oldParentIndex;
  62. // restore containment
  63. collectionAdd(oldParent.children, shape, oldParentIndex);
  64. canvas.addShape(shape, oldParent);
  65. return shape;
  66. };
  67. function isConnection(element) {
  68. return element.waypoints;
  69. }