2014-06-30 15:03:35 +00:00
|
|
|
'use strict';
|
|
|
|
|
2015-02-02 13:46:21 +00:00
|
|
|
var map = require('lodash/collection/map'),
|
|
|
|
assign = require('lodash/object/assign'),
|
|
|
|
pick = require('lodash/object/pick');
|
2014-06-30 15:03:35 +00:00
|
|
|
|
|
|
|
|
2014-08-04 14:32:11 +00:00
|
|
|
function BpmnFactory(moddle) {
|
|
|
|
this._model = moddle;
|
2014-06-30 15:03:35 +00:00
|
|
|
}
|
|
|
|
|
2014-08-04 14:32:11 +00:00
|
|
|
BpmnFactory.$inject = [ 'moddle' ];
|
2014-06-30 15:03:35 +00:00
|
|
|
|
|
|
|
|
2014-07-18 12:39:15 +00:00
|
|
|
BpmnFactory.prototype._needsId = function(element) {
|
|
|
|
return element.$instanceOf('bpmn:RootElement') ||
|
|
|
|
element.$instanceOf('bpmn:FlowElement') ||
|
2015-04-16 07:11:04 +00:00
|
|
|
element.$instanceOf('bpmn:MessageFlow') ||
|
2015-10-06 10:33:21 +00:00
|
|
|
element.$instanceOf('bpmn:DataAssociation') ||
|
2014-07-18 12:39:15 +00:00
|
|
|
element.$instanceOf('bpmn:Artifact') ||
|
2015-03-31 13:02:04 +00:00
|
|
|
element.$instanceOf('bpmn:Participant') ||
|
2015-08-12 06:18:49 +00:00
|
|
|
element.$instanceOf('bpmn:Lane') ||
|
2015-03-31 13:02:04 +00:00
|
|
|
element.$instanceOf('bpmn:Process') ||
|
|
|
|
element.$instanceOf('bpmn:Collaboration') ||
|
2014-07-18 12:39:15 +00:00
|
|
|
element.$instanceOf('bpmndi:BPMNShape') ||
|
|
|
|
element.$instanceOf('bpmndi:BPMNEdge') ||
|
|
|
|
element.$instanceOf('bpmndi:BPMNDiagram') ||
|
|
|
|
element.$instanceOf('bpmndi:BPMNPlane');
|
|
|
|
};
|
|
|
|
|
2014-06-30 15:03:35 +00:00
|
|
|
BpmnFactory.prototype._ensureId = function(element) {
|
2014-08-11 15:45:47 +00:00
|
|
|
|
2014-08-12 09:03:11 +00:00
|
|
|
// generate semantic ids for elements
|
|
|
|
// bpmn:SequenceFlow -> SequenceFlow_ID
|
|
|
|
var prefix = (element.$type || '').replace(/^[^:]*:/g, '') + '_';
|
2014-08-11 15:45:47 +00:00
|
|
|
|
2014-07-18 12:39:15 +00:00
|
|
|
if (!element.id && this._needsId(element)) {
|
2014-08-11 15:45:47 +00:00
|
|
|
element.id = this._model.ids.nextPrefixed(prefix, element);
|
2014-06-30 15:03:35 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-07-18 12:39:15 +00:00
|
|
|
|
2014-06-30 15:03:35 +00:00
|
|
|
BpmnFactory.prototype.create = function(type, attrs) {
|
2014-07-18 12:39:15 +00:00
|
|
|
var element = this._model.create(type, attrs || {});
|
2014-06-30 15:03:35 +00:00
|
|
|
|
|
|
|
this._ensureId(element);
|
|
|
|
|
|
|
|
return element;
|
|
|
|
};
|
|
|
|
|
2014-07-18 12:39:15 +00:00
|
|
|
|
2014-07-30 14:07:43 +00:00
|
|
|
BpmnFactory.prototype.createDiLabel = function() {
|
|
|
|
return this.create('bpmndi:BPMNLabel', {
|
|
|
|
bounds: this.createDiBounds()
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-07-23 16:53:33 +00:00
|
|
|
BpmnFactory.prototype.createDiShape = function(semantic, bounds, attrs) {
|
2014-06-30 15:03:35 +00:00
|
|
|
|
2015-02-02 13:46:21 +00:00
|
|
|
return this.create('bpmndi:BPMNShape', assign({
|
2014-06-30 15:03:35 +00:00
|
|
|
bpmnElement: semantic,
|
|
|
|
bounds: this.createDiBounds(bounds)
|
|
|
|
}, attrs));
|
|
|
|
};
|
|
|
|
|
2014-07-18 12:39:15 +00:00
|
|
|
|
2014-06-30 15:03:35 +00:00
|
|
|
BpmnFactory.prototype.createDiBounds = function(bounds) {
|
|
|
|
return this.create('dc:Bounds', bounds);
|
|
|
|
};
|
|
|
|
|
2014-07-18 12:39:15 +00:00
|
|
|
|
2014-07-23 16:53:33 +00:00
|
|
|
BpmnFactory.prototype.createDiWaypoints = function(waypoints) {
|
2015-02-02 13:46:21 +00:00
|
|
|
return map(waypoints, function(pos) {
|
2014-06-30 15:03:35 +00:00
|
|
|
return this.createDiWaypoint(pos);
|
|
|
|
}, this);
|
|
|
|
};
|
|
|
|
|
2014-07-23 16:53:33 +00:00
|
|
|
BpmnFactory.prototype.createDiWaypoint = function(point) {
|
2015-02-02 13:46:21 +00:00
|
|
|
return this.create('dc:Point', pick(point, [ 'x', 'y' ]));
|
2014-07-18 12:39:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-07-23 16:53:33 +00:00
|
|
|
BpmnFactory.prototype.createDiEdge = function(semantic, waypoints, attrs) {
|
2015-02-02 13:46:21 +00:00
|
|
|
return this.create('bpmndi:BPMNEdge', assign({
|
2014-07-23 16:53:33 +00:00
|
|
|
bpmnElement: semantic
|
|
|
|
}, attrs));
|
2014-06-30 15:03:35 +00:00
|
|
|
};
|
|
|
|
|
2015-03-31 13:02:04 +00:00
|
|
|
BpmnFactory.prototype.createDiPlane = function(semantic) {
|
|
|
|
return this.create('bpmndi:BPMNPlane', {
|
|
|
|
bpmnElement: semantic
|
|
|
|
});
|
|
|
|
};
|
2014-06-30 15:03:35 +00:00
|
|
|
|
2014-10-27 11:07:57 +00:00
|
|
|
module.exports = BpmnFactory;
|