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

93.94% Statements 31/33
63.64% Branches 14/22
100% Functions 7/7
93.94% Lines 31/33

    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
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119

    1×

    1189×

    1×

    1×
    48×
    48×
    48×
    48×
    48×

    48×

    48×
    169×

    169×

    48×

    48×
    169×
    30×
    64×

    169×

    48×

    48×

    48×
    169×
    30×

    30×

    139×

    139×
    77×

    139×

    48×

    338×
     import {
      assign,
      forEach,
      isNumber,
      map,
      pick,
      values
    } from 'min-dash';

    import {
      getBBox,
      getParents
    } from '../../../util/Elements';

    var round = Math.round;

    export default function CreateElementsHandler(modeling) {
      this._modeling = modeling;
    }

    CreateElementsHandler.$inject = [
      'modeling'
    ];

    CreateElementsHandler.prototype.preExecute = function(context) {
      var elements = context.elements,
          parent = context.parent,
          parentIndex = context.parentIndex,
          position = context.position,
          hints = context.hints;

      var modeling = this._modeling;

      // make sure each element has x and y
      forEach(elements, function(element) {
        Iif (!isNumber(element.x)) {
          element.x = 0;
        }

        Iif (!isNumber(element.y)) {
          element.y = 0;
        }
      });

      var bbox = getBBox(elements);

      // center elements around position
      forEach(elements, function(element) {
        if (isConnection(element)) {
          element.waypoints = map(element.waypoints, function(waypoint) {
            return {
              x: round(waypoint.x - bbox.x - bbox.width / 2 + position.x),
              y: round(waypoint.y - bbox.y - bbox.height / 2 + position.y)
            };
          });
        }

        assign(element, {
          x: round(element.x - bbox.x - bbox.width / 2 + position.x),
          y: round(element.y - bbox.y - bbox.height / 2 + position.y)
        });
      });

      var parents = getParents(elements);

      var cache = {};

      forEach(elements, function(element) {
        if (isConnection(element)) {
          cache[ element.id ] = isNumber(parentIndex) ?
            modeling.createConnection(
              cache[ element.source.id ],
              cache[ element.target.id ],
              parentIndex,
              element,
              element.parent || parent,
              hints
            ) :
            modeling.createConnection(
              cache[ element.source.id ],
              cache[ element.target.id ],
              element,
              element.parent || parent,
              hints
            );

          return;
        }

        var createShapeHints = assign({}, hints);

        if (parents.indexOf(element) === -1) {
          createShapeHints.autoResize = false;
        }

        cache[ element.id ] = isNumber(parentIndex) ?
          modeling.createShape(
            element,
            pick(element, [ 'x', 'y', 'width', 'height' ]),
            element.parent || parent,
            parentIndex,
            createShapeHints
          ) :
          modeling.createShape(
            element,
            pick(element, [ 'x', 'y', 'width', 'height' ]),
            element.parent || parent,
            createShapeHints
          );
      });

      context.elements = values(cache);
    };

    // helpers //////////

    function isConnection(element) {
      return !!element.waypoints;
    }