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

95.95% Statements 71/74
88.89% Branches 48/54
100% Functions 7/7
95.95% Lines 71/74

    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
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223

    101×

    101×

    101×

    10×
    10×

    10×
    9×

    1×
    1×

    91×

    11×
    11×

    11×
    5×

    6×
    1×

    6×

    5×

    5×

    3×

    3×
    2×

    6×

    80×
    62×

    18×
    7×

    11×
    9×

    2×

    101×
    101×

    101×

    101×
    101×
    101×

    101×
    7×

    94×

    94×
    94×
    94×

    94×
    94×

    94×

    4×
    4×
    4×

    4×

    4×
    1×

    1×

    3×
    1×

    93×

    93×

    93×
    93×

    93×

    4×
    4×

    4×

    4×

    101×

    94×
    94×
    94×

    188×

    94×
    94×

    94×

     import {
      getDistancePointPoint,
      rotateVector,
      getAngle
    } from './GeometricUtil';

    import {
      getAttachment
    } from './LineAttachmentUtil';

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

    export function findNewLabelLineStartIndex(oldWaypoints, newWaypoints, attachment, hints) {

      var index = attachment.segmentIndex;

      var offset = newWaypoints.length - oldWaypoints.length;

      // segmentMove happend
      if (hints.segmentMove) {

        var oldSegmentStartIndex = hints.segmentMove.segmentStartIndex,
            newSegmentStartIndex = hints.segmentMove.newSegmentStartIndex;

        // if label was on moved segment return new segment index
        if (index === oldSegmentStartIndex) {
          return newSegmentStartIndex;
        }

        // label is after new segment index
        Eif (index >= newSegmentStartIndex) {
          return (index+offset < newSegmentStartIndex) ? newSegmentStartIndex : index+offset;
        }

        // if label is before new segment index
        return index;
      }

      // bendpointMove happend
      if (hints.bendpointMove) {

        var insert = hints.bendpointMove.insert,
            bendpointIndex = hints.bendpointMove.bendpointIndex,
            newIndex;

        // waypoints length didnt change
        if (offset === 0) {
          return index;
        }

        // label behind new/removed bendpoint
        if (index >= bendpointIndex) {
          newIndex = insert ? index + 1 : index - 1;
        }

        // label before new/removed bendpoint
        if (index < bendpointIndex) {

          newIndex = index;

          // decide label should take right or left segment
          if (insert && attachment.type !== 'bendpoint' && bendpointIndex-1 === index) {

            var rel = relativePositionMidWaypoint(newWaypoints, bendpointIndex);

            if (rel < attachment.relativeLocation) {
              newIndex++;
            }
          }
        }

        return newIndex;
      }

      // start/end changed
      if (offset === 0) {
        return index;
      }

      if (hints.connectionStart) {
        return (index === 0) ? 0 : null;
      }

      if (hints.connectionEnd) {
        return (index === oldWaypoints.length - 2) ? newWaypoints.length - 2 : null;
      }

      // if nothing fits, return null
      return null;
    }

    /**
     * Calculate the required adjustment (move delta) for the given label
     * after the connection waypoints got updated.
     *
     * @param {djs.model.Label} label
     * @param {Array<Point>} newWaypoints
     * @param {Array<Point>} oldWaypoints
     * @param {Object} hints
     *
     * @return {Point} delta
     */
    export function getLabelAdjustment(label, newWaypoints, oldWaypoints, hints) {

      var x = 0,
          y = 0;

      var labelPosition = getLabelMid(label);

      // get closest attachment
      var attachment = getAttachment(labelPosition, oldWaypoints),
          oldLabelLineIndex = attachment.segmentIndex,
          newLabelLineIndex = findNewLabelLineStartIndex(oldWaypoints, newWaypoints, attachment, hints);

      if (newLabelLineIndex === null) {
        return { x: x, y: y };
      }

      // should never happen
      // TODO(@janstuemmel): throw an error here when connectionSegmentMove is refactored
      Iif (newLabelLineIndex < 0 ||
          newLabelLineIndex > newWaypoints.length - 2) {
        return { x: x, y: y };
      }

      var oldLabelLine = getLine(oldWaypoints, oldLabelLineIndex),
          newLabelLine = getLine(newWaypoints, newLabelLineIndex),
          oldFoot = attachment.position;

      var relativeFootPosition = getRelativeFootPosition(oldLabelLine, oldFoot),
          angleDelta = getAngleDelta(oldLabelLine, newLabelLine);

      // special rule if label on bendpoint
      if (attachment.type === 'bendpoint') {

        var offset = newWaypoints.length - oldWaypoints.length,
            oldBendpointIndex = attachment.bendpointIndex,
            oldBendpoint = oldWaypoints[oldBendpointIndex];

        // bendpoint position hasnt changed, return same position
        Iif (newWaypoints.indexOf(oldBendpoint) !== -1) {
          return { x: x, y: y };
        }

        // new bendpoint and old bendpoint have same index, then just return the offset
        if (offset === 0) {
          var newBendpoint = newWaypoints[oldBendpointIndex];

          return {
            x: newBendpoint.x - attachment.position.x,
            y: newBendpoint.y - attachment.position.y
          };
        }

        // if bendpoints get removed
        if (offset < 0 && oldBendpointIndex !== 0 && oldBendpointIndex < oldWaypoints.length - 1) {
          relativeFootPosition = relativePositionMidWaypoint(oldWaypoints, oldBendpointIndex);
        }
      }

      var newFoot = {
        x: (newLabelLine[1].x - newLabelLine[0].x) * relativeFootPosition + newLabelLine[0].x,
        y: (newLabelLine[1].y - newLabelLine[0].y) * relativeFootPosition + newLabelLine[0].y
      };

      // the rotated vector to label
      var newLabelVector = rotateVector({
        x: labelPosition.x - oldFoot.x,
        y: labelPosition.y - oldFoot.y
      }, angleDelta);

      // the new relative position
      x = newFoot.x + newLabelVector.x - labelPosition.x;
      y = newFoot.y + newLabelVector.y - labelPosition.y;

      return roundPoint({
        x: x,
        y: y
      });
    }

    // HELPERS //////////////////////

    function relativePositionMidWaypoint(waypoints, idx) {

      var distanceSegment1 = getDistancePointPoint(waypoints[idx-1], waypoints[idx]),
          distanceSegment2 = getDistancePointPoint(waypoints[idx], waypoints[idx+1]);

      var relativePosition = distanceSegment1 / (distanceSegment1 + distanceSegment2);

      return relativePosition;
    }

    function getLabelMid(label) {
      return {
        x: label.x + label.width / 2,
        y: label.y + label.height / 2
      };
    }

    function getAngleDelta(l1, l2) {
      var a1 = getAngle(l1),
          a2 = getAngle(l2);
      return a2 - a1;
    }

    function getLine(waypoints, idx) {
      return [ waypoints[idx], waypoints[idx+1] ];
    }

    function getRelativeFootPosition(line, foot) {

      var length = getDistancePointPoint(line[0], line[1]),
          lengthToFoot = getDistancePointPoint(line[0], foot);

      return length === 0 ? 0 : lengthToFoot / length;
    }