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

Add bulk-activate actions for debug node

Adds the actions:

 - core:activate-all-debug-nodes
 - core:activate-all-flow-debug-nodes

to match the deactivate* actions.

Also adds:

 - core:activate-selected-debug-nodes
 - core:deactivate-selected-debug-nodes

Adds a new httpAdmin route - /debug/(enable/disable) - that can be
use to bulk enable/disable nodes via HTTP Post.
This commit is contained in:
Nick O'Leary 2020-05-27 12:20:23 +01:00
parent 95d1b7bc36
commit 876a7a4646
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
4 changed files with 151 additions and 60 deletions

View File

@ -104,7 +104,7 @@
"minami": "1.2.3", "minami": "1.2.3",
"mocha": "^5.2.0", "mocha": "^5.2.0",
"mosca": "^2.8.3", "mosca": "^2.8.3",
"node-red-node-test-helper": "^0.2.3", "node-red-node-test-helper": "^0.2.5",
"node-sass": "^4.13.1", "node-sass": "^4.13.1",
"should": "^8.4.0", "should": "^8.4.0",
"sinon": "1.17.7", "sinon": "1.17.7",

View File

@ -43,9 +43,20 @@
var subWindow = null; var subWindow = null;
function activateAjaxCall(node, active, successCallback) { function activateAjaxCall(node, active, successCallback) {
var url;
var body;
if (Array.isArray(node)) {
url = "debug/"+(active?"enable":"disable");
body = {nodes: node.map(function(n) { return n.id})}
node = node[0];
} else {
url = "debug/"+node.id+"/"+(active?"enable":"disable");
}
$.ajax({ $.ajax({
url: "debug/"+node.id+"/"+(active?"enable":"disable"), url: url,
type: "POST", type: "POST",
data: body,
success: successCallback, success: successCallback,
error: function(jqXHR,textStatus,errorThrown) { error: function(jqXHR,textStatus,errorThrown) {
if (jqXHR.status == 404) { if (jqXHR.status == 404) {
@ -126,7 +137,6 @@
} }
}, },
onpaletteadd: function() { onpaletteadd: function() {
console.log(RED);
var options = { var options = {
messageMouseEnter: function(sourceId) { messageMouseEnter: function(sourceId) {
if (sourceId) { if (sourceId) {
@ -283,44 +293,76 @@
RED.events.on("project:change", this.clearMessageList); RED.events.on("project:change", this.clearMessageList);
RED.actions.add("core:clear-debug-messages", function() { RED.debug.clearMessageList(true) }); RED.actions.add("core:clear-debug-messages", function() { RED.debug.clearMessageList(true) });
RED.actions.add("core:activate-selected-debug-nodes", function() { setDebugNodeState(getSelectedDebugNodes(true), true); });
RED.actions.add("core:activate-all-debug-nodes", function() { setDebugNodeState(getMatchingDebugNodes(true, true),true); });
RED.actions.add("core:activate-all-flow-debug-nodes", function() { setDebugNodeState(getMatchingDebugNodes(true, false),true); });
RED.actions.add("core:deactivate-all-debug-nodes", function() { deactivateAllDebugNodes(true); }); RED.actions.add("core:deactivate-selected-debug-nodes", function() { setDebugNodeState(getSelectedDebugNodes(false), false); });
RED.actions.add("core:deactivate-all-flow-debug-nodes", function() { deactivateAllDebugNodes(false); }); RED.actions.add("core:deactivate-all-debug-nodes", function() { setDebugNodeState(getMatchingDebugNodes(false, true),false); });
RED.actions.add("core:deactivate-all-flow-debug-nodes", function() { setDebugNodeState(getMatchingDebugNodes(false, false),false); });
function deactivateAllDebugNodes(globally) { function getSelectedDebugNodes(state) {
var nodes = [];
var selection = RED.view.selection();
if (selection.nodes) {
selection.nodes.forEach(function(n) {
if (RED.nodes.subflow(n.z)) {
return;
}
if (n.type === 'debug' && n.active !== state) {
nodes.push(n);
} else if (n.type === 'group') {
nodes = nodes.concat( RED.group.getNodes(n,true).filter(function(n) {
return n.type === 'debug' && n.active !== state
}));
}
});
}
return nodes;
}
function getMatchingDebugNodes(state,globally) {
var nodes = [];
var filter = {type:"debug"};
if (!globally) {
filter.z = RED.workspaces.active();
}
var candidateNodes = RED.nodes.filterNodes(filter);
nodes = candidateNodes.filter(function(n) {
return n.active !== state && !RED.nodes.subflow(n.z)
})
return nodes;
}
function setDebugNodeState(nodes,state) {
var historyEvents = []; var historyEvents = [];
RED.nodes.eachNode(function(n) { if (nodes.length > 0) {
if (n.type === "debug" && n.active === true) { activateAjaxCall(nodes,false, function(resp, textStatus, xhr) {
if (globally === true || n.z == RED.workspaces.active()) { nodes.forEach(function(n) {
if(RED.nodes.subflow(n.z) === undefined) {
historyEvents.push({ historyEvents.push({
t: "edit", t: "edit",
node: n, node: n,
changed: n.changed, changed: n.changed,
changes: { changes: {
active: n.active active: n.active
},
callback: function() {
activateAjaxCall(n, n.active);
} }
}); });
n.active = false; n.active = state;
n.changed = true; n.changed = true;
n.dirty = true; n.dirty = true;
activateAjaxCall(n, n.active); })
}
}
}
});
if (historyEvents.length > 0) {
RED.history.push({ RED.history.push({
t: "multi", t: "multi",
events: historyEvents, events: historyEvents,
dirty: RED.nodes.dirty() dirty: RED.nodes.dirty(),
callback: function() {
activateAjaxCall(nodes,nodes[0].active);
}
}); });
RED.nodes.dirty(true); RED.nodes.dirty(true);
}
RED.view.redraw(); RED.view.redraw();
});
}
} }
$("#red-ui-sidebar-debug-open").on("click", function(e) { $("#red-ui-sidebar-debug-open").on("click", function(e) {

View File

@ -185,24 +185,49 @@ module.exports = function(RED) {
}); });
RED.log.addHandler(DebugNode.logHandler); RED.log.addHandler(DebugNode.logHandler);
RED.httpAdmin.post("/debug/:id/:state", RED.auth.needsPermission("debug.write"), function(req,res) { function setNodeState(node,state) {
var node = RED.nodes.getNode(req.params.id); if (state) {
var state = req.params.state;
if (node !== null && typeof node !== "undefined" ) {
if (state === "enable") {
node.active = true; node.active = true;
res.sendStatus(200);
if (node.tostatus) { node.status({fill:"grey", shape:"dot"}); } if (node.tostatus) { node.status({fill:"grey", shape:"dot"}); }
} else if (state === "disable") { } else {
node.active = false; node.active = false;
res.sendStatus(201);
if (node.tostatus && node.hasOwnProperty("oldStatus")) { if (node.tostatus && node.hasOwnProperty("oldStatus")) {
node.oldStatus.shape = "dot"; node.oldStatus.shape = "dot";
node.status(node.oldStatus); node.status(node.oldStatus);
} }
} else {
res.sendStatus(404);
} }
}
RED.httpAdmin.post("/debug/:state", RED.auth.needsPermission("debug.write"), function(req,res) {
var state = req.params.state;
if (state !== 'enable' && state !== 'disable') {
res.sendStatus(404);
return;
}
var nodes = req.body && req.body.nodes;
if (Array.isArray(nodes)) {
nodes.forEach(function(id) {
var node = RED.nodes.getNode(id);
if (node !== null && typeof node !== "undefined" ) {
setNodeState(node, state === "enable");
}
})
res.sendStatus(state === "enable" ? 200 : 201);
} else {
res.sendStatus(400);
}
})
RED.httpAdmin.post("/debug/:id/:state", RED.auth.needsPermission("debug.write"), function(req,res) {
var state = req.params.state;
if (state !== 'enable' && state !== 'disable') {
res.sendStatus(404);
return;
}
var node = RED.nodes.getNode(req.params.id);
if (node !== null && typeof node !== "undefined" ) {
setNodeState(node,state === "enable");
res.sendStatus(state === "enable" ? 200 : 201);
} else { } else {
res.sendStatus(404); res.sendStatus(404);
} }

View File

@ -603,6 +603,30 @@ describe('debug node', function() {
.post('/debug/n99/enable') .post('/debug/n99/enable')
.expect(404).end(done); .expect(404).end(done);
}); });
it('should return 400 for invalid bulk disable', function(done) {
var flow = [{id:"n1", type:"debug", active: true }];
helper.load(debugNode, flow, function() {
helper.request()
.post('/debug/disable')
.send({})
.set('Content-type', 'application/json')
.expect(400).end(done);
});
})
it('should return success for bulk disable', function(done) {
var flow = [{id:"n1", type:"debug", active: true }];
helper.load(debugNode, flow, function() {
helper.request()
.post('/debug/disable')
.send({nodes:['n1']})
.set('Content-type', 'application/json')
.expect(201).end(done);
});
})
}); });
describe('get', function() { describe('get', function() {