UpdateCanvasRootHandler.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import {
  2. add as collectionAdd,
  3. remove as collectionRemove
  4. } from 'diagram-js/lib/util/Collections';
  5. export default function UpdateCanvasRootHandler(canvas, modeling) {
  6. this._canvas = canvas;
  7. this._modeling = modeling;
  8. }
  9. UpdateCanvasRootHandler.$inject = [
  10. 'canvas',
  11. 'modeling'
  12. ];
  13. UpdateCanvasRootHandler.prototype.execute = function(context) {
  14. var canvas = this._canvas;
  15. var newRoot = context.newRoot,
  16. newRootBusinessObject = newRoot.businessObject,
  17. oldRoot = canvas.getRootElement(),
  18. oldRootBusinessObject = oldRoot.businessObject,
  19. bpmnDefinitions = oldRootBusinessObject.$parent,
  20. diPlane = oldRootBusinessObject.di;
  21. // (1) replace process old <> new root
  22. canvas.setRootElement(newRoot, true);
  23. // (2) update root elements
  24. collectionAdd(bpmnDefinitions.rootElements, newRootBusinessObject);
  25. newRootBusinessObject.$parent = bpmnDefinitions;
  26. collectionRemove(bpmnDefinitions.rootElements, oldRootBusinessObject);
  27. oldRootBusinessObject.$parent = null;
  28. // (3) wire di
  29. oldRootBusinessObject.di = null;
  30. diPlane.bpmnElement = newRootBusinessObject;
  31. newRootBusinessObject.di = diPlane;
  32. context.oldRoot = oldRoot;
  33. // TODO(nikku): return changed elements?
  34. // return [ newRoot, oldRoot ];
  35. };
  36. UpdateCanvasRootHandler.prototype.revert = function(context) {
  37. var canvas = this._canvas;
  38. var newRoot = context.newRoot,
  39. newRootBusinessObject = newRoot.businessObject,
  40. oldRoot = context.oldRoot,
  41. oldRootBusinessObject = oldRoot.businessObject,
  42. bpmnDefinitions = newRootBusinessObject.$parent,
  43. diPlane = newRootBusinessObject.di;
  44. // (1) replace process old <> new root
  45. canvas.setRootElement(oldRoot, true);
  46. // (2) update root elements
  47. collectionRemove(bpmnDefinitions.rootElements, newRootBusinessObject);
  48. newRootBusinessObject.$parent = null;
  49. collectionAdd(bpmnDefinitions.rootElements, oldRootBusinessObject);
  50. oldRootBusinessObject.$parent = bpmnDefinitions;
  51. // (3) wire di
  52. newRootBusinessObject.di = null;
  53. diPlane.bpmnElement = oldRootBusinessObject;
  54. oldRootBusinessObject.di = diPlane;
  55. // TODO(nikku): return changed elements?
  56. // return [ newRoot, oldRoot ];
  57. };