实现这个功能非常简单,只需要重写moveCells方法就可以了。下面是源文件中的代码:
mxGraph.prototype.moveCells = function(cells, dx, dy, clone, target, evt) {
if (cells != null && (dx != 0 || dy != 0 || clone || target != null)) {
this.model.beginUpdate();
try {
if (clone) {
cells = this.cloneCells(cells, this.isCloneInvalidEdges());
if (target == null) {
target = this.getDefaultParent();
}
}
this.cellsMoved(cells, dx, dy, !clone && this.isDisconnectOnMove() && this.isAllowDanglingEdges(), target == null);
if (target != null) {
var index = this.model.getChildCount(target);
this.cellsAdded(cells, target, index, null, null, true);
}
this.fireEvent(new mxEventObject(mxEvent.MOVE_CELLS, 'cells', cells, 'dx', dx, 'dy', dy, 'clone', clone, 'target', target, 'event', evt));
} finally {
this.model.endUpdate();
}
}
return cells;
};
接下来要对这个方法进行改造。加一句就可以了
mxGraph.prototype.moveCells = function(cells, dx, dy, clone, target, evt) {
clone=evt.ctrlKey;//对,就是这啦!
if (cells != null && (dx != 0 || dy != 0 || clone || target != null)) {
this.model.beginUpdate();
try {
if (clone) {
cells = this.cloneCells(cells, this.isCloneInvalidEdges());
if (target == null) {
target = this.getDefaultParent();
}
}
this.cellsMoved(cells, dx, dy, !clone && this.isDisconnectOnMove() && this.isAllowDanglingEdges(), target == null);
if (target != null) {
var index = this.model.getChildCount(target);
this.cellsAdded(cells, target, index, null, null, true);
}
this.fireEvent(new mxEventObject(mxEvent.MOVE_CELLS, 'cells', cells, 'dx', dx, 'dy', dy, 'clone', clone, 'target', target, 'event', evt));
} finally {
this.model.endUpdate();
}
}
return cells;
};
是的,实现这个功能的确很简单。但是往往实际项目中会有不同的需求。比如一个数据库关系图,复制一个字段到另一张表中的时候;选择了多个图形并且包括关系线的是时候是否需要复制关系;如果图形存在子图形,是否需要一同复制;当前选择的图形是不是允许移动/复制;移动进入目标图形,目标图形是否允许该操作等等,这些就需要在这个方法区域中进行复杂的判断。