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

100% Statements 44/44
100% Branches 30/30
100% Functions 9/9
100% Lines 44/44

    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×

    1128×

    1128×

    1128×

    22×

    22×
    4×

    85×

    85×
    41×

    85×
    34×

    34×

    85×

    32×

    32×
    10×

    22×
    18×

    73×
    73×
    73×
    73×
    73×
    73×
    73×
    73×
    73×

    73×

    73×
    29×

    73×
    3×

    73×
    53×
    53×

    20×

    1×

    1×

    2256×
    759×
    759×

    759×
    73×

     import inherits from 'inherits';

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

    import {
      add as collectionAdd,
      remove as collectionRemove
    } from 'diagram-js/lib/util/Collections';

    import {
      find
    } from 'min-dash';

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

    var TARGET_REF_PLACEHOLDER_NAME = '__targetRef_placeholder';

    /**
     * This behavior makes sure we always set a fake
     * DataInputAssociation#targetRef as demanded by the BPMN 2.0
     * XSD schema.
     *
     * The reference is set to a bpmn:Property{ name: '__targetRef_placeholder' }
     * which is created on the fly and cleaned up afterwards if not needed
     * anymore.
     *
     * @param {EventBus} eventBus
     * @param {BpmnFactory} bpmnFactory
     */
    export default function DataInputAssociationBehavior(eventBus, bpmnFactory) {

      CommandInterceptor.call(this, eventBus);

      this.executed([
        'connection.create',
        'connection.delete',
        'connection.move',
        'connection.reconnectEnd'
      ], ifDataInputAssociation(fixTargetRef));

      this.reverted([
        'connection.create',
        'connection.delete',
        'connection.move',
        'connection.reconnectEnd'
      ], ifDataInputAssociation(fixTargetRef));

      function usesTargetRef(element, targetRef, removedConnection) {

        var inputAssociations = element.get('dataInputAssociations');

        return find(inputAssociations, function(association) {
          return association !== removedConnection &&
                 association.targetRef === targetRef;
        });
      }

      function getTargetRef(element, create) {

        var properties = element.get('properties');

        var targetRefProp = find(properties, function(p) {
          return p.name === TARGET_REF_PLACEHOLDER_NAME;
        });

        if (!targetRefProp && create) {
          targetRefProp = bpmnFactory.create('bpmn:Property', {
            name: TARGET_REF_PLACEHOLDER_NAME
          });

          collectionAdd(properties, targetRefProp);
        }

        return targetRefProp;
      }

      function cleanupTargetRef(element, connection) {

        var targetRefProp = getTargetRef(element);

        if (!targetRefProp) {
          return;
        }

        if (!usesTargetRef(element, targetRefProp, connection)) {
          collectionRemove(element.get('properties'), targetRefProp);
        }
      }

      /**
       * Make sure targetRef is set to a valid property or
       * `null` if the connection is detached.
       *
       * @param {Event} event
       */
      function fixTargetRef(event) {

        var context = event.context,
            connection = context.connection,
            connectionBo = connection.businessObject,
            target = connection.target,
            targetBo = target && target.businessObject,
            newTarget = context.newTarget,
            newTargetBo = newTarget && newTarget.businessObject,
            oldTarget = context.oldTarget || context.target,
            oldTargetBo = oldTarget && oldTarget.businessObject;

        var dataAssociation = connection.businessObject,
            targetRefProp;

        if (oldTargetBo && oldTargetBo !== targetBo) {
          cleanupTargetRef(oldTargetBo, connectionBo);
        }

        if (newTargetBo && newTargetBo !== targetBo) {
          cleanupTargetRef(newTargetBo, connectionBo);
        }

        if (targetBo) {
          targetRefProp = getTargetRef(targetBo, true);
          dataAssociation.targetRef = targetRefProp;
        } else {
          dataAssociation.targetRef = null;
        }
      }
    }

    DataInputAssociationBehavior.$inject = [
      'eventBus',
      'bpmnFactory'
    ];

    inherits(DataInputAssociationBehavior, CommandInterceptor);

    /**
     * Only call the given function when the event
     * touches a bpmn:DataInputAssociation.
     *
     * @param {Function} fn
     * @return {Function}
     */
    function ifDataInputAssociation(fn) {

      return function(event) {
        var context = event.context,
            connection = context.connection;

        if (is(connection, 'bpmn:DataInputAssociation')) {
          return fn(event);
        }
      };
    }