all files / bpmn-js/lib/features/modeling/cmd/ UpdateCanvasRootHandler.js

100% Statements 36/36
100% Branches 0/0
100% Functions 3/3
100% Lines 36/36
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81              1189× 1189×                 37×   37× 37× 37× 37× 37× 37×     37×     37× 37×   37× 37×     37×   37× 37×   37×               13×   13× 13× 13× 13× 13× 13×     13×     13× 13×   13× 13×     13×   13× 13×        
import {
  add as collectionAdd,
  remove as collectionRemove
} from 'diagram-js/lib/util/Collections';
 
 
export default function UpdateCanvasRootHandler(canvas, modeling) {
  this._canvas = canvas;
  this._modeling = modeling;
}
 
UpdateCanvasRootHandler.$inject = [
  'canvas',
  'modeling'
];
 
 
UpdateCanvasRootHandler.prototype.execute = function(context) {
 
  var canvas = this._canvas;
 
  var newRoot = context.newRoot,
      newRootBusinessObject = newRoot.businessObject,
      oldRoot = canvas.getRootElement(),
      oldRootBusinessObject = oldRoot.businessObject,
      bpmnDefinitions = oldRootBusinessObject.$parent,
      diPlane = oldRootBusinessObject.di;
 
  // (1) replace process old <> new root
  canvas.setRootElement(newRoot, true);
 
  // (2) update root elements
  collectionAdd(bpmnDefinitions.rootElements, newRootBusinessObject);
  newRootBusinessObject.$parent = bpmnDefinitions;
 
  collectionRemove(bpmnDefinitions.rootElements, oldRootBusinessObject);
  oldRootBusinessObject.$parent = null;
 
  // (3) wire di
  oldRootBusinessObject.di = null;
 
  diPlane.bpmnElement = newRootBusinessObject;
  newRootBusinessObject.di = diPlane;
 
  context.oldRoot = oldRoot;
 
  // TODO(nikku): return changed elements?
  // return [ newRoot, oldRoot ];
};
 
 
UpdateCanvasRootHandler.prototype.revert = function(context) {
 
  var canvas = this._canvas;
 
  var newRoot = context.newRoot,
      newRootBusinessObject = newRoot.businessObject,
      oldRoot = context.oldRoot,
      oldRootBusinessObject = oldRoot.businessObject,
      bpmnDefinitions = newRootBusinessObject.$parent,
      diPlane = newRootBusinessObject.di;
 
  // (1) replace process old <> new root
  canvas.setRootElement(oldRoot, true);
 
  // (2) update root elements
  collectionRemove(bpmnDefinitions.rootElements, newRootBusinessObject);
  newRootBusinessObject.$parent = null;
 
  collectionAdd(bpmnDefinitions.rootElements, oldRootBusinessObject);
  oldRootBusinessObject.$parent = bpmnDefinitions;
 
  // (3) wire di
  newRootBusinessObject.di = null;
 
  diPlane.bpmnElement = oldRootBusinessObject;
  oldRootBusinessObject.di = diPlane;
 
  // TODO(nikku): return changed elements?
  // return [ newRoot, oldRoot ];
};