From 6042395b8120ce1ad7a3906e241b4a1641041b89 Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Tue, 8 Nov 2016 17:00:47 +0000 Subject: [PATCH] Allow a node to reorder its outputs and maintain links Fixes #1031 --- editor/js/history.js | 11 ++++++++++- editor/js/ui/editor.js | 30 ++++++++++++++++++++++++++---- nodes/core/logic/10-switch.html | 27 ++++++++++++++++++++++++--- 3 files changed, 60 insertions(+), 8 deletions(-) diff --git a/editor/js/history.js b/editor/js/history.js index 215de3835..c22c71912 100644 --- a/editor/js/history.js +++ b/editor/js/history.js @@ -236,7 +236,16 @@ RED.history = (function() { RED.editor.updateNodeProperties(n); }); } else { - RED.editor.updateNodeProperties(ev.node); + var outputMap; + if (ev.outputMap) { + outputMap = {}; + for (var port in ev.outputMap) { + if (ev.outputMap.hasOwnProperty(port) && ev.outputMap[port] !== -1) { + outputMap[ev.outputMap[port]] = port; + } + } + } + RED.editor.updateNodeProperties(ev.node,outputMap); RED.editor.validateNode(ev.node); } if (ev.links) { diff --git a/editor/js/ui/editor.js b/editor/js/ui/editor.js index 111200de5..c689352b0 100644 --- a/editor/js/ui/editor.js +++ b/editor/js/ui/editor.js @@ -180,19 +180,31 @@ RED.editor = (function() { * Marks the node as dirty and needing a size check. * Removes any links to non-existant outputs. * @param node - the node that has been updated + * @param outputMap - (optional) a map of old->new port numbers if wires should be moved * @returns {array} the links that were removed due to this update */ - function updateNodeProperties(node) { + function updateNodeProperties(node, outputMap) { node.resize = true; node.dirty = true; var removedLinks = []; if (node.ports) { + if (outputMap) { + RED.nodes.eachLink(function(l) { + if (l.source === node && outputMap.hasOwnProperty(l.sourcePort)) { + if (outputMap[l.sourcePort] === -1) { + removedLinks.push(l); + } else { + l.sourcePort = outputMap[l.sourcePort]; + } + } + }); + } if (node.outputs < node.ports.length) { while (node.outputs < node.ports.length) { node.ports.pop(); } RED.nodes.eachLink(function(l) { - if (l.source === node && l.sourcePort >= node.outputs) { + if (l.source === node && l.sourcePort >= node.outputs && removedLinks.indexOf(l) === -1) { removedLinks.push(l); } }); @@ -597,6 +609,7 @@ RED.editor = (function() { var changed = false; var wasDirty = RED.nodes.dirty(); var d; + var outputMap; if (editing_node._def.oneditsave) { var oldValues = {}; @@ -681,8 +694,14 @@ RED.editor = (function() { var credsChanged = updateNodeCredentials(editing_node,credDefinition,prefix); changed = changed || credsChanged; } - - var removedLinks = updateNodeProperties(editing_node); + if (editing_node.hasOwnProperty("_outputs")) { + outputMap = editing_node._outputs; + delete editing_node._outputs; + if (Object.keys(outputMap).length > 0) { + changed = true; + } + } + var removedLinks = updateNodeProperties(editing_node,outputMap); if (changed) { var wasChanged = editing_node.changed; editing_node.changed = true; @@ -712,6 +731,9 @@ RED.editor = (function() { dirty:wasDirty, changed:wasChanged }; + if (outputMap) { + historyEvent.outputMap = outputMap; + } if (subflowInstances) { historyEvent.subflow = { instances:subflowInstances diff --git a/nodes/core/logic/10-switch.html b/nodes/core/logic/10-switch.html index 0d666f514..1330df14d 100644 --- a/nodes/core/logic/10-switch.html +++ b/nodes/core/logic/10-switch.html @@ -115,7 +115,10 @@ $("#node-input-rule-container").css('min-height','250px').css('min-width','450px').editableList({ addItem: function(container,i,opt) { - var rule = opt; + if (!opt.hasOwnProperty('r')) { + opt.r = {}; + } + var rule = opt.r; if (!rule.hasOwnProperty('t')) { rule.t = 'eq'; } @@ -177,6 +180,12 @@ selectField.change(); }, removeItem: function(opt) { + if (opt.hasOwnProperty('i')) { + var removedList = $("#node-input-rule-container").data('removedList')||[]; + removedList.push(opt.i); + $("#node-input-rule-container").data('removedList',removedList); + } + var rules = $("#node-input-rule-container").editableList('items'); rules.each(function(i) { $(this).find(".node-input-rule-index").html(i+1); }); }, @@ -191,15 +200,21 @@ for (var i=0;i