SubProcessPlaneBehavior.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. import inherits from 'inherits-browser';
  2. import CommandInterceptor from 'diagram-js/lib/command/CommandInterceptor';
  3. import { find } from 'min-dash';
  4. import { isExpanded } from '../../../util/DiUtil';
  5. import { getBusinessObject, getDi, is } from '../../../util/ModelUtil';
  6. import { getMid } from 'diagram-js/lib/layout/LayoutUtil';
  7. import { getBBox } from 'diagram-js/lib/util/Elements';
  8. import {
  9. getPlaneIdFromShape,
  10. getShapeIdFromPlane,
  11. isPlane,
  12. toPlaneId
  13. } from '../../../util/DrilldownUtil';
  14. /**
  15. * @typedef {import('diagram-js/lib/core/Canvas').default} Canvas
  16. * @typedef {import('diagram-js/lib/core/EventBus').default} EventBus
  17. * @typedef {import('../Modeling').default} Modeling
  18. * @typedef {import('../ElementFactory').default} ElementFactory
  19. * @typedef {import('../BpmnFactory').default} BpmnFactory
  20. * @typedef {import('../../../Modeler').default} Modeler
  21. * @typedef {import('diagram-js/lib/core/ElementRegistry').default} ElementRegistry
  22. *
  23. * @typedef {import('../../../model/Types').Element} Element
  24. * @typedef {import('../../../model/Types').Root} Root
  25. * @typedef {import('../../../model/Types').ModdleElement} ModdleElement
  26. */
  27. var LOW_PRIORITY = 400;
  28. var HIGH_PRIORITY = 600;
  29. var DEFAULT_POSITION = {
  30. x: 180,
  31. y: 160
  32. };
  33. /**
  34. * Creates bpmndi:BPMNPlane elements and canvas planes when collapsed subprocesses are created.
  35. *
  36. * @param {Canvas} canvas
  37. * @param {EventBus} eventBus
  38. * @param {Modeling} modeling
  39. * @param {ElementFactory} elementFactory
  40. * @param {BpmnFactory} bpmnFactory
  41. * @param {Modeler} bpmnjs
  42. * @param {ElementRegistry} elementRegistry
  43. */
  44. export default function SubProcessPlaneBehavior(
  45. canvas, eventBus, modeling,
  46. elementFactory, bpmnFactory, bpmnjs, elementRegistry) {
  47. CommandInterceptor.call(this, eventBus);
  48. this._canvas = canvas;
  49. this._eventBus = eventBus;
  50. this._modeling = modeling;
  51. this._elementFactory = elementFactory;
  52. this._bpmnFactory = bpmnFactory;
  53. this._bpmnjs = bpmnjs;
  54. this._elementRegistry = elementRegistry;
  55. var self = this;
  56. function isCollapsedSubProcess(element) {
  57. return is(element, 'bpmn:SubProcess') && !isExpanded(element);
  58. }
  59. function createRoot(context) {
  60. var shape = context.shape,
  61. rootElement = context.newRootElement;
  62. var businessObject = getBusinessObject(shape);
  63. rootElement = self._addDiagram(rootElement || businessObject);
  64. context.newRootElement = canvas.addRootElement(rootElement);
  65. }
  66. function removeRoot(context) {
  67. var shape = context.shape;
  68. var businessObject = getBusinessObject(shape);
  69. self._removeDiagram(businessObject);
  70. var rootElement = context.newRootElement = elementRegistry.get(getPlaneIdFromShape(businessObject));
  71. canvas.removeRootElement(rootElement);
  72. }
  73. // add plane elements for newly created sub-processes
  74. // this ensures we can actually drill down into the element
  75. this.executed('shape.create', function(context) {
  76. var shape = context.shape;
  77. if (!isCollapsedSubProcess(shape)) {
  78. return;
  79. }
  80. createRoot(context);
  81. }, true);
  82. this.postExecuted('shape.create', function(context) {
  83. var shape = context.shape,
  84. rootElement = context.newRootElement;
  85. if (!rootElement || !shape.children) {
  86. return;
  87. }
  88. self._showRecursively(shape.children);
  89. self._moveChildrenToShape(shape, rootElement);
  90. }, true);
  91. this.reverted('shape.create', function(context) {
  92. var shape = context.shape;
  93. if (!isCollapsedSubProcess(shape)) {
  94. return;
  95. }
  96. removeRoot(context);
  97. }, true);
  98. this.preExecuted('shape.delete', function(context) {
  99. var shape = context.shape;
  100. if (!isCollapsedSubProcess(shape)) {
  101. return;
  102. }
  103. var attachedRoot = elementRegistry.get(getPlaneIdFromShape(shape));
  104. if (!attachedRoot) {
  105. return;
  106. }
  107. modeling.removeElements(attachedRoot.children.slice());
  108. }, true);
  109. this.executed('shape.delete', function(context) {
  110. var shape = context.shape;
  111. if (!isCollapsedSubProcess(shape)) {
  112. return;
  113. }
  114. removeRoot(context);
  115. }, true);
  116. this.reverted('shape.delete', function(context) {
  117. var shape = context.shape;
  118. if (!isCollapsedSubProcess(shape)) {
  119. return;
  120. }
  121. createRoot(context);
  122. }, true);
  123. this.preExecuted('shape.replace', function(context) {
  124. var oldShape = context.oldShape;
  125. var newShape = context.newShape;
  126. if (!isCollapsedSubProcess(oldShape) || !isCollapsedSubProcess(newShape)) {
  127. return;
  128. }
  129. // old plane could have content,
  130. // we remove it so it is not recursively deleted from 'shape.delete'
  131. context.oldRoot = canvas.removeRootElement(getPlaneIdFromShape(oldShape));
  132. }, true);
  133. this.postExecuted('shape.replace', function(context) {
  134. var newShape = context.newShape,
  135. source = context.oldRoot,
  136. target = canvas.findRoot(getPlaneIdFromShape(newShape));
  137. if (!source || !target) {
  138. return;
  139. }
  140. var elements = source.children;
  141. modeling.moveElements(elements, { x: 0, y: 0 }, target);
  142. }, true);
  143. // rename primary elements when the secondary element changes
  144. // this ensures rootElement.id = element.id + '_plane'
  145. this.executed('element.updateProperties', function(context) {
  146. var shape = context.element;
  147. if (!is(shape, 'bpmn:SubProcess')) {
  148. return;
  149. }
  150. var properties = context.properties;
  151. var oldProperties = context.oldProperties;
  152. var oldId = oldProperties.id,
  153. newId = properties.id;
  154. if (oldId === newId) {
  155. return;
  156. }
  157. if (isPlane(shape)) {
  158. elementRegistry.updateId(shape, toPlaneId(newId));
  159. elementRegistry.updateId(oldId, newId);
  160. return;
  161. }
  162. var planeElement = elementRegistry.get(toPlaneId(oldId));
  163. if (!planeElement) {
  164. return;
  165. }
  166. elementRegistry.updateId(toPlaneId(oldId), toPlaneId(newId));
  167. }, true);
  168. this.reverted('element.updateProperties', function(context) {
  169. var shape = context.element;
  170. if (!is(shape, 'bpmn:SubProcess')) {
  171. return;
  172. }
  173. var properties = context.properties;
  174. var oldProperties = context.oldProperties;
  175. var oldId = oldProperties.id,
  176. newId = properties.id;
  177. if (oldId === newId) {
  178. return;
  179. }
  180. if (isPlane(shape)) {
  181. elementRegistry.updateId(shape, toPlaneId(oldId));
  182. elementRegistry.updateId(newId, oldId);
  183. return;
  184. }
  185. var planeElement = elementRegistry.get(toPlaneId(newId));
  186. if (!planeElement) {
  187. return;
  188. }
  189. elementRegistry.updateId(planeElement, toPlaneId(oldId));
  190. }, true);
  191. // re-throw element.changed to re-render primary shape if associated plane has
  192. // changed (e.g. bpmn:name property has changed)
  193. eventBus.on('element.changed', function(context) {
  194. var element = context.element;
  195. if (!isPlane(element)) {
  196. return;
  197. }
  198. var plane = element;
  199. var primaryShape = elementRegistry.get(getShapeIdFromPlane(plane));
  200. // do not re-throw if no associated primary shape (e.g. bpmn:Process)
  201. if (!primaryShape || primaryShape === plane) {
  202. return;
  203. }
  204. eventBus.fire('element.changed', { element: primaryShape });
  205. });
  206. // create/remove plane for the subprocess
  207. this.executed('shape.toggleCollapse', LOW_PRIORITY, function(context) {
  208. var shape = context.shape;
  209. if (!is(shape, 'bpmn:SubProcess')) {
  210. return;
  211. }
  212. if (!isExpanded(shape)) {
  213. createRoot(context);
  214. self._showRecursively(shape.children);
  215. } else {
  216. removeRoot(context);
  217. }
  218. }, true);
  219. // create/remove plane for the subprocess
  220. this.reverted('shape.toggleCollapse', LOW_PRIORITY, function(context) {
  221. var shape = context.shape;
  222. if (!is(shape, 'bpmn:SubProcess')) {
  223. return;
  224. }
  225. if (!isExpanded(shape)) {
  226. createRoot(context);
  227. self._showRecursively(shape.children);
  228. } else {
  229. removeRoot(context);
  230. }
  231. }, true);
  232. // move elements between planes
  233. this.postExecuted('shape.toggleCollapse', HIGH_PRIORITY, function(context) {
  234. var shape = context.shape;
  235. if (!is(shape, 'bpmn:SubProcess')) {
  236. return;
  237. }
  238. var rootElement = context.newRootElement;
  239. if (!rootElement) {
  240. return;
  241. }
  242. if (!isExpanded(shape)) {
  243. // collapsed
  244. self._moveChildrenToShape(shape, rootElement);
  245. } else {
  246. self._moveChildrenToShape(rootElement, shape);
  247. }
  248. }, true);
  249. // copy-paste ///////////
  250. // add elements in plane to tree
  251. eventBus.on('copyPaste.createTree', function(context) {
  252. var element = context.element,
  253. children = context.children;
  254. if (!isCollapsedSubProcess(element)) {
  255. return;
  256. }
  257. var id = getPlaneIdFromShape(element);
  258. var parent = elementRegistry.get(id);
  259. if (parent) {
  260. // do not copy invisible root element
  261. children.push.apply(children, parent.children);
  262. }
  263. });
  264. // set plane children as direct children of collapsed shape
  265. eventBus.on('copyPaste.copyElement', function(context) {
  266. var descriptor = context.descriptor,
  267. element = context.element,
  268. elements = context.elements;
  269. var parent = element.parent;
  270. var isPlane = is(getDi(parent), 'bpmndi:BPMNPlane');
  271. if (!isPlane) {
  272. return;
  273. }
  274. var parentId = getShapeIdFromPlane(parent);
  275. var referencedShape = find(elements, function(element) {
  276. return element.id === parentId;
  277. });
  278. if (!referencedShape) {
  279. return;
  280. }
  281. descriptor.parent = referencedShape.id;
  282. });
  283. // hide children during pasting
  284. eventBus.on('copyPaste.pasteElement', function(context) {
  285. var descriptor = context.descriptor;
  286. if (!descriptor.parent) {
  287. return;
  288. }
  289. if (isCollapsedSubProcess(descriptor.parent) || descriptor.parent.hidden) {
  290. descriptor.hidden = true;
  291. }
  292. });
  293. }
  294. inherits(SubProcessPlaneBehavior, CommandInterceptor);
  295. /**
  296. * Moves the child elements from source to target.
  297. *
  298. * If the target is a plane, the children are moved to the top left corner.
  299. * Otherwise, the center of the target is used.
  300. *
  301. * @param {Root} source
  302. * @param {Root} target
  303. */
  304. SubProcessPlaneBehavior.prototype._moveChildrenToShape = function(source, target) {
  305. var modeling = this._modeling;
  306. var children = source.children;
  307. var offset;
  308. if (!children) {
  309. return;
  310. }
  311. // add external labels that weren't children of sub process
  312. children = children.concat(children.reduce(function(labels, child) {
  313. if (child.label && child.label.parent !== source) {
  314. return labels.concat(child.label);
  315. }
  316. return labels;
  317. }, []));
  318. // only change plane if there are no visible children, but don't move them
  319. var visibleChildren = children.filter(function(child) {
  320. return !child.hidden;
  321. });
  322. if (!visibleChildren.length) {
  323. modeling.moveElements(children, { x: 0, y: 0 }, target, { autoResize: false });
  324. return;
  325. }
  326. var childrenBounds = getBBox(visibleChildren);
  327. // target is a plane
  328. if (!target.x) {
  329. offset = {
  330. x: DEFAULT_POSITION.x - childrenBounds.x,
  331. y: DEFAULT_POSITION.y - childrenBounds.y
  332. };
  333. }
  334. // source is a plane
  335. else {
  336. // move relative to the center of the shape
  337. var targetMid = getMid(target);
  338. var childrenMid = getMid(childrenBounds);
  339. offset = {
  340. x: targetMid.x - childrenMid.x,
  341. y: targetMid.y - childrenMid.y
  342. };
  343. }
  344. modeling.moveElements(children, offset, target, { autoResize: false });
  345. };
  346. /**
  347. * Sets `hidden` property on all children of the given shape.
  348. *
  349. * @param {Element[]} elements
  350. * @param {boolean} [hidden=false]
  351. *
  352. * @return {Element[]}
  353. */
  354. SubProcessPlaneBehavior.prototype._showRecursively = function(elements, hidden) {
  355. var self = this;
  356. var result = [];
  357. elements.forEach(function(element) {
  358. element.hidden = !!hidden;
  359. result = result.concat(element);
  360. if (element.children) {
  361. result = result.concat(
  362. self._showRecursively(element.children, element.collapsed || hidden)
  363. );
  364. }
  365. });
  366. return result;
  367. };
  368. /**
  369. * Adds a given root element to the BPMNDI diagrams.
  370. *
  371. * @param {Root|ModdleElement} planeElement
  372. *
  373. * @return {Root}
  374. */
  375. SubProcessPlaneBehavior.prototype._addDiagram = function(planeElement) {
  376. var bpmnjs = this._bpmnjs;
  377. var diagrams = bpmnjs.getDefinitions().diagrams;
  378. if (!planeElement.businessObject) {
  379. planeElement = this._createNewDiagram(planeElement);
  380. }
  381. diagrams.push(planeElement.di.$parent);
  382. return planeElement;
  383. };
  384. /**
  385. * Creates a new plane element for the given sub process.
  386. *
  387. * @param {ModdleElement} bpmnElement
  388. *
  389. * @return {Root}
  390. */
  391. SubProcessPlaneBehavior.prototype._createNewDiagram = function(bpmnElement) {
  392. var bpmnFactory = this._bpmnFactory,
  393. elementFactory = this._elementFactory;
  394. var diPlane = bpmnFactory.create('bpmndi:BPMNPlane', {
  395. bpmnElement: bpmnElement
  396. });
  397. var diDiagram = bpmnFactory.create('bpmndi:BPMNDiagram', {
  398. plane: diPlane
  399. });
  400. diPlane.$parent = diDiagram;
  401. // add a virtual element (not being drawn),
  402. // a copy cat of our BpmnImporter code
  403. var planeElement = elementFactory.createRoot({
  404. id: getPlaneIdFromShape(bpmnElement),
  405. type: bpmnElement.$type,
  406. di: diPlane,
  407. businessObject: bpmnElement,
  408. collapsed: true
  409. });
  410. return planeElement;
  411. };
  412. /**
  413. * Removes the diagram for a given root element.
  414. *
  415. * @param {Root} rootElement
  416. *
  417. * @return {ModdleElement}
  418. */
  419. SubProcessPlaneBehavior.prototype._removeDiagram = function(rootElement) {
  420. var bpmnjs = this._bpmnjs;
  421. var diagrams = bpmnjs.getDefinitions().diagrams;
  422. var removedDiagram = find(diagrams, function(diagram) {
  423. return diagram.plane.bpmnElement.id === rootElement.id;
  424. });
  425. diagrams.splice(diagrams.indexOf(removedDiagram), 1);
  426. return removedDiagram;
  427. };
  428. SubProcessPlaneBehavior.$inject = [
  429. 'canvas',
  430. 'eventBus',
  431. 'modeling',
  432. 'elementFactory',
  433. 'bpmnFactory',
  434. 'bpmnjs',
  435. 'elementRegistry'
  436. ];