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

Allow update of global flow

This commit is contained in:
Nick O'Leary 2016-01-14 14:57:13 +00:00
parent 6e75089f3a
commit 17e3b71d9c

View File

@ -387,9 +387,10 @@ function updateMissingTypes() {
} }
function addFlow(flow) { function addFlow(flow) {
var i,node; var i,node;
if (!flow.hasOwnProperty('nodes')) {
throw new Error('missing nodes property');
}
flow.id = redUtil.generateId(); flow.id = redUtil.generateId();
var nodes = [{ var nodes = [{
@ -449,6 +450,9 @@ function getFlow(id) {
if (flow.label) { if (flow.label) {
result.label = flow.label; result.label = flow.label;
} }
if (id !== 'global') {
result.nodes = [];
}
if (flow.nodes) { if (flow.nodes) {
var nodeIds = Object.keys(flow.nodes); var nodeIds = Object.keys(flow.nodes);
if (nodeIds.length > 0) { if (nodeIds.length > 0) {
@ -462,6 +466,9 @@ function getFlow(id) {
result.configs = configIds.map(function(configId) { result.configs = configIds.map(function(configId) {
return clone(flow.configs[configId]); return clone(flow.configs[configId]);
}) })
if (result.configs.length === 0) {
delete result.configs;
}
} }
if (flow.subflows) { if (flow.subflows) {
var subflowIds = Object.keys(flow.subflows); var subflowIds = Object.keys(flow.subflows);
@ -480,40 +487,63 @@ function getFlow(id) {
delete subflow.instances; delete subflow.instances;
return subflow; return subflow;
}); });
if (result.subflows.length === 0) {
delete result.subflows;
}
} }
return result; return result;
} }
function updateFlow(id,newFlow) { function updateFlow(id,newFlow) {
if (id === 'global') { var label = id;
// TODO: handle global update if (id !== 'global') {
throw new Error('not allowed to update global'); if (!activeFlowConfig.flows[id]) {
}
var flow = activeFlowConfig.flows[id];
if (!flow) {
var e = new Error(); var e = new Error();
e.code = 404; e.code = 404;
throw e; throw e;
} }
label = activeFlowConfig.flows[id].label;
}
var newConfig = clone(activeConfig); var newConfig = clone(activeConfig);
var nodes;
if (id === 'global') {
// Remove all nodes whose z is not a known flow
// When subflows can be owned by a flow, this logic will have to take
// that into account
newConfig = newConfig.filter(function(node) {
return node.type === 'tab' || (node.hasOwnProperty('z') && activeFlowConfig.flows.hasOwnProperty(node.z));
})
// Add in the new config nodes
nodes = newFlow.configs||[];
if (newFlow.subflows) {
// Add in the new subflows
newFlow.subflows.forEach(function(sf) {
nodes = nodes.concat(sf.nodes||[]).concat(sf.configs||[]);
delete sf.nodes;
delete sf.configs;
nodes.push(sf);
});
}
} else {
newConfig = newConfig.filter(function(node) { newConfig = newConfig.filter(function(node) {
return node.z !== id && node.id !== id; return node.z !== id && node.id !== id;
}); });
var tabNode = { var tabNode = {
type:'tab', type:'tab',
label:newFlow.label, label:newFlow.label,
id:id id:id
} }
var nodes = [tabNode].concat(newFlow.nodes||[]).concat(newFlow.configs||[]); nodes = [tabNode].concat(newFlow.nodes||[]).concat(newFlow.configs||[]);
nodes.forEach(function(n) { nodes.forEach(function(n) {
n.z = id; n.z = id;
}); });
newConfig = newConfig.concat(nodes); }
newConfig = newConfig.concat(nodes);
return setConfig(newConfig,'flows',true).then(function() { return setConfig(newConfig,'flows',true).then(function() {
log.info(log._("nodes.flows.updated-flow",{label:(flow.label?flow.label+" ":"")+"["+flow.id+"]"})); log.info(log._("nodes.flows.updated-flow",{label:(label?label+" ":"")+"["+id+"]"}));
}) })
} }