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

92% Statements 23/25
75% Branches 6/8
100% Functions 3/3
92% Lines 23/25

    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
    1189×
    1189×

    1×

    1×

    229×
    229×
    229×
    229×
    229×
    229×

    229×

    229×

    229×
    229×

    229×
    142×

    229×

    229×

    1×
    46×

    46×

    46×
    46×
     export default function CreateConnectionHandler(canvas, layouter) {
      this._canvas = canvas;
      this._layouter = layouter;
    }

    CreateConnectionHandler.$inject = [ 'canvas', 'layouter' ];

    // api //////////////////////

    /**
     * Appends a shape to a target shape
     *
     * @param {Object} context
     * @param {djs.element.Base} context.source the source object
     * @param {djs.element.Base} context.target the parent object
     * @param {Point} context.position position of the new element
     */
    CreateConnectionHandler.prototype.execute = function(context) {

      var connection = context.connection,
          source = context.source,
          target = context.target,
          parent = context.parent,
          parentIndex = context.parentIndex,
          hints = context.hints;

      Iif (!source || !target) {
        throw new Error('source and target required');
      }

      Iif (!parent) {
        throw new Error('parent required');
      }

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

      if (!connection.waypoints) {
        connection.waypoints = this._layouter.layoutConnection(connection, hints);
      }

      // add connection
      this._canvas.addConnection(connection, parent, parentIndex);

      return connection;
    };

    CreateConnectionHandler.prototype.revert = function(context) {
      var connection = context.connection;

      this._canvas.removeConnection(connection);

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