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

Copy/clone config nodes properly on import/export

This commit is contained in:
Nick O'Leary 2015-07-08 22:12:52 +01:00
parent f70e9ea076
commit fde9d40098
7 changed files with 82 additions and 18 deletions

View File

@ -210,6 +210,7 @@ RED.nodes = (function() {
function removeNode(id) {
var removedLinks = [];
var removedNodes = [];
var node;
if (id in configNodes) {
node = configNodes[id];
@ -231,8 +232,13 @@ RED.nodes = (function() {
var configNode = configNodes[node[d]];
if (configNode) {
updatedConfigNode = true;
var users = configNode.users;
users.splice(users.indexOf(node),1);
if (configNode._def.exclusive) {
removeNode(node[d]);
removedNodes.push(configNode);
} else {
var users = configNode.users;
users.splice(users.indexOf(node),1);
}
}
}
}
@ -246,7 +252,7 @@ RED.nodes = (function() {
if (node._def.onremove) {
node._def.onremove.call(n);
}
return removedLinks;
return {links:removedLinks,nodes:removedNodes};
}
function removeLink(l) {
@ -539,6 +545,32 @@ RED.nodes = (function() {
return nns;
}
function compareNodes(nodeA,nodeB) {
if (nodeA.id != nodeB.id || nodeA.type != nodeB.type) {
return false;
}
var def = nodeA._def;
for (var d in def.defaults) {
if (def.defaults.hasOwnProperty(d)) {
var vA = nodeA[d];
var vB = nodeB[d];
if (typeof vA !== typeof vB) {
return false;
}
if (vA === null || typeof vA === "string" || typeof vA === "number") {
if (vA !== vB) {
return false;
}
} else {
if (JSON.stringify(vA) !== JSON.stringify(vB)) {
return false;
}
}
}
}
return true;
}
function importNodes(newNodesObj,createNewIds) {
var i;
var n;
@ -607,6 +639,9 @@ RED.nodes = (function() {
var workspace_map = {};
var new_subflows = [];
var subflow_map = {};
var node_map = {};
var new_nodes = [];
var new_links = [];
var nid;
var def;
for (i=0;i<newNodes.length;i++) {
@ -653,7 +688,8 @@ RED.nodes = (function() {
} else {
def = registry.getNodeType(n.type);
if (def && def.category == "config") {
if (!RED.nodes.node(n.id)) {
var existingConfigNode = RED.nodes.node(n.id);
if (!existingConfigNode || !compareNodes(existingConfigNode,n) || existingConfigNode._def.exclusive) {
var configNode = {id:n.id,type:n.type,users:[]};
for (var d in def.defaults) {
if (def.defaults.hasOwnProperty(d)) {
@ -662,23 +698,23 @@ RED.nodes = (function() {
}
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) {
defaultWorkspace = { type:"tab", id:getID(), label:"Sheet 1" };
defaultWorkspace = { type:"tab", id:getID(), label:RED._('workspace.defaultName',{number:1})};
addWorkspace(defaultWorkspace);
RED.workspaces.add(defaultWorkspace);
new_workspaces.push(defaultWorkspace);
activeWorkspace = RED.workspaces.active();
}
var node_map = {};
var new_nodes = [];
var new_links = [];
for (i=0;i<newNodes.length;i++) {
n = newNodes[i];
// TODO: remove workspace in next release+1
@ -749,7 +785,15 @@ RED.nodes = (function() {
node.outputs = n.outputs||node._def.outputs;
for (var d2 in node._def.defaults) {
if (node._def.defaults.hasOwnProperty(d2)) {
node[d2] = n[d2];
if (node._def.defaults[d2].type) {
if (node_map[n[d2]]) {
node[d2] = node_map[n[d2]].id;
} else {
node[d2] = n[d2];
}
} else {
node[d2] = n[d2];
}
}
}
}

View File

@ -518,7 +518,8 @@ RED.editor = (function() {
for (var d in definition.defaults) {
if (definition.defaults.hasOwnProperty(d)) {
if (definition.defaults[d].type) {
if (definition.defaults[d].exclusive) {
var configTypeDef = RED.nodes.getType(definition.defaults[d].type);
if (configTypeDef.exclusive) {
prepareConfigNodeButton(node,d,definition.defaults[d].type);
} else {
prepareConfigNodeSelect(node,d,definition.defaults[d].type);

View File

@ -153,10 +153,14 @@ RED.subflow = (function() {
}
});
var removedConfigNodes = [];
for (var i=0;i<removedNodes.length;i++) {
var rmlinks = RED.nodes.remove(removedNodes[i].id);
removedLinks = removedLinks.concat(rmlinks);
var removedEntities = RED.nodes.remove(removedNodes[i].id);
removedLinks = removedLinks.concat(removedEntities.links);
removedConfigNodes = removedConfigNodes.concat(removedEntities.nodes);
}
// TODO: this whole delete logic should be in RED.nodes.removeSubflow..
removedNodes = removedNodes.concat(removedConfigNodes);
var activeSubflow = getSubflow();

View File

@ -756,9 +756,10 @@ RED.view = (function() {
if (node.x < 0) {
node.x = 25
}
var rmlinks = RED.nodes.remove(node.id);
var removedEntities = RED.nodes.remove(node.id);
removedNodes.push(node);
removedLinks = removedLinks.concat(rmlinks);
removedNodes = removedNodes.concat(removedEntities.nodes);
removedLinks = removedLinks.concat(removedEntities.links);
} else {
if (node.direction === "out") {
removedSubflowOutputs.push(node);
@ -851,8 +852,21 @@ RED.view = (function() {
var nns = [];
for (var n=0;n<moving_set.length;n++) {
var node = moving_set[n].n;
// The only time a node.type == subflow can be selected is the
// input/output 'proxy' nodes. They cannot be copied.
if (node.type != "subflow") {
for (var d in node._def.defaults) {
if (node._def.defaults.hasOwnProperty(d)) {
if (node._def.defaults[d].type) {
var configNode = RED.nodes.node(node[d]);
if (configNode && configNode._def.exclusive) {
nns.push(RED.nodes.convertNode(configNode));
}
}
}
}
nns.push(RED.nodes.convertNode(node));
//TODO: if the node has an exclusive config node, it should also be copied, to ensure it remains exclusive...
}
}
clipboard = JSON.stringify(nns);

View File

@ -29,9 +29,9 @@ RED.workspaces = (function() {
do {
workspaceIndex += 1;
//TODO: nls of Sheet
} while($("#workspace-tabs a[title='Sheet "+workspaceIndex+"']").size() !== 0);
} while($("#workspace-tabs a[title='"+RED._('workspace.defaultName',{number:workspaceIndex})+"']").size() !== 0);
ws = {type:"tab",id:tabId,label:"Sheet "+workspaceIndex};
ws = {type:"tab",id:tabId,label:RED._('workspace.defaultName',{number:workspaceIndex})};
RED.nodes.addWorkspace(ws);
workspace_tabs.addTab(ws);
workspace_tabs.activateTab(tabId);

View File

@ -9,6 +9,7 @@
}
},
"workspace": {
"defaultName": "Sheet __number__",
"renameSheet": "Rename sheet",
"confirmDelete": "Confirm delete",
"delete": "Are you sure you want to delete '__label__'?",

View File

@ -160,7 +160,7 @@
name: {value:""},
url: {value:"",required:true},
method: {value:"get",required:true},
swaggerDoc: {type:"swagger-doc", exclusive:true, required:false}
swaggerDoc: {type:"swagger-doc", required:false}
},
inputs:0,
outputs:1,