# all files / bpmn-js/lib/util/ LabelUtil.js

100% Statements 34/34
100% Branches 21/21
100% Functions 7/7
100% Lines 34/34

    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

    1×

    1×

    17256×

    2106×

    33×

    33×
    33×

    33×

    33×

    33×
    33×

    33×
    26×

    7×

    33×

    33×

    33×
    33×

    33×

    554×
    33×
    521×
    47×

    474×

    2731×
    2731×

    2731×
    2228×

    2228×

    2228×

    503×

    503×

    2731×

    4993×

     import {
      assign
    } from 'min-dash';

    import { is } from './ModelUtil';

    export var DEFAULT_LABEL_SIZE = {
      width: 90,
      height: 20
    };

    export var FLOW_LABEL_INDENT = 15;

    /**
     * Returns true if the given semantic has an external label
     *
     * @param {BpmnElement} semantic
     * @return {Boolean} true if has label
     */
    export function isLabelExternal(semantic) {
      return is(semantic, 'bpmn:Event') ||
             is(semantic, 'bpmn:Gateway') ||
             is(semantic, 'bpmn:DataStoreReference') ||
             is(semantic, 'bpmn:DataObjectReference') ||
             is(semantic, 'bpmn:DataInput') ||
             is(semantic, 'bpmn:DataOutput') ||
             is(semantic, 'bpmn:SequenceFlow') ||
             is(semantic, 'bpmn:MessageFlow') ||
             is(semantic, 'bpmn:Group');
    }

    /**
     * Returns true if the given element has an external label
     *
     * @param {djs.model.shape} element
     * @return {Boolean} true if has label
     */
    export function hasExternalLabel(element) {
      return isLabel(element.label);
    }

    /**
     * Get the position for sequence flow labels
     *
     * @param  {Array<Point>} waypoints
     * @return {Point} the label position
     */
    export function getFlowLabelPosition(waypoints) {

      // get the waypoints mid
      var mid = waypoints.length / 2 - 1;

      var first = waypoints[Math.floor(mid)];
      var second = waypoints[Math.ceil(mid + 0.01)];

      // get position
      var position = getWaypointsMid(waypoints);

      // calculate angle
      var angle = Math.atan((second.y - first.y) / (second.x - first.x));

      var x = position.x,
          y = position.y;

      if (Math.abs(angle) < Math.PI / 2) {
        y -= FLOW_LABEL_INDENT;
      } else {
        x += FLOW_LABEL_INDENT;
      }

      return { x: x, y: y };
    }

    /**
     * Get the middle of a number of waypoints
     *
     * @param  {Array<Point>} waypoints
     * @return {Point} the mid point
     */
    export function getWaypointsMid(waypoints) {

      var mid = waypoints.length / 2 - 1;

      var first = waypoints[Math.floor(mid)];
      var second = waypoints[Math.ceil(mid + 0.01)];

      return {
        x: first.x + (second.x - first.x) / 2,
        y: first.y + (second.y - first.y) / 2
      };
    }

    export function getExternalLabelMid(element) {

      if (element.waypoints) {
        return getFlowLabelPosition(element.waypoints);
      } else if (is(element, 'bpmn:Group')) {
        return {
          x: element.x + element.width / 2,
          y: element.y + DEFAULT_LABEL_SIZE.height / 2
        };
      } else {
        return {
          x: element.x + element.width / 2,
          y: element.y + element.height + DEFAULT_LABEL_SIZE.height / 2
        };
      }
    }

    /**
     * Returns the bounds of an elements label, parsed from the elements DI or
     * generated from its bounds.
     *
     * @param {BpmnElement} semantic
     * @param {djs.model.Base} element
     */
    export function getExternalLabelBounds(semantic, element) {

      var mid,
          size,
          bounds,
          di = semantic.di,
          label = di.label;

      if (label && label.bounds) {
        bounds = label.bounds;

        size = {
          width: Math.max(DEFAULT_LABEL_SIZE.width, bounds.width),
          height: bounds.height
        };

        mid = {
          x: bounds.x + bounds.width / 2,
          y: bounds.y + bounds.height / 2
        };
      } else {

        mid = getExternalLabelMid(element);

        size = DEFAULT_LABEL_SIZE;
      }

      return assign({
        x: mid.x - size.width / 2,
        y: mid.y - size.height / 2
      }, size);
    }

    export function isLabel(element) {
      return element && !!element.labelTarget;
    }