From a0f91aa81473b5b7253f442af7f4d113013ae43e Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Sat, 24 Sep 2016 22:20:28 +0100 Subject: [PATCH] Avoid duplicating existing subflows on import --- editor/js/nodes.js | 103 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 79 insertions(+), 24 deletions(-) diff --git a/editor/js/nodes.js b/editor/js/nodes.js index fdb9ac76b..0a8efa2a9 100644 --- a/editor/js/nodes.js +++ b/editor/js/nodes.js @@ -565,6 +565,45 @@ RED.nodes = (function() { return nns; } + function checkForMatchingSubflow(subflow,subflowNodes) { + var i; + var match = null; + try { + RED.nodes.eachSubflow(function(sf) { + if (sf.name != subflow.name || + sf.info != subflow.info || + sf.in.length != subflow.in.length || + sf.out.length != subflow.out.length) { + return; + } + var sfNodes = RED.nodes.filterNodes({z:sf.id}); + if (sfNodes.length != subflowNodes.length) { + return; + } + + var subflowNodeSet = [subflow].concat(subflowNodes); + var sfNodeSet = [sf].concat(sfNodes); + + var exportableSubflowNodes = JSON.stringify(subflowNodeSet); + var exportableSFNodes = JSON.stringify(createExportableNodeSet(sfNodeSet)); + var nodeMap = {}; + for (i=0;i 0) { var typeList = ""; @@ -662,6 +707,7 @@ RED.nodes = (function() { var workspace_map = {}; var new_subflows = []; var subflow_map = {}; + var subflow_blacklist = {}; var node_map = {}; var new_nodes = []; var new_links = []; @@ -690,28 +736,33 @@ RED.nodes = (function() { RED.workspaces.add(n); new_workspaces.push(n); } else if (n.type === "subflow") { - subflow_map[n.id] = n; - if (createNewIds) { - nid = getID(); - n.id = nid; + var matchingSubflow = checkForMatchingSubflow(n,nodeZmap[n.id]); + if (matchingSubflow) { + subflow_blacklist[n.id] = matchingSubflow; + } else { + subflow_map[n.id] = n; + if (createNewIds) { + nid = getID(); + n.id = nid; + } + // TODO: handle createNewIds - map old to new subflow ids + n.in.forEach(function(input,i) { + input.type = "subflow"; + input.direction = "in"; + input.z = n.id; + input.i = i; + input.id = getID(); + }); + n.out.forEach(function(output,i) { + output.type = "subflow"; + output.direction = "out"; + output.z = n.id; + output.i = i; + output.id = getID(); + }); + new_subflows.push(n); + addSubflow(n,createNewIds); } - // TODO: handle createNewIds - map old to new subflow ids - n.in.forEach(function(input,i) { - input.type = "subflow"; - input.direction = "in"; - input.z = n.id; - input.i = i; - input.id = getID(); - }); - n.out.forEach(function(output,i) { - output.type = "subflow"; - output.direction = "out"; - output.z = n.id; - output.i = i; - output.id = getID(); - }); - new_subflows.push(n); - addSubflow(n,createNewIds); } } @@ -732,7 +783,9 @@ RED.nodes = (function() { var existingConfigNode = null; if (createNewIds) { if (n.z) { - if (subflow_map[n.z]) { + if (subflow_blacklist[n.z]) { + continue; + } else if (subflow_map[n.z]) { n.z = subflow_map[n.z].id; } else { n.z = workspace_map[n.z]; @@ -796,7 +849,9 @@ RED.nodes = (function() { if (!def || def.category != "config") { var node = {x:n.x,y:n.y,z:n.z,type:0,wires:n.wires,changed:false}; if (createNewIds) { - if (subflow_map[node.z]) { + if (subflow_blacklist[n.z]) { + continue; + } else if (subflow_map[node.z]) { node.z = subflow_map[node.z].id; } else { node.z = workspace_map[node.z]; @@ -831,7 +886,7 @@ RED.nodes = (function() { node._def = def; if (n.type.substring(0,7) === "subflow") { var parentId = n.type.split(":")[1]; - var subflow = subflow_map[parentId]||getSubflow(parentId); + var subflow = subflow_blacklist[parentId]||subflow_map[parentId]||getSubflow(parentId); if (createNewIds) { parentId = subflow.id; node.type = "subflow:"+parentId;