chore(modeling): move BpmnLabelSupport to behavior

* Make sure the label support is part of our
behavior definitions.
* Adjust test cases accordingly.
This commit is contained in:
Nico Rehwaldt 2016-03-08 14:12:52 +01:00
parent 0393d51893
commit 1bceaaa229
6 changed files with 161 additions and 86 deletions

View File

@ -3,8 +3,8 @@
var assign = require('lodash/object/assign'),
inherits = require('inherits');
var LabelUtil = require('../../util/LabelUtil'),
is = require('../../util/ModelUtil').is;
var LabelUtil = require('../../../util/LabelUtil'),
is = require('../../../util/ModelUtil').is;
var hasExternalLabel = LabelUtil.hasExternalLabel,
getExternalLabelMid = LabelUtil.getExternalLabelMid;
@ -12,6 +12,15 @@ var hasExternalLabel = LabelUtil.hasExternalLabel,
var CommandInterceptor = require('diagram-js/lib/command/CommandInterceptor');
/**
* A component that makes sure that external labels are added
* together with respective elements and properly updated (DI wise)
* during move.
*
* @param {EventBus} eventBus
* @param {Modeling} modeling
* @param {BpmnFactory} bpmnFactory
*/
function LabelSupport(eventBus, modeling, bpmnFactory) {
CommandInterceptor.call(this, eventBus);
@ -37,8 +46,9 @@ function LabelSupport(eventBus, modeling, bpmnFactory) {
}
});
// update di information on label movement and creation
this.executed([ 'label.create', 'shape.moved' ], function(e) {
// update di information on label creation
this.executed([ 'label.create' ], function(e) {
var element = e.context.shape,
businessObject,
di;

View File

@ -7,6 +7,7 @@ module.exports = {
'createParticipantBehavior',
'dataInputAssociationBehavior',
'deleteLaneBehavior',
'labelBehavior',
'modelingFeedback',
'removeParticipantBehavior',
'replaceConnectionBehavior',
@ -21,6 +22,7 @@ module.exports = {
createParticipantBehavior: [ 'type', require('./CreateParticipantBehavior') ],
dataInputAssociationBehavior: [ 'type', require('./DataInputAssociationBehavior') ],
deleteLaneBehavior: [ 'type', require('./DeleteLaneBehavior') ],
labelBehavior: [ 'type', require('./LabelBehavior') ],
modelingFeedback: [ 'type', require('./ModelingFeedback') ],
removeParticipantBehavior: [ 'type', require('./RemoveParticipantBehavior') ],
replaceConnectionBehavior: [ 'type', require('./ReplaceConnectionBehavior') ],

View File

@ -1,5 +1,5 @@
module.exports = {
__init__: [ 'modeling', 'bpmnUpdater', 'bpmnLabelSupport' ],
__init__: [ 'modeling', 'bpmnUpdater' ],
__depends__: [
require('./behavior'),
require('../label-editing'),
@ -18,7 +18,6 @@ module.exports = {
bpmnUpdater: [ 'type', require('./BpmnUpdater') ],
elementFactory: [ 'type', require('./ElementFactory') ],
modeling: [ 'type', require('./Modeling') ],
bpmnLabelSupport: [ 'type', require('./BpmnLabelSupport') ],
layouter: [ 'type', require('./BpmnLayouter') ],
connectionDocking: [ 'type', require('diagram-js/lib/layout/CroppingConnectionDocking') ]
};

View File

@ -4,16 +4,11 @@
var find = require('lodash/collection/find');
var assign = require('lodash/object/assign');
var modelingModule = require('../../../../lib/features/modeling'),
coreModule = require('../../../../lib/core');
var LabelUtil = require('../../../../lib/util/LabelUtil');
describe('features/modeling - append shape', function() {
var diagramXML = require('../../../fixtures/bpmn/simple.bpmn');
@ -74,44 +69,6 @@ describe('features/modeling - append shape', function() {
}));
describe('should add external label', function() {
it('correctly wired and positioned', inject(function(elementRegistry, modeling, commandStack) {
// given
var startEventShape = elementRegistry.get('StartEvent_1');
// when
var targetShape = modeling.appendShape(startEventShape, { type: 'bpmn:EndEvent' });
var label = targetShape.label;
// then
expect(label).to.exist;
expect(elementRegistry.get(label.id)).to.exist;
expect(label).to.have.bounds(assign({ x: 441, y: 278 }, LabelUtil.DEFAULT_LABEL_SIZE));
}));
it('with di', inject(function(elementRegistry, modeling, commandStack) {
// given
var startEventShape = elementRegistry.get('StartEvent_1');
// when
var targetShape = modeling.appendShape(startEventShape, { type: 'bpmn:EndEvent' }),
target = targetShape.businessObject;
// then
expect(target.di.label).to.exist;
expect(target.di.label).to.have.bounds(targetShape.label);
}));
});
it('should add connection', inject(function(elementRegistry, modeling) {
// given

View File

@ -1,37 +0,0 @@
'use strict';
var TestHelper = require('../../../TestHelper');
/* global bootstrapModeler, inject */
var modelingModule = require('../../../../lib/features/modeling'),
coreModule = require('../../../../lib/core');
describe('features - bpmn-label-support', function() {
var diagramXML = require('../../../fixtures/bpmn/basic.bpmn');
var testModules = [ modelingModule, coreModule ];
beforeEach(bootstrapModeler(diagramXML, { modules: testModules }));
describe('should not add a label on create', function() {
it('should not add a label on create', inject(function(elementFactory, elementRegistry, modeling, canvas) {
// when
var startEvent = elementRegistry.get('StartEvent_1'),
task = elementRegistry.get('Task_1');
modeling.connect(startEvent, task);
var labels = elementRegistry.filter(function(element) {
return element.type === 'label';
});
// then
expect(labels).to.have.length(2);
}));
});
});

View File

@ -0,0 +1,144 @@
'use strict';
var TestHelper = require('../../../../TestHelper');
/* global bootstrapModeler, inject */
var assign = require('lodash/object/assign');
var modelingModule = require('../../../../../lib/features/modeling'),
coreModule = require('../../../../../lib/core');
var LabelUtil = require('../../../../../lib/util/LabelUtil');
describe('behavior - LabelBehavior', function() {
var diagramXML = require('../../../../fixtures/bpmn/basic.bpmn');
var testModules = [ modelingModule, coreModule ];
beforeEach(bootstrapModeler(diagramXML, { modules: testModules }));
describe('add label', function() {
it('should add to sequence flow', inject(function(elementRegistry, modeling) {
// given
var startEvent = elementRegistry.get('StartEvent_1'),
task = elementRegistry.get('Task_1');
// when
var connection = modeling.connect(startEvent, task);
// then
expect(connection.label).to.exist;
}));
it('should add to exclusive gateway', inject(function(elementFactory, elementRegistry, modeling) {
// given
var parentShape = elementRegistry.get('Process_1'),
newShapeAttrs = { type: 'bpmn:ExclusiveGateway' };
// when
var newShape = modeling.createShape(newShapeAttrs, { x: 50, y: 50 }, parentShape);
// then
expect(newShape.label).to.exist;
}));
it('should not add to task', inject(function(elementFactory, elementRegistry, modeling) {
// given
var parentShape = elementRegistry.get('Process_1'),
newShapeAttrs = { type: 'bpmn:Task' };
// when
var newShape = modeling.createShape(newShapeAttrs, { x: 50, y: 50 }, parentShape);
// then
expect(newShape.label).not.to.exist;
}));
describe('on append', function() {
it('correctly wired and positioned', inject(function(elementRegistry, modeling, commandStack) {
// given
var startEventShape = elementRegistry.get('StartEvent_1');
// when
var targetShape = modeling.appendShape(startEventShape, { type: 'bpmn:EndEvent' });
var label = targetShape.label;
// then
expect(label).to.exist;
expect(elementRegistry.get(label.id)).to.exist;
expect(label).to.have.bounds(assign({ x: 262, y: 138 }, LabelUtil.DEFAULT_LABEL_SIZE));
}));
it('with di', inject(function(elementRegistry, modeling, commandStack) {
// given
var startEventShape = elementRegistry.get('StartEvent_1');
// when
var targetShape = modeling.appendShape(startEventShape, { type: 'bpmn:EndEvent' }),
target = targetShape.businessObject;
// then
expect(target.di.label).to.exist;
expect(target.di.label).to.have.bounds(targetShape.label);
}));
});
it('should add with di', inject(function(elementFactory, elementRegistry, modeling) {
// given
var startEventShape = elementRegistry.get('StartEvent_1');
// when
var targetShape = modeling.appendShape(startEventShape, { type: 'bpmn:EndEvent' }),
target = targetShape.businessObject;
// then
expect(target.di.label).to.exist;
expect(target.di.label).to.have.bounds(targetShape.label);
}));
});
describe('move label', function() {
it('should move start event label', inject(function(elementRegistry, modeling) {
// given
var startEventShape = elementRegistry.get('StartEvent_1'),
startEvent = startEventShape.businessObject,
labelShape = startEventShape.label;
// when
modeling.moveElements([ labelShape ], { x: 10, y: -10 });
// then
expect(labelShape).to.have.position({ x: 156, y: 128 });
expect(startEvent.di.label).to.have.position({ x: 156, y: 128 });
}));
});
});