在画布定义中,设置validCycle的验证规则,只能对用户在手动在画布上绘制线进行验证,如果是addLinkData添加的线则没有生效,所以我们需要在addLinkData前先判断一下是否符合validCycle的验证规则,符合才允许添加
主要用到的有toolManager.linkingTool工具基类中的isValidCycle验证函数
isValidCycle(from, to, ignore) {boolean} 1.5 |
Checks whether a proposed link would be valid according to Diagram.validCycle. This does not distinguish between different ports on a node, so this method does not need to take port arguments. This is called by isValidLink. This method may be overridden, but we recommend that you call this base method. It is rare that you will want to override this method. Please read the Introduction page on Extensions for how to override methods and how to call this base method. Parameters: {Node} from 源节点对象 {Node} to 目标节点对象 {Link} ignore 忽略验证的线 may be null; this is useful during relinking to ignore the originalLink Returns: {boolean} |
用例
//添加通过验证规则的线 源节点 fromkey ,目标节点toKey
function addIsValidLink(fromkey, toKey) {
var node1 = diagram.findNodeForKey(fromkey);
var node2 = diagram.findNodeForKey(toKey);
var tool = diagram.toolManager.linkingTool;
var isValid = tool.isValidCycle(node1, node2);
console.log(isValid);
if(isValid) {
data = {};
data.from = fromkey;
data.to = toKey;
diagram.model.addLinkData(data);
}
}