all files / diagram-js/lib/features/modeling/cmd/ MoveConnectionHandler.js

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