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

Save/restore tabs

This commit is contained in:
Nicholas O'Leary 2013-10-25 21:34:00 +01:00
parent f3e33f4c29
commit 3604286793
5 changed files with 115 additions and 67 deletions

View File

@ -121,7 +121,7 @@
</div><!-- /palette -->
<div id="workspace">
<ul id="workspace-tabs"><li><a href="#tab-0">main</a></li></ul>
<ul id="workspace-tabs"></ul>
<div id="chart"></div>
</div>

View File

@ -21,12 +21,20 @@ RED.nodes = function() {
var configNodes = {};
var links = [];
var defaultWorkspace;
var workspaces = {};
function registerType(nt,def) {
node_defs[nt] = def;
// TODO: too tightly coupled into palette UI
RED.palette.add(nt,def);
}
function getID() {
return (1+Math.random()*4294967295).toString(16);
}
function getType(type) {
return node_defs[type];
}
@ -87,6 +95,10 @@ RED.nodes = function() {
}
}
function addWorkspace(ws) {
workspaces[ws.id] = ws;
}
function getAllFlowNodes(node) {
var visited = {};
visited[node.id] = true;
@ -167,6 +179,9 @@ RED.nodes = function() {
//TODO: rename this (createCompleteNodeSet)
function createCompleteNodeSet() {
var nns = [];
for (var i in workspaces) {
nns.push(workspaces[i]);
}
for (var i in configNodes) {
nns.push(convertNode(configNodes[i]));
}
@ -194,12 +209,27 @@ RED.nodes = function() {
}
for (var i in newNodes) {
var n = newNodes[i];
if (!getType(n.type)) {
if (n.type != "workspace" && !getType(n.type)) {
// TODO: get this UI thing out of here! (see below as well)
RED.notify("<strong>Failed to import nodes</strong>: unrecognised type '"+n.type+"'","error");
return null;
}
}
for (var i in newNodes) {
var n = newNodes[i];
if (n.type === "workspace") {
if (defaultWorkspace == null) {
defaultWorkspace = n;
}
addWorkspace(n);
RED.view.addWorkspace(n);
}
}
if (defaultWorkspace == null) {
defaultWorkspace = { type:"workspace", id:getID(), label:"default" };
addWorkspace(defaultWorkspace);
RED.view.addWorkspace(defaultWorkspace);
}
var node_map = {};
var new_nodes = [];
@ -207,54 +237,57 @@ RED.nodes = function() {
for (var i in newNodes) {
var n = newNodes[i];
var def = getType(n.type);
if (def && def.category == "config") {
if (!RED.nodes.node(n.id)) {
var configNode = {id:n.id,type:n.type,users:[]};
for (var d in def.defaults) {
configNode[d] = n[d];
if (n.type !== "workspace") {
var def = getType(n.type);
if (def && def.category == "config") {
if (!RED.nodes.node(n.id)) {
var configNode = {id:n.id,type:n.type,users:[]};
for (var d in def.defaults) {
configNode[d] = n[d];
}
configNode.label = def.label;
configNode._def = def;
RED.nodes.add(configNode);
}
configNode.label = def.label;
configNode._def = def;
RED.nodes.add(configNode);
}
} else {
if (n.z == null) {
n.z = 0;
}
var node = {x:n.x,y:n.y,z:n.z,type:0,wires:n.wires};
if (createNewIds) {
node.id = (1+Math.random()*4294967295).toString(16);
} else {
node.id = n.id;
}
node.type = n.type;
node._def = def;
if (!node._def) {
node._def = {
color:"#fee",
defaults: {},
label: "unknown: "+n.type,
labelStyle: "node_label_italic",
outputs: n.outputs||n.wires.length
}
}
node.outputs = n.outputs||node._def.outputs;
for (var d in node._def.defaults) {
node[d] = n[d];
if (node._def.defaults[d].type) {
var configNode = RED.nodes.node(n[d]);
if (configNode) {
configNode.users.push(node);
var node = {x:n.x,y:n.y,z:n.z,type:0,wires:n.wires};
if (createNewIds) {
node.z = RED.view.getWorkspace();
node.id = getID();
} else {
node.id = n.id;
if (node.z == null || !workspaces[node.z]) {
node.z = RED.view.getWorkspace();
}
}
}
node.type = n.type;
node._def = def;
if (!node._def) {
node._def = {
color:"#fee",
defaults: {},
label: "unknown: "+n.type,
labelStyle: "node_label_italic",
outputs: n.outputs||n.wires.length
}
}
node.outputs = n.outputs||node._def.outputs;
addNode(node);
RED.editor.validateNode(node);
node_map[n.id] = node;
new_nodes.push(node);
for (var d in node._def.defaults) {
node[d] = n[d];
if (node._def.defaults[d].type) {
var configNode = RED.nodes.node(n[d]);
if (configNode) {
configNode.users.push(node);
}
}
}
addNode(node);
RED.editor.validateNode(node);
node_map[n.id] = node;
new_nodes.push(node);
}
}
}
for (var i in new_nodes) {
@ -288,6 +321,7 @@ RED.nodes = function() {
addLink: addLink,
remove: removeNode,
removeLink: removeLink,
addWorkspace: addWorkspace,
eachNode: function(cb) {
for (var n in nodes) {
cb(nodes[n]);
@ -309,6 +343,7 @@ RED.nodes = function() {
getAllFlowNodes: getAllFlowNodes,
createExportableNodeSet: createExportableNodeSet,
createCompleteNodeSet: createCompleteNodeSet,
id: getID,
nodes: nodes, // TODO: exposed for d3 vis
links: links // TODO: exposed for d3 vis
};

View File

@ -72,6 +72,10 @@ RED.tabs = function() {
if (options.onadd) {
options.onadd(tab);
}
link.attr("title",tab.label);
if (ul.find("li.red-ui-tab").size() == 1) {
activateTab(link);
}
},
activateTab: activateTab,
resize: updateTabWidths

View File

@ -70,8 +70,7 @@ RED.view = function() {
var workspace_tabs = RED.tabs.create({
id: "workspace-tabs",
onchange: function(id) {
console.log(id);
RED.view.setWorkspace(id.split("-")[1]);
RED.view.setWorkspace(id.slice(1));
},
ondblclick: function(id) {
console.log("DC:",id);
@ -87,15 +86,13 @@ RED.view = function() {
$('#workspace-menu-list').append(menuli);
}
});
workspace_tabs.addTab({id:"tab-1",label:"Workspace 1"});
workspace_tabs.addTab({id:"tab-2",label:"Workspace 2"});
workspace_tabs.addTab({id:"tab-3",label:"Workspace 3"});
workspace_tabs.addTab({id:"tab-4",label:"Workspace 4"});
$('#btn-workspace-add').on("click",function() {
var id = Math.floor(Math.random()*2000);
workspace_tabs.addTab({id:"tab-"+id,label:"Workspace "+id});
workspace_tabs.activateTab("tab-"+id);
var tabId = RED.nodes.id();
var ws = {type:"workspace",id:tabId,label:"Workspace "+tabId};
RED.nodes.addWorkspace(ws);
workspace_tabs.addTab(ws);
workspace_tabs.activateTab(tabId);
});
@ -454,6 +451,7 @@ RED.view = function() {
table += "<tr><td>Type</td><td>&nbsp;"+node.type+"</td></tr>";
table += "<tr><td>ID</td><td>&nbsp;"+node.id+"</td></tr>";
table += "<tr><td>WS</td><td>&nbsp;"+node.z+"</td></tr>";
table += '<tr class="blank"><td colspan="2">&nbsp;Properties</td></tr>';
for (var n in node._def.defaults) {
if ((n != "func")&&(n != "template")) {
@ -994,6 +992,13 @@ RED.view = function() {
mouse_mode = state;
}
},
addWorkspace: function(ws) {
workspace_tabs.addTab(ws);
workspace_tabs.resize();
},
getWorkspace: function() {
return activeWorkspace;
},
setWorkspace: function(z) {
activeWorkspace = z;
clearSelection();

View File

@ -292,9 +292,11 @@ var parseConfig = function() {
missingTypes = [];
for (var i in activeConfig) {
var type = activeConfig[i].type;
var nt = node_type_registry.get(type);
if (!nt && missingTypes.indexOf(type) == -1) {
missingTypes.push(type);
if (type != "workspace") {
var nt = node_type_registry.get(type);
if (!nt && missingTypes.indexOf(type) == -1) {
missingTypes.push(type);
}
}
};
if (missingTypes.length > 0) {
@ -309,19 +311,21 @@ var parseConfig = function() {
events.emit("nodes-starting");
for (var i in activeConfig) {
var nn = null;
var nt = node_type_registry.get(activeConfig[i].type);
if (nt) {
try {
nn = new nt(activeConfig[i]);
if (activeConfig[i].type != "workspace") {
var nt = node_type_registry.get(activeConfig[i].type);
if (nt) {
try {
nn = new nt(activeConfig[i]);
}
catch (err) {
util.log("[red] "+activeConfig[i].type+" : "+err);
}
}
catch (err) {
util.log("[red] "+activeConfig[i].type+" : "+err);
// console.log(nn);
if (nn == null) {
util.log("[red] unknown type: "+activeConfig[i].type);
}
}
// console.log(nn);
if (nn == null) {
util.log("[red] unknown type: "+activeConfig[i].type);
}
}
// Clean up any orphaned credentials
var deletedCredentials = false;