Skip to content

Instantly share code, notes, and snippets.

@Steveorevo
Last active August 18, 2022 07:36
Show Gist options
  • Save Steveorevo/7510cc8c718dbb22f8a062271a66109e to your computer and use it in GitHub Desktop.
Save Steveorevo/7510cc8c718dbb22f8a062271a66109e to your computer and use it in GitHub Desktop.
Determine if a Node-RED node is enabled and all of its subflow parents and subsequent tab
/**
* isNodeEnabled checks to see if the given node is enabled and is in an enabled tab
* and/or subflow instance (vs just in the palette); checks it's parents/ancestors.
*
* @param {object} RED The RED object for accessing nodes.
* @param {string} node_id The node ID to check for a valid instance.
* @return {boolean} True if the node is a valid enabled instance, false otherwise.
*/
function isNodeEnabled(RED, node_id) {
// Get the list of revelant nodes in first pass.
let ancestors = [];
let theNode = null;
RED.nodes.eachNode(function(node) {
if (node.id === node_id) {
theNode = node;
}
if (node.type == 'tab' || node.type.startsWith('subflow:')) {
ancestors.push(node);
}
});
// Walk up the ancestors to see if the node is d/disabled anywhere along the way
while(true) {
// Check if the node is not found
if (theNode == null) {
return false;
}
if (theNode.hasOwnProperty('d')) {
if (theNode.d) {
return false;
}
}
if (theNode.hasOwnProperty('disabled')) {
if (theNode.disabled) {
return false;
}
}
// If the node is an enabled tab return true
if (theNode.type == 'tab') return true;
// Find the node's parent
let parent = null;
for (let i = 0; i < ancestors.length; i++) {
if (ancestors[i].id === theNode.z || ancestors[i].type === ('subflow:' + theNode.z)) {
parent = ancestors[i];
break;
}
}
theNode = parent;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment