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

95.56% Statements 43/45
85.71% Branches 12/14
100% Functions 12/12
95.56% Lines 43/45

    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

    1×
    1×

    1128×

    2403×
    2403×

    2403×

    1893×

    1893×

    2403×

    2403×

    2403×
    1143×

    1143×

    2403×

    1128×

    1128×
    2403×

    1128×
    2403×

    1128×

    1893×
    1893×

    1893×

    1893×
    441×

    1452×
    171×

    1452×
    1006×

    1×

    1×

    1143×
    1143×

    1143×

    1143×
    171×

    1143×
    1006×

    1143×
    2403×

    1143×
    2403×

    2403×

     import inherits from 'inherits';

    import CommandInterceptor from 'diagram-js/lib/command/CommandInterceptor';

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

    var LOW_PRIORITY = 500,
        HIGH_PRIORITY = 5000;

    /**
     * BPMN specific delete lane behavior
     */
    export default function UpdateFlowNodeRefsBehavior(eventBus, modeling, translate) {

      CommandInterceptor.call(this, eventBus);

      /**
       * Ok, this is it:
       *
       * We have to update the Lane#flowNodeRefs _and_
       * FlowNode#lanes with every FlowNode move/resize and
       * Lane move/resize.
       *
       * We want to group that stuff to recompute containments
       * as efficient as possible.
       *
       * Yea!
       */

      // the update context
      var context;

      function initContext() {
        context = context || new UpdateContext();
        context.enter();

        return context;
      }

      function getContext() {
        Iif (!context) {
          throw new Error(translate('out of bounds release'));
        }

        return context;
      }

      function releaseContext() {

        Iif (!context) {
          throw new Error(translate('out of bounds release'));
        }

        var triggerUpdate = context.leave();

        if (triggerUpdate) {
          modeling.updateLaneRefs(context.flowNodes, context.lanes);

          context = null;
        }

        return triggerUpdate;
      }

      var laneRefUpdateEvents = [
        'spaceTool',
        'lane.add',
        'lane.resize',
        'lane.split',
        'elements.move',
        'elements.delete',
        'shape.create',
        'shape.delete',
        'shape.move',
        'shape.resize'
      ];

      // listen to a lot of stuff to group lane updates

      this.preExecute(laneRefUpdateEvents, HIGH_PRIORITY, function(event) {
        initContext();
      });

      this.postExecuted(laneRefUpdateEvents, LOW_PRIORITY, function(event) {
        releaseContext();
      });

      // Mark flow nodes + lanes that need an update

      this.preExecute([
        'shape.create',
        'shape.move',
        'shape.delete',
        'shape.resize'
      ], function(event) {

        var context = event.context,
            shape = context.shape;

        var updateContext = getContext();

        // no need to update labels
        if (shape.labelTarget) {
          return;
        }

        if (is(shape, 'bpmn:Lane')) {
          updateContext.addLane(shape);
        }

        if (is(shape, 'bpmn:FlowNode')) {
          updateContext.addFlowNode(shape);
        }
      });
    }

    UpdateFlowNodeRefsBehavior.$inject = [
      'eventBus',
      'modeling' ,
      'translate'
    ];

    inherits(UpdateFlowNodeRefsBehavior, CommandInterceptor);

    function UpdateContext() {

      this.flowNodes = [];
      this.lanes = [];

      this.counter = 0;

      this.addLane = function(lane) {
        this.lanes.push(lane);
      };

      this.addFlowNode = function(flowNode) {
        this.flowNodes.push(flowNode);
      };

      this.enter = function() {
        this.counter++;
      };

      this.leave = function() {
        this.counter--;

        return !this.counter;
      };
    }