Modeling.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. import inherits from 'inherits';
  2. import BaseModeling from 'diagram-js/lib/features/modeling/Modeling';
  3. import UpdatePropertiesHandler from './cmd/UpdatePropertiesHandler';
  4. import UpdateCanvasRootHandler from './cmd/UpdateCanvasRootHandler';
  5. import AddLaneHandler from './cmd/AddLaneHandler';
  6. import SplitLaneHandler from './cmd/SplitLaneHandler';
  7. import ResizeLaneHandler from './cmd/ResizeLaneHandler';
  8. import UpdateFlowNodeRefsHandler from './cmd/UpdateFlowNodeRefsHandler';
  9. import IdClaimHandler from './cmd/IdClaimHandler';
  10. import SetColorHandler from './cmd/SetColorHandler';
  11. import UpdateLabelHandler from '../label-editing/cmd/UpdateLabelHandler';
  12. /**
  13. * BPMN 2.0 modeling features activator
  14. *
  15. * @param {EventBus} eventBus
  16. * @param {ElementFactory} elementFactory
  17. * @param {CommandStack} commandStack
  18. * @param {BpmnRules} bpmnRules
  19. */
  20. export default function Modeling(
  21. eventBus, elementFactory, commandStack,
  22. bpmnRules) {
  23. BaseModeling.call(this, eventBus, elementFactory, commandStack);
  24. this._bpmnRules = bpmnRules;
  25. }
  26. inherits(Modeling, BaseModeling);
  27. Modeling.$inject = [
  28. 'eventBus',
  29. 'elementFactory',
  30. 'commandStack',
  31. 'bpmnRules'
  32. ];
  33. Modeling.prototype.getHandlers = function() {
  34. var handlers = BaseModeling.prototype.getHandlers.call(this);
  35. handlers['element.updateProperties'] = UpdatePropertiesHandler;
  36. handlers['canvas.updateRoot'] = UpdateCanvasRootHandler;
  37. handlers['lane.add'] = AddLaneHandler;
  38. handlers['lane.resize'] = ResizeLaneHandler;
  39. handlers['lane.split'] = SplitLaneHandler;
  40. handlers['lane.updateRefs'] = UpdateFlowNodeRefsHandler;
  41. handlers['id.updateClaim'] = IdClaimHandler;
  42. handlers['element.setColor'] = SetColorHandler;
  43. handlers['element.updateLabel'] = UpdateLabelHandler;
  44. return handlers;
  45. };
  46. Modeling.prototype.updateLabel = function(element, newLabel, newBounds, hints) {
  47. this._commandStack.execute('element.updateLabel', {
  48. element: element,
  49. newLabel: newLabel,
  50. newBounds: newBounds,
  51. hints: hints || {}
  52. });
  53. };
  54. Modeling.prototype.connect = function(source, target, attrs, hints) {
  55. var bpmnRules = this._bpmnRules;
  56. if (!attrs) {
  57. attrs = bpmnRules.canConnect(source, target);
  58. }
  59. if (!attrs) {
  60. return;
  61. }
  62. return this.createConnection(source, target, attrs, source.parent, hints);
  63. };
  64. Modeling.prototype.updateProperties = function(element, properties) {
  65. this._commandStack.execute('element.updateProperties', {
  66. element: element,
  67. properties: properties
  68. });
  69. };
  70. Modeling.prototype.resizeLane = function(laneShape, newBounds, balanced) {
  71. this._commandStack.execute('lane.resize', {
  72. shape: laneShape,
  73. newBounds: newBounds,
  74. balanced: balanced
  75. });
  76. };
  77. Modeling.prototype.addLane = function(targetLaneShape, location) {
  78. var context = {
  79. shape: targetLaneShape,
  80. location: location
  81. };
  82. this._commandStack.execute('lane.add', context);
  83. return context.newLane;
  84. };
  85. Modeling.prototype.splitLane = function(targetLane, count) {
  86. this._commandStack.execute('lane.split', {
  87. shape: targetLane,
  88. count: count
  89. });
  90. };
  91. /**
  92. * Transform the current diagram into a collaboration.
  93. *
  94. * @return {djs.model.Root} the new root element
  95. */
  96. Modeling.prototype.makeCollaboration = function() {
  97. var collaborationElement = this._create('root', {
  98. type: 'bpmn:Collaboration'
  99. });
  100. var context = {
  101. newRoot: collaborationElement
  102. };
  103. this._commandStack.execute('canvas.updateRoot', context);
  104. return collaborationElement;
  105. };
  106. Modeling.prototype.updateLaneRefs = function(flowNodeShapes, laneShapes) {
  107. this._commandStack.execute('lane.updateRefs', {
  108. flowNodeShapes: flowNodeShapes,
  109. laneShapes: laneShapes
  110. });
  111. };
  112. /**
  113. * Transform the current diagram into a process.
  114. *
  115. * @return {djs.model.Root} the new root element
  116. */
  117. Modeling.prototype.makeProcess = function() {
  118. var processElement = this._create('root', {
  119. type: 'bpmn:Process'
  120. });
  121. var context = {
  122. newRoot: processElement
  123. };
  124. this._commandStack.execute('canvas.updateRoot', context);
  125. };
  126. Modeling.prototype.claimId = function(id, moddleElement) {
  127. this._commandStack.execute('id.updateClaim', {
  128. id: id,
  129. element: moddleElement,
  130. claiming: true
  131. });
  132. };
  133. Modeling.prototype.unclaimId = function(id, moddleElement) {
  134. this._commandStack.execute('id.updateClaim', {
  135. id: id,
  136. element: moddleElement
  137. });
  138. };
  139. Modeling.prototype.setColor = function(elements, colors) {
  140. if (!elements.length) {
  141. elements = [ elements ];
  142. }
  143. this._commandStack.execute('element.setColor', {
  144. elements: elements,
  145. colors: colors
  146. });
  147. };