# all files / Github/bpmn-js/lib/features/modeling/behavior/ ImportDockingFix.js

100% Statements 19/19
100% Branches 10/10
100% Functions 5/5
100% Lines 19/19

    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

    6404×

    6404×

    6404×
    6404×

    6404×
    414×
    60×

    354×

    5990×

    6404×

    3202×

    3202×

    3202×

    1128×

    13625×

    13625×
    3202×

    1×

    828×
     import {
      getMid
    } from 'diagram-js/lib/layout/LayoutUtil';

    import lineIntersect from './util/LineIntersect';

    /**
     * Fix broken dockings after DI imports.
     *
     * @param {EventBus} eventBus
     */
    export default function ImportDockingFix(eventBus) {

      function adjustDocking(startPoint, nextPoint, elementMid) {

        var elementTop = {
          x: elementMid.x,
          y: elementMid.y - 50
        };

        var elementLeft = {
          x: elementMid.x - 50,
          y: elementMid.y
        };

        var verticalIntersect = lineIntersect(startPoint, nextPoint, elementMid, elementTop),
            horizontalIntersect = lineIntersect(startPoint, nextPoint, elementMid, elementLeft);

        // original is horizontal or vertical center cross intersection
        var centerIntersect;

        if (verticalIntersect && horizontalIntersect) {
          if (getDistance(verticalIntersect, elementMid) > getDistance(horizontalIntersect, elementMid)) {
            centerIntersect = horizontalIntersect;
          } else {
            centerIntersect = verticalIntersect;
          }
        } else {
          centerIntersect = verticalIntersect || horizontalIntersect;
        }

        startPoint.original = centerIntersect;
      }

      function fixDockings(connection) {
        var waypoints = connection.waypoints;

        adjustDocking(
          waypoints[0],
          waypoints[1],
          getMid(connection.source)
        );

        adjustDocking(
          waypoints[waypoints.length - 1],
          waypoints[waypoints.length - 2],
          getMid(connection.target)
        );
      }

      eventBus.on('bpmnElement.added', function(e) {

        var element = e.element;

        if (element.waypoints) {
          fixDockings(element);
        }
      });
    }

    ImportDockingFix.$inject = [
      'eventBus'
    ];

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

    function getDistance(p1, p2) {
      return Math.sqrt(Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2));
    }