From 39d4f1c57e442c51fdb73dc53cda77727b99b406 Mon Sep 17 00:00:00 2001 From: Maciej Barelkowski Date: Fri, 25 Jan 2019 11:18:03 +0100 Subject: [PATCH] feat(modeling): set isHorizontal=true for partipant/lane DIs Closes #934 --- .../modeling/behavior/IsHorizontalFix.js | 41 +++ lib/features/modeling/behavior/index.js | 3 + .../modeling/behavior/IsHorizontalFix.bpmn | 40 +++ .../modeling/behavior/IsHorizontalFixSpec.js | 237 ++++++++++++++++++ 4 files changed, 321 insertions(+) create mode 100644 lib/features/modeling/behavior/IsHorizontalFix.js create mode 100644 test/spec/features/modeling/behavior/IsHorizontalFix.bpmn create mode 100644 test/spec/features/modeling/behavior/IsHorizontalFixSpec.js diff --git a/lib/features/modeling/behavior/IsHorizontalFix.js b/lib/features/modeling/behavior/IsHorizontalFix.js new file mode 100644 index 00000000..6697f4af --- /dev/null +++ b/lib/features/modeling/behavior/IsHorizontalFix.js @@ -0,0 +1,41 @@ +import inherits from 'inherits'; + +import CommandInterceptor from 'diagram-js/lib/command/CommandInterceptor'; + +import { + getBusinessObject +} from '../../../util/ModelUtil'; + +import { + isAny +} from '../util/ModelingUtil'; + +/** + * A component that makes sure that each created or updated + * Pool and Lane is assigned an isHorizontal property set to true. + * + * @param {EventBus} eventBus + */ +export default function IsHorizontalFix(eventBus) { + + CommandInterceptor.call(this, eventBus); + + var elementTypesToUpdate = [ + 'bpmn:Participant', + 'bpmn:Lane' + ]; + + this.executed([ 'shape.move', 'shape.create', 'shape.resize' ], function(event) { + var bo = getBusinessObject(event.context.shape); + + if (isAny(bo, elementTypesToUpdate) && !bo.di.get('isHorizontal')) { + // set attribute directly to avoid modeling#updateProperty side effects + bo.di.set('isHorizontal', true); + } + }); + +} + +IsHorizontalFix.$inject = [ 'eventBus' ]; + +inherits(IsHorizontalFix, CommandInterceptor); diff --git a/lib/features/modeling/behavior/index.js b/lib/features/modeling/behavior/index.js index 98f05102..2784cc8d 100644 --- a/lib/features/modeling/behavior/index.js +++ b/lib/features/modeling/behavior/index.js @@ -10,6 +10,7 @@ import DataStoreBehavior from './DataStoreBehavior'; import DeleteLaneBehavior from './DeleteLaneBehavior'; import DropOnFlowBehavior from './DropOnFlowBehavior'; import ImportDockingFix from './ImportDockingFix'; +import IsHorizontalFix from './IsHorizontalFix'; import LabelBehavior from './LabelBehavior'; import ModelingFeedback from './ModelingFeedback'; import ReplaceConnectionBehavior from './ReplaceConnectionBehavior'; @@ -36,6 +37,7 @@ export default { 'deleteLaneBehavior', 'dropOnFlowBehavior', 'importDockingFix', + 'isHorizontalFix', 'labelBehavior', 'modelingFeedback', 'removeElementBehavior', @@ -60,6 +62,7 @@ export default { deleteLaneBehavior: [ 'type', DeleteLaneBehavior ], dropOnFlowBehavior: [ 'type', DropOnFlowBehavior ], importDockingFix: [ 'type', ImportDockingFix ], + isHorizontalFix: [ 'type', IsHorizontalFix ], labelBehavior: [ 'type', LabelBehavior ], modelingFeedback: [ 'type', ModelingFeedback ], replaceConnectionBehavior: [ 'type', ReplaceConnectionBehavior ], diff --git a/test/spec/features/modeling/behavior/IsHorizontalFix.bpmn b/test/spec/features/modeling/behavior/IsHorizontalFix.bpmn new file mode 100644 index 00000000..d3da434f --- /dev/null +++ b/test/spec/features/modeling/behavior/IsHorizontalFix.bpmn @@ -0,0 +1,40 @@ + + + + + + + + + StartEvent_1 + + + + + + + println execution.eventName + + + println end + + + + + + + + + + + + + + + + + + + + + diff --git a/test/spec/features/modeling/behavior/IsHorizontalFixSpec.js b/test/spec/features/modeling/behavior/IsHorizontalFixSpec.js new file mode 100644 index 00000000..6e0334ab --- /dev/null +++ b/test/spec/features/modeling/behavior/IsHorizontalFixSpec.js @@ -0,0 +1,237 @@ +import { + bootstrapModeler, + inject +} from 'test/TestHelper'; + +import modelingModule from 'lib/features/modeling'; +import coreModule from 'lib/core'; + +import { getBusinessObject } from 'lib/util/ModelUtil'; + + +describe('features/modeling/behavior - IsHorizontalFix', function() { + + var diagramXML; + + + describe('set on create', function() { + + diagramXML = require('test/fixtures/bpmn/simple.bpmn'); + + beforeEach(bootstrapModeler(diagramXML, { + modules: [ + coreModule, + modelingModule + ] + })); + + + it('should set isHorizontal=true when participant is created', + inject(function(canvas, elementFactory, modeling) { + // given + var processShape = canvas.getRootElement(), + participantShape = elementFactory.createParticipantShape(true); + + // when + var participant = modeling.createShape(participantShape, { x: 350, y: 200 }, processShape); + + // then + var isHorizontal = getBusinessObject(participant).di.get('isHorizontal'); + + expect(isHorizontal).to.be.true; + }) + ); + + + it('should set isHorizontal=true when lane is created', + inject(function(canvas, elementFactory, modeling) { + // given + var processShape = canvas.getRootElement(), + participantShape = elementFactory.createParticipantShape(true), + participant = modeling.createShape(participantShape, { x: 350, y: 200 }, processShape); + + // when + var lane = modeling.addLane(participant, 'bottom'); + + // then + var isHorizontal = getBusinessObject(lane).di.get('isHorizontal'); + + expect(isHorizontal).to.be.true; + }) + ); + + }); + + + describe('set on change', function() { + + diagramXML = require('./IsHorizontalFix.bpmn'); + + beforeEach(bootstrapModeler(diagramXML, { + modules: [ + coreModule, + modelingModule + ] + })); + + + it('should set isHorizontal=true when participant is moved', + inject(function(elementRegistry, modeling) { + + // given + var participant = elementRegistry.get('Participant'); + + // when + modeling.moveElements([ participant ], { x: 0, y: 0 }); + + // then + var isHorizontal = getBusinessObject(participant).di.get('isHorizontal'); + + expect(isHorizontal).to.be.true; + }) + ); + + + it('should set isHorizontal=true when lane is moved', + inject(function(elementRegistry, modeling) { + + // given + var lane = elementRegistry.get('Lane'); + + // when + modeling.moveElements([ lane ], { x: 0, y: 0 }); + + // then + var isHorizontal = getBusinessObject(lane).di.get('isHorizontal'); + + expect(isHorizontal).to.be.true; + }) + ); + + + it('should set isHorizontal=true when participant is resized', + inject(function(elementRegistry, modeling) { + + // given + var participant = elementRegistry.get('Participant'); + + // when + modeling.resizeShape(participant, { x: 0, y: 0, width: 10, height: 10 }); + + // then + var isHorizontal = getBusinessObject(participant).di.get('isHorizontal'); + + expect(isHorizontal).to.be.true; + }) + ); + + + it('should set isHorizontal=true when lane is resized', + inject(function(elementRegistry, modeling) { + + // given + var lane = elementRegistry.get('Lane'); + + // when + modeling.resizeLane(lane, { x: 0, y: 0, width: 10, height: 10 }); + + // then + var isHorizontal = getBusinessObject(lane).di.get('isHorizontal'); + + expect(isHorizontal).to.be.true; + }) + ); + + }); + + + describe('never unset on revert', function() { + + diagramXML = require('./IsHorizontalFix.bpmn'); + + beforeEach(bootstrapModeler(diagramXML, { + modules: [ + coreModule, + modelingModule + ] + })); + + + it('should not unset isHorizontal=true when participant move action is reverted', + inject(function(commandStack, elementRegistry, modeling) { + + // given + var participant = elementRegistry.get('Participant'); + + modeling.moveElements([ participant ], { x: 0, y: 0 }); + + // when + commandStack.undo(); + + // then + var isHorizontal = getBusinessObject(participant).di.get('isHorizontal'); + + expect(isHorizontal).to.be.true; + }) + ); + + + it('should not unset isHorizontal=true when lane move action is reverted', + inject(function(commandStack, elementRegistry, modeling) { + + // given + var lane = elementRegistry.get('Lane'); + + modeling.moveElements([ lane ], { x: 0, y: 0 }); + + // when + commandStack.undo(); + + // then + var isHorizontal = getBusinessObject(lane).di.get('isHorizontal'); + + expect(isHorizontal).to.be.true; + }) + ); + + + it('should not unset isHorizontal=true when participant resize action is reverted', + inject(function(commandStack, elementRegistry, modeling) { + + // given + var participant = elementRegistry.get('Participant'); + + modeling.resizeShape(participant, { x: 0, y: 0, width: 10, height: 10 }); + + // when + commandStack.undo(); + + // then + var isHorizontal = getBusinessObject(participant).di.get('isHorizontal'); + + expect(isHorizontal).to.be.true; + }) + ); + + + it('should not unset isHorizontal=true when lane resize action is reverted', + inject(function(commandStack, elementRegistry, modeling) { + + // given + var lane = elementRegistry.get('Lane'); + + modeling.resizeLane(lane, { x: 0, y: 0, width: 10, height: 10 }); + + // when + commandStack.undo(); + + // then + var isHorizontal = getBusinessObject(lane).di.get('isHorizontal'); + + expect(isHorizontal).to.be.true; + }) + ); + + }); + +});