1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Improve handling of imported config nodes

Avoid creating unnecessary duplicates of config nodes, whilst
honouring the scope of the any existing node.

Also, 'undo' now removes any added config node
This commit is contained in:
Nick O'Leary 2015-09-25 17:43:28 +01:00
parent 719bb4263e
commit ef10ade0cc
2 changed files with 70 additions and 32 deletions

View File

@ -291,7 +291,6 @@ RED.nodes = (function() {
var result = removeNode(removedNodes[n].id); var result = removeNode(removedNodes[n].id);
removedLinks = removedLinks.concat(result.links); removedLinks = removedLinks.concat(result.links);
} }
console.log(removedLinks);
return {nodes:removedNodes,links:removedLinks}; return {nodes:removedNodes,links:removedLinks};
} }
@ -554,8 +553,11 @@ RED.nodes = (function() {
return nns; return nns;
} }
function compareNodes(nodeA,nodeB) { function compareNodes(nodeA,nodeB,idMustMatch) {
if (nodeA.id != nodeB.id || nodeA.type != nodeB.type) { if (idMustMatch && nodeA.id != nodeB.id) {
return false;
}
if (nodeA.type != nodeB.type) {
return false; return false;
} }
var def = nodeA._def; var def = nodeA._def;
@ -694,26 +696,6 @@ RED.nodes = (function() {
}); });
new_subflows.push(n); new_subflows.push(n);
addSubflow(n,createNewIds); addSubflow(n,createNewIds);
} else {
def = registry.getNodeType(n.type);
if (def && def.category == "config") {
var existingConfigNode = RED.nodes.node(n.id);
if (!existingConfigNode || !compareNodes(existingConfigNode,n) || existingConfigNode._def.exclusive) {
var configNode = {id:n.id, z:n.z, type:n.type, users:[]};
for (var d in def.defaults) {
if (def.defaults.hasOwnProperty(d)) {
configNode[d] = n[d];
}
}
configNode.label = def.label;
configNode._def = def;
if (existingConfigNode || createNewIds) {
configNode.id = getID();
}
node_map[n.id] = configNode;
RED.nodes.add(configNode);
}
}
} }
} }
if (defaultWorkspace == null) { if (defaultWorkspace == null) {
@ -724,6 +706,60 @@ RED.nodes = (function() {
activeWorkspace = RED.workspaces.active(); activeWorkspace = RED.workspaces.active();
} }
for (i=0;i<newNodes.length;i++) {
n = newNodes[i];
def = registry.getNodeType(n.type);
if (def && def.category == "config") {
var existingConfigNode = null;
if (createNewIds) {
if (n.z) {
if (subflow_map[n.z]) {
n.z = subflow_map[n.z].id;
} else {
n.z = workspace_map[n.z];
if (!workspaces[n.z]) {
n.z = activeWorkspace;
}
}
}
existingConfigNode = RED.nodes.node(n.id);
if (existingConfigNode) {
if (n.z && existingConfigNode.z !== n.z) {
existingConfigNode = null;
// Check the config nodes on n.z
for (var cn in configNodes) {
if (configNodes.hasOwnProperty(cn)) {
if (configNodes[cn].z === n.z && compareNodes(configNodes[cn],n,false)) {
existingConfigNode = configNodes[cn];
node_map[n.id] = configNodes[cn];
break;
}
}
}
}
}
}
if (!existingConfigNode) { //} || !compareNodes(existingConfigNode,n,true) || existingConfigNode._def.exclusive || existingConfigNode.z !== n.z) {
var configNode = {id:n.id, z:n.z, type:n.type, users:[]};
for (var d in def.defaults) {
if (def.defaults.hasOwnProperty(d)) {
configNode[d] = n[d];
}
}
configNode.label = def.label;
configNode._def = def;
if (createNewIds) {
configNode.id = getID();
}
node_map[n.id] = configNode;
new_nodes.push(configNode);
RED.nodes.add(configNode);
}
}
}
for (i=0;i<newNodes.length;i++) { for (i=0;i<newNodes.length;i++) {
n = newNodes[i]; n = newNodes[i];
// TODO: remove workspace in next release+1 // TODO: remove workspace in next release+1
@ -818,6 +854,7 @@ RED.nodes = (function() {
} }
for (i=0;i<new_nodes.length;i++) { for (i=0;i<new_nodes.length;i++) {
n = new_nodes[i]; n = new_nodes[i];
if (n.wires) {
for (var w1=0;w1<n.wires.length;w1++) { for (var w1=0;w1<n.wires.length;w1++) {
var wires = (n.wires[w1] instanceof Array)?n.wires[w1]:[n.wires[w1]]; var wires = (n.wires[w1] instanceof Array)?n.wires[w1]:[n.wires[w1]];
for (var w2=0;w2<wires.length;w2++) { for (var w2=0;w2<wires.length;w2++) {
@ -830,6 +867,7 @@ RED.nodes = (function() {
} }
delete n.wires; delete n.wires;
} }
}
for (i=0;i<new_subflows.length;i++) { for (i=0;i<new_subflows.length;i++) {
n = new_subflows[i]; n = new_subflows[i];
n.in.forEach(function(input) { n.in.forEach(function(input) {

View File

@ -1698,7 +1698,7 @@ RED.view = (function() {
var new_workspaces = result[2]; var new_workspaces = result[2];
var new_subflows = result[3]; var new_subflows = result[3];
var new_ms = new_nodes.filter(function(n) { return n.z == RED.workspaces.active() }).map(function(n) { return {n:n};}); var new_ms = new_nodes.filter(function(n) { return n.hasOwnProperty('x') && n.hasOwnProperty('y') && n.z == RED.workspaces.active() }).map(function(n) { return {n:n};});
var new_node_ids = new_nodes.map(function(n){ return n.id; }); var new_node_ids = new_nodes.map(function(n){ return n.id; });
// TODO: pick a more sensible root node // TODO: pick a more sensible root node