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

100% Statements 23/23
100% Branches 0/0
100% Functions 3/3
100% Lines 23/23

    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

    1128×
    1128×

    1×

    1×

    170×
    170×

    170×

    170×

    170×
    170×

    170×

    170×
    170×

    170×

    1×

    46×
    46×
    46×

    46×
    46×

    46×

    46×

    46×

     import {
      add as collectionAdd,
      indexOf as collectionIdx
    } from '../../../util/Collections';

    /**
     * A handler that implements reversible deletion of Connections.
     */
    export default function DeleteConnectionHandler(canvas, modeling) {
      this._canvas = canvas;
      this._modeling = modeling;
    }

    DeleteConnectionHandler.$inject = [
      'canvas',
      'modeling'
    ];

    DeleteConnectionHandler.prototype.execute = function(context) {

      var connection = context.connection,
          parent = connection.parent;

      context.parent = parent;

      // remember containment
      context.parentIndex = collectionIdx(parent.children, connection);

      context.source = connection.source;
      context.target = connection.target;

      this._canvas.removeConnection(connection);

      connection.source = null;
      connection.target = null;

      return connection;
    };

    /**
     * Command revert implementation.
     */
    DeleteConnectionHandler.prototype.revert = function(context) {

      var connection = context.connection,
          parent = context.parent,
          parentIndex = context.parentIndex;

      connection.source = context.source;
      connection.target = context.target;

      // restore containment
      collectionAdd(parent.children, connection, parentIndex);

      this._canvas.addConnection(connection, parent);

      return connection;
    };