# all files / Github/bpmn-js/lib/features/snapping/ BpmnConnectSnapping.js

98.55% Statements 68/69
98.08% Branches 51/52
100% Functions 14/14
98.55% Lines 68/69

    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
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207

    1×

    1×

    1×

    1×

    1×

    1×

    89×

    40×
    40×
    40×

    40×

    40×
    13×

    40×

    40×
    24×

    40×

    18×

    18×
    4×

    18×
    4×

    18×
    10×

    22×

    6×

    2×

    6×

    4×

    16×

    1×

    24×
    48×

    48×
    8×
    40×
    22×

    48×
    30×

    4×
    8×
    8×

    8×

    4×
    2×

    10×
    10×
    10×

    10×
    6×

    10×
    4×

    10×
    10×

    10×
    8×
    4×

    4×

    8×

    10×
    10×

    126×

    26×
    104×

    56×

    24×
    6×

    18×

     import {
      mid,
      setSnapped
    } from 'diagram-js/lib/features/snapping/SnapUtil';

    import { isCmd } from 'diagram-js/lib/features/keyboard/KeyboardUtil';

    import {
      getOrientation
    } from 'diagram-js/lib/layout/LayoutUtil';

    import { is } from '../../util/ModelUtil';

    import { some } from 'min-dash';

    import { isAny } from '../modeling/util/ModelingUtil';

    var HIGHER_PRIORITY = 1250;

    var BOUNDARY_TO_HOST_THRESHOLD = 40;

    var TARGET_BOUNDS_PADDING = 20;

    var TASK_CENTER_PADDING = 20;

    var AXES = [ 'x', 'y' ];

    var abs = Math.abs;

    /**
     * Snap during connect.
     *
     * @param {EventBus} eventBus
     * @param {Rules} rules
     */
    export default function BpmnConnectSnapping(eventBus, rules) {
      eventBus.on([
        'connect.hover',
        'connect.move',
        'connect.end',
      ], HIGHER_PRIORITY, function(event) {
        var context = event.context,
            source = context.source,
            target = context.target;

        Iif (event.originalEvent && isCmd(event.originalEvent)) {
          return;
        }

        if (!context.initialSourcePosition) {
          context.initialSourcePosition = context.sourcePosition;
        }

        var connectionAttrs = rules.allowed('connection.create', {
          source: source,
          target: target
        });

        if (target && connectionAttrs) {
          snapInsideTarget(event, target, getTargetBoundsPadding(target));
        }

        if (target && isAnyType(connectionAttrs, [
          'bpmn:Association',
          'bpmn:DataInputAssociation',
          'bpmn:DataOutputAssociation',
          'bpmn:SequenceFlow'
        ])) {

          // snap source
          context.sourcePosition = mid(source);

          if (isAny(target, ['bpmn:Event', 'bpmn:Gateway'])) {
            snapToPosition(event, mid(target));
          }

          if (is(target, 'bpmn:Task')) {
            snapTaskMid(event, target);
          }

          if (is(source, 'bpmn:BoundaryEvent') && target === source.host) {
            snapBoundaryEventLoop(event, source, target);
          }

        } else if (isType(connectionAttrs, 'bpmn:MessageFlow')) {

          if (is(source, 'bpmn:Event')) {

            // snap source
            context.sourcePosition = mid(source);
          }

          if (is(target, 'bpmn:Event')) {

            // snap target
            snapToPosition(event, mid(target));
          }

        } else {

          // un-snap source
          context.sourcePosition = context.initialSourcePosition;
        }
      });
    }

    BpmnConnectSnapping.$inject = [
      'eventBus',
      'rules'
    ];

    function snapInsideTarget(event, target, padding) {

      AXES.forEach(function(axis) {
        var matchingTargetDimension = getDimensionForAxis(axis, target),
            newCoordinate;

        if (event[axis] < target[axis] + padding) {
          newCoordinate = target[axis] + padding;
        } else if (event[axis] > target[axis] + matchingTargetDimension - padding) {
          newCoordinate = target[axis] + matchingTargetDimension - padding;
        }

        if (newCoordinate) {
          setSnapped(event, axis, newCoordinate);
        }
      });
    }

    /**
     * Snap to task target mid if event position in center area
     */
    function snapTaskMid(event, target) {

      var isCenter = AXES.every(function(axis) {
        var coordinate = event[axis],
            matchingTargetDimension = getDimensionForAxis(axis, target);

        return coordinate > target[axis] + TASK_CENTER_PADDING
          && coordinate < target[axis] + matchingTargetDimension - TASK_CENTER_PADDING;
      });

      if (isCenter) {
        snapToPosition(event, mid(target));
      }

    }

    // snap outside of Boundary Event surroundings
    function snapBoundaryEventLoop(event, source, target) {
      var sourceMid = mid(source),
          orientation = getOrientation(sourceMid, target, -10),
          snappingAxes = [];

      if (/top|bottom/.test(orientation)) {
        snappingAxes.push('x');
      }

      if (/left|right/.test(orientation)) {
        snappingAxes.push('y');
      }

      snappingAxes.forEach(function(axis) {
        var coordinate = event[axis], newCoordinate;

        if (abs(coordinate - sourceMid[axis]) < BOUNDARY_TO_HOST_THRESHOLD) {
          if (coordinate > sourceMid[axis]) {
            newCoordinate = sourceMid[axis] + BOUNDARY_TO_HOST_THRESHOLD;
          }
          else {
            newCoordinate = sourceMid[axis] - BOUNDARY_TO_HOST_THRESHOLD;
          }

          setSnapped(event, axis, newCoordinate);
        }
      });
    }

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

    function snapToPosition(event, position) {
      setSnapped(event, 'x', position.x);
      setSnapped(event, 'y', position.y);
    }

    function isType(attrs, type) {
      return attrs && attrs.type === type;
    }

    function isAnyType(attrs, types) {
      return some(types, function(type) {
        return isType(attrs, type);
      });
    }

    function getDimensionForAxis(axis, element) {
      return axis === 'x' ? element.width : element.height;
    }

    function getTargetBoundsPadding(target) {
      if (is(target, 'bpmn:Task')) {
        return 10;
      } else {
        return TARGET_BOUNDS_PADDING;
      }
    }