BpmnFactory.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. /**
  13. * @typedef {import('../../model/Types').Moddle} Moddle
  14. * @typedef {import('../../model/Types').ModdleElement} ModdleElement
  15. *
  16. * @typedef {import('diagram-js/lib/util/Types').Point} Point
  17. */
  18. /**
  19. * A factory for BPMN elements.
  20. *
  21. * @param {Moddle} moddle
  22. */
  23. export default function BpmnFactory(moddle) {
  24. this._model = moddle;
  25. }
  26. BpmnFactory.$inject = [ 'moddle' ];
  27. /**
  28. * @param {ModdleElement} element
  29. *
  30. * @return {boolean}
  31. */
  32. BpmnFactory.prototype._needsId = function(element) {
  33. return isAny(element, [
  34. 'bpmn:RootElement',
  35. 'bpmn:FlowElement',
  36. 'bpmn:MessageFlow',
  37. 'bpmn:DataAssociation',
  38. 'bpmn:Artifact',
  39. 'bpmn:Participant',
  40. 'bpmn:Lane',
  41. 'bpmn:LaneSet',
  42. 'bpmn:Process',
  43. 'bpmn:Collaboration',
  44. 'bpmndi:BPMNShape',
  45. 'bpmndi:BPMNEdge',
  46. 'bpmndi:BPMNDiagram',
  47. 'bpmndi:BPMNPlane',
  48. 'bpmn:Property',
  49. 'bpmn:CategoryValue'
  50. ]);
  51. };
  52. /**
  53. * @param {ModdleElement} element
  54. */
  55. BpmnFactory.prototype._ensureId = function(element) {
  56. if (element.id) {
  57. this._model.ids.claim(element.id, element);
  58. return;
  59. }
  60. // generate semantic ids for elements
  61. // bpmn:SequenceFlow -> SequenceFlow_ID
  62. var prefix;
  63. if (is(element, 'bpmn:Activity')) {
  64. prefix = 'Activity';
  65. } else if (is(element, 'bpmn:Event')) {
  66. prefix = 'Event';
  67. } else if (is(element, 'bpmn:Gateway')) {
  68. prefix = 'Gateway';
  69. } else if (isAny(element, [ 'bpmn:SequenceFlow', 'bpmn:MessageFlow' ])) {
  70. prefix = 'Flow';
  71. } else {
  72. prefix = (element.$type || '').replace(/^[^:]*:/g, '');
  73. }
  74. prefix += '_';
  75. if (!element.id && this._needsId(element)) {
  76. element.id = this._model.ids.nextPrefixed(prefix, element);
  77. }
  78. };
  79. /**
  80. * Create BPMN element.
  81. *
  82. * @param {string} type
  83. * @param {Object} [attrs]
  84. *
  85. * @return {ModdleElement}
  86. */
  87. BpmnFactory.prototype.create = function(type, attrs) {
  88. var element = this._model.create(type, attrs || {});
  89. this._ensureId(element);
  90. return element;
  91. };
  92. /**
  93. * @return {ModdleElement}
  94. */
  95. BpmnFactory.prototype.createDiLabel = function() {
  96. return this.create('bpmndi:BPMNLabel', {
  97. bounds: this.createDiBounds()
  98. });
  99. };
  100. /**
  101. * @param {ModdleElement} semantic
  102. * @param {Object} [attrs]
  103. * @return {ModdleElement}
  104. */
  105. BpmnFactory.prototype.createDiShape = function(semantic, attrs) {
  106. return this.create('bpmndi:BPMNShape', assign({
  107. bpmnElement: semantic,
  108. bounds: this.createDiBounds()
  109. }, attrs));
  110. };
  111. /**
  112. * @return {ModdleElement}
  113. */
  114. BpmnFactory.prototype.createDiBounds = function(bounds) {
  115. return this.create('dc:Bounds', bounds);
  116. };
  117. /**
  118. * @param {Point[]} waypoints
  119. *
  120. * @return {ModdleElement[]}
  121. */
  122. BpmnFactory.prototype.createDiWaypoints = function(waypoints) {
  123. var self = this;
  124. return map(waypoints, function(pos) {
  125. return self.createDiWaypoint(pos);
  126. });
  127. };
  128. /**
  129. * @param {Point} point
  130. *
  131. * @return {ModdleElement}
  132. */
  133. BpmnFactory.prototype.createDiWaypoint = function(point) {
  134. return this.create('dc:Point', pick(point, [ 'x', 'y' ]));
  135. };
  136. /**
  137. * @param {ModdleElement} semantic
  138. * @param {Object} [attrs]
  139. *
  140. * @return {ModdleElement}
  141. */
  142. BpmnFactory.prototype.createDiEdge = function(semantic, attrs) {
  143. return this.create('bpmndi:BPMNEdge', assign({
  144. bpmnElement: semantic,
  145. waypoint: this.createDiWaypoints([])
  146. }, attrs));
  147. };
  148. /**
  149. * @param {ModdleElement} semantic
  150. * @param {Object} [attrs]
  151. *
  152. * @return {ModdleElement}
  153. */
  154. BpmnFactory.prototype.createDiPlane = function(semantic, attrs) {
  155. return this.create('bpmndi:BPMNPlane', assign({
  156. bpmnElement: semantic
  157. }, attrs));
  158. };