# all files / Github/diagram-js/lib/features/bendpoints/ BendpointUtil.js

100% Statements 54/54
100% Branches 8/8
100% Functions 7/7
100% Lines 54/54

    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

    1×
    1×

    10×
    10×

    10×

    10×

    10×

    10×
    10×

    10×

    245×
    245×

    245×

    245×
    245×

    245×

    245×

    245×
    245×

    245×

    245×

    245×
    52×

    245×

    114×

    114×

    114×
    114×
    114×
    114×
    114×

    114×
    114×

    114×

    114×

    114×
    114×

    114×

    114×

    114×

    114×

    114×
    114×
    114×

    114×

    114×

    114×
    114×

    114×

    114×

    114×

    114×
    114×

    114×

     import {
      toPoint
    } from '../../util/Event';

    import {
      getMidPoint,
      pointsAligned
    } from '../../util/Geometry';

    import {
      append as svgAppend,
      attr as svgAttr,
      classes as svgClasses,
      create as svgCreate
    } from 'tiny-svg';

    import {
      rotate,
      translate
    } from '../../util/SvgTransformUtil';

    import {
      getApproxIntersection
    } from '../../util/LineIntersection';

    export var BENDPOINT_CLS = 'djs-bendpoint';
    export var SEGMENT_DRAGGER_CLS = 'djs-segment-dragger';

    export function toCanvasCoordinates(canvas, event) {

      var position = toPoint(event),
          clientRect = canvas._container.getBoundingClientRect(),
          offset;

      // canvas relative position

      offset = {
        x: clientRect.left,
        y: clientRect.top
      };

      // update actual event payload with canvas relative measures

      var viewbox = canvas.viewbox();

      return {
        x: viewbox.x + (position.x - offset.x) / viewbox.scale,
        y: viewbox.y + (position.y - offset.y) / viewbox.scale
      };
    }

    export function getConnectionIntersection(canvas, waypoints, event) {
      var localPosition = toCanvasCoordinates(canvas, event),
          intersection = getApproxIntersection(waypoints, localPosition);

      return intersection;
    }

    export function addBendpoint(parentGfx, cls) {
      var groupGfx = svgCreate('g');
      svgClasses(groupGfx).add(BENDPOINT_CLS);

      svgAppend(parentGfx, groupGfx);

      var visual = svgCreate('circle');
      svgAttr(visual, {
        cx: 0,
        cy: 0,
        r: 4
      });
      svgClasses(visual).add('djs-visual');

      svgAppend(groupGfx, visual);

      var hit = svgCreate('circle');
      svgAttr(hit, {
        cx: 0,
        cy: 0,
        r: 10
      });
      svgClasses(hit).add('djs-hit');

      svgAppend(groupGfx, hit);

      if (cls) {
        svgClasses(groupGfx).add(cls);
      }

      return groupGfx;
    }

    function createParallelDragger(parentGfx, segmentStart, segmentEnd, alignment) {
      var draggerGfx = svgCreate('g');

      svgAppend(parentGfx, draggerGfx);

      var width = 14,
          height = 3,
          padding = 6,
          hitWidth = calculateHitWidth(segmentStart, segmentEnd, alignment, padding),
          hitHeight = height + padding;

      var visual = svgCreate('rect');
      svgAttr(visual, {
        x: -width / 2,
        y: -height / 2,
        width: width,
        height: height
      });
      svgClasses(visual).add('djs-visual');

      svgAppend(draggerGfx, visual);

      var hit = svgCreate('rect');
      svgAttr(hit, {
        x: -hitWidth / 2,
        y: -hitHeight / 2,
        width: hitWidth,
        height: hitHeight
      });
      svgClasses(hit).add('djs-hit');

      svgAppend(draggerGfx, hit);

      rotate(draggerGfx, alignment === 'v' ? 90 : 0, 0, 0);

      return draggerGfx;
    }

    export function addSegmentDragger(parentGfx, segmentStart, segmentEnd) {

      var groupGfx = svgCreate('g'),
          mid = getMidPoint(segmentStart, segmentEnd),
          alignment = pointsAligned(segmentStart, segmentEnd);

      svgAppend(parentGfx, groupGfx);

      createParallelDragger(groupGfx, segmentStart, segmentEnd, alignment);

      svgClasses(groupGfx).add(SEGMENT_DRAGGER_CLS);
      svgClasses(groupGfx).add(alignment === 'h' ? 'horizontal' : 'vertical');

      translate(groupGfx, mid.x, mid.y);

      return groupGfx;
    }

    /**
     * Calculates region for segment move which is 2/3 of the full segment length
     * @param {Number} segmentLength
     *
     * @return {Number}
     */
    export function calculateSegmentMoveRegion(segmentLength) {
      return Math.abs(Math.round(segmentLength * 2 / 3));
    }

    // helper //////////

    function calculateHitWidth(segmentStart, segmentEnd, alignment) {
      var segmentLengthXAxis = segmentEnd.x - segmentStart.x,
          segmentLengthYAxis = segmentEnd.y - segmentStart.y;

      return alignment === 'h' ?
        calculateSegmentMoveRegion(segmentLengthXAxis) :
        calculateSegmentMoveRegion(segmentLengthYAxis);
    }