# all files / bpmn-js/lib/features/label-editing/ LabelEditingPreview.js

100% Statements 40/40
96% Branches 24/25
100% Functions 5/5
100% Lines 40/40

    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

    1×
    1×

    143×

    143×

    143×
    90×

    90×

    90×
    7×

    7×

    7×

    7×

    7×

    7×

    7×

    7×

    90×

    53×
    37×

    28×

    143×

    2×
    2×
    2×

    2×

    2×

    2×

    143×
    110×

    110×
    67×
    67×

    110×
    110×

    110×
    2×

    2×

    1×

    7×

    7×
     import {
      append as svgAppend,
      attr as svgAttr,
      create as svgCreate,
      remove as svgRemove
    } from 'tiny-svg';

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

    import {
      translate
    } from 'diagram-js/lib/util/SvgTransformUtil';

    var MARKER_HIDDEN = 'djs-element-hidden',
        MARKER_LABEL_HIDDEN = 'djs-label-hidden';

    export default function LabelEditingPreview(
        eventBus, canvas, elementRegistry,
        pathMap) {

      var self = this;

      var defaultLayer = canvas.getDefaultLayer();

      var element, absoluteElementBBox, gfx;

      eventBus.on('directEditing.activate', function(context) {
        var activeProvider = context.active;

        element = activeProvider.element.label || activeProvider.element;

        // text annotation
        if (is(element, 'bpmn:TextAnnotation')) {
          absoluteElementBBox = canvas.getAbsoluteBBox(element);

          gfx = svgCreate('g');

          var textPathData = pathMap.getScaledPath('TEXT_ANNOTATION', {
            xScaleFactor: 1,
            yScaleFactor: 1,
            containerWidth: element.width,
            containerHeight: element.height,
            position: {
              mx: 0.0,
              my: 0.0
            }
          });

          var path = self.path = svgCreate('path');

          svgAttr(path, {
            d: textPathData,
            strokeWidth: 2,
            stroke: getStrokeColor(element)
          });

          svgAppend(gfx, path);

          svgAppend(defaultLayer, gfx);

          translate(gfx, element.x, element.y);
        }

        if (is(element, 'bpmn:TextAnnotation') ||
            element.labelTarget) {
          canvas.addMarker(element, MARKER_HIDDEN);
        } else if (is(element, 'bpmn:Task') ||
                   is(element, 'bpmn:CallActivity') ||
                   is(element, 'bpmn:SubProcess') ||
                   is(element, 'bpmn:Participant')) {
          canvas.addMarker(element, MARKER_LABEL_HIDDEN);
        }
      });

      eventBus.on('directEditing.resize', function(context) {

        // text annotation
        Eif (is(element, 'bpmn:TextAnnotation')) {
          var height = context.height,
              dy = context.dy;

          var newElementHeight = Math.max(element.height / absoluteElementBBox.height * (height + dy), 0);

          var textPathData = pathMap.getScaledPath('TEXT_ANNOTATION', {
            xScaleFactor: 1,
            yScaleFactor: 1,
            containerWidth: element.width,
            containerHeight: newElementHeight,
            position: {
              mx: 0.0,
              my: 0.0
            }
          });

          svgAttr(self.path, {
            d: textPathData
          });
        }
      });

      eventBus.on([ 'directEditing.complete', 'directEditing.cancel' ], function(context) {
        var activeProvider = context.active;

        if (activeProvider) {
          canvas.removeMarker(activeProvider.element.label || activeProvider.element, MARKER_HIDDEN);
          canvas.removeMarker(element, MARKER_LABEL_HIDDEN);
        }

        element = undefined;
        absoluteElementBBox = undefined;

        if (gfx) {
          svgRemove(gfx);

          gfx = undefined;
        }
      });
    }

    LabelEditingPreview.$inject = [
      'eventBus',
      'canvas',
      'elementRegistry',
      'pathMap'
    ];

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

    function getStrokeColor(element, defaultColor) {
      var bo = getBusinessObject(element);

      return bo.di.get('stroke') || defaultColor || 'black';
    }