Fix number of users when deleting nodes

This commit is contained in:
GogoVega 2024-06-18 20:48:33 +02:00
parent 778e3aebea
commit 2f01fe5832
No known key found for this signature in database
GPG Key ID: E1E048B63AC5AC2B
2 changed files with 25 additions and 37 deletions

View File

@ -679,7 +679,8 @@ RED.nodes = (function() {
} else {
if (n.wires && (n.wires.length > n.outputs)) { n.outputs = n.wires.length; }
n.dirty = true;
updateConfigNodeUsers(n);
// TODO: The event should be triggered?
updateConfigNodeUsers(n, { action: "add", emitEvent: false });
if (n._def.category == "subflows" && typeof n.i === "undefined") {
var nextId = 0;
RED.nodes.eachNode(function(node) {
@ -2293,6 +2294,7 @@ RED.nodes = (function() {
const isConfigNode = def?.category === "config";
// Update the node definition for subflow instance
// TODO: A thing with `node.i`
if (!isUnknownNode && node.type.substring(0, 7) === "subflow") {
const parentId = node.type.split(":")[1];
const subflow = subflowMap[parentId] || getSubflow(parentId);
@ -2343,20 +2345,6 @@ RED.nodes = (function() {
}
}
// Update Users Count
// NOTE: the only reliable way to get the right number is to scan all the nodes
// Replacing a Config Node overwrites the list giving a wrong number
allNodes.eachNode(function (node) {
const def = registry.getNodeType(node.type);
if (def?.category === "config") {
node.users = [];
}
});
allNodes.eachNode(function (node) {
// TODO: The event will be triggered after the import - Quid?
updateConfigNodeUsers(node, { emitEvent: false });
});
const newLinks = [];
// Remap all Wires and (Config) Node references
for (const node of [...newNodes, ...newGroups]) {
@ -2378,6 +2366,7 @@ RED.nodes = (function() {
delete node.wires;
}
// Update the Group id
if (node.g && nodeMap[node.g]) {
node.g = nodeMap[node.g].id;
} else {
@ -2392,27 +2381,18 @@ RED.nodes = (function() {
});
}
// Update the old Node Id with the new one
// Update the node id for select inputs and Links nodes
for (const d in node._def.defaults) {
// Config Node, Node or Links (not sure)
if (node._def.defaults.hasOwnProperty(d) && node._def.defaults[d].type) {
let nodeList = node[d];
if (!Array.isArray(nodeList)) {
nodeList = [nodeList];
}
nodeList = nodeList.map(function(id) {
nodeList = nodeList.map(function (id) {
const n = nodeMap[id];
if (n) {
if (n._def.category === "config") {
// TODO: addNode calls updateConfigNodeUsers so remove here
if (n.users.indexOf(node) === -1) {
n.users.push(node);
}
}
return n.id;
}
return id;
return n ? n.id : id;
});
node[d] = Array.isArray(node[d]) ? nodeList : nodeList[0];
@ -2643,7 +2623,7 @@ RED.nodes = (function() {
// Update any config nodes referenced by the provided node to ensure their 'users' list is correct
function updateConfigNodeUsers(n, options) {
const defaultOptions = { emitEvent: true };
const defaultOptions = { action: "add", emitEvent: true };
options = Object.assign({}, defaultOptions, options);
for (var d in n._def.defaults) {
@ -2654,10 +2634,20 @@ RED.nodes = (function() {
if (type && type.category == "config") {
var configNode = configNodes[n[d]];
if (configNode) {
if (configNode.users.indexOf(n) === -1) {
configNode.users.push(n);
if (options.emitEvent) {
RED.events.emit('nodes:change', configNode);
if (options.action === "add") {
if (configNode.users.indexOf(n) === -1) {
configNode.users.push(n);
if (options.emitEvent) {
RED.events.emit('nodes:change', configNode);
}
}
} else if (options.action === "remove") {
if (configNode.users.indexOf(n) !== -1) {
const users = configNode.users;
users.splice(users.indexOf(n), 1);
if (options.emitEvent) {
RED.events.emit('nodes:change', configNode);
}
}
}
}

View File

@ -508,10 +508,8 @@ RED.subflow = (function() {
var activeSubflow = RED.nodes.subflow(id);
RED.nodes.eachNode(function(n) {
if (!keepInstanceNodes && n.type == "subflow:"+id) {
removedNodes.push(n);
}
if (n.z == id) {
if (n.z === id || !keepInstanceNodes && n.type === "subflow:" + id) {
RED.nodes.updateConfigNodeUsers(n, { emitEvent: false, action: "remove" });
removedNodes.push(n);
}
});