# all files / bpmn-js/lib/features/grid-snapping/behavior/ LayoutConnectionBehavior.js

100% Statements 37/37
93.75% Branches 15/16
100% Functions 7/7
100% Lines 37/37

    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
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144

    1×

    75×

    75×

    75×

    75×

    33×
    33×
    33×
    33×

    33×
    10×

    23×
    14×

    9×

    1×

    1×

    1×
    9×

    9×

    9×

    10×

    10×
    10×

    9×

    23×

    10×

    10×

    10×

    10×

    10×

    1×

    10×

    9×

    10×
    10×
    10×

    10×
     import inherits from 'inherits';

    import CommandInterceptor from 'diagram-js/lib/command/CommandInterceptor';

    import { pointsAligned } from 'diagram-js/lib/util/Geometry';

    import {
      assign
    } from 'min-dash';

    var HIGH_PRIORITY = 3000;

    /**
     * Snaps connections with Manhattan layout.
     */
    export default function LayoutConnectionBehavior(eventBus, gridSnapping, modeling) {
      CommandInterceptor.call(this, eventBus);

      this._gridSnapping = gridSnapping;

      var self = this;

      this.postExecuted([
        'connection.create',
        'connection.layout'
      ], HIGH_PRIORITY, function(event) {
        var context = event.context,
            connection = context.connection,
            hints = context.hints || {},
            waypoints = connection.waypoints;

        if (hints.connectionStart || hints.connectionEnd) {
          return;
        }

        if (!hasMiddleSegments(waypoints)) {
          return;
        }

        modeling.updateWaypoints(connection, self.snapMiddleSegments(waypoints));
      });
    }

    LayoutConnectionBehavior.$inject = [
      'eventBus',
      'gridSnapping',
      'modeling'
    ];

    inherits(LayoutConnectionBehavior, CommandInterceptor);

    /**
     * Snap middle segments of a given connection.
     *
     * @param {Array<Point>} waypoints
     *
     * @returns {Array<Point>}
     */
    LayoutConnectionBehavior.prototype.snapMiddleSegments = function(waypoints) {
      var gridSnapping = this._gridSnapping,
          snapped;

      waypoints = waypoints.slice();

      for (var i = 1; i < waypoints.length - 2; i++) {

        snapped = snapSegment(gridSnapping, waypoints[i], waypoints[i + 1]);

        waypoints[i] = snapped[0];
        waypoints[i + 1] = snapped[1];
      }

      return waypoints;
    };

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

    /**
     * Check wether a connection has a middle segments.
     *
     * @param {Array} waypoints
     *
     * @returns {boolean}
     */
    function hasMiddleSegments(waypoints) {
      return waypoints.length > 3;
    }

    /**
     * Check wether an alignment is horizontal.
     *
     * @param {string} aligned
     *
     * @returns {boolean}
     */
    function horizontallyAligned(aligned) {
      return aligned === 'h';
    }

    /**
     * Check wether an alignment is vertical.
     *
     * @param {string} aligned
     *
     * @returns {boolean}
     */
    function verticallyAligned(aligned) {
      return aligned === 'v';
    }

    /**
     * Get middle segments from a given connection.
     *
     * @param {Array} waypoints
     *
     * @returns {Array}
     */
    function snapSegment(gridSnapping, segmentStart, segmentEnd) {

      var aligned = pointsAligned(segmentStart, segmentEnd);

      var snapped = {};

      if (horizontallyAligned(aligned)) {

        // snap horizontally
        snapped.y = gridSnapping.snapValue(segmentStart.y);
      }

      if (verticallyAligned(aligned)) {

        // snap vertically
        snapped.x = gridSnapping.snapValue(segmentStart.x);
      }

      Eif ('x' in snapped || 'y' in snapped) {
        segmentStart = assign({}, segmentStart, snapped);
        segmentEnd = assign({}, segmentEnd, snapped);
      }

      return [ segmentStart, segmentEnd ];
    }