2015-01-02 15:15:18 +00:00
|
|
|
'use strict';
|
|
|
|
|
2015-06-18 14:57:58 +00:00
|
|
|
var reduce = require('lodash/object/transform'),
|
|
|
|
keys = require('lodash/object/keys'),
|
2015-12-08 12:29:03 +00:00
|
|
|
forEach = require('lodash/collection/forEach'),
|
|
|
|
assign = require('lodash/object/assign');
|
|
|
|
|
|
|
|
var getBusinessObject = require('../../../util/ModelUtil').getBusinessObject;
|
2015-01-02 15:15:18 +00:00
|
|
|
|
|
|
|
var DEFAULT_FLOW = 'default',
|
2015-04-13 08:55:17 +00:00
|
|
|
NAME = 'name',
|
|
|
|
ID = 'id';
|
2015-01-02 15:15:18 +00:00
|
|
|
|
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.
|
|
|
|
*/
|
2015-10-05 11:35:10 +00:00
|
|
|
function UpdatePropertiesHandler(elementRegistry, moddle) {
|
2015-01-02 15:15:18 +00:00
|
|
|
this._elementRegistry = elementRegistry;
|
2015-10-05 11:35:10 +00:00
|
|
|
this._moddle = moddle;
|
2015-01-02 15:15:18 +00:00
|
|
|
}
|
|
|
|
|
2015-10-05 11:35:10 +00:00
|
|
|
UpdatePropertiesHandler.$inject = [ 'elementRegistry', 'moddle' ];
|
2015-01-02 15:15:18 +00:00
|
|
|
|
|
|
|
module.exports = UpdatePropertiesHandler;
|
|
|
|
|
|
|
|
|
|
|
|
////// 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.mode.Base>} the updated element
|
|
|
|
*/
|
|
|
|
UpdatePropertiesHandler.prototype.execute = function(context) {
|
|
|
|
|
|
|
|
var element = context.element,
|
|
|
|
changed = [ element ];
|
|
|
|
|
|
|
|
if (!element) {
|
|
|
|
throw new Error('element required');
|
|
|
|
}
|
|
|
|
|
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),
|
2015-06-18 14:57:58 +00:00
|
|
|
oldProperties = context.oldProperties || getProperties(businessObject, keys(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]);
|
|
|
|
}
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NAME in properties && element.label) {
|
|
|
|
changed.push(element.label);
|
2015-09-14 14:03:46 +00:00
|
|
|
|
|
|
|
// show the label
|
2015-09-14 14:03:46 +00:00
|
|
|
element.label.hidden = !properties[NAME];
|
2015-01-02 15:15:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// update properties
|
2015-06-18 14:57:58 +00:00
|
|
|
setProperties(businessObject, 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;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reverts the update on a BPMN elements properties.
|
|
|
|
*
|
|
|
|
* @param {Object} context
|
|
|
|
*
|
|
|
|
* @return {djs.mode.Base} the updated element
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
setProperties(businessObject, 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]);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function getProperties(businessObject, propertyNames) {
|
|
|
|
return reduce(propertyNames, function(result, key) {
|
|
|
|
result[key] = businessObject.get(key);
|
|
|
|
return result;
|
|
|
|
}, {});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function setProperties(businessObject, properties) {
|
|
|
|
forEach(properties, function(value, key) {
|
|
|
|
businessObject.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;
|
2015-11-25 20:53:02 +00:00
|
|
|
}
|