From 3604286793575c0b8598dbf218a91d6190cb6ada Mon Sep 17 00:00:00 2001 From: Nicholas O'Leary Date: Fri, 25 Oct 2013 21:34:00 +0100 Subject: [PATCH] Save/restore tabs --- public/index.html | 2 +- public/red/nodes.js | 123 +++++++++++++++++++++++++++--------------- public/red/ui/tabs.js | 4 ++ public/red/ui/view.js | 23 ++++---- red/nodes.js | 30 ++++++----- 5 files changed, 115 insertions(+), 67 deletions(-) diff --git a/public/index.html b/public/index.html index 16f078031..ece4cc500 100644 --- a/public/index.html +++ b/public/index.html @@ -121,7 +121,7 @@
- +
diff --git a/public/red/nodes.js b/public/red/nodes.js index 1b1417506..6438d10cd 100644 --- a/public/red/nodes.js +++ b/public/red/nodes.js @@ -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("Failed to import nodes: 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; + + 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) { @@ -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 }; diff --git a/public/red/ui/tabs.js b/public/red/ui/tabs.js index 8dfe3d01b..3af5e2235 100644 --- a/public/red/ui/tabs.js +++ b/public/red/ui/tabs.js @@ -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 diff --git a/public/red/ui/view.js b/public/red/ui/view.js index b4df0ded5..1e37581a0 100644 --- a/public/red/ui/view.js +++ b/public/red/ui/view.js @@ -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 += "Type "+node.type+""; table += "ID "+node.id+""; + table += "WS "+node.z+""; table += ' Properties'; 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(); diff --git a/red/nodes.js b/red/nodes.js index 5a2b560c3..ac55f0179 100644 --- a/red/nodes.js +++ b/red/nodes.js @@ -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;