all files / Github/diagram-js/lib/features/modeling/cmd/ DeleteShapeHandler.js

100% Statements 31/31
100% Branches 2/2
100% Functions 8/8
100% Lines 31/31
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 82 83 84 85 86 87 88 89 90 91 92 93 94 95                          1128× 1128×                   347×   347×     347×     21×     347× 38×       347× 105× 20×   85×               414×   414× 414×   414×     414×     414×   414×               94×   94× 94× 94×     94×   94×   94×       105×    
import {
  add as collectionAdd,
  indexOf as collectionIdx
} from '../../../util/Collections';
 
import { saveClear } from '../../../util/Removal';
 
 
/**
 * A handler that implements reversible deletion of shapes.
 *
 */
export default function DeleteShapeHandler(canvas, modeling) {
  this._canvas = canvas;
  this._modeling = modeling;
}
 
DeleteShapeHandler.$inject = [ 'canvas', 'modeling' ];
 
 
/**
 * - Remove connections
 * - Remove all direct children
 */
DeleteShapeHandler.prototype.preExecute = function(context) {
 
  var modeling = this._modeling;
 
  var shape = context.shape;
 
  // remove connections
  saveClear(shape.incoming, function(connection) {
    // To make sure that the connection isn't removed twice
    // For example if a container is removed
    modeling.removeConnection(connection, { nested: true });
  });
 
  saveClear(shape.outgoing, function(connection) {
    modeling.removeConnection(connection, { nested: true });
  });
 
  // remove child shapes and connections
  saveClear(shape.children, function(child) {
    if (isConnection(child)) {
      modeling.removeConnection(child, { nested: true });
    } else {
      modeling.removeShape(child, { nested: true });
    }
  });
};
 
/**
 * Remove shape and remember the parent
 */
DeleteShapeHandler.prototype.execute = function(context) {
  var canvas = this._canvas;
 
  var shape = context.shape,
      oldParent = shape.parent;
 
  context.oldParent = oldParent;
 
  // remove containment
  context.oldParentIndex = collectionIdx(oldParent.children, shape);
 
  // remove shape
  canvas.removeShape(shape);
 
  return shape;
};
 
 
/**
 * Command revert implementation
 */
DeleteShapeHandler.prototype.revert = function(context) {
 
  var canvas = this._canvas;
 
  var shape = context.shape,
      oldParent = context.oldParent,
      oldParentIndex = context.oldParentIndex;
 
  // restore containment
  collectionAdd(oldParent.children, shape, oldParentIndex);
 
  canvas.addShape(shape, oldParent);
 
  return shape;
};
 
function isConnection(element) {
  return element.waypoints;
}