chore(modeling): make append behavior a CommandInterceptor

Append behavior inherits from CommandInterceptor now.

Connection type inference related code is reused from Modeling#connect.
This commit is contained in:
Nico Rehwaldt 2015-04-28 13:57:03 +02:00
parent 183a41cf26
commit d8ef4772cd
1 changed files with 18 additions and 25 deletions

View File

@ -1,21 +1,26 @@
'use strict';
var inherits = require('inherits');
function AppendBehavior(eventBus, elementFactory) {
var is = require('../../../util/ModelUtil').is;
// assign the correct connection
// when appending a shape to another shape
var CommandInterceptor = require('diagram-js/lib/command/CommandInterceptor');
eventBus.on('commandStack.shape.append.preExecute', function(event) {
var context = event.context,
source = context.source,
shape = context.shape,
parent = context.parent || source.parent;
function AppendBehavior(eventBus, elementFactory, bpmnRules) {
CommandInterceptor.call(this, eventBus);
// assign correct shape position unless already set
this.preExecute('shape.append', function(context) {
var source = context.source,
shape = context.shape;
if (!context.position) {
if (shape.businessObject.$instanceOf('bpmn:TextAnnotation')) {
if (is(shape, 'bpmn:TextAnnotation')) {
context.position = {
x: source.x + source.width / 2 + 75,
y: source.y - (50) - shape.height / 2
@ -27,24 +32,12 @@ function AppendBehavior(eventBus, elementFactory) {
};
}
}
if (!context.connection) {
var connectionAttrs;
// connect flow nodes in the same container
if (shape.businessObject.$instanceOf('bpmn:FlowNode') && parent.children.indexOf(source) !== -1) {
connectionAttrs = { type: 'bpmn:SequenceFlow' };
} else {
// association always works
connectionAttrs = { type: 'bpmn:Association' };
}
context.connection = elementFactory.create('connection', connectionAttrs);
}
});
}, true);
}
AppendBehavior.$inject = [ 'eventBus', 'elementFactory' ];
AppendBehavior.$inject = [ 'eventBus', 'elementFactory', 'bpmnRules' ];
inherits(AppendBehavior, CommandInterceptor);
module.exports = AppendBehavior;