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><!-- /palette -->
<div id="workspace"> <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 id="chart"></div>
</div> </div>

View File

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

View File

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

View File

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

View File

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