# all files / bpmn-js/lib/features/modeling/cmd/ UpdatePropertiesHandler.js

98.72% Statements 77/78
92.86% Branches 26/28
100% Functions 15/15
98.72% Lines 77/78

    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
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234

    1×
    1×
    1×

    1×

    1189×
    1189×
    1189×
    1189×
    1189×

    1×

    1×

    338×
    338×
    338×

    338×

    338×
    338×

    338×
    338×
    338×

    338×
    145×

    145×

    145×

    338×

    37×
    25×

    37×
    16×

    338×

    338×
    338×

    338×

    1×
    317×
    317×

    317×

    317×
    241×

    76×

    76×

    1×

    66×
    66×
    66×
    66×
    66×
    66×

    66×

    66×
    20×

    20×

    20×

    66×

    404×

    317×

    317×

    319×
    252×

    67×

    319×

    67×
    91×

    91×

    404×

    407×
    304×

    103×
    103×

    103×
    145×

    1×

    338×

    338×
    338×
    37×

    338×
     import {
      reduce,
      keys,
      forEach,
      assign
    } from 'min-dash';

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

    var DEFAULT_FLOW = 'default',
        ID = 'id',
        DI = 'di';

    var NULL_DIMENSIONS = {
      width: 0,
      height: 0
    };

    /**
     * A handler that implements a BPMN 2.0 property update.
     *
     * This should be used to set simple properties on elements with
     * an underlying BPMN business object.
     *
     * Use respective diagram-js provided handlers if you would
     * like to perform automated modeling.
     */
    export default function UpdatePropertiesHandler(
        elementRegistry, moddle, translate,
        modeling, textRenderer) {

      this._elementRegistry = elementRegistry;
      this._moddle = moddle;
      this._translate = translate;
      this._modeling = modeling;
      this._textRenderer = textRenderer;
    }

    UpdatePropertiesHandler.$inject = [
      'elementRegistry',
      'moddle',
      'translate',
      'modeling',
      'textRenderer'
    ];

    // api //////////////////////

    /**
     * Updates a BPMN element with a list of new properties
     *
     * @param {Object} context
     * @param {djs.model.Base} context.element the element to update
     * @param {Object} context.properties a list of properties to set on the element's
     *                                    businessObject (the BPMN model element)
     *
     * @return {Array<djs.model.Base>} the updated element
     */
    UpdatePropertiesHandler.prototype.execute = function(context) {

      var element = context.element,
          changed = [ element ],
          translate = this._translate;

      Iif (!element) {
        throw new Error(translate('element required'));
      }

      var elementRegistry = this._elementRegistry,
          ids = this._moddle.ids;

      var businessObject = element.businessObject,
          properties = unwrapBusinessObjects(context.properties),
          oldProperties = context.oldProperties || getProperties(businessObject, properties);

      if (isIdChange(properties, businessObject)) {
        ids.unclaim(businessObject[ID]);

        elementRegistry.updateId(element, properties[ID]);

        ids.claim(properties[ID], businessObject);
      }

      // correctly indicate visual changes on default flow updates
      if (DEFAULT_FLOW in properties) {

        if (properties[DEFAULT_FLOW]) {
          changed.push(elementRegistry.get(properties[DEFAULT_FLOW].id));
        }

        if (businessObject[DEFAULT_FLOW]) {
          changed.push(elementRegistry.get(businessObject[DEFAULT_FLOW].id));
        }
      }

      // update properties
      setProperties(businessObject, properties);

      // store old values
      context.oldProperties = oldProperties;
      context.changed = changed;

      // indicate changed on objects affected by the update
      return changed;
    };

    UpdatePropertiesHandler.prototype.postExecute = function(context) {
      var element = context.element,
          label = element.label;

      var text = label && getBusinessObject(label).name;

      if (!text) {
        return;
      }

      // get layouted text bounds and resize external
      // external label accordingly
      var newLabelBounds = this._textRenderer.getExternalLabelBounds(label, text);

      this._modeling.resizeShape(label, newLabelBounds, NULL_DIMENSIONS);
    };

    /**
     * Reverts the update on a BPMN elements properties.
     *
     * @param  {Object} context
     *
     * @return {djs.model.Base} the updated element
     */
    UpdatePropertiesHandler.prototype.revert = function(context) {

      var element = context.element,
          properties = context.properties,
          oldProperties = context.oldProperties,
          businessObject = element.businessObject,
          elementRegistry = this._elementRegistry,
          ids = this._moddle.ids;

      // update properties
      setProperties(businessObject, oldProperties);

      if (isIdChange(properties, businessObject)) {
        ids.unclaim(properties[ID]);

        elementRegistry.updateId(element, oldProperties[ID]);

        ids.claim(oldProperties[ID], businessObject);
      }

      return context.changed;
    };

    function isIdChange(properties, businessObject) {
      return ID in properties && properties[ID] !== businessObject[ID];
    }

    function getProperties(businessObject, properties) {
      var propertyNames = keys(properties);

      return reduce(propertyNames, function(result, key) {

        // handle DI seperately
        if (key !== DI) {
          result[key] = businessObject.get(key);
        } else {
          result[key] = getDiProperties(businessObject.di, keys(properties.di));
        }

        return result;
      }, {});
    }

    function getDiProperties(di, propertyNames) {
      return reduce(propertyNames, function(result, key) {
        result[key] = di.get(key);

        return result;
      }, {});
    }

    function setProperties(businessObject, properties) {
      forEach(properties, function(value, key) {

        if (key !== DI) {
          businessObject.set(key, value);
        } else {
          // only update, if businessObject.di exists
          Eif (businessObject.di) {
            setDiProperties(businessObject.di, value);
          }
        }
      });
    }

    function setDiProperties(di, properties) {
      forEach(properties, function(value, key) {
        di.set(key, value);
      });
    }

    var referencePropertyNames = [ 'default' ];

    /**
     * Make sure we unwrap the actual business object
     * behind diagram element that may have been
     * passed as arguments.
     *
     * @param  {Object} properties
     *
     * @return {Object} unwrappedProps
     */
    function unwrapBusinessObjects(properties) {

      var unwrappedProps = assign({}, properties);

      referencePropertyNames.forEach(function(name) {
        if (name in properties) {
          unwrappedProps[name] = getBusinessObject(unwrappedProps[name]);
        }
      });

      return unwrappedProps;
    }