all files / lib/features/modeling/cmd/ ReconnectConnectionHandler.js

94.12% Statements 32/34
90% Branches 18/20
100% Functions 3/3
94.12% Lines 32/34
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                    13× 13× 13× 13× 13×     13×       13×       13×   13×         13×     13×     13×   13×                    
import { isArray } from 'min-dash';
 
 
/**
 * Reconnect connection handler
 */
export default function ReconnectConnectionHandler() { }
 
ReconnectConnectionHandler.$inject = [ ];
 
ReconnectConnectionHandler.prototype.execute = function(context) {
 
  var newSource = context.newSource,
      newTarget = context.newTarget,
      connection = context.connection,
      dockingOrPoints = context.dockingOrPoints,
      oldWaypoints = connection.waypoints,
      newWaypoints;
 
  Iif (!newSource && !newTarget) {
    throw new Error('newSource or newTarget are required');
  }
 
  Iif (newSource && newTarget) {
    throw new Error('must specify either newSource or newTarget');
  }
 
  context.oldWaypoints = oldWaypoints;
 
  if (isArray(dockingOrPoints)) {
    newWaypoints = dockingOrPoints;
  } else {
    newWaypoints = oldWaypoints.slice();
 
    newWaypoints.splice(newSource ? 0 : -1, 1, dockingOrPoints);
  }
 
  if (newSource) {
    context.oldSource = connection.source;
    connection.source = newSource;
  }
 
  if (newTarget) {
    context.oldTarget = connection.target;
    connection.target = newTarget;
  }
 
  connection.waypoints = newWaypoints;
 
  return connection;
};
 
ReconnectConnectionHandler.prototype.revert = function(context) {
 
  var newSource = context.newSource,
      newTarget = context.newTarget,
      connection = context.connection;
 
  if (newSource) {
    connection.source = context.oldSource;
  }
 
  if (newTarget) {
    connection.target = context.oldTarget;
  }
 
  connection.waypoints = context.oldWaypoints;
 
  return connection;
};