# all files / Github/diagram-js/lib/features/modeling/cmd/ ReconnectConnectionHandler.js

95.83% Statements 46/48
93.33% Branches 28/30
100% Functions 5/5
95.83% Lines 46/48

    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
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98

    2256×

    1×

    1×

    174×
    174×
    174×
    174×

    174×

    174×

    174×
    58×
    58×

    174×
    82×
    82×

    174×
    92×
    92×

    174×

    1×
    164×
    164×
    164×
    164×

    164×
    164×

    164×
    6×

    158×
    54×

    104×

    158×

    158×

    1×

    26×
    26×
    26×
    26×

    26×
    14×

    26×
    12×

    26×
    11×

    26×

    158×
     import { isArray } from 'min-dash';

    /**
     * Reconnect connection handler
     */
    export default function ReconnectConnectionHandler(modeling) {
      this._modeling = modeling;
    }

    ReconnectConnectionHandler.$inject = [ 'modeling' ];

    ReconnectConnectionHandler.prototype.execute = function(context) {

      var newSource = context.newSource,
          newTarget = context.newTarget,
          connection = context.connection,
          dockingOrPoints = context.dockingOrPoints;

      Iif (!newSource && !newTarget) {
        throw new Error('newSource or newTarget are required');
      }

      Iif (newSource && newTarget) {
        throw new Error('must specify either newSource or newTarget');
      }

      if (isArray(dockingOrPoints)) {
        context.oldWaypoints = connection.waypoints;
        connection.waypoints = dockingOrPoints;
      }

      if (newSource) {
        context.oldSource = connection.source;
        connection.source = newSource;
      }

      if (newTarget) {
        context.oldTarget = connection.target;
        connection.target = newTarget;
      }

      return connection;
    };

    ReconnectConnectionHandler.prototype.postExecute = function(context) {
      var connection = context.connection,
          dockingOrPoints = context.dockingOrPoints,
          newSource = context.newSource,
          movedEnd = newSource ? 'connectionStart' : 'connectionEnd',
          newWaypoint,
          hints = context.hints,
          layoutHints = {};

      if (hints.layoutConnection === false) {
        return;
      }

      if (isArray(dockingOrPoints)) {
        newWaypoint = newSource ? dockingOrPoints[0] : dockingOrPoints[dockingOrPoints.length - 1];
      } else {
        newWaypoint = dockingOrPoints;
      }

      layoutHints[movedEnd] = getDocking(newWaypoint);

      this._modeling.layoutConnection(connection, layoutHints);
    };

    ReconnectConnectionHandler.prototype.revert = function(context) {

      var oldSource = context.oldSource,
          oldTarget = context.oldTarget,
          oldWaypoints = context.oldWaypoints,
          connection = context.connection;

      if (oldSource) {
        connection.source = oldSource;
      }

      if (oldTarget) {
        connection.target = oldTarget;
      }

      if (oldWaypoints) {
        connection.waypoints = oldWaypoints;
      }

      return connection;
    };

    // helper ///////////////

    function getDocking(point) {
      return point.original || point;
    }