BpmnFactory.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import {
  2. map,
  3. assign,
  4. pick
  5. } from 'min-dash';
  6. import {
  7. isAny
  8. } from './util/ModelingUtil';
  9. export default function BpmnFactory(moddle) {
  10. this._model = moddle;
  11. }
  12. BpmnFactory.$inject = [ 'moddle' ];
  13. BpmnFactory.prototype._needsId = function(element) {
  14. return isAny(element, [
  15. 'bpmn:RootElement',
  16. 'bpmn:FlowElement',
  17. 'bpmn:MessageFlow',
  18. 'bpmn:DataAssociation',
  19. 'bpmn:Artifact',
  20. 'bpmn:Participant',
  21. 'bpmn:Lane',
  22. 'bpmn:LaneSet',
  23. 'bpmn:Process',
  24. 'bpmn:Collaboration',
  25. 'bpmndi:BPMNShape',
  26. 'bpmndi:BPMNEdge',
  27. 'bpmndi:BPMNDiagram',
  28. 'bpmndi:BPMNPlane',
  29. 'bpmn:Property'
  30. ]);
  31. };
  32. BpmnFactory.prototype._ensureId = function(element) {
  33. // generate semantic ids for elements
  34. // bpmn:SequenceFlow -> SequenceFlow_ID
  35. var prefix = (element.$type || '').replace(/^[^:]*:/g, '') + '_';
  36. if (!element.id && this._needsId(element)) {
  37. element.id = this._model.ids.nextPrefixed(prefix, element);
  38. }
  39. };
  40. BpmnFactory.prototype.create = function(type, attrs) {
  41. var element = this._model.create(type, attrs || {});
  42. this._ensureId(element);
  43. return element;
  44. };
  45. BpmnFactory.prototype.createDiLabel = function() {
  46. return this.create('bpmndi:BPMNLabel', {
  47. bounds: this.createDiBounds()
  48. });
  49. };
  50. BpmnFactory.prototype.createDiShape = function(semantic, bounds, attrs) {
  51. return this.create('bpmndi:BPMNShape', assign({
  52. bpmnElement: semantic,
  53. bounds: this.createDiBounds(bounds)
  54. }, attrs));
  55. };
  56. BpmnFactory.prototype.createDiBounds = function(bounds) {
  57. return this.create('dc:Bounds', bounds);
  58. };
  59. BpmnFactory.prototype.createDiWaypoints = function(waypoints) {
  60. var self = this;
  61. return map(waypoints, function(pos) {
  62. return self.createDiWaypoint(pos);
  63. });
  64. };
  65. BpmnFactory.prototype.createDiWaypoint = function(point) {
  66. return this.create('dc:Point', pick(point, [ 'x', 'y' ]));
  67. };
  68. BpmnFactory.prototype.createDiEdge = function(semantic, waypoints, attrs) {
  69. return this.create('bpmndi:BPMNEdge', assign({
  70. bpmnElement: semantic
  71. }, attrs));
  72. };
  73. BpmnFactory.prototype.createDiPlane = function(semantic) {
  74. return this.create('bpmndi:BPMNPlane', {
  75. bpmnElement: semantic
  76. });
  77. };