Skip to content

Commit

Permalink
feat(modeling): create generic ids for new elements
Browse files Browse the repository at this point in the history
This makes sure that the semantic ID prefix reflects
the common type of all replace options.

ID prefixes for elements will match the examples:
  * `bpmn:ServiceTask` => `Activity_<id_suffix>`
  * `bpmn:EndEvent` => `Event_<id_suffix>`
  * `bpmn:EventBasedGateway` => `Gateway_<id_suffix>`
  * `bpmn:SequenceFlow` => `Flow_<id_suffix>`

Related to camunda/camunda-modeler#1654
  • Loading branch information
barmac authored and nikku committed Feb 6, 2020
1 parent 0c0ebea commit 035bb0c
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 9 deletions.
20 changes: 19 additions & 1 deletion lib/features/modeling/BpmnFactory.js
Expand Up @@ -8,6 +8,10 @@ import {
isAny
} from './util/ModelingUtil';

import {
is
} from '../../util/ModelUtil';


export default function BpmnFactory(moddle) {
this._model = moddle;
Expand Down Expand Up @@ -41,7 +45,21 @@ BpmnFactory.prototype._ensureId = function(element) {

// generate semantic ids for elements
// bpmn:SequenceFlow -> SequenceFlow_ID
var prefix = (element.$type || '').replace(/^[^:]*:/g, '') + '_';
var prefix;

if (is(element, 'bpmn:Activity')) {
prefix = 'Activity';
} else if (is(element, 'bpmn:Event')) {
prefix = 'Event';
} else if (is(element, 'bpmn:Gateway')) {
prefix = 'Gateway';
} else if (is(element, 'bpmn:FlowElement')) {
prefix = 'Flow';
} else {
prefix = (element.$type || '').replace(/^[^:]*:/g, '');
}

prefix += '_';

if (!element.id && this._needsId(element)) {
element.id = this._model.ids.nextPrefixed(prefix, element);
Expand Down
44 changes: 36 additions & 8 deletions test/spec/features/modeling/BpmnFactorySpec.js
Expand Up @@ -26,14 +26,6 @@ describe('features - bpmn-factory', function() {
}));


it('should assign id (with semantic prefix)', inject(function(bpmnFactory) {
var task = bpmnFactory.create('bpmn:ServiceTask');

expect(task.$type).to.equal('bpmn:ServiceTask');
expect(task.id).to.match(/^ServiceTask_/g);
}));


it('should assign id (with semantic prefix)', inject(function(bpmnFactory) {
var plane = bpmnFactory.create('bpmndi:BPMNPlane');

Expand All @@ -48,6 +40,42 @@ describe('features - bpmn-factory', function() {
expect(set.id).to.exist;
}));


describe('generic id', function() {

it('should assign id with generic semantic prefix (Activity)', inject(function(bpmnFactory) {
var task = bpmnFactory.create('bpmn:ServiceTask');

expect(task.$type).to.equal('bpmn:ServiceTask');
expect(task.id).to.match(/^Activity_/g);
}));


it('should assign id with generic semantic prefix (Gateway)', inject(function(bpmnFactory) {
var task = bpmnFactory.create('bpmn:ParallelGateway');

expect(task.$type).to.equal('bpmn:ParallelGateway');
expect(task.id).to.match(/^Gateway_/g);
}));


it('should assign id with generic semantic prefix (Event)', inject(function(bpmnFactory) {
var task = bpmnFactory.create('bpmn:EndEvent');

expect(task.$type).to.equal('bpmn:EndEvent');
expect(task.id).to.match(/^Event_/g);
}));


it('should assign id with generic semantic prefix (FlowElement)', inject(
function(bpmnFactory) {
var task = bpmnFactory.create('bpmn:SequenceFlow');

expect(task.$type).to.equal('bpmn:SequenceFlow');
expect(task.id).to.match(/^Flow_/g);
})
);
});
});


Expand Down

0 comments on commit 035bb0c

Please sign in to comment.