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

93.15% Statements 68/73
82.69% Branches 43/52
100% Functions 19/19
93.15% Lines 68/73

    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

    1189×

    1189×

    10×
    38×

    62×
    156×

    15×

    15×

    15×
    12×

    12×

    1189×

    420×
    420×

    420×

    11×

    11×

    1189×
    414×
    414×

    414×
    525×

    414×
    12×

    12×
    45×

    1189×
    420×
    420×
    420×

    420×

    1×

    1189×
    828×
    828×
    828×
    828×

    828×

    85×

    743×

    2×

    2×

    1189×
    333×
    333×
    333×

    333×

    31×

    9×

    9×

    1189×
    31×
    31×
    31×

    31×

    31×

    6×
    3×

    1×

    1×

    9×
    9×

    9×
    9×
    9×

    1×
    1×
    1×

     import inherits from 'inherits';

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

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

    import { isAny } from '../util/ModelingUtil';

    import UpdateSemanticParentHandler from '../cmd/UpdateSemanticParentHandler';

    /**
     * BPMN specific data store behavior
     */
    export default function DataStoreBehavior(
        canvas, commandStack, elementRegistry,
        eventBus) {

      CommandInterceptor.call(this, eventBus);

      commandStack.registerHandler('dataStore.updateContainment', UpdateSemanticParentHandler);

      function getFirstParticipant() {
        return elementRegistry.filter(function(element) {
          return is(element, 'bpmn:Participant');
        })[0];
      }

      function getDataStores(element) {
        return element.children.filter(function(child) {
          return is(child, 'bpmn:DataStoreReference') && !child.labelTarget;
        });
      }

      function updateDataStoreParent(dataStore, newDataStoreParent) {
        var dataStoreBo = dataStore.businessObject || dataStore;

        newDataStoreParent = newDataStoreParent || getFirstParticipant();

        if (newDataStoreParent) {
          var newDataStoreParentBo = newDataStoreParent.businessObject || newDataStoreParent;

          commandStack.execute('dataStore.updateContainment', {
            dataStoreBo: dataStoreBo,
            newSemanticParent: newDataStoreParentBo.processRef || newDataStoreParentBo,
            newDiParent: newDataStoreParentBo.di
          });
        }
      }

      // disable auto-resize for data stores
      this.preExecute('shape.create', function(event) {

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

        if (is(shape, 'bpmn:DataStoreReference') &&
            shape.type !== 'label') {

          Iif (!context.hints) {
            context.hints = {};
          }

          // prevent auto resizing
          context.hints.autoResize = false;
        }
      });

      // disable auto-resize for data stores
      this.preExecute('elements.move', function(event) {
        var context = event.context,
            shapes = context.shapes;

        var dataStoreReferences = shapes.filter(function(shape) {
          return is(shape, 'bpmn:DataStoreReference');
        });

        if (dataStoreReferences.length) {
          Iif (!context.hints) {
            context.hints = {};
          }

          // prevent auto resizing for data store references
          context.hints.autoResize = shapes.filter(function(shape) {
            return !is(shape, 'bpmn:DataStoreReference');
          });
        }
      });

      // update parent on data store created
      this.postExecute('shape.create', function(event) {
        var context = event.context,
            shape = context.shape,
            parent = shape.parent;

        if (is(shape, 'bpmn:DataStoreReference') &&
            shape.type !== 'label' &&
            is(parent, 'bpmn:Collaboration')) {

          updateDataStoreParent(shape);
        }
      });

      // update parent on data store moved
      this.postExecute('shape.move', function(event) {
        var context = event.context,
            shape = context.shape,
            oldParent = context.oldParent,
            parent = shape.parent;

        if (is(oldParent, 'bpmn:Collaboration')) {

          // do nothing if not necessary
          return;
        }

        if (is(shape, 'bpmn:DataStoreReference') &&
            shape.type !== 'label' &&
            is(parent, 'bpmn:Collaboration')) {

          var participant = is(oldParent, 'bpmn:Participant') ?
            oldParent :
            getAncestor(oldParent, 'bpmn:Participant');

          updateDataStoreParent(shape, participant);
        }
      });

      // update data store parents on participant or subprocess deleted
      this.postExecute('shape.delete', function(event) {
        var context = event.context,
            shape = context.shape,
            rootElement = canvas.getRootElement();

        if (isAny(shape, [ 'bpmn:Participant', 'bpmn:SubProcess' ])
            && is(rootElement, 'bpmn:Collaboration')) {
          getDataStores(rootElement)
            .filter(function(dataStore) {
              return isDescendant(dataStore, shape);
            })
            .forEach(function(dataStore) {
              updateDataStoreParent(dataStore);
            });
        }
      });

      // update data store parents on collaboration -> process
      this.postExecute('canvas.updateRoot', function(event) {
        var context = event.context,
            oldRoot = context.oldRoot,
            newRoot = context.newRoot;

        var dataStores = getDataStores(oldRoot);

        dataStores.forEach(function(dataStore) {

          if (is(newRoot, 'bpmn:Process')) {
            updateDataStoreParent(dataStore, newRoot);
          }

        });
      });
    }

    DataStoreBehavior.$inject = [
      'canvas',
      'commandStack',
      'elementRegistry',
      'eventBus',
    ];

    inherits(DataStoreBehavior, CommandInterceptor);

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

    function isDescendant(descendant, ancestor) {
      var descendantBo = descendant.businessObject || descendant,
          ancestorBo = ancestor.businessObject || ancestor;

      while (descendantBo.$parent) {
        Eif (descendantBo.$parent === ancestorBo.processRef || ancestorBo) {
          return true;
        }

        descendantBo = descendantBo.$parent;
      }

      return false;
    }

    function getAncestor(element, type) {

      while (element.parent) {
        Eif (is(element.parent, type)) {
          return element.parent;
        }

        element = element.parent;
      }
    }