MoveConnectionHandler.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import { forEach } from 'min-dash';
  2. import {
  3. add as collectionAdd,
  4. remove as collectionRemove
  5. } from '../../../util/Collections';
  6. /**
  7. * A handler that implements reversible moving of connections.
  8. *
  9. * The handler differs from the layout connection handler in a sense
  10. * that it preserves the connection layout.
  11. */
  12. export default function MoveConnectionHandler() { }
  13. MoveConnectionHandler.prototype.execute = function(context) {
  14. var connection = context.connection,
  15. delta = context.delta;
  16. var newParent = context.newParent || connection.parent,
  17. newParentIndex = context.newParentIndex,
  18. oldParent = connection.parent;
  19. // save old parent in context
  20. context.oldParent = oldParent;
  21. context.oldParentIndex = collectionRemove(oldParent.children, connection);
  22. // add to new parent at position
  23. collectionAdd(newParent.children, connection, newParentIndex);
  24. // update parent
  25. connection.parent = newParent;
  26. // update waypoint positions
  27. forEach(connection.waypoints, function(p) {
  28. p.x += delta.x;
  29. p.y += delta.y;
  30. if (p.original) {
  31. p.original.x += delta.x;
  32. p.original.y += delta.y;
  33. }
  34. });
  35. return connection;
  36. };
  37. MoveConnectionHandler.prototype.revert = function(context) {
  38. var connection = context.connection,
  39. newParent = connection.parent,
  40. oldParent = context.oldParent,
  41. oldParentIndex = context.oldParentIndex,
  42. delta = context.delta;
  43. // remove from newParent
  44. collectionRemove(newParent.children, connection);
  45. // restore previous location in old parent
  46. collectionAdd(oldParent.children, connection, oldParentIndex);
  47. // restore parent
  48. connection.parent = oldParent;
  49. // revert to old waypoint positions
  50. forEach(connection.waypoints, function(p) {
  51. p.x -= delta.x;
  52. p.y -= delta.y;
  53. if (p.original) {
  54. p.original.x -= delta.x;
  55. p.original.y -= delta.y;
  56. }
  57. });
  58. return connection;
  59. };