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

Catch node can target specific nodes

This commit is contained in:
Nick O'Leary 2015-08-13 13:58:19 +01:00
parent c64b5c2850
commit 3a6192bf73
5 changed files with 378 additions and 125 deletions

View File

@ -637,16 +637,20 @@ RED.editor = (function() {
} }
$("#dialog-form").find('[data-i18n]').each(function() { $("#dialog-form").find('[data-i18n]').each(function() {
var current = $(this).attr("data-i18n"); var current = $(this).attr("data-i18n");
if (current.indexOf(":") === -1) { var keys = current.split(";");
var prefix = ""; for (var i=0;i<keys.length;i++) {
if (current.indexOf("[")===0) { var key = keys[i];
var parts = current.split("]"); if (key.indexOf(":") === -1) {
prefix = parts[0]+"]"; var prefix = "";
current = parts[1]; if (key.indexOf("[")===0) {
var parts = key.split("]");
prefix = parts[0]+"]";
key = parts[1];
}
keys[i] = prefix+ns+":"+key;
} }
$(this).attr("data-i18n",prefix+ns+":"+current);
} }
$(this).attr("data-i18n",keys.join(";"));
}); });
$('<input type="text" style="display: none;" />').appendTo("#dialog-form"); $('<input type="text" style="display: none;" />').appendTo("#dialog-form");
prepareEditDialog(node,node._def,"node-input"); prepareEditDialog(node,node._def,"node-input");

View File

@ -15,6 +15,25 @@
--> -->
<script type="text/x-red" data-template-name="catch"> <script type="text/x-red" data-template-name="catch">
<div class="form-row">
<label style="width: auto" for="node-input-scope" data-i18n="catch.label.source"></label>
<select id="node-input-scope-select">
<option value="all" data-i18n="catch.scope.all"></option>
<option value="target" data-i18n="catch.scope.selected"></options>
</select>
</div>
<div class="form-row node-input-target-row" style="display: none;">
<div id="node-input-catch-target-container-div" style="position: relative; box-sizing: border-box; border-radius: 2px; height: 180px; border: 1px solid #ccc;overflow:hidden; ">
<div style="box-sizing: border-box; line-height: 20px; font-size: 0.8em; border-bottom: 1px solid #ddd; height: 20px;">
<input type="checkbox" data-i18n="[title]catch.label.selectAll" id="node-input-target-node-checkbox-all" style="width: 30px; margin: 0 2px 1px 2px;">
<div style="display: inline-block;"><a id="node-input-target-sort-label" href="#" data-i18n="[title]catch.label.sortByLabel"><span data-i18n="catch.label.node"></span> <i class="node-input-catch-sort-label-a fa fa-caret-down"></i><i class="node-input-catch-sort-label-d fa fa-caret-up"></i></a></div>
<div style="position: absolute; right: 10px; width: 50px; display: inline-block; text-align: right;"><a id="node-input-target-sort-type" href="#" data-i18n="[title]catch.label.sortByType"><i class="node-input-catch-sort-sublabel-a fa fa-caret-down"></i><i class="node-input-catch-sort-sublabel-d fa fa-caret-up"></i> <span data-i18n="catch.label.type"></span></a></div>
</div>
<div style="background: #fbfbfb; box-sizing: border-box; position:absolute; top:20px;bottom:0;left:0px;right:0px; overflow-y: scroll; overflow-x: hidden;">
<ul id="node-input-catch-target-container" style=" list-style-type:none; margin: 0;"></ul>
</div>
</div>
</div>
<div class="form-row"> <div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="common.label.name"></span></label> <label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="common.label.name"></span></label>
<input type="text" id="node-input-name" data-i18n="[placeholder]common.label.name"> <input type="text" id="node-input-name" data-i18n="[placeholder]common.label.name">
@ -41,22 +60,230 @@
</p> </p>
<p>If the message already had a <code>error</code> property, it is copied to <code>_error</code>.</p> <p>If the message already had a <code>error</code> property, it is copied to <code>_error</code>.</p>
</script> </script>
<style>
#node-input-catch-target-container {
position: relative;
}
#node-input-catch-target-container li {
padding: 2px 5px;
background: none;
font-size: 0.8em;
margin:0;
white-space: nowrap;
}
#node-input-catch-target-container li label {
margin-bottom: 0;
width: 100%;
}
#node-input-catch-target-container li label input {
vertical-align: top;
width:15px;
margin-right: 10px;
}
#node-input-catch-target-container li:hover,
#node-input-catch-target-container li:hover .node-input-target-node-sublabel {
background: #f0f0f0;
}
.node-input-target-node-sublabel {
position:absolute;
right: 0px;
padding-right: 10px;
padding-left: 10px;
font-size: 0.8em;
background: #fbfbfb;
}
</style>
<script type="text/javascript"> <script type="text/javascript">
RED.nodes.registerType('catch',{ RED.nodes.registerType('catch',{
category: 'input', category: 'input',
color:"#e49191", color:"#e49191",
defaults: { defaults: {
name: {value:""} name: {value:""},
scope: {value:null}
}, },
inputs:0, inputs:0,
outputs:1, outputs:1,
icon: "alert.png", icon: "alert.png",
label: function() { label: function() {
return this.name||this._("catch.catch"); return this.name||this.scope?this._("catch.catchNodes",{number:this.scope.length}):this._("catch.catch");
}, },
labelStyle: function() { labelStyle: function() {
return this.name?"node_label_italic":""; return this.name?"node_label_italic":"";
},
oneditprepare: function() {
var nodeList = $("#node-input-catch-target-container");
var node = this;
function createNodeList() {
node.scope = node.scope || [];
nodeList.empty();
var candidateNodes = RED.nodes.filterNodes({z:node.z});
var allChecked = true;
candidateNodes.forEach(function(n) {
if (n.id === node.id) {
return;
}
var isChecked = node.scope.indexOf(n.id) !== -1;
allChecked = allChecked && isChecked;
var container = $('<li/>',{class:"node-input-target-node"});
var row = $('<label/>',{for:"node-input-target-node-"+n.id}).appendTo(container);
$('<input>',{type:"checkbox",class:"node-input-target-node-checkbox",id:"node-input-target-node-"+n.id})
.data('node-id',n.id)
.prop('checked', isChecked)
.appendTo(row);
container.on('mouseover',function(e) {
n.highlighted = true;
n.dirty = true;
RED.view.redraw();
});
container.on('mouseout',function(e) {
n.highlighted = false;
n.dirty = true;
RED.view.redraw();
});
var labelSpan = $('<span>');
var nodeDef = RED.nodes.getType(n.type);
var label;
var sublabel;
if (nodeDef) {
var l = nodeDef.label;
label = (typeof l === "function" ? l.call(n) : l)||"";
sublabel = n.type;
if (sublabel.indexOf("subflow:") === 0) {
var subflowId = sublabel.substring(8);
var subflow = RED.nodes.subflow(subflowId);
sublabel = "subflow : "+subflow.name;
}
}
if (!nodeDef || !label) {
label = n.type;
}
$('<span>',{class:"node-input-target-node-label",style:"white-space:nowrap"}).text(label).appendTo(row);
if (sublabel) {
$('<span>',{class:"node-input-target-node-sublabel"}).text(sublabel).appendTo(row);
}
container.appendTo(nodeList);
});
$(".node-input-target-node-checkbox").change(function() {
if (!this.checked) {
$("#node-input-target-node-checkbox-all").prop('checked',false);
}
});
$("#node-input-target-node-checkbox-all").prop('checked',allChecked);
sortNodeList('label');
}
function sortNodeList(sortOn) {
var currentSort = nodeList.data('currentSort');
var currentSortOrder = nodeList.data('currentSortOrder');
if (!currentSort) {
currentSort = sortOn;
currentSortOrder = 'a';
} else {
if (currentSort === sortOn) {
currentSortOrder = (currentSortOrder === 'a'?'d':'a');
} else {
currentSortOrder = 'a';
}
currentSort = sortOn;
}
nodeList.data('currentSort',currentSort);
nodeList.data('currentSortOrder',currentSortOrder);
$("#node-input-catch-target-container-div .fa").hide();
$(".node-input-catch-sort-"+currentSort+"-"+currentSortOrder).show();
var items = nodeList.find("li").get();
items.sort(function(a,b) {
var labelA = $(a).find(".node-input-target-node-"+currentSort).text().toLowerCase();
var labelB = $(b).find(".node-input-target-node-"+currentSort).text().toLowerCase();
if (labelA < labelB) { return currentSortOrder==='a'?-1:1; }
if (labelA > labelB) { return currentSortOrder==='a'?1:-1; }
return 0;
});
$.each(items, function(i, li){
nodeList.append(li);
});
}
$("#node-input-target-sort-label").click(function(e) {
e.preventDefault();
sortNodeList('label');
});
$("#node-input-target-sort-type").click(function(e) {
e.preventDefault();
sortNodeList('sublabel')
});
$("#node-input-target-node-checkbox-all").change(function() {
$(".node-input-target-node-checkbox").prop('checked',this.checked);
})
$("#node-input-scope-select").change(function(e) {
var scope = $(this).children("option:selected").val();
if (scope === "target") {
createNodeList();
$(".node-input-target-row").show();
} else {
$(".node-input-target-row").hide();
}
});
if (this.scope == null) {
$("#node-input-scope-select").val("all");
} else {
$("#node-input-scope-select").val("target");
}
$("#node-input-scope-select").change();
function dialogResize() {
var rows = $("#dialog-form>div:not(.node-input-target-row)");
var height = $("#dialog-form").height();
for (var i=0;i<rows.size();i++) {
height -= $(rows[i]).outerHeight(true);
}
var editorRow = $("#dialog-form>div.node-input-target-row");
height -= (parseInt(editorRow.css("marginTop"))+parseInt(editorRow.css("marginBottom")));
$("#node-input-catch-target-container-div").css("height",height+"px");
};
$( "#dialog" ).on("dialogresize", dialogResize);
$( "#dialog" ).one("dialogopen", function(ev) {
var size = $( "#dialog" ).dialog('option','sizeCache-catch');
if (size) {
$("#dialog").dialog('option','width',size.width);
$("#dialog").dialog('option','height',size.height);
dialogResize();
}
});
$( "#dialog" ).one("dialogclose", function(ev,ui) {
$( "#dialog" ).off("dialogresize",dialogResize);
});
},
oneditsave: function() {
var scope = $("#node-input-scope-select").children("option:selected").val();
if (scope === 'all') {
this.scope = null;
} else {
var node = this;
node.scope = [];
$(".node-input-target-node-checkbox").each(function(n) {
if ($(this).prop("checked")) {
node.scope.push($(this).data('node-id'));
}
})
}
} }
}); });
</script> </script>

View File

@ -20,6 +20,7 @@ module.exports = function(RED) {
function CatchNode(n) { function CatchNode(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
var node = this; var node = this;
this.scope = n.scope;
this.on("input",function(msg) { this.on("input",function(msg) {
this.send(msg); this.send(msg);
}); });

View File

@ -68,7 +68,20 @@
} }
}, },
"catch": { "catch": {
"catch": "catch" "catch": "catch all",
"catchNodes": "catch (__number__)",
"label": {
"source": "Catch errors from",
"node": "node",
"type": "type",
"selectAll": "select all",
"sortByLabel": "sort by label",
"sortByType": "sort by type"
},
"scope": {
"all": "all nodes",
"selected": "selected nodes"
}
}, },
"debug": { "debug": {
"output": "Output", "output": "Output",

View File

@ -57,7 +57,7 @@ function createSubflow(sf,sfn,subflows) {
var node; var node;
var wires; var wires;
var i,j,k; var i,j,k;
// Clone all of the subflow node definitions and give them new IDs // Clone all of the subflow node definitions and give them new IDs
for (i=0;i<sf.nodes.length;i++) { for (i=0;i<sf.nodes.length;i++) {
node = clone(sf.nodes[i].config); node = clone(sf.nodes[i].config);
@ -72,7 +72,7 @@ function createSubflow(sf,sfn,subflows) {
for (i=0;i<newNodes.length;i++) { for (i=0;i<newNodes.length;i++) {
node = newNodes[i]; node = newNodes[i];
var outputs = node.wires; var outputs = node.wires;
for (j=0;j<outputs.length;j++) { for (j=0;j<outputs.length;j++) {
wires = outputs[j]; wires = outputs[j];
for (k=0;k<wires.length;k++) { for (k=0;k<wires.length;k++) {
@ -80,7 +80,7 @@ function createSubflow(sf,sfn,subflows) {
} }
} }
} }
// Create a subflow node to accept inbound messages and route appropriately // Create a subflow node to accept inbound messages and route appropriately
var Node = require("./Node"); var Node = require("./Node");
var subflowInstance = { var subflowInstance = {
@ -95,20 +95,20 @@ function createSubflow(sf,sfn,subflows) {
subflowInstance._originalWires = clone(subflowInstance.wires); subflowInstance._originalWires = clone(subflowInstance.wires);
} }
var subflowNode = new Node(subflowInstance); var subflowNode = new Node(subflowInstance);
subflowNode.on("input", function(msg) { this.send(msg);}); subflowNode.on("input", function(msg) { this.send(msg);});
subflowNode._updateWires = subflowNode.updateWires; subflowNode._updateWires = subflowNode.updateWires;
subflowNode.updateWires = function(newWires) { subflowNode.updateWires = function(newWires) {
// Wire the subflow outputs // Wire the subflow outputs
if (sf.config.out) { if (sf.config.out) {
var node,wires,i,j; var node,wires,i,j;
// Restore the original wiring to the internal nodes // Restore the original wiring to the internal nodes
subflowInstance.wires = clone(subflowInstance._originalWires); subflowInstance.wires = clone(subflowInstance._originalWires);
for (i=0;i<sf.config.out.length;i++) { for (i=0;i<sf.config.out.length;i++) {
wires = sf.config.out[i].wires; wires = sf.config.out[i].wires;
for (j=0;j<wires.length;j++) { for (j=0;j<wires.length;j++) {
@ -120,10 +120,10 @@ function createSubflow(sf,sfn,subflows) {
} }
} }
} }
var modifiedNodes = {}; var modifiedNodes = {};
var subflowInstanceModified = false; var subflowInstanceModified = false;
for (i=0;i<sf.config.out.length;i++) { for (i=0;i<sf.config.out.length;i++) {
wires = sf.config.out[i].wires; wires = sf.config.out[i].wires;
for (j=0;j<wires.length;j++) { for (j=0;j<wires.length;j++) {
@ -146,7 +146,7 @@ function createSubflow(sf,sfn,subflows) {
} }
} }
} }
nodes.push(subflowNode); nodes.push(subflowNode);
// Wire the subflow outputs // Wire the subflow outputs
@ -170,12 +170,12 @@ function createSubflow(sf,sfn,subflows) {
} }
} }
} }
// Instantiate the nodes // Instantiate the nodes
for (i=0;i<newNodes.length;i++) { for (i=0;i<newNodes.length;i++) {
node = newNodes[i]; node = newNodes[i];
var type = node.type; var type = node.type;
var m = /^subflow:(.+)$/.exec(type); var m = /^subflow:(.+)$/.exec(type);
if (!m) { if (!m) {
nodes.push(createNode(type,node)); nodes.push(createNode(type,node));
@ -184,13 +184,13 @@ function createSubflow(sf,sfn,subflows) {
nodes = nodes.concat(createSubflow(subflows[subflowId],node,subflows)); nodes = nodes.concat(createSubflow(subflows[subflowId],node,subflows));
} }
} }
subflowNode.instanceNodes = {}; subflowNode.instanceNodes = {};
nodes.forEach(function(node) { nodes.forEach(function(node) {
subflowNode.instanceNodes[node.id] = node; subflowNode.instanceNodes[node.id] = node;
}); });
return nodes; return nodes;
} }
@ -221,7 +221,8 @@ function createCatchNodeMap(nodes) {
for (id in nodes) { for (id in nodes) {
if (nodes.hasOwnProperty(id)) { if (nodes.hasOwnProperty(id)) {
if (nodes[id].type === "catch") { if (nodes[id].type === "catch") {
catchNodes[nodes[id].z] = nodes[id]; catchNodes[nodes[id].z] = catchNodes[nodes[id].z] || [];
catchNodes[nodes[id].z].push(nodes[id]);
} }
} }
} }
@ -245,7 +246,8 @@ function createCatchNodeMap(nodes) {
} }
} }
if (catchNodes[z]) { if (catchNodes[z]) {
catchNodes[id] = catchNodes[z]; catchNodes[id] = catchNodes[id]||[];
catchNodes[id].push(catchNodes[z]);
} }
} }
} }
@ -255,32 +257,32 @@ function createCatchNodeMap(nodes) {
var subflowInstanceRE = /^subflow:(.+)$/; var subflowInstanceRE = /^subflow:(.+)$/;
function Flow(config) { function Flow(config) {
this.activeNodes = {}; this.activeNodes = {};
this.subflowInstanceNodes = {}; this.subflowInstanceNodes = {};
this.catchNodeMap = {}; this.catchNodeMap = {};
this.started = false; this.started = false;
this.parseConfig(config); this.parseConfig(config);
} }
Flow.prototype.parseConfig = function(config) { Flow.prototype.parseConfig = function(config) {
var i; var i;
var nodeConfig; var nodeConfig;
var nodeType; var nodeType;
this.config = config; this.config = config;
this.allNodes = {}; this.allNodes = {};
this.nodes = {}; this.nodes = {};
this.subflows = {}; this.subflows = {};
this.configNodes = {}; this.configNodes = {};
var unknownTypes = {}; var unknownTypes = {};
for (i=0;i<this.config.length;i++) { for (i=0;i<this.config.length;i++) {
nodeConfig = this.config[i]; nodeConfig = this.config[i];
nodeType = nodeConfig.type; nodeType = nodeConfig.type;
@ -292,19 +294,19 @@ Flow.prototype.parseConfig = function(config) {
nodes: [] nodes: []
} }
} }
} }
//console.log("Known subflows:",Object.keys(this.subflows)); //console.log("Known subflows:",Object.keys(this.subflows));
for (i=0;i<this.config.length;i++) { for (i=0;i<this.config.length;i++) {
nodeConfig = this.config[i]; nodeConfig = this.config[i];
nodeType = nodeConfig.type; nodeType = nodeConfig.type;
if (nodeConfig.credentials) { if (nodeConfig.credentials) {
delete nodeConfig.credentials; delete nodeConfig.credentials;
} }
if (nodeType != "tab" && nodeType != "subflow") { if (nodeType != "tab" && nodeType != "subflow") {
var m = subflowInstanceRE.exec(nodeType); var m = subflowInstanceRE.exec(nodeType);
if ((m && !this.subflows[m[1]]) || (!m && !typeRegistry.get(nodeType))) { if ((m && !this.subflows[m[1]]) || (!m && !typeRegistry.get(nodeType))) {
@ -326,8 +328,8 @@ Flow.prototype.parseConfig = function(config) {
for (var prop in nodeConfig) { for (var prop in nodeConfig) {
if (nodeConfig.hasOwnProperty(prop) && if (nodeConfig.hasOwnProperty(prop) &&
prop != "type" && prop != "type" &&
prop != "id" && prop != "id" &&
prop != "z" && prop != "z" &&
prop != "wires" && prop != "wires" &&
this.allNodes[nodeConfig[prop]]) { this.allNodes[nodeConfig[prop]]) {
this.configNodes[nodeConfig[prop]] = this.allNodes[nodeConfig[prop]]; this.configNodes[nodeConfig[prop]] = this.allNodes[nodeConfig[prop]];
@ -336,7 +338,7 @@ Flow.prototype.parseConfig = function(config) {
} }
} }
} }
//console.log("NODES"); //console.log("NODES");
//for (i in this.nodes) { //for (i in this.nodes) {
// if (this.nodes.hasOwnProperty(i)) { // if (this.nodes.hasOwnProperty(i)) {
@ -352,8 +354,8 @@ Flow.prototype.parseConfig = function(config) {
// } // }
// } // }
//} //}
this.missingTypes = Object.keys(unknownTypes); this.missingTypes = Object.keys(unknownTypes);
} }
Flow.prototype.start = function(configDiff) { Flow.prototype.start = function(configDiff) {
@ -365,16 +367,16 @@ Flow.prototype.start = function(configDiff) {
} }
} }
} }
this.started = true; this.started = true;
if (this.missingTypes.length > 0) { if (this.missingTypes.length > 0) {
throw new Error(Log._("nodes.flow.missing-types")); throw new Error(Log._("nodes.flow.missing-types"));
} }
events.emit("nodes-starting"); events.emit("nodes-starting");
var id; var id;
var node; var node;
for (id in this.configNodes) { for (id in this.configNodes) {
if (this.configNodes.hasOwnProperty(id)) { if (this.configNodes.hasOwnProperty(id)) {
node = this.configNodes[id]; node = this.configNodes[id];
@ -383,7 +385,7 @@ Flow.prototype.start = function(configDiff) {
} }
} }
} }
for (id in this.nodes) { for (id in this.nodes) {
if (this.nodes.hasOwnProperty(id)) { if (this.nodes.hasOwnProperty(id)) {
node = this.nodes[id]; node = this.nodes[id];
@ -408,16 +410,16 @@ Flow.prototype.start = function(configDiff) {
} }
} }
} }
this.catchNodeMap = createCatchNodeMap(this.activeNodes); this.catchNodeMap = createCatchNodeMap(this.activeNodes);
credentials.clean(this.config); credentials.clean(this.config);
events.emit("nodes-started"); events.emit("nodes-started");
} }
Flow.prototype.stop = function(configDiff) { Flow.prototype.stop = function(configDiff) {
var nodeList; var nodeList;
if (configDiff) { if (configDiff) {
nodeList = configDiff.stop; nodeList = configDiff.stop;
} else { } else {
@ -450,7 +452,7 @@ Flow.prototype.stop = function(configDiff) {
}); });
} }
Flow.prototype.getMissingTypes = function() { Flow.prototype.getMissingTypes = function() {
return this.missingTypes; return this.missingTypes;
} }
@ -466,7 +468,7 @@ Flow.prototype.typeRegistered = function(type) {
} }
} }
return false; return false;
} }
Flow.prototype.getNode = function(id) { Flow.prototype.getNode = function(id) {
@ -487,10 +489,10 @@ Flow.prototype.eachNode = function(callback) {
} }
Flow.prototype.diffConfig = function(config,type) { Flow.prototype.diffConfig = function(config,type) {
var activeNodesToStop = []; var activeNodesToStop = [];
var nodesToRewire = []; var nodesToRewire = [];
if (type && type!="full") { if (type && type!="full") {
var diff = diffFlow(this,config); var diff = diffFlow(this,config);
//var diff = { //var diff = {
@ -499,16 +501,16 @@ Flow.prototype.diffConfig = function(config,type) {
// linked:[] // linked:[]
// wiringChanged: [] // wiringChanged: []
//} //}
var nodesToStop = []; var nodesToStop = [];
nodesToRewire = diff.wiringChanged; nodesToRewire = diff.wiringChanged;
if (type == "nodes") { if (type == "nodes") {
nodesToStop = diff.deleted.concat(diff.changed); nodesToStop = diff.deleted.concat(diff.changed);
} else if (type == "flows") { } else if (type == "flows") {
nodesToStop = diff.deleted.concat(diff.changed).concat(diff.linked); nodesToStop = diff.deleted.concat(diff.changed).concat(diff.linked);
} }
for (var i=0;i<nodesToStop.length;i++) { for (var i=0;i<nodesToStop.length;i++) {
var id = nodesToStop[i]; var id = nodesToStop[i];
if (this.subflowInstanceNodes[id]) { if (this.subflowInstanceNodes[id]) {
@ -520,7 +522,7 @@ Flow.prototype.diffConfig = function(config,type) {
} else { } else {
activeNodesToStop = Object.keys(this.activeNodes); activeNodesToStop = Object.keys(this.activeNodes);
} }
return { return {
type: type, type: type,
stop: activeNodesToStop, stop: activeNodesToStop,
@ -530,7 +532,7 @@ Flow.prototype.diffConfig = function(config,type) {
} }
function diffFlow(flow,config) { function diffFlow(flow,config) {
//if (!flow.started) { //if (!flow.started) {
// throw new Error("Cannot diff an unstarted flow"); // throw new Error("Cannot diff an unstarted flow");
//} //}
@ -540,13 +542,13 @@ function diffFlow(flow,config) {
var deletedSubflows = {}; var deletedSubflows = {};
var deletedTabs = {}; var deletedTabs = {};
var linkChangedNodes = {}; var linkChangedNodes = {};
var activeLinks = {}; var activeLinks = {};
var newLinks = {}; var newLinks = {};
var changedSubflowStack = []; var changedSubflowStack = [];
var changedSubflows = {}; var changedSubflows = {};
var buildNodeLinks = function(nodeLinks,n,nodes) { var buildNodeLinks = function(nodeLinks,n,nodes) {
nodeLinks[n.id] = nodeLinks[n.id] || []; nodeLinks[n.id] = nodeLinks[n.id] || [];
if (n.wires) { if (n.wires) {
@ -563,11 +565,11 @@ function diffFlow(flow,config) {
} }
} }
} }
config.forEach(function(node) { config.forEach(function(node) {
flowNodes[node.id] = node; flowNodes[node.id] = node;
}); });
config.forEach(function(node) { config.forEach(function(node) {
var changed = false; var changed = false;
if (node.credentials) { if (node.credentials) {
@ -589,7 +591,7 @@ function diffFlow(flow,config) {
changedNodes[node.id] = node; changedNodes[node.id] = node;
} }
}); });
flow.config.forEach(function(node) { flow.config.forEach(function(node) {
if (!flowNodes[node.id]) { if (!flowNodes[node.id]) {
if (node.type === "tab") { if (node.type === "tab") {
@ -602,7 +604,7 @@ function diffFlow(flow,config) {
} }
buildNodeLinks(activeLinks,node,flow.allNodes); buildNodeLinks(activeLinks,node,flow.allNodes);
}); });
flow.config.forEach(function(node) { flow.config.forEach(function(node) {
for (var prop in node) { for (var prop in node) {
if (node.hasOwnProperty(prop) && prop != "z" && prop != "id" && prop != "wires") { if (node.hasOwnProperty(prop) && prop != "z" && prop != "id" && prop != "wires") {
@ -614,9 +616,9 @@ function diffFlow(flow,config) {
} }
} }
} }
}); });
var checkSubflowMembership = function(nodes,id) { var checkSubflowMembership = function(nodes,id) {
var node = nodes[id]; var node = nodes[id];
if (node) { if (node) {
@ -631,13 +633,13 @@ function diffFlow(flow,config) {
} }
} }
}; };
Object.keys(changedNodes).forEach(function(n) { checkSubflowMembership(flowNodes,n)}); Object.keys(changedNodes).forEach(function(n) { checkSubflowMembership(flowNodes,n)});
Object.keys(deletedNodes).forEach(function(n) { checkSubflowMembership(flow.allNodes,n)}); Object.keys(deletedNodes).forEach(function(n) { checkSubflowMembership(flow.allNodes,n)});
while (changedSubflowStack.length > 0) { while (changedSubflowStack.length > 0) {
var subflowId = changedSubflowStack.pop(); var subflowId = changedSubflowStack.pop();
config.forEach(function(node) { config.forEach(function(node) {
if (node.type == "subflow:"+subflowId) { if (node.type == "subflow:"+subflowId) {
if (!changedNodes[node.id]) { if (!changedNodes[node.id]) {
@ -646,17 +648,17 @@ function diffFlow(flow,config) {
} }
} }
}); });
} }
config.forEach(function(node) { config.forEach(function(node) {
buildNodeLinks(newLinks,node,flowNodes); buildNodeLinks(newLinks,node,flowNodes);
}); });
var markLinkedNodes = function(linkChanged,otherChangedNodes,linkMap,allNodes) { var markLinkedNodes = function(linkChanged,otherChangedNodes,linkMap,allNodes) {
var stack = Object.keys(changedNodes).concat(Object.keys(otherChangedNodes)); var stack = Object.keys(changedNodes).concat(Object.keys(otherChangedNodes));
var visited = {}; var visited = {};
while(stack.length > 0) { while(stack.length > 0) {
var id = stack.pop(); var id = stack.pop();
var linkedNodes = linkMap[id]; var linkedNodes = linkMap[id];
@ -675,7 +677,7 @@ function diffFlow(flow,config) {
} }
markLinkedNodes(linkChangedNodes,{},newLinks,flowNodes); markLinkedNodes(linkChangedNodes,{},newLinks,flowNodes);
markLinkedNodes(linkChangedNodes,{},activeLinks,flow.allNodes); markLinkedNodes(linkChangedNodes,{},activeLinks,flow.allNodes);
var modifiedLinkNodes = {}; var modifiedLinkNodes = {};
config.forEach(function(node) { config.forEach(function(node) {
@ -683,13 +685,13 @@ function diffFlow(flow,config) {
// only concerned about unchanged nodes whose wiring may have changed // only concerned about unchanged nodes whose wiring may have changed
var newNodeLinks = newLinks[node.id]; var newNodeLinks = newLinks[node.id];
var oldNodeLinks = activeLinks[node.id]; var oldNodeLinks = activeLinks[node.id];
var newLinkMap = {}; var newLinkMap = {};
newNodeLinks.forEach(function(l) { newLinkMap[l] = (newLinkMap[l]||0)+1;}); newNodeLinks.forEach(function(l) { newLinkMap[l] = (newLinkMap[l]||0)+1;});
var oldLinkMap = {}; var oldLinkMap = {};
oldNodeLinks.forEach(function(l) { oldLinkMap[l] = (oldLinkMap[l]||0)+1;}); oldNodeLinks.forEach(function(l) { oldLinkMap[l] = (oldLinkMap[l]||0)+1;});
newNodeLinks.forEach(function(link) { newNodeLinks.forEach(function(link) {
if (newLinkMap[link] != oldLinkMap[link]) { if (newLinkMap[link] != oldLinkMap[link]) {
modifiedLinkNodes[node.id] = node; modifiedLinkNodes[node.id] = node;
@ -714,11 +716,11 @@ function diffFlow(flow,config) {
}); });
markLinkedNodes(linkChangedNodes,modifiedLinkNodes,newLinks,flowNodes); markLinkedNodes(linkChangedNodes,modifiedLinkNodes,newLinks,flowNodes);
// config.forEach(function(n) { // config.forEach(function(n) {
// console.log((changedNodes[n.id]!=null)?"[C]":"[ ]",(linkChangedNodes[n.id]!=null)?"[L]":"[ ]","[ ]",n.id,n.type,n.name); // console.log((changedNodes[n.id]!=null)?"[C]":"[ ]",(linkChangedNodes[n.id]!=null)?"[L]":"[ ]","[ ]",n.id,n.type,n.name);
// }); // });
// //
// Object.keys(deletedNodes).forEach(function(id) { // Object.keys(deletedNodes).forEach(function(id) {
// var n = flow.allNodes[id]; // var n = flow.allNodes[id];
// console.log("[ ] [ ] [D]",n.id,n.type); // console.log("[ ] [ ] [D]",n.id,n.type);
@ -729,7 +731,7 @@ function diffFlow(flow,config) {
linked: Object.keys(linkChangedNodes).filter(function(id) { return linkChangedNodes[id].type != "subflow" && (!linkChangedNodes[id].z || flowNodes[linkChangedNodes[id].z].type != "subflow")}), linked: Object.keys(linkChangedNodes).filter(function(id) { return linkChangedNodes[id].type != "subflow" && (!linkChangedNodes[id].z || flowNodes[linkChangedNodes[id].z].type != "subflow")}),
wiringChanged: [] wiringChanged: []
} }
config.forEach(function(n) { config.forEach(function(n) {
if (!flowNodes[n.z] || flowNodes[n.z].type != "subflow") { if (!flowNodes[n.z] || flowNodes[n.z].type != "subflow") {
var originalNode = flow.allNodes[n.id]; var originalNode = flow.allNodes[n.id];
@ -738,51 +740,57 @@ function diffFlow(flow,config) {
} }
} }
}); });
return diff; return diff;
} }
Flow.prototype.handleError = function(node,logMessage,msg) { Flow.prototype.handleError = function(node,logMessage,msg) {
var targetCatchNode = null; var count = 1;
if (this.catchNodeMap[node.z]) { if (msg && msg.hasOwnProperty("error")) {
targetCatchNode = this.catchNodeMap[node.z]; if (msg.error.hasOwnProperty("source")) {
} else if (this.activeNodes[node.z] && this.catchNodeMap[this.activeNodes[node.z].z]) { if (msg.error.source.id === node.id) {
targetCatchNode = this.catchNodeMap[this.activeNodes[node.z].z]; count = msg.error.source.count+1;
} if (count === 10) {
node.warn(Log._("nodes.flow.error-loop"));
if (targetCatchNode) { return;
var count = 1;
if (msg && msg.hasOwnProperty("error")) {
if (msg.error.hasOwnProperty("source")) {
if (msg.error.source.id === node.id) {
count = msg.error.source.count+1;
if (count === 10) {
node.warn(Log._("nodes.flow.error-loop"));
return;
}
} }
} }
} }
}
var errorMessage;
if (msg) { var targetCatchNodes = null;
errorMessage = redUtil.cloneMessage(msg); if (this.catchNodeMap[node.z]) {
} else { targetCatchNodes = this.catchNodeMap[node.z];
errorMessage = {}; } else if (this.activeNodes[node.z] && this.catchNodeMap[this.activeNodes[node.z].z]) {
} targetCatchNodes = this.catchNodeMap[this.activeNodes[node.z].z];
if (errorMessage.hasOwnProperty("error")) { }
errorMessage._error = errorMessage.error;
} if (targetCatchNodes) {
errorMessage.error = { targetCatchNodes.forEach(function(targetCatchNode) {
message: logMessage.toString(), console.log(targetCatchNode.scope);
source: { if (targetCatchNode.scope && targetCatchNode.scope.indexOf(node.id) === -1) {
id: node.id, return;
type: node.type,
count: count
} }
}; var errorMessage;
targetCatchNode.receive(errorMessage); if (msg) {
errorMessage = redUtil.cloneMessage(msg);
} else {
errorMessage = {};
}
if (errorMessage.hasOwnProperty("error")) {
errorMessage._error = errorMessage.error;
}
errorMessage.error = {
message: logMessage.toString(),
source: {
id: node.id,
type: node.type,
count: count
}
};
targetCatchNode.receive(errorMessage);
})
} }
} }