MoveShapeHandler.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import {
  2. assign,
  3. forEach,
  4. pick
  5. } from 'min-dash';
  6. import MoveHelper from './helper/MoveHelper';
  7. import {
  8. add as collectionAdd,
  9. remove as collectionRemove
  10. } from '../../../util/Collections';
  11. import {
  12. getMovedSourceAnchor,
  13. getMovedTargetAnchor
  14. } from './helper/AnchorsHelper';
  15. /**
  16. * A handler that implements reversible moving of shapes.
  17. */
  18. export default function MoveShapeHandler(modeling) {
  19. this._modeling = modeling;
  20. this._helper = new MoveHelper(modeling);
  21. }
  22. MoveShapeHandler.$inject = [ 'modeling' ];
  23. MoveShapeHandler.prototype.execute = function(context) {
  24. var shape = context.shape,
  25. delta = context.delta,
  26. newParent = context.newParent || shape.parent,
  27. newParentIndex = context.newParentIndex,
  28. oldParent = shape.parent;
  29. context.oldBounds = pick(shape, [ 'x', 'y', 'width', 'height' ]);
  30. // save old parent in context
  31. context.oldParent = oldParent;
  32. context.oldParentIndex = collectionRemove(oldParent.children, shape);
  33. // add to new parent at position
  34. collectionAdd(newParent.children, shape, newParentIndex);
  35. // update shape parent + position
  36. assign(shape, {
  37. parent: newParent,
  38. x: shape.x + delta.x,
  39. y: shape.y + delta.y
  40. });
  41. return shape;
  42. };
  43. MoveShapeHandler.prototype.postExecute = function(context) {
  44. var shape = context.shape,
  45. delta = context.delta,
  46. hints = context.hints;
  47. var modeling = this._modeling;
  48. if (hints.layout !== false) {
  49. forEach(shape.incoming, function(c) {
  50. modeling.layoutConnection(c, {
  51. connectionEnd: getMovedTargetAnchor(c, shape, delta)
  52. });
  53. });
  54. forEach(shape.outgoing, function(c) {
  55. modeling.layoutConnection(c, {
  56. connectionStart: getMovedSourceAnchor(c, shape, delta)
  57. });
  58. });
  59. }
  60. if (hints.recurse !== false) {
  61. this.moveChildren(context);
  62. }
  63. };
  64. MoveShapeHandler.prototype.revert = function(context) {
  65. var shape = context.shape,
  66. oldParent = context.oldParent,
  67. oldParentIndex = context.oldParentIndex,
  68. delta = context.delta;
  69. // restore previous location in old parent
  70. collectionAdd(oldParent.children, shape, oldParentIndex);
  71. // revert to old position and parent
  72. assign(shape, {
  73. parent: oldParent,
  74. x: shape.x - delta.x,
  75. y: shape.y - delta.y
  76. });
  77. return shape;
  78. };
  79. MoveShapeHandler.prototype.moveChildren = function(context) {
  80. var delta = context.delta,
  81. shape = context.shape;
  82. this._helper.moveRecursive(shape.children, delta, null);
  83. };
  84. MoveShapeHandler.prototype.getNewParent = function(context) {
  85. return context.newParent || context.shape.parent;
  86. };