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

Remove direct access to RED.nodes.nodes/links

- Adds RED.nodes.filterNodes and RED.nodes.filterLinks for
   doing simply queries to find elements that match a criteria
This commit is contained in:
Nick O'Leary 2015-03-15 23:31:38 +00:00
parent 71db193675
commit cf8fe16b09
3 changed files with 53 additions and 14 deletions

View File

@ -779,6 +779,45 @@ RED.nodes = (function() {
return [new_nodes,new_links,new_workspaces,new_subflows]; return [new_nodes,new_links,new_workspaces,new_subflows];
} }
// TODO: only supports filter.z
function filterNodes(filter) {
var result = [];
for (var n=0;n<nodes.length;n++) {
var node = nodes[n];
if (filter.z && node.z !== filter.z) {
continue;
}
result.push(node);
}
return result;
}
function filterLinks(filter) {
var result = [];
for (var n=0;n<links.length;n++) {
var link = links[n];
if (filter.source) {
if (filter.source.id && link.source.id !== filter.source.id) {
continue;
}
if (filter.source.z && link.source.z !== filter.source.z) {
continue;
}
}
if (filter.target) {
if (filter.target.id && link.target.id !== filter.target.id) {
continue;
}
if (filter.target.z && link.target.z !== filter.target.z) {
continue;
}
}
result.push(link);
}
return result;
}
// TODO: DRY // TODO: DRY
var eventHandler = (function() { var eventHandler = (function() {
var handlers = {}; var handlers = {};
@ -854,7 +893,12 @@ RED.nodes = (function() {
} }
} }
}, },
node: getNode, node: getNode,
filterNodes: filterNodes,
filterLinks: filterLinks,
import: importNodes, import: importNodes,
refreshValidation: refreshValidation, refreshValidation: refreshValidation,
getAllFlowNodes: getAllFlowNodes, getAllFlowNodes: getAllFlowNodes,
@ -867,8 +911,6 @@ RED.nodes = (function() {
} else { } else {
setDirty(d); setDirty(d);
} }
}, }
nodes: nodes, // TODO: exposed for d3 vis
links: links // TODO: exposed for d3 vis
}; };
})(); })();

View File

@ -232,13 +232,14 @@ RED.view = (function() {
var drag_line = vis.append("svg:path").attr("class", "drag_line"); var drag_line = vis.append("svg:path").attr("class", "drag_line");
function updateActiveNodes() { function updateActiveNodes() {
//TODO: remove direct access to RED.nodes.nodes var activeWorkspace = RED.workspaces.active();
activeNodes = RED.nodes.nodes.filter(function(d) {
return d.z == RED.workspaces.active(); activeNodes = RED.nodes.filterNodes({z:activeWorkspace});
activeLinks = RED.nodes.filterLinks({
source:{z:activeWorkspace},
target:{z:activeWorkspace}
}); });
activeLinks = RED.nodes.links.filter(function(d) {
return d.source.z == RED.workspaces.active() && d.target.z == RED.workspaces.active();
})
} }
function init() { function init() {
@ -1185,7 +1186,6 @@ RED.view = (function() {
vis.selectAll(".subflowinput").remove(); vis.selectAll(".subflowinput").remove();
} }
//var node = vis.selectAll(".nodegroup").data(RED.nodes.nodes.filter(function(d) { return d.z == RED.workspaces.active() }),function(d){return d.id});
var node = vis.selectAll(".nodegroup").data(activeNodes,function(d){return d.id}); var node = vis.selectAll(".nodegroup").data(activeNodes,function(d){return d.id});
node.exit().remove(); node.exit().remove();

View File

@ -44,10 +44,7 @@ RED.workspaces = (function() {
} }
var nodes = []; var nodes = [];
if (!force) { if (!force) {
//TODO: remove direct access to RED.nodes.nodes nodes = RED.nodes.filterNodes({z:ws.id});
nodes = RED.nodes.nodes.filter(function(d) {
return d.z == ws.id;
});
} }
if (force || nodes.length === 0) { if (force || nodes.length === 0) {
removeWorkspace(ws); removeWorkspace(ws);