feat(modeling): allow passing shapes to modeling#updateProperties

Closes #430
This commit is contained in:
Nico Rehwaldt 2015-12-08 13:29:03 +01:00
parent 65c94ea9ac
commit a046b14d6f
2 changed files with 112 additions and 22 deletions

View File

@ -2,7 +2,10 @@
var reduce = require('lodash/object/transform'),
keys = require('lodash/object/keys'),
forEach = require('lodash/collection/forEach');
forEach = require('lodash/collection/forEach'),
assign = require('lodash/object/assign');
var getBusinessObject = require('../../../util/ModelUtil').getBusinessObject;
var DEFAULT_FLOW = 'default',
NAME = 'name',
@ -28,21 +31,6 @@ UpdatePropertiesHandler.$inject = [ 'elementRegistry', 'moddle' ];
module.exports = UpdatePropertiesHandler;
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);
});
}
////// api /////////////////////////////////////////////
/**
@ -68,7 +56,7 @@ UpdatePropertiesHandler.prototype.execute = function(context) {
ids = this._moddle.ids;
var businessObject = element.businessObject,
properties = context.properties,
properties = unwrapBusinessObjects(context.properties),
oldProperties = context.oldProperties || getProperties(businessObject, keys(properties));
if (isIdChange(properties, businessObject)) {
@ -139,3 +127,43 @@ UpdatePropertiesHandler.prototype.revert = function(context) {
function isIdChange(properties, businessObject) {
return ID in properties && properties[ID] !== businessObject[ID];
}
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;
}

View File

@ -54,7 +54,7 @@ describe('features/modeling - update properties', function() {
}));
it('updating default flow', inject(function(elementRegistry, modeling) {
it('unsetting default flow', inject(function(elementRegistry, modeling) {
// given
var gatewayShape = elementRegistry.get('ExclusiveGateway_1');
@ -70,6 +70,24 @@ describe('features/modeling - update properties', function() {
}));
it('updating default flow', inject(function(elementRegistry, modeling) {
// given
var gatewayShape = elementRegistry.get('ExclusiveGateway_1'),
newDefaultFlowConnection = elementRegistry.get('SequenceFlow_2'),
newDefaultFlow = newDefaultFlowConnection.businessObject;
// when
modeling.updateProperties(gatewayShape, { 'default': newDefaultFlow });
// then
expect(gatewayShape.businessObject['default']).to.eql(newDefaultFlow);
// flow got updated, too
expect(updatedElements).to.include(newDefaultFlowConnection);
}));
it('setting name', inject(function(elementRegistry, modeling) {
// given
@ -173,20 +191,43 @@ describe('features/modeling - update properties', function() {
}));
it('updating default flow', inject(function(elementRegistry, commandStack, modeling) {
it('unsetting default flow', inject(function(elementRegistry, commandStack, modeling) {
// given
var gatewayShape = elementRegistry.get('ExclusiveGateway_1');
var gatewayShape = elementRegistry.get('ExclusiveGateway_1'),
oldDefaultFlow = gatewayShape.businessObject['default'];
// when
modeling.updateProperties(gatewayShape, { 'default': undefined });
commandStack.undo();
// then
expect(gatewayShape.businessObject['default']).to.exist;
expect(gatewayShape.businessObject['default']).to.eql(oldDefaultFlow);
// flow got updated, too
expect(updatedElements).to.include(elementRegistry.get('SequenceFlow_1'));
expect(updatedElements).to.include(elementRegistry.get(oldDefaultFlow.id));
}));
it('updating default flow', inject(function(elementRegistry, commandStack, modeling) {
// given
var gatewayShape = elementRegistry.get('ExclusiveGateway_1'),
newDefaultFlowConnection = elementRegistry.get('SequenceFlow_2'),
newDefaultFlow = newDefaultFlowConnection.businessObject,
oldDefaultFlowConnection = elementRegistry.get('SequenceFlow_1'),
oldDefaultFlow = oldDefaultFlowConnection.businessObject;
// when
modeling.updateProperties(gatewayShape, { 'default': newDefaultFlow });
commandStack.undo();
// then
expect(gatewayShape.businessObject['default']).to.eql(oldDefaultFlow);
// flow got updated, too
expect(updatedElements).to.include(newDefaultFlowConnection);
expect(updatedElements).to.include(oldDefaultFlowConnection);
}));
@ -335,6 +376,27 @@ describe('features/modeling - update properties', function() {
});
describe('unwrap diagram elements', function() {
it('updating default flow with connection', inject(function(elementRegistry, modeling) {
// given
var gatewayShape = elementRegistry.get('ExclusiveGateway_1'),
newDefaultFlowConnection = elementRegistry.get('SequenceFlow_2');
// when
modeling.updateProperties(gatewayShape, { 'default': newDefaultFlowConnection });
// then
expect(gatewayShape.businessObject['default']).to.eql(newDefaultFlowConnection.businessObject);
// flow got updated, too
expect(updatedElements).to.include(newDefaultFlowConnection);
}));
});
describe('error handling', function() {
it('should ignore unchanged id', inject(function(elementRegistry, modeling) {