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

Handle node imports containing tab nodes

Closes #172
This commit is contained in:
Nick O'Leary 2014-09-08 10:53:18 +01:00
parent a03861cb6d
commit 995268c5df
2 changed files with 66 additions and 49 deletions

View File

@ -309,6 +309,9 @@ RED.nodes = (function() {
//"DO NOT DEPLOY while in this state.<br/>Either, add missing types to Node-RED, restart and then reload page,<br/>or delete unknown "+n.name+", rewire as required, and then deploy.","error"); //"DO NOT DEPLOY while in this state.<br/>Either, add missing types to Node-RED, restart and then reload page,<br/>or delete unknown "+n.name+", rewire as required, and then deploy.","error");
} }
var new_workspaces = [];
var workspace_map = {};
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
@ -319,14 +322,21 @@ RED.nodes = (function() {
if (defaultWorkspace == null) { if (defaultWorkspace == null) {
defaultWorkspace = n; defaultWorkspace = n;
} }
if (createNewIds) {
var nid = getID();
workspace_map[n.id] = nid;
n.id = nid;
}
addWorkspace(n); addWorkspace(n);
RED.view.addWorkspace(n); RED.view.addWorkspace(n);
new_workspaces.push(n);
} }
} }
if (defaultWorkspace == null) { if (defaultWorkspace == null) {
defaultWorkspace = { type:"tab", id:getID(), label:"Sheet 1" }; defaultWorkspace = { type:"tab", id:getID(), label:"Sheet 1" };
addWorkspace(defaultWorkspace); addWorkspace(defaultWorkspace);
RED.view.addWorkspace(defaultWorkspace); RED.view.addWorkspace(defaultWorkspace);
new_workspaces.push(defaultWorkspace);
} }
var node_map = {}; var node_map = {};
@ -353,7 +363,10 @@ RED.nodes = (function() {
} else { } else {
var node = {x:n.x,y:n.y,z:n.z,type:0,wires:n.wires,changed:false}; var node = {x:n.x,y:n.y,z:n.z,type:0,wires:n.wires,changed:false};
if (createNewIds) { if (createNewIds) {
node.z = RED.view.getWorkspace(); node.z = workspace_map[node.z];
if (!workspaces[node.z]) {
node.z = RED.view.getWorkspace();
}
node.id = getID(); node.id = getID();
} else { } else {
node.id = n.id; node.id = n.id;
@ -401,7 +414,7 @@ RED.nodes = (function() {
} }
delete n.wires; delete n.wires;
} }
return [new_nodes,new_links]; return [new_nodes,new_links,new_workspaces];
} catch(error) { } catch(error) {
//TODO: get this UI thing out of here! (see above as well) //TODO: get this UI thing out of here! (see above as well)
RED.notify("<strong>Error</strong>: "+error,"error"); RED.notify("<strong>Error</strong>: "+error,"error");

View File

@ -1397,61 +1397,65 @@ RED.view = (function() {
if (result) { if (result) {
var new_nodes = result[0]; var new_nodes = result[0];
var new_links = result[1]; var new_links = result[1];
var new_ms = new_nodes.map(function(n) { n.z = activeWorkspace; return {n:n};}); var new_workspaces = result[2];
var new_node_ids = new_nodes.map(function(n){ return n.id; });
// TODO: pick a more sensible root node
var root_node = new_ms[0].n;
var dx = root_node.x;
var dy = root_node.y;
if (mouse_position == null) {
mouse_position = [0,0];
}
var minX = 0;
var minY = 0;
var i;
var node;
for (i=0;i<new_ms.length;i++) { var new_ms = new_nodes.filter(function(n) { return n.z == activeWorkspace }).map(function(n) { return {n:n};});
node = new_ms[i]; var new_node_ids = new_nodes.map(function(n){ return n.id; });
node.n.selected = true;
node.n.changed = true; // TODO: pick a more sensible root node
node.n.x -= dx - mouse_position[0]; if (new_ms.length > 0) {
node.n.y -= dy - mouse_position[1]; var root_node = new_ms[0].n;
node.dx = node.n.x - mouse_position[0]; var dx = root_node.x;
node.dy = node.n.y - mouse_position[1]; var dy = root_node.y;
minX = Math.min(node.n.x-node_width/2-5,minX);
minY = Math.min(node.n.y-node_height/2-5,minY); if (mouse_position == null) {
} mouse_position = [0,0];
for (i=0;i<new_ms.length;i++) { }
node = new_ms[i];
node.n.x -= minX; var minX = 0;
node.n.y -= minY; var minY = 0;
node.dx -= minX; var i;
node.dy -= minY; var node;
}
if (!touchImport) { for (i=0;i<new_ms.length;i++) {
mouse_mode = RED.state.IMPORT_DRAGGING; node = new_ms[i];
node.n.selected = true;
node.n.changed = true;
node.n.x -= dx - mouse_position[0];
node.n.y -= dy - mouse_position[1];
node.dx = node.n.x - mouse_position[0];
node.dy = node.n.y - mouse_position[1];
minX = Math.min(node.n.x-node_width/2-5,minX);
minY = Math.min(node.n.y-node_height/2-5,minY);
}
for (i=0;i<new_ms.length;i++) {
node = new_ms[i];
node.n.x -= minX;
node.n.y -= minY;
node.dx -= minX;
node.dy -= minY;
}
if (!touchImport) {
mouse_mode = RED.state.IMPORT_DRAGGING;
}
RED.keyboard.add(/* ESCAPE */ 27,function(){
RED.keyboard.remove(/* ESCAPE */ 27);
clearSelection();
RED.history.pop();
mouse_mode = 0;
});
clearSelection();
moving_set = new_ms;
} }
RED.keyboard.add(/* ESCAPE */ 27,function(){ RED.history.push({t:'add',nodes:new_node_ids,links:new_links,workspaces:new_workspaces,dirty:RED.view.dirty()});
RED.keyboard.remove(/* ESCAPE */ 27);
clearSelection();
RED.history.pop();
mouse_mode = 0;
});
RED.history.push({t:'add',nodes:new_node_ids,links:new_links,dirty:RED.view.dirty()});
clearSelection();
moving_set = new_ms;
redraw(); redraw();
} }
} catch(error) { } catch(error) {
console.log(error); console.log(error.stack);
RED.notify("<strong>Error</strong>: "+error,"error"); RED.notify("<strong>Error</strong>: "+error,"error");
} }
} }