In JointJs, how can I move a all elements together

2019-09-15 04:07发布

问题:

I'm using JointJs to create a diagram.

When I create an element with a number of embedded elements, I can easily drag them all by dragging the parent. However, when I drag the children, they move on their own. How can I move the parent and all children by dragging either the parent or one of the children elements?

回答1:

it's a little bit tricky:

https://jsfiddle.net/vtalas/xk73L947/

the magic is in this part:

paper.on('cell:pointermove', function (cellView, evt) {

  if (cellView.model.isLink()) {
    return ;
  }

  var parent = cellView.model.getAncestors()[0];

  // if we trying to move with embedded cell 
  if (parent) {

    // cancel move for the child (currently dragged element)
    cellView.pointerup(evt);
    var view = paper.findViewByModel(parent);

    // substitute currently dragged element with the parent
    paper.sourceView = view;

    // get parent's position and continue dragging (with the parent, children are updated automaticaly)
    var localPoint = paper.snapToGrid({ x: evt.clientX, y: evt.clientY });
    view.pointerdown(evt, localPoint.x, localPoint.y);
  }
});