Modeling.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. import inherits from 'inherits-browser';
  2. import BaseModeling from 'diagram-js/lib/features/modeling/Modeling';
  3. import UpdateModdlePropertiesHandler from './cmd/UpdateModdlePropertiesHandler';
  4. import UpdatePropertiesHandler from './cmd/UpdatePropertiesHandler';
  5. import UpdateCanvasRootHandler from './cmd/UpdateCanvasRootHandler';
  6. import AddLaneHandler from './cmd/AddLaneHandler';
  7. import SplitLaneHandler from './cmd/SplitLaneHandler';
  8. import ResizeLaneHandler from './cmd/ResizeLaneHandler';
  9. import UpdateFlowNodeRefsHandler from './cmd/UpdateFlowNodeRefsHandler';
  10. import IdClaimHandler from './cmd/IdClaimHandler';
  11. import SetColorHandler from './cmd/SetColorHandler';
  12. import UpdateLabelHandler from '../label-editing/cmd/UpdateLabelHandler';
  13. /**
  14. * @typedef {import('../rules/BpmnRules').default} BpmnRules
  15. * @typedef {import('diagram-js/lib/command/CommandStack').default} CommandStack
  16. * @typedef {import('./ElementFactory').default} ElementFactory
  17. * @typedef {import('diagram-js/lib/core/EventBus').default} EventBus
  18. *
  19. * @typedef {import('diagram-js/lib/features/modeling/Modeling').ModelingHints} ModelingHints
  20. *
  21. * @typedef {import('../../model/Types').Connection} Connection
  22. * @typedef {import('../../model/Types').Element} Element
  23. * @typedef {import('../../model/Types').Label} Label
  24. * @typedef {import('../../model/Types').Parent} Parent
  25. * @typedef {import('../../model/Types').Root} Root
  26. * @typedef {import('../../model/Types').Shape} Shape
  27. * @typedef {import('../../model/Types').ModdleElement} ModdleElement
  28. *
  29. * @typedef {import('diagram-js/lib/util/Types').Rect} Rect
  30. *
  31. * @typedef {import('../../util/Types').Colors} Colors
  32. *
  33. * @typedef { {
  34. * removeShape?: boolean;
  35. * } } UpdateLabelHints
  36. */
  37. /**
  38. * The BPMN 2.0 modeling entry point.
  39. *
  40. * @template {Connection} [T=Connection]
  41. * @template {Element} [U=Element]
  42. * @template {Label} [V=Label]
  43. * @template {Parent} [W=Parent]
  44. * @template {Shape} [X=Shape]
  45. *
  46. * @extends {BaseModeling<T, U, V, W, X>}
  47. *
  48. * @param {EventBus} eventBus
  49. * @param {ElementFactory} elementFactory
  50. * @param {CommandStack} commandStack
  51. * @param {BpmnRules} bpmnRules
  52. */
  53. export default function Modeling(
  54. eventBus,
  55. elementFactory,
  56. commandStack,
  57. bpmnRules
  58. ) {
  59. BaseModeling.call(this, eventBus, elementFactory, commandStack);
  60. this._bpmnRules = bpmnRules;
  61. }
  62. inherits(Modeling, BaseModeling);
  63. Modeling.$inject = [
  64. 'eventBus',
  65. 'elementFactory',
  66. 'commandStack',
  67. 'bpmnRules'
  68. ];
  69. Modeling.prototype.getHandlers = function() {
  70. var handlers = BaseModeling.prototype.getHandlers.call(this);
  71. handlers['element.updateModdleProperties'] = UpdateModdlePropertiesHandler;
  72. handlers['element.updateProperties'] = UpdatePropertiesHandler;
  73. handlers['canvas.updateRoot'] = UpdateCanvasRootHandler;
  74. handlers['lane.add'] = AddLaneHandler;
  75. handlers['lane.resize'] = ResizeLaneHandler;
  76. handlers['lane.split'] = SplitLaneHandler;
  77. handlers['lane.updateRefs'] = UpdateFlowNodeRefsHandler;
  78. handlers['id.updateClaim'] = IdClaimHandler;
  79. handlers['element.setColor'] = SetColorHandler;
  80. handlers['element.updateLabel'] = UpdateLabelHandler;
  81. return handlers;
  82. };
  83. /**
  84. * Update an element's label.
  85. *
  86. * @param {Element} element The element.
  87. * @param {string} newLabel The new label.
  88. * @param {Rect} [newBounds] The optional bounds of the label.
  89. * @param {UpdateLabelHints} [hints] The optional hints.
  90. */
  91. Modeling.prototype.updateLabel = function(element, newLabel, newBounds, hints) {
  92. this._commandStack.execute('element.updateLabel', {
  93. element: element,
  94. newLabel: newLabel,
  95. newBounds: newBounds,
  96. hints: hints || {}
  97. });
  98. };
  99. /**
  100. * @param {Element} source
  101. * @param {Element} target
  102. * @param {Partial<Connection>} attrs
  103. * @param {ModelingHints} [hints]
  104. *
  105. * @return {T}
  106. */
  107. Modeling.prototype.connect = function(source, target, attrs, hints) {
  108. var bpmnRules = this._bpmnRules;
  109. if (!attrs) {
  110. attrs = bpmnRules.canConnect(source, target);
  111. }
  112. if (!attrs) {
  113. return;
  114. }
  115. return this.createConnection(source, target, attrs, source.parent, hints);
  116. };
  117. /**
  118. * Update a model element's properties.
  119. *
  120. * @param {Element} element The element.
  121. * @param {ModdleElement} moddleElement The model element.
  122. * @param {Object} properties The updated properties.
  123. */
  124. Modeling.prototype.updateModdleProperties = function(element, moddleElement, properties) {
  125. this._commandStack.execute('element.updateModdleProperties', {
  126. element: element,
  127. moddleElement: moddleElement,
  128. properties: properties
  129. });
  130. };
  131. /**
  132. * Update an element's properties.
  133. *
  134. * @param {Element} element The element.
  135. * @param {Object} properties The updated properties.
  136. */
  137. Modeling.prototype.updateProperties = function(element, properties) {
  138. this._commandStack.execute('element.updateProperties', {
  139. element: element,
  140. properties: properties
  141. });
  142. };
  143. /**
  144. * Resize a lane.
  145. *
  146. * @param {Shape} laneShape The lane.
  147. * @param {Rect} newBounds The new bounds of the lane.
  148. * @param {boolean} [balanced] Wether to resize neighboring lanes.
  149. */
  150. Modeling.prototype.resizeLane = function(laneShape, newBounds, balanced) {
  151. this._commandStack.execute('lane.resize', {
  152. shape: laneShape,
  153. newBounds: newBounds,
  154. balanced: balanced
  155. });
  156. };
  157. /**
  158. * Add a lane.
  159. *
  160. * @param {Shape} targetLaneShape The shape to add the lane to.
  161. * @param {string} location The location.
  162. *
  163. * @return {Shape} The added lane.
  164. */
  165. Modeling.prototype.addLane = function(targetLaneShape, location) {
  166. var context = {
  167. shape: targetLaneShape,
  168. location: location
  169. };
  170. this._commandStack.execute('lane.add', context);
  171. return context.newLane;
  172. };
  173. /**
  174. * Split a lane.
  175. *
  176. * @param {Shape} targetLane The lane to split.
  177. * @param {number} count The number of lanes to split the lane into. Must not
  178. * exceed the number of existing lanes.
  179. */
  180. Modeling.prototype.splitLane = function(targetLane, count) {
  181. this._commandStack.execute('lane.split', {
  182. shape: targetLane,
  183. count: count
  184. });
  185. };
  186. /**
  187. * Turn a process into a collaboration.
  188. *
  189. * @return {Root} The root of the collaboration.
  190. */
  191. Modeling.prototype.makeCollaboration = function() {
  192. var collaborationElement = this._create('root', {
  193. type: 'bpmn:Collaboration'
  194. });
  195. var context = {
  196. newRoot: collaborationElement
  197. };
  198. this._commandStack.execute('canvas.updateRoot', context);
  199. return collaborationElement;
  200. };
  201. /**
  202. * Transform a collaboration into a process.
  203. *
  204. * @return {Root} The root of the process.
  205. */
  206. Modeling.prototype.makeProcess = function() {
  207. var processElement = this._create('root', {
  208. type: 'bpmn:Process'
  209. });
  210. var context = {
  211. newRoot: processElement
  212. };
  213. this._commandStack.execute('canvas.updateRoot', context);
  214. };
  215. /**
  216. * Update the referenced lanes of each flow node.
  217. *
  218. * @param {Shape[]} flowNodeShapes The flow nodes to update.
  219. * @param {Shape[]} laneShapes The lanes.
  220. */
  221. Modeling.prototype.updateLaneRefs = function(flowNodeShapes, laneShapes) {
  222. this._commandStack.execute('lane.updateRefs', {
  223. flowNodeShapes: flowNodeShapes,
  224. laneShapes: laneShapes
  225. });
  226. };
  227. /**
  228. * Claim an ID.
  229. *
  230. * @param {string} id The ID to claim.
  231. * @param {ModdleElement} moddleElement The model element the ID is claimed for.
  232. */
  233. Modeling.prototype.claimId = function(id, moddleElement) {
  234. this._commandStack.execute('id.updateClaim', {
  235. id: id,
  236. element: moddleElement,
  237. claiming: true
  238. });
  239. };
  240. /**
  241. * Unclaim an ID.
  242. *
  243. * @param {string} id The ID to unclaim.
  244. * @param {ModdleElement} moddleElement The model element the ID is claimed for.
  245. */
  246. Modeling.prototype.unclaimId = function(id, moddleElement) {
  247. this._commandStack.execute('id.updateClaim', {
  248. id: id,
  249. element: moddleElement
  250. });
  251. };
  252. /**
  253. * Set the color(s) of one or many elements.
  254. *
  255. * @param {Element[]} elements The elements to set the color(s) for.
  256. * @param {Colors} colors The color(s) to set.
  257. */
  258. Modeling.prototype.setColor = function(elements, colors) {
  259. if (!elements.length) {
  260. elements = [ elements ];
  261. }
  262. this._commandStack.execute('element.setColor', {
  263. elements: elements,
  264. colors: colors
  265. });
  266. };