# all files / diagram-js/lib/features/label-support/ LabelSupport.js

98.25% Statements 56/57
87.5% Branches 14/16
100% Functions 15/15
98.25% Lines 56/57

    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

    1×
    1×

    1189×

    1189×

    1189×

    52×
    52×
    52×

    52×
    52×

    1189×

    52×
    52×

    52×

    52×

    65×

    3×
    3×

    3×

    52×
    3×

    1189×
    414×
    414×
    414×

    414×

    414×
    720×

    111×
    50×

    414×

    1189×

    481×
    481×

    481×
    50×

    1189×

    388×
    388×
    388×

    388×
    61×
    61×

    61×

    1189×

    82×
    82×
    82×
    82×

    82×
    9×

    9×

    1×

    1×

    104×

    129×

     import {
      forEach,
      filter
    } from 'min-dash';

    import inherits from 'inherits';

    var LOW_PRIORITY = 250,
        HIGH_PRIORITY = 1400;

    import {
      add as collectionAdd,
      indexOf as collectionIdx
    } from '../../util/Collections';

    import { saveClear } from '../../util/Removal';

    import CommandInterceptor from '../../command/CommandInterceptor';

    /**
     * A handler that makes sure labels are properly moved with
     * their label targets.
     *
     * @param {didi.Injector} injector
     * @param {EventBus} eventBus
     * @param {Modeling} modeling
     */
    export default function LabelSupport(injector, eventBus, modeling) {

      CommandInterceptor.call(this, eventBus);

      var movePreview = injector.get('movePreview', false);

      // remove labels from the collection that are being
      // moved with other elements anyway
      eventBus.on('shape.move.start', HIGH_PRIORITY, function(e) {

        var context = e.context,
            shapes = context.shapes,
            validatedShapes = context.validatedShapes;

        context.shapes = removeLabels(shapes);
        context.validatedShapes = removeLabels(validatedShapes);
      });

      // add labels to visual's group
      movePreview && eventBus.on('shape.move.start', LOW_PRIORITY, function(e) {

        var context = e.context,
            shapes = context.shapes;

        var labels = [];

        forEach(shapes, function(element) {

          forEach(element.labels, function(label) {

            Eif (!label.hidden && context.shapes.indexOf(label) === -1) {
              labels.push(label);
            }

            Iif (element.labelTarget) {
              labels.push(element);
            }
          });
        });

        forEach(labels, function(label) {
          movePreview.makeDraggable(context, label, true);
        });

      });

      // add all labels to move closure
      this.preExecuted('elements.move', HIGH_PRIORITY, function(e) {
        var context = e.context,
            closure = context.closure,
            enclosedElements = closure.enclosedElements;

        var enclosedLabels = [];

        // find labels that are not part of
        // move closure yet and add them
        forEach(enclosedElements, function(element) {
          forEach(element.labels, function(label) {

            if (!enclosedElements[label.id]) {
              enclosedLabels.push(label);
            }
          });
        });

        closure.addAll(enclosedLabels);
      });

      this.preExecute([
        'connection.delete',
        'shape.delete'
      ], function(e) {

        var context = e.context,
            element = context.connection || context.shape;

        saveClear(element.labels, function(label) {
          modeling.removeShape(label, { nested: true });
        });
      });

      this.execute('shape.delete', function(e) {

        var context = e.context,
            shape = context.shape,
            labelTarget = shape.labelTarget;

        // unset labelTarget
        if (labelTarget) {
          context.labelTargetIndex = collectionIdx(labelTarget.labels, shape);
          context.labelTarget = labelTarget;

          shape.labelTarget = null;
        }
      });

      this.revert('shape.delete', function(e) {

        var context = e.context,
            shape = context.shape,
            labelTarget = context.labelTarget,
            labelTargetIndex = context.labelTargetIndex;

        // restore labelTarget
        if (labelTarget) {
          collectionAdd(labelTarget.labels, shape, labelTargetIndex);

          shape.labelTarget = labelTarget;
        }
      });

    }

    inherits(LabelSupport, CommandInterceptor);

    LabelSupport.$inject = [
      'injector',
      'eventBus',
      'modeling'
    ];

    /**
     * Return a filtered list of elements that do not
     * contain attached elements with hosts being part
     * of the selection.
     *
     * @param  {Array<djs.model.Base>} elements
     *
     * @return {Array<djs.model.Base>} filtered
     */
    function removeLabels(elements) {

      return filter(elements, function(element) {

        // filter out labels that are move together
        // with their label targets
        return elements.indexOf(element.labelTarget) === -1;
      });
    }