2015-03-12 01:08:47 +01:00
|
|
|
/**
|
2017-01-11 16:24:33 +01:00
|
|
|
* Copyright JS Foundation and other contributors, http://js.foundation
|
2015-03-12 01:08:47 +01:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
**/
|
|
|
|
|
|
|
|
RED.subflow = (function() {
|
2015-07-01 00:42:03 +02:00
|
|
|
|
2015-03-12 01:08:47 +01:00
|
|
|
|
2018-04-26 18:53:45 +02:00
|
|
|
var _subflowEditTemplate = '<script type="text/x-red" data-template-name="subflow"><div class="form-row"><label for="node-input-name" data-i18n="[append]editor:common.label.name"><i class="fa fa-tag"></i> </label><input type="text" id="node-input-name"></div></script>';
|
|
|
|
var _subflowTemplateEditTemplate = '<script type="text/x-red" data-template-name="subflow-template"><div class="form-row"><i class="fa fa-tag"></i><label for="subflow-input-name" data-i18n="common.label.name"></label><input type="text" id="subflow-input-name"></div><div class="form-row" style="margin-bottom: 0px;"><label for="subflow-input-info" data-i18n="editor:subflow.info"></label><a href="https://help.github.com/articles/markdown-basics/" style="font-size: 0.8em; float: right;" data-i18n="[html]subflow.format"></a></div><div class="form-row node-text-editor-row"><div style="height: 250px;" class="node-text-editor" id="subflow-input-info-editor"></div></div><div class="form-row form-tips" id="subflow-dialog-user-count"></div></script>';
|
|
|
|
|
|
|
|
|
2015-03-12 01:08:47 +01:00
|
|
|
function getSubflow() {
|
2015-03-13 00:38:37 +01:00
|
|
|
return RED.nodes.subflow(RED.workspaces.active());
|
2015-03-12 01:08:47 +01:00
|
|
|
}
|
2015-07-01 00:42:03 +02:00
|
|
|
|
2015-07-30 12:03:37 +02:00
|
|
|
function findAvailableSubflowIOPosition(subflow,isInput) {
|
|
|
|
var pos = {x:50,y:30};
|
|
|
|
if (!isInput) {
|
|
|
|
pos.x += 110;
|
|
|
|
}
|
2015-03-12 01:08:47 +01:00
|
|
|
for (var i=0;i<subflow.out.length+subflow.in.length;i++) {
|
|
|
|
var port;
|
|
|
|
if (i < subflow.out.length) {
|
|
|
|
port = subflow.out[i];
|
|
|
|
} else {
|
|
|
|
port = subflow.in[i-subflow.out.length];
|
|
|
|
}
|
|
|
|
if (port.x == pos.x && port.y == pos.y) {
|
|
|
|
pos.x += 55;
|
|
|
|
i=0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pos;
|
|
|
|
}
|
2015-07-01 00:42:03 +02:00
|
|
|
|
2015-03-12 01:08:47 +01:00
|
|
|
function addSubflowInput() {
|
2015-03-13 00:38:37 +01:00
|
|
|
var subflow = RED.nodes.subflow(RED.workspaces.active());
|
2015-07-30 12:03:37 +02:00
|
|
|
if (subflow.in.length === 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var position = findAvailableSubflowIOPosition(subflow,true);
|
2015-03-12 01:08:47 +01:00
|
|
|
var newInput = {
|
|
|
|
type:"subflow",
|
|
|
|
direction:"in",
|
|
|
|
z:subflow.id,
|
|
|
|
i:subflow.in.length,
|
|
|
|
x:position.x,
|
|
|
|
y:position.y,
|
|
|
|
id:RED.nodes.id()
|
|
|
|
};
|
|
|
|
var oldInCount = subflow.in.length;
|
|
|
|
subflow.in.push(newInput);
|
|
|
|
subflow.dirty = true;
|
2015-03-15 22:54:36 +01:00
|
|
|
var wasDirty = RED.nodes.dirty();
|
2015-03-12 01:08:47 +01:00
|
|
|
var wasChanged = subflow.changed;
|
|
|
|
subflow.changed = true;
|
2015-07-30 12:03:37 +02:00
|
|
|
var result = refresh(true);
|
2015-03-12 01:08:47 +01:00
|
|
|
var historyEvent = {
|
|
|
|
t:'edit',
|
|
|
|
node:subflow,
|
|
|
|
dirty:wasDirty,
|
|
|
|
changed:wasChanged,
|
|
|
|
subflow: {
|
2015-07-30 12:03:37 +02:00
|
|
|
inputCount: oldInCount,
|
|
|
|
instances: result.instances
|
2015-03-12 01:08:47 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
RED.history.push(historyEvent);
|
|
|
|
RED.view.select();
|
2015-07-30 12:03:37 +02:00
|
|
|
RED.nodes.dirty(true);
|
|
|
|
RED.view.redraw();
|
|
|
|
$("#workspace-subflow-input-add").addClass("active");
|
|
|
|
$("#workspace-subflow-input-remove").removeClass("active");
|
|
|
|
}
|
|
|
|
|
|
|
|
function removeSubflowInput() {
|
|
|
|
var activeSubflow = RED.nodes.subflow(RED.workspaces.active());
|
|
|
|
if (activeSubflow.in.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var removedInput = activeSubflow.in[0];
|
|
|
|
var removedInputLinks = [];
|
|
|
|
RED.nodes.eachLink(function(l) {
|
|
|
|
if (l.source.type == "subflow" && l.source.z == activeSubflow.id && l.source.i == removedInput.i) {
|
|
|
|
removedInputLinks.push(l);
|
|
|
|
} else if (l.target.type == "subflow:"+activeSubflow.id) {
|
|
|
|
removedInputLinks.push(l);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
removedInputLinks.forEach(function(l) { RED.nodes.removeLink(l)});
|
|
|
|
activeSubflow.in = [];
|
|
|
|
$("#workspace-subflow-input-add").removeClass("active");
|
|
|
|
$("#workspace-subflow-input-remove").addClass("active");
|
|
|
|
activeSubflow.changed = true;
|
|
|
|
return {subflowInputs: [ removedInput ], links:removedInputLinks};
|
2015-03-12 01:08:47 +01:00
|
|
|
}
|
2015-07-01 00:42:03 +02:00
|
|
|
|
2015-03-12 01:08:47 +01:00
|
|
|
function addSubflowOutput(id) {
|
2015-03-13 00:38:37 +01:00
|
|
|
var subflow = RED.nodes.subflow(RED.workspaces.active());
|
2015-07-30 12:03:37 +02:00
|
|
|
var position = findAvailableSubflowIOPosition(subflow,false);
|
2015-07-01 00:42:03 +02:00
|
|
|
|
2015-03-12 01:08:47 +01:00
|
|
|
var newOutput = {
|
|
|
|
type:"subflow",
|
|
|
|
direction:"out",
|
|
|
|
z:subflow.id,
|
|
|
|
i:subflow.out.length,
|
|
|
|
x:position.x,
|
|
|
|
y:position.y,
|
|
|
|
id:RED.nodes.id()
|
|
|
|
};
|
|
|
|
var oldOutCount = subflow.out.length;
|
|
|
|
subflow.out.push(newOutput);
|
|
|
|
subflow.dirty = true;
|
2015-03-15 22:54:36 +01:00
|
|
|
var wasDirty = RED.nodes.dirty();
|
2015-03-12 01:08:47 +01:00
|
|
|
var wasChanged = subflow.changed;
|
|
|
|
subflow.changed = true;
|
2015-07-01 00:42:03 +02:00
|
|
|
|
2015-07-30 12:03:37 +02:00
|
|
|
var result = refresh(true);
|
|
|
|
|
2015-03-12 01:08:47 +01:00
|
|
|
var historyEvent = {
|
|
|
|
t:'edit',
|
|
|
|
node:subflow,
|
|
|
|
dirty:wasDirty,
|
|
|
|
changed:wasChanged,
|
|
|
|
subflow: {
|
2015-07-30 12:03:37 +02:00
|
|
|
outputCount: oldOutCount,
|
|
|
|
instances: result.instances
|
2015-03-12 01:08:47 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
RED.history.push(historyEvent);
|
|
|
|
RED.view.select();
|
2015-07-30 12:03:37 +02:00
|
|
|
RED.nodes.dirty(true);
|
|
|
|
RED.view.redraw();
|
2018-05-11 15:05:52 +02:00
|
|
|
$("#workspace-subflow-output .spinner-value").text(subflow.out.length);
|
2015-03-12 01:08:47 +01:00
|
|
|
}
|
2015-07-01 00:42:03 +02:00
|
|
|
|
2015-07-30 12:03:37 +02:00
|
|
|
function removeSubflowOutput(removedSubflowOutputs) {
|
|
|
|
var activeSubflow = RED.nodes.subflow(RED.workspaces.active());
|
|
|
|
if (activeSubflow.out.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (typeof removedSubflowOutputs === "undefined") {
|
|
|
|
removedSubflowOutputs = [activeSubflow.out[activeSubflow.out.length-1]];
|
|
|
|
}
|
|
|
|
var removedLinks = [];
|
|
|
|
removedSubflowOutputs.sort(function(a,b) { return b.i-a.i});
|
|
|
|
for (i=0;i<removedSubflowOutputs.length;i++) {
|
|
|
|
var output = removedSubflowOutputs[i];
|
|
|
|
activeSubflow.out.splice(output.i,1);
|
|
|
|
var subflowRemovedLinks = [];
|
|
|
|
var subflowMovedLinks = [];
|
|
|
|
RED.nodes.eachLink(function(l) {
|
|
|
|
if (l.target.type == "subflow" && l.target.z == activeSubflow.id && l.target.i == output.i) {
|
|
|
|
subflowRemovedLinks.push(l);
|
|
|
|
}
|
|
|
|
if (l.source.type == "subflow:"+activeSubflow.id) {
|
|
|
|
if (l.sourcePort == output.i) {
|
|
|
|
subflowRemovedLinks.push(l);
|
|
|
|
} else if (l.sourcePort > output.i) {
|
|
|
|
subflowMovedLinks.push(l);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
subflowRemovedLinks.forEach(function(l) { RED.nodes.removeLink(l)});
|
|
|
|
subflowMovedLinks.forEach(function(l) { l.sourcePort--; });
|
|
|
|
|
|
|
|
removedLinks = removedLinks.concat(subflowRemovedLinks);
|
|
|
|
for (var j=output.i;j<activeSubflow.out.length;j++) {
|
|
|
|
activeSubflow.out[j].i--;
|
|
|
|
activeSubflow.out[j].dirty = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
activeSubflow.changed = true;
|
|
|
|
|
|
|
|
return {subflowOutputs: removedSubflowOutputs, links: removedLinks}
|
|
|
|
}
|
|
|
|
|
|
|
|
function refresh(markChange) {
|
|
|
|
var activeSubflow = RED.nodes.subflow(RED.workspaces.active());
|
|
|
|
refreshToolbar(activeSubflow);
|
|
|
|
var subflowInstances = [];
|
|
|
|
if (activeSubflow) {
|
|
|
|
RED.nodes.filterNodes({type:"subflow:"+activeSubflow.id}).forEach(function(n) {
|
|
|
|
subflowInstances.push({
|
|
|
|
id: n.id,
|
|
|
|
changed: n.changed
|
|
|
|
});
|
|
|
|
if (markChange) {
|
|
|
|
n.changed = true;
|
|
|
|
}
|
|
|
|
n.inputs = activeSubflow.in.length;
|
|
|
|
n.outputs = activeSubflow.out.length;
|
|
|
|
while (n.outputs < n.ports.length) {
|
|
|
|
n.ports.pop();
|
|
|
|
}
|
|
|
|
n.resize = true;
|
|
|
|
n.dirty = true;
|
|
|
|
RED.editor.updateNodeProperties(n);
|
|
|
|
});
|
|
|
|
RED.editor.validateNode(activeSubflow);
|
|
|
|
return {
|
|
|
|
instances: subflowInstances
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function refreshToolbar(activeSubflow) {
|
|
|
|
if (activeSubflow) {
|
|
|
|
$("#workspace-subflow-input-add").toggleClass("active", activeSubflow.in.length !== 0);
|
|
|
|
$("#workspace-subflow-input-remove").toggleClass("active",activeSubflow.in.length === 0);
|
|
|
|
|
2018-05-11 15:05:52 +02:00
|
|
|
$("#workspace-subflow-output .spinner-value").text(activeSubflow.out.length);
|
2015-07-30 12:03:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function showWorkspaceToolbar(activeSubflow) {
|
|
|
|
var toolbar = $("#workspace-toolbar");
|
|
|
|
toolbar.empty();
|
|
|
|
|
2015-10-23 23:14:21 +02:00
|
|
|
$('<a class="button" id="workspace-subflow-edit" href="#" data-i18n="[append]subflow.editSubflowProperties"><i class="fa fa-pencil"></i> </a>').appendTo(toolbar);
|
2015-07-30 12:03:37 +02:00
|
|
|
$('<span style="margin-left: 5px;" data-i18n="subflow.input"></span> '+
|
|
|
|
'<div style="display: inline-block;" class="button-group">'+
|
|
|
|
'<a id="workspace-subflow-input-remove" class="button active" href="#">0</a>'+
|
|
|
|
'<a id="workspace-subflow-input-add" class="button" href="#">1</a>'+
|
|
|
|
'</div>').appendTo(toolbar);
|
|
|
|
|
|
|
|
$('<span style="margin-left: 5px;" data-i18n="subflow.output"></span> <div id="workspace-subflow-output" style="display: inline-block;" class="button-group spinner-group">'+
|
|
|
|
'<a id="workspace-subflow-output-remove" class="button" href="#"><i class="fa fa-minus"></i></a>'+
|
|
|
|
'<div class="spinner-value">3</div>'+
|
|
|
|
'<a id="workspace-subflow-output-add" class="button" href="#"><i class="fa fa-plus"></i></a>'+
|
|
|
|
'</div>').appendTo(toolbar);
|
|
|
|
|
|
|
|
// $('<a class="button disabled" id="workspace-subflow-add-input" href="#" data-i18n="[append]subflow.input"><i class="fa fa-plus"></i> </a>').appendTo(toolbar);
|
|
|
|
// $('<a class="button" id="workspace-subflow-add-output" href="#" data-i18n="[append]subflow.output"><i class="fa fa-plus"></i> </a>').appendTo(toolbar);
|
|
|
|
$('<a class="button" id="workspace-subflow-delete" href="#" data-i18n="[append]subflow.deleteSubflow"><i class="fa fa-trash"></i> </a>').appendTo(toolbar);
|
|
|
|
toolbar.i18n();
|
|
|
|
|
|
|
|
|
|
|
|
$("#workspace-subflow-output-remove").click(function(event) {
|
2015-03-12 01:08:47 +01:00
|
|
|
event.preventDefault();
|
2015-07-30 12:03:37 +02:00
|
|
|
var wasDirty = RED.nodes.dirty();
|
|
|
|
var wasChanged = activeSubflow.changed;
|
|
|
|
var result = removeSubflowOutput();
|
|
|
|
if (result) {
|
|
|
|
var inst = refresh(true);
|
|
|
|
RED.history.push({
|
|
|
|
t:'delete',
|
|
|
|
links:result.links,
|
|
|
|
subflowOutputs: result.subflowOutputs,
|
|
|
|
changed: wasChanged,
|
|
|
|
dirty:wasDirty,
|
|
|
|
subflow: {
|
|
|
|
instances: inst.instances
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
RED.view.select();
|
|
|
|
RED.nodes.dirty(true);
|
|
|
|
RED.view.redraw(true);
|
|
|
|
}
|
2015-03-12 01:08:47 +01:00
|
|
|
});
|
2015-07-30 12:03:37 +02:00
|
|
|
$("#workspace-subflow-output-add").click(function(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
addSubflowOutput();
|
|
|
|
});
|
|
|
|
|
|
|
|
$("#workspace-subflow-input-add").click(function(event) {
|
2015-03-12 01:08:47 +01:00
|
|
|
event.preventDefault();
|
|
|
|
addSubflowInput();
|
|
|
|
});
|
2015-07-30 12:03:37 +02:00
|
|
|
$("#workspace-subflow-input-remove").click(function(event) {
|
2015-03-12 01:08:47 +01:00
|
|
|
event.preventDefault();
|
2015-07-30 12:03:37 +02:00
|
|
|
var wasDirty = RED.nodes.dirty();
|
|
|
|
var wasChanged = activeSubflow.changed;
|
|
|
|
activeSubflow.changed = true;
|
|
|
|
var result = removeSubflowInput();
|
|
|
|
if (result) {
|
|
|
|
var inst = refresh(true);
|
|
|
|
RED.history.push({
|
|
|
|
t:'delete',
|
|
|
|
links:result.links,
|
|
|
|
changed: wasChanged,
|
|
|
|
subflowInputs: result.subflowInputs,
|
|
|
|
dirty:wasDirty,
|
|
|
|
subflow: {
|
|
|
|
instances: inst.instances
|
|
|
|
}
|
|
|
|
});
|
|
|
|
RED.view.select();
|
|
|
|
RED.nodes.dirty(true);
|
|
|
|
RED.view.redraw(true);
|
2015-03-12 01:08:47 +01:00
|
|
|
}
|
2015-07-30 12:03:37 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
$("#workspace-subflow-edit").click(function(event) {
|
|
|
|
RED.editor.editSubflow(RED.nodes.subflow(RED.workspaces.active()));
|
|
|
|
event.preventDefault();
|
2015-03-12 01:08:47 +01:00
|
|
|
});
|
2015-07-01 00:42:03 +02:00
|
|
|
|
2015-03-12 01:08:47 +01:00
|
|
|
$("#workspace-subflow-delete").click(function(event) {
|
|
|
|
event.preventDefault();
|
2015-03-15 22:54:36 +01:00
|
|
|
var startDirty = RED.nodes.dirty();
|
2016-12-20 20:42:38 +01:00
|
|
|
var historyEvent = removeSubflow(RED.workspaces.active());
|
|
|
|
historyEvent.t = 'delete';
|
|
|
|
historyEvent.dirty = startDirty;
|
2015-07-01 00:42:03 +02:00
|
|
|
|
2016-12-20 20:42:38 +01:00
|
|
|
RED.history.push(historyEvent);
|
2015-09-25 14:29:55 +02:00
|
|
|
|
2015-03-12 01:08:47 +01:00
|
|
|
});
|
2015-07-01 00:42:03 +02:00
|
|
|
|
2015-07-30 12:03:37 +02:00
|
|
|
refreshToolbar(activeSubflow);
|
|
|
|
|
|
|
|
$("#chart").css({"margin-top": "40px"});
|
|
|
|
$("#workspace-toolbar").show();
|
|
|
|
}
|
|
|
|
function hideWorkspaceToolbar() {
|
|
|
|
$("#workspace-toolbar").hide().empty();
|
|
|
|
$("#chart").css({"margin-top": "0"});
|
|
|
|
}
|
|
|
|
|
2016-12-20 20:42:38 +01:00
|
|
|
function removeSubflow(id) {
|
|
|
|
var removedNodes = [];
|
|
|
|
var removedLinks = [];
|
|
|
|
|
|
|
|
var activeSubflow = RED.nodes.subflow(id);
|
2015-07-30 12:03:37 +02:00
|
|
|
|
2016-12-20 20:42:38 +01:00
|
|
|
RED.nodes.eachNode(function(n) {
|
|
|
|
if (n.type == "subflow:"+activeSubflow.id) {
|
|
|
|
removedNodes.push(n);
|
|
|
|
}
|
|
|
|
if (n.z == activeSubflow.id) {
|
|
|
|
removedNodes.push(n);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
RED.nodes.eachConfig(function(n) {
|
|
|
|
if (n.z == activeSubflow.id) {
|
|
|
|
removedNodes.push(n);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
var removedConfigNodes = [];
|
|
|
|
for (var i=0;i<removedNodes.length;i++) {
|
|
|
|
var removedEntities = RED.nodes.remove(removedNodes[i].id);
|
|
|
|
removedLinks = removedLinks.concat(removedEntities.links);
|
|
|
|
removedConfigNodes = removedConfigNodes.concat(removedEntities.nodes);
|
|
|
|
}
|
|
|
|
// TODO: this whole delete logic should be in RED.nodes.removeSubflow..
|
|
|
|
removedNodes = removedNodes.concat(removedConfigNodes);
|
|
|
|
|
|
|
|
RED.nodes.removeSubflow(activeSubflow);
|
|
|
|
RED.workspaces.remove(activeSubflow);
|
|
|
|
RED.nodes.dirty(true);
|
|
|
|
RED.view.redraw();
|
|
|
|
|
|
|
|
return {
|
|
|
|
nodes:removedNodes,
|
|
|
|
links:removedLinks,
|
|
|
|
subflow: {
|
|
|
|
subflow: activeSubflow
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-30 12:03:37 +02:00
|
|
|
function init() {
|
|
|
|
RED.events.on("workspace:change",function(event) {
|
|
|
|
var activeSubflow = RED.nodes.subflow(event.workspace);
|
|
|
|
if (activeSubflow) {
|
|
|
|
showWorkspaceToolbar(activeSubflow);
|
|
|
|
} else {
|
|
|
|
hideWorkspaceToolbar();
|
|
|
|
}
|
|
|
|
});
|
2015-07-10 20:49:31 +02:00
|
|
|
RED.events.on("view:selection-changed",function(selection) {
|
2015-03-12 12:21:05 +01:00
|
|
|
if (!selection.nodes) {
|
2015-04-13 17:48:38 +02:00
|
|
|
RED.menu.setDisabled("menu-item-subflow-convert",true);
|
2015-03-12 12:21:05 +01:00
|
|
|
} else {
|
2015-04-13 17:48:38 +02:00
|
|
|
RED.menu.setDisabled("menu-item-subflow-convert",false);
|
2015-03-12 12:21:05 +01:00
|
|
|
}
|
|
|
|
});
|
2015-07-01 00:42:03 +02:00
|
|
|
|
2016-12-04 23:59:43 +01:00
|
|
|
RED.actions.add("core:create-subflow",createSubflow);
|
|
|
|
RED.actions.add("core:convert-to-subflow",convertToSubflow);
|
2018-04-26 18:53:45 +02:00
|
|
|
|
|
|
|
$(_subflowEditTemplate).appendTo(document.body);
|
|
|
|
$(_subflowTemplateEditTemplate).appendTo(document.body);
|
|
|
|
|
2015-03-12 01:08:47 +01:00
|
|
|
}
|
2015-07-01 00:42:03 +02:00
|
|
|
|
2015-03-12 01:08:47 +01:00
|
|
|
function createSubflow() {
|
|
|
|
var lastIndex = 0;
|
|
|
|
RED.nodes.eachSubflow(function(sf) {
|
|
|
|
var m = (new RegExp("^Subflow (\\d+)$")).exec(sf.name);
|
|
|
|
if (m) {
|
|
|
|
lastIndex = Math.max(lastIndex,m[1]);
|
|
|
|
}
|
|
|
|
});
|
2015-07-01 00:42:03 +02:00
|
|
|
|
2015-03-12 01:08:47 +01:00
|
|
|
var name = "Subflow "+(lastIndex+1);
|
2015-07-01 00:42:03 +02:00
|
|
|
|
2015-03-12 01:08:47 +01:00
|
|
|
var subflowId = RED.nodes.id();
|
|
|
|
var subflow = {
|
|
|
|
type:"subflow",
|
|
|
|
id:subflowId,
|
|
|
|
name:name,
|
2015-10-23 23:14:21 +02:00
|
|
|
info:"",
|
2015-03-12 01:08:47 +01:00
|
|
|
in: [],
|
|
|
|
out: []
|
|
|
|
};
|
|
|
|
RED.nodes.addSubflow(subflow);
|
|
|
|
RED.history.push({
|
|
|
|
t:'createSubflow',
|
2015-07-30 12:03:37 +02:00
|
|
|
subflow: {
|
|
|
|
subflow:subflow
|
|
|
|
},
|
2015-03-15 22:54:36 +01:00
|
|
|
dirty:RED.nodes.dirty()
|
2015-03-12 01:08:47 +01:00
|
|
|
});
|
2015-03-13 00:38:37 +01:00
|
|
|
RED.workspaces.show(subflowId);
|
2015-10-23 23:14:21 +02:00
|
|
|
RED.nodes.dirty(true);
|
2015-03-12 01:08:47 +01:00
|
|
|
}
|
2015-07-01 00:42:03 +02:00
|
|
|
|
2015-03-12 01:08:47 +01:00
|
|
|
function convertToSubflow() {
|
|
|
|
var selection = RED.view.selection();
|
|
|
|
if (!selection.nodes) {
|
2015-07-01 00:42:03 +02:00
|
|
|
RED.notify(RED._("subflow.errors.noNodesSelected"),"error");
|
2015-03-12 01:08:47 +01:00
|
|
|
return;
|
|
|
|
}
|
2016-06-30 00:51:08 +02:00
|
|
|
var i,n;
|
2015-03-12 01:08:47 +01:00
|
|
|
var nodes = {};
|
|
|
|
var new_links = [];
|
|
|
|
var removedLinks = [];
|
2015-07-01 00:42:03 +02:00
|
|
|
|
2015-03-12 01:08:47 +01:00
|
|
|
var candidateInputs = [];
|
|
|
|
var candidateOutputs = [];
|
2015-07-30 12:03:37 +02:00
|
|
|
var candidateInputNodes = {};
|
|
|
|
|
2015-07-01 00:42:03 +02:00
|
|
|
|
2015-03-12 01:08:47 +01:00
|
|
|
var boundingBox = [selection.nodes[0].x,
|
|
|
|
selection.nodes[0].y,
|
|
|
|
selection.nodes[0].x,
|
|
|
|
selection.nodes[0].y];
|
2015-07-01 00:42:03 +02:00
|
|
|
|
2015-03-12 01:08:47 +01:00
|
|
|
for (i=0;i<selection.nodes.length;i++) {
|
2016-06-30 00:51:08 +02:00
|
|
|
n = selection.nodes[i];
|
2015-03-12 01:08:47 +01:00
|
|
|
nodes[n.id] = {n:n,outputs:{}};
|
|
|
|
boundingBox = [
|
|
|
|
Math.min(boundingBox[0],n.x),
|
|
|
|
Math.min(boundingBox[1],n.y),
|
|
|
|
Math.max(boundingBox[2],n.x),
|
|
|
|
Math.max(boundingBox[3],n.y)
|
|
|
|
]
|
|
|
|
}
|
2015-07-01 00:42:03 +02:00
|
|
|
|
2015-03-12 01:08:47 +01:00
|
|
|
var center = [(boundingBox[2]+boundingBox[0]) / 2,(boundingBox[3]+boundingBox[1]) / 2];
|
2015-07-01 00:42:03 +02:00
|
|
|
|
2015-03-12 01:08:47 +01:00
|
|
|
RED.nodes.eachLink(function(link) {
|
|
|
|
if (nodes[link.source.id] && nodes[link.target.id]) {
|
|
|
|
// A link wholely within the selection
|
|
|
|
}
|
2015-07-01 00:42:03 +02:00
|
|
|
|
2015-03-12 01:08:47 +01:00
|
|
|
if (nodes[link.source.id] && !nodes[link.target.id]) {
|
|
|
|
// An outbound link from the selection
|
|
|
|
candidateOutputs.push(link);
|
|
|
|
removedLinks.push(link);
|
|
|
|
}
|
|
|
|
if (!nodes[link.source.id] && nodes[link.target.id]) {
|
|
|
|
// An inbound link
|
|
|
|
candidateInputs.push(link);
|
2015-07-30 12:03:37 +02:00
|
|
|
candidateInputNodes[link.target.id] = link.target;
|
2015-03-12 01:08:47 +01:00
|
|
|
removedLinks.push(link);
|
|
|
|
}
|
|
|
|
});
|
2015-07-01 00:42:03 +02:00
|
|
|
|
2015-03-12 01:08:47 +01:00
|
|
|
var outputs = {};
|
|
|
|
candidateOutputs = candidateOutputs.filter(function(v) {
|
|
|
|
if (outputs[v.source.id+":"+v.sourcePort]) {
|
|
|
|
outputs[v.source.id+":"+v.sourcePort].targets.push(v.target);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
v.targets = [];
|
|
|
|
v.targets.push(v.target);
|
|
|
|
outputs[v.source.id+":"+v.sourcePort] = v;
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
candidateOutputs.sort(function(a,b) { return a.source.y-b.source.y});
|
2015-07-01 00:42:03 +02:00
|
|
|
|
2015-07-30 12:03:37 +02:00
|
|
|
if (Object.keys(candidateInputNodes).length > 1) {
|
2015-07-01 00:42:03 +02:00
|
|
|
RED.notify(RED._("subflow.errors.multipleInputsToSelection"),"error");
|
2015-03-12 01:08:47 +01:00
|
|
|
return;
|
|
|
|
}
|
2015-07-01 00:42:03 +02:00
|
|
|
|
2015-03-12 01:08:47 +01:00
|
|
|
var lastIndex = 0;
|
|
|
|
RED.nodes.eachSubflow(function(sf) {
|
|
|
|
var m = (new RegExp("^Subflow (\\d+)$")).exec(sf.name);
|
|
|
|
if (m) {
|
|
|
|
lastIndex = Math.max(lastIndex,m[1]);
|
|
|
|
}
|
|
|
|
});
|
2015-07-01 00:42:03 +02:00
|
|
|
|
2015-03-12 01:08:47 +01:00
|
|
|
var name = "Subflow "+(lastIndex+1);
|
2015-07-01 00:42:03 +02:00
|
|
|
|
2015-03-12 01:08:47 +01:00
|
|
|
var subflowId = RED.nodes.id();
|
|
|
|
var subflow = {
|
|
|
|
type:"subflow",
|
|
|
|
id:subflowId,
|
|
|
|
name:name,
|
2015-10-23 23:14:21 +02:00
|
|
|
info:"",
|
2015-07-30 12:03:37 +02:00
|
|
|
in: Object.keys(candidateInputNodes).map(function(v,i) { var index = i; return {
|
2015-03-12 01:08:47 +01:00
|
|
|
type:"subflow",
|
|
|
|
direction:"in",
|
2015-07-30 12:03:37 +02:00
|
|
|
x:candidateInputNodes[v].x-(candidateInputNodes[v].w/2)-80,
|
|
|
|
y:candidateInputNodes[v].y,
|
2015-03-12 01:08:47 +01:00
|
|
|
z:subflowId,
|
|
|
|
i:index,
|
|
|
|
id:RED.nodes.id(),
|
2015-07-30 12:03:37 +02:00
|
|
|
wires:[{id:candidateInputNodes[v].id}]
|
2015-03-12 01:08:47 +01:00
|
|
|
}}),
|
|
|
|
out: candidateOutputs.map(function(v,i) { var index = i; return {
|
|
|
|
type:"subflow",
|
|
|
|
direction:"in",
|
|
|
|
x:v.source.x+(v.source.w/2)+80,
|
|
|
|
y:v.source.y,
|
|
|
|
z:subflowId,
|
|
|
|
i:index,
|
|
|
|
id:RED.nodes.id(),
|
|
|
|
wires:[{id:v.source.id,port:v.sourcePort}]
|
|
|
|
}})
|
|
|
|
};
|
2015-07-30 12:03:37 +02:00
|
|
|
|
2015-03-12 01:08:47 +01:00
|
|
|
RED.nodes.addSubflow(subflow);
|
2015-07-01 00:42:03 +02:00
|
|
|
|
2015-03-12 01:08:47 +01:00
|
|
|
var subflowInstance = {
|
|
|
|
id:RED.nodes.id(),
|
|
|
|
type:"subflow:"+subflow.id,
|
|
|
|
x: center[0],
|
|
|
|
y: center[1],
|
2015-03-13 00:38:37 +01:00
|
|
|
z: RED.workspaces.active(),
|
2015-03-12 01:08:47 +01:00
|
|
|
inputs: subflow.in.length,
|
|
|
|
outputs: subflow.out.length,
|
|
|
|
h: Math.max(30/*node_height*/,(subflow.out.length||0) * 15),
|
|
|
|
changed:true
|
|
|
|
}
|
|
|
|
subflowInstance._def = RED.nodes.getType(subflowInstance.type);
|
|
|
|
RED.editor.validateNode(subflowInstance);
|
|
|
|
RED.nodes.add(subflowInstance);
|
2015-07-01 00:42:03 +02:00
|
|
|
|
2015-03-12 01:08:47 +01:00
|
|
|
candidateInputs.forEach(function(l) {
|
|
|
|
var link = {source:l.source, sourcePort:l.sourcePort, target: subflowInstance};
|
|
|
|
new_links.push(link);
|
|
|
|
RED.nodes.addLink(link);
|
|
|
|
});
|
2015-07-01 00:42:03 +02:00
|
|
|
|
2015-03-12 01:08:47 +01:00
|
|
|
candidateOutputs.forEach(function(output,i) {
|
|
|
|
output.targets.forEach(function(target) {
|
|
|
|
var link = {source:subflowInstance, sourcePort:i, target: target};
|
|
|
|
new_links.push(link);
|
|
|
|
RED.nodes.addLink(link);
|
|
|
|
});
|
|
|
|
});
|
2015-07-01 00:42:03 +02:00
|
|
|
|
2015-03-12 01:08:47 +01:00
|
|
|
subflow.in.forEach(function(input) {
|
|
|
|
input.wires.forEach(function(wire) {
|
|
|
|
var link = {source: input, sourcePort: 0, target: RED.nodes.node(wire.id) }
|
|
|
|
new_links.push(link);
|
|
|
|
RED.nodes.addLink(link);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
subflow.out.forEach(function(output,i) {
|
|
|
|
output.wires.forEach(function(wire) {
|
|
|
|
var link = {source: RED.nodes.node(wire.id), sourcePort: wire.port , target: output }
|
|
|
|
new_links.push(link);
|
|
|
|
RED.nodes.addLink(link);
|
|
|
|
});
|
|
|
|
});
|
2015-07-01 00:42:03 +02:00
|
|
|
|
2015-03-12 01:08:47 +01:00
|
|
|
for (i=0;i<removedLinks.length;i++) {
|
|
|
|
RED.nodes.removeLink(removedLinks[i]);
|
|
|
|
}
|
2015-07-01 00:42:03 +02:00
|
|
|
|
2015-03-12 01:08:47 +01:00
|
|
|
for (i=0;i<selection.nodes.length;i++) {
|
2016-06-30 00:51:08 +02:00
|
|
|
n = selection.nodes[i];
|
|
|
|
if (/^link /.test(n.type)) {
|
|
|
|
n.links = n.links.filter(function(id) {
|
|
|
|
var isLocalLink = nodes.hasOwnProperty(id);
|
|
|
|
if (!isLocalLink) {
|
|
|
|
var otherNode = RED.nodes.node(id);
|
|
|
|
if (otherNode && otherNode.links) {
|
|
|
|
var i = otherNode.links.indexOf(n.id);
|
|
|
|
if (i > -1) {
|
|
|
|
otherNode.links.splice(i,1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return isLocalLink;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
n.z = subflow.id;
|
2015-03-12 01:08:47 +01:00
|
|
|
}
|
2015-07-01 00:42:03 +02:00
|
|
|
|
2015-03-12 01:08:47 +01:00
|
|
|
RED.history.push({
|
|
|
|
t:'createSubflow',
|
|
|
|
nodes:[subflowInstance.id],
|
|
|
|
links:new_links,
|
2015-07-30 12:03:37 +02:00
|
|
|
subflow: {
|
|
|
|
subflow: subflow
|
|
|
|
},
|
2015-07-01 00:42:03 +02:00
|
|
|
|
2015-03-13 00:38:37 +01:00
|
|
|
activeWorkspace: RED.workspaces.active(),
|
2015-03-12 01:08:47 +01:00
|
|
|
removedLinks: removedLinks,
|
2015-07-01 00:42:03 +02:00
|
|
|
|
2015-03-15 22:54:36 +01:00
|
|
|
dirty:RED.nodes.dirty()
|
2015-03-12 01:08:47 +01:00
|
|
|
});
|
2016-06-30 00:51:08 +02:00
|
|
|
RED.view.select(null);
|
2015-03-20 00:11:55 +01:00
|
|
|
RED.editor.validateNode(subflow);
|
2015-03-15 22:54:36 +01:00
|
|
|
RED.nodes.dirty(true);
|
2015-03-12 12:21:05 +01:00
|
|
|
RED.view.redraw(true);
|
2015-03-12 01:08:47 +01:00
|
|
|
}
|
2015-07-01 00:42:03 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2015-03-12 01:08:47 +01:00
|
|
|
return {
|
|
|
|
init: init,
|
|
|
|
createSubflow: createSubflow,
|
2015-07-30 12:03:37 +02:00
|
|
|
convertToSubflow: convertToSubflow,
|
2016-12-20 20:42:38 +01:00
|
|
|
removeSubflow: removeSubflow,
|
2015-07-30 12:03:37 +02:00
|
|
|
refresh: refresh,
|
|
|
|
removeInput: removeSubflowInput,
|
|
|
|
removeOutput: removeSubflowOutput
|
2015-03-12 01:08:47 +01:00
|
|
|
}
|
|
|
|
})();
|