2018-04-02 19:01:53 +00:00
|
|
|
import {
|
|
|
|
reduce,
|
|
|
|
keys,
|
|
|
|
forEach,
|
|
|
|
assign
|
|
|
|
} from 'min-dash';
|
2015-12-08 12:29:03 +00:00
|
|
|
|
2018-04-02 19:01:53 +00:00
|
|
|
import {
|
2021-08-06 08:45:10 +00:00
|
|
|
getBusinessObject,
|
|
|
|
getDi
|
2018-04-02 19:01:53 +00:00
|
|
|
} from '../../../util/ModelUtil';
|
2015-01-02 15:15:18 +00:00
|
|
|
|
|
|
|
var DEFAULT_FLOW = 'default',
|
2016-11-28 13:26:42 +00:00
|
|
|
ID = 'id',
|
|
|
|
DI = 'di';
|
2015-01-02 15:15:18 +00:00
|
|
|
|
2017-02-13 12:47:00 +00:00
|
|
|
var NULL_DIMENSIONS = {
|
|
|
|
width: 0,
|
|
|
|
height: 0
|
|
|
|
};
|
2015-02-02 13:46:21 +00:00
|
|
|
|
2015-01-02 15:15:18 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2018-05-24 11:42:40 +00:00
|
|
|
export default function UpdatePropertiesHandler(
|
|
|
|
elementRegistry, moddle, translate,
|
|
|
|
modeling, textRenderer) {
|
|
|
|
|
2015-01-02 15:15:18 +00:00
|
|
|
this._elementRegistry = elementRegistry;
|
2015-10-05 11:35:10 +00:00
|
|
|
this._moddle = moddle;
|
2016-02-25 16:40:56 +00:00
|
|
|
this._translate = translate;
|
2017-02-13 12:47:00 +00:00
|
|
|
this._modeling = modeling;
|
2018-05-24 11:42:40 +00:00
|
|
|
this._textRenderer = textRenderer;
|
2015-01-02 15:15:18 +00:00
|
|
|
}
|
|
|
|
|
2018-04-02 19:01:53 +00:00
|
|
|
UpdatePropertiesHandler.$inject = [
|
|
|
|
'elementRegistry',
|
|
|
|
'moddle',
|
|
|
|
'translate',
|
2018-05-24 11:42:40 +00:00
|
|
|
'modeling',
|
|
|
|
'textRenderer'
|
2018-04-02 19:01:53 +00:00
|
|
|
];
|
2015-01-02 15:15:18 +00:00
|
|
|
|
|
|
|
|
2018-02-27 08:57:22 +00:00
|
|
|
// api //////////////////////
|
2015-01-02 15:15:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
*
|
2016-08-19 14:16:37 +00:00
|
|
|
* @return {Array<djs.model.Base>} the updated element
|
2015-01-02 15:15:18 +00:00
|
|
|
*/
|
|
|
|
UpdatePropertiesHandler.prototype.execute = function(context) {
|
|
|
|
|
|
|
|
var element = context.element,
|
2016-11-28 13:26:42 +00:00
|
|
|
changed = [ element ],
|
2016-02-25 16:40:56 +00:00
|
|
|
translate = this._translate;
|
2015-01-02 15:15:18 +00:00
|
|
|
|
|
|
|
if (!element) {
|
2016-02-25 16:40:56 +00:00
|
|
|
throw new Error(translate('element required'));
|
2015-01-02 15:15:18 +00:00
|
|
|
}
|
|
|
|
|
2015-10-05 11:35:10 +00:00
|
|
|
var elementRegistry = this._elementRegistry,
|
|
|
|
ids = this._moddle.ids;
|
2015-01-02 15:15:18 +00:00
|
|
|
|
|
|
|
var businessObject = element.businessObject,
|
2015-12-08 12:29:03 +00:00
|
|
|
properties = unwrapBusinessObjects(context.properties),
|
2021-08-06 08:45:10 +00:00
|
|
|
oldProperties = context.oldProperties || getProperties(element, properties);
|
2015-01-02 15:15:18 +00:00
|
|
|
|
2015-11-25 20:53:02 +00:00
|
|
|
if (isIdChange(properties, businessObject)) {
|
2015-10-05 11:35:10 +00:00
|
|
|
ids.unclaim(businessObject[ID]);
|
|
|
|
|
2015-04-13 08:55:17 +00:00
|
|
|
elementRegistry.updateId(element, properties[ID]);
|
2016-04-18 14:15:00 +00:00
|
|
|
|
|
|
|
ids.claim(properties[ID], businessObject);
|
2015-04-13 08:55:17 +00:00
|
|
|
}
|
|
|
|
|
2015-01-02 15:15:18 +00:00
|
|
|
// 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
|
2021-08-06 08:45:10 +00:00
|
|
|
setProperties(element, properties);
|
2015-01-02 15:15:18 +00:00
|
|
|
|
|
|
|
// store old values
|
|
|
|
context.oldProperties = oldProperties;
|
|
|
|
context.changed = changed;
|
|
|
|
|
|
|
|
// indicate changed on objects affected by the update
|
|
|
|
return changed;
|
|
|
|
};
|
|
|
|
|
2017-02-13 12:47:00 +00:00
|
|
|
|
|
|
|
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
|
2018-06-13 07:01:16 +00:00
|
|
|
var newLabelBounds = this._textRenderer.getExternalLabelBounds(label, text);
|
2017-02-13 12:47:00 +00:00
|
|
|
|
|
|
|
this._modeling.resizeShape(label, newLabelBounds, NULL_DIMENSIONS);
|
|
|
|
};
|
|
|
|
|
2015-01-02 15:15:18 +00:00
|
|
|
/**
|
|
|
|
* Reverts the update on a BPMN elements properties.
|
|
|
|
*
|
|
|
|
* @param {Object} context
|
|
|
|
*
|
2016-08-19 14:16:37 +00:00
|
|
|
* @return {djs.model.Base} the updated element
|
2015-01-02 15:15:18 +00:00
|
|
|
*/
|
|
|
|
UpdatePropertiesHandler.prototype.revert = function(context) {
|
|
|
|
|
|
|
|
var element = context.element,
|
2015-10-05 11:35:10 +00:00
|
|
|
properties = context.properties,
|
2015-04-13 08:55:17 +00:00
|
|
|
oldProperties = context.oldProperties,
|
|
|
|
businessObject = element.businessObject,
|
2015-10-05 11:35:10 +00:00
|
|
|
elementRegistry = this._elementRegistry,
|
|
|
|
ids = this._moddle.ids;
|
2015-01-02 15:15:18 +00:00
|
|
|
|
2015-06-18 14:57:58 +00:00
|
|
|
// update properties
|
2021-08-06 08:45:10 +00:00
|
|
|
setProperties(element, oldProperties);
|
2015-01-02 15:15:18 +00:00
|
|
|
|
2015-11-25 20:53:02 +00:00
|
|
|
if (isIdChange(properties, businessObject)) {
|
2015-10-05 11:35:10 +00:00
|
|
|
ids.unclaim(properties[ID]);
|
|
|
|
|
2015-04-13 08:55:17 +00:00
|
|
|
elementRegistry.updateId(element, oldProperties[ID]);
|
2016-04-18 14:15:00 +00:00
|
|
|
|
|
|
|
ids.claim(oldProperties[ID], businessObject);
|
2015-04-13 08:55:17 +00:00
|
|
|
}
|
|
|
|
|
2015-01-02 15:15:18 +00:00
|
|
|
return context.changed;
|
2015-07-13 08:37:43 +00:00
|
|
|
};
|
2015-11-25 20:53:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
function isIdChange(properties, businessObject) {
|
|
|
|
return ID in properties && properties[ID] !== businessObject[ID];
|
2015-12-08 12:29:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-08-06 08:45:10 +00:00
|
|
|
function getProperties(element, properties) {
|
|
|
|
var propertyNames = keys(properties),
|
|
|
|
businessObject = element.businessObject,
|
|
|
|
di = getDi(element);
|
2016-11-28 13:26:42 +00:00
|
|
|
|
2015-12-08 12:29:03 +00:00
|
|
|
return reduce(propertyNames, function(result, key) {
|
2016-11-28 13:26:42 +00:00
|
|
|
|
2019-09-09 10:21:33 +00:00
|
|
|
// handle DI separately
|
2016-11-28 13:26:42 +00:00
|
|
|
if (key !== DI) {
|
|
|
|
result[key] = businessObject.get(key);
|
2021-08-06 08:45:10 +00:00
|
|
|
|
2016-11-28 13:26:42 +00:00
|
|
|
} else {
|
2021-08-06 08:45:10 +00:00
|
|
|
result[key] = getDiProperties(di, keys(properties.di));
|
2016-11-28 13:26:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}, {});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function getDiProperties(di, propertyNames) {
|
|
|
|
return reduce(propertyNames, function(result, key) {
|
2021-08-06 08:45:10 +00:00
|
|
|
result[key] = di && di.get(key);
|
2016-11-28 13:26:42 +00:00
|
|
|
|
2015-12-08 12:29:03 +00:00
|
|
|
return result;
|
|
|
|
}, {});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-08-06 08:45:10 +00:00
|
|
|
function setProperties(element, properties) {
|
|
|
|
var businessObject = element.businessObject,
|
|
|
|
di = getDi(element);
|
|
|
|
|
2015-12-08 12:29:03 +00:00
|
|
|
forEach(properties, function(value, key) {
|
2018-01-31 11:15:32 +00:00
|
|
|
|
|
|
|
if (key !== DI) {
|
|
|
|
businessObject.set(key, value);
|
|
|
|
} else {
|
2019-08-19 08:39:20 +00:00
|
|
|
|
2021-08-06 08:45:10 +00:00
|
|
|
// only update, if di exists
|
|
|
|
if (di) {
|
|
|
|
setDiProperties(di, value);
|
2018-01-31 11:15:32 +00:00
|
|
|
}
|
|
|
|
}
|
2015-12-08 12:29:03 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-11-28 13:26:42 +00:00
|
|
|
function setDiProperties(di, properties) {
|
|
|
|
forEach(properties, function(value, key) {
|
|
|
|
di.set(key, value);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-08 12:29:03 +00:00
|
|
|
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;
|
2017-02-13 12:47:00 +00:00
|
|
|
}
|