BpmnFactory.js 2.5 KB

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