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

93.75% Statements 15/16
75% Branches 6/8
100% Functions 5/5
93.75% Lines 15/16

    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

    1189×

    1×

    1×

    52×

    52×

    52×
    52×

    52×

    52×

    1×
    52×

    52×

    52×

    52×
    36×

     import { some } from 'min-dash';

    /**
     * A handler that implements reversible appending of shapes
     * to a source shape.
     *
     * @param {canvas} Canvas
     * @param {elementFactory} ElementFactory
     * @param {modeling} Modeling
     */
    export default function AppendShapeHandler(modeling) {
      this._modeling = modeling;
    }

    AppendShapeHandler.$inject = [ 'modeling' ];

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

    /**
     * Creates a new shape
     *
     * @param {Object} context
     * @param {ElementDescriptor} context.shape the new shape
     * @param {ElementDescriptor} context.source the source object
     * @param {ElementDescriptor} context.parent the parent object
     * @param {Point} context.position position of the new element
     */
    AppendShapeHandler.prototype.preExecute = function(context) {

      var source = context.source;

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

      var target = context.target || source.parent,
          shape = context.shape;

      shape = context.shape =
        this._modeling.createShape(
          shape,
          context.position,
          target, { attach: context.attach });

      context.shape = shape;
    };

    AppendShapeHandler.prototype.postExecute = function(context) {
      var parent = context.connectionParent || context.shape.parent;

      Eif (!existsConnection(context.source, context.shape)) {

        // create connection
        this._modeling.connect(context.source, context.shape, context.connection, parent);
      }
    };

    function existsConnection(source, target) {
      return some(source.outgoing, function(c) {
        return c.target === target;
      });
    }