Prevent dangling reused edge

This commit is contained in:
David Kaltschmidt
2015-10-29 17:02:45 +00:00
parent 1831e07224
commit 6cf93f8e17

View File

@@ -155,6 +155,24 @@ export function hasUnseenNodes(nodes, cache) {
return hasUnseen;
}
/**
* Determine if edge has same endpoints in new nodes as well as in the nodeCache
* @param {Map} edge Edge with source and target
* @param {Map} nodes new node set
* @param {Map} nodeCache set of previous nodes
* @return {Boolean} True if old and new endpoints have same coordinates
*/
function hasSameEndpoints(edge, nodes, nodeCache) {
const oldSource = nodeCache.get(edge.get('source'));
const oldTarget = nodeCache.get(edge.get('target'));
const newSource = nodes.get(edge.get('source'));
const newTarget = nodes.get(edge.get('target'));
return (oldSource.get('x') === newSource.get('x')
&& oldSource.get('y') === newSource.get('y')
&& oldTarget.get('x') === newTarget.get('x')
&& oldTarget.get('y') === newTarget.get('y'));
}
/**
* Clones a previous layout
* @param {Object} layout Layout object
@@ -180,7 +198,7 @@ function copyLayoutProperties(layout, nodeCache, edgeCache) {
return node.merge(nodeCache.get(node.get('id')));
});
layout.edges = layout.edges.map(edge => {
if (edgeCache.has(edge.get('id'))) {
if (edgeCache.has(edge.get('id')) && hasSameEndpoints(edge, layout.nodes, nodeCache)) {
return edge.merge(edgeCache.get(edge.get('id')));
}
return setSimpleEdgePoints(edge, nodeCache);