@5minds/node-red-dashboard-2-processcube-process-progress-bar
Version:
A ui component for showing progress bars tracking a process
57 lines (46 loc) • 1.91 kB
JavaScript
const util = require('util');
module.exports = function (RED) {
function ProcessProgressBarNode(config) {
RED.nodes.createNode(this, config);
const node = this;
const group = RED.nodes.getNode(config.group);
const base = group.getBase();
//server-side event handlers
const evts = {
onAction: true,
onInput: function (msg, send, done) {
let activeStep = null;
let finishedSteps = [];
if (msg.payload?.userTask) {
const flowNodeId = msg.payload.userTask.flowNodeId;
const action = msg.payload.action;
const flowNodeIds = config.steps.map(step => step.flowNodeId);
const flowNodeIdExists = flowNodeIds.includes(flowNodeId);
if (flowNodeIdExists && (action === 'new' || ['running', 'suspended'].includes(msg.payload.userTask.state))) {
activeStep = flowNodeId;
finishedSteps = flowNodeIds.slice(0, flowNodeIds.indexOf(flowNodeId));
} else if (flowNodeIdExists && (action === 'finished' || msg.payload.userTask.state === 'finished')) {
finishedSteps = flowNodeIds.slice(0, flowNodeIds.indexOf(flowNodeId) + 1)
}
}
msg.processProgressBar = {
activeStep, finishedSteps
};
base.stores.data.save(base, node, msg);
},
};
if (group) {
group.register(node, config, evts);
} else {
node.error('No group configured');
}
}
RED.nodes.registerType('ui-process-progress-bar', ProcessProgressBarNode, {
defaults: {
outputs: { value: 0 },
},
outputs: function (config) {
return 0;
},
});
};