/** * Copyright JS Foundation and other contributors, http://js.foundation * * 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.nodes = (function() { var node_defs = {}; var nodes = []; var configNodes = {}; var links = []; var defaultWorkspace; var workspaces = {}; var workspacesOrder =[]; var subflows = {}; var loadedFlowVersion = null; var initialLoad; var dirty = false; function setDirty(d) { dirty = d; RED.events.emit("nodes:change",{dirty:dirty}); } var registry = (function() { var moduleList = {}; var nodeList = []; var nodeSets = {}; var typeToId = {}; var nodeDefinitions = {}; var iconSets = {}; nodeDefinitions['tab'] = { defaults: { label: {value:""}, disabled: {value: false}, info: {value: ""} } }; var exports = { setModulePendingUpdated: function(module,version) { moduleList[module].pending_version = version; RED.events.emit("registry:module-updated",{module:module,version:version}); }, getModule: function(module) { return moduleList[module]; }, getNodeSetForType: function(nodeType) { return exports.getNodeSet(typeToId[nodeType]); }, getModuleList: function() { return moduleList; }, getNodeList: function() { return nodeList; }, getNodeTypes: function() { return Object.keys(nodeDefinitions); }, setNodeList: function(list) { nodeList = []; for(var i=0;i n.outputs)) { n.outputs = n.wires.length; } if (n.outputs) { for (var i=0;i 0) { node.credentials = credentialSet; } } } if (n._def.category != "config") { node.x = n.x; node.y = n.y; node.wires = []; for(var i=0;i 0 && n.inputLabels && !/^\s*$/.test(n.inputLabels.join(""))) { node.inputLabels = n.inputLabels.slice(); } if (n.outputs > 0 && n.outputLabels && !/^\s*$/.test(n.outputLabels.join(""))) { node.outputLabels = n.outputLabels.slice(); } if ((!n._def.defaults || !n._def.defaults.hasOwnProperty("icon")) && n.icon) { var defIcon = RED.utils.getDefaultNodeIcon(n._def, n); if (n.icon !== defIcon.module+"/"+defIcon.file) { node.icon = n.icon; } } } return node; } function convertSubflow(n) { var node = {}; node.id = n.id; node.type = n.type; node.name = n.name; node.info = n.info; node.in = []; node.out = []; n.in.forEach(function(p) { var nIn = {x:p.x,y:p.y,wires:[]}; var wires = links.filter(function(d) { return d.source === p }); for (var i=0;i 0 && n.inputLabels && !/^\s*$/.test(n.inputLabels.join(""))) { node.inputLabels = n.inputLabels.slice(); } if (node.out.length > 0 && n.outputLabels && !/^\s*$/.test(n.outputLabels.join(""))) { node.outputLabels = n.outputLabels.slice(); } if (n.icon) { if (n.icon !== "node-red/subflow.png") { node.icon = n.icon; } } return node; } /** * Converts the current node selection to an exportable JSON Object **/ function createExportableNodeSet(set, exportedSubflows, exportedConfigNodes) { var nns = []; exportedConfigNodes = exportedConfigNodes || {}; exportedSubflows = exportedSubflows || {}; for (var n=0;n 0) { var typeList = "
  • "+unknownTypes.join("
  • ")+"
"; var type = "type"+(unknownTypes.length > 1?"s":""); RED.notify("

"+RED._("clipboard.importUnrecognised",{count:unknownTypes.length})+"

"+typeList,"error",false,10000); } var activeWorkspace = RED.workspaces.active(); //TODO: check the z of the subflow instance and check _that_ if it exists var activeSubflow = getSubflow(activeWorkspace); for (i=0;i node.outputs) { console.log("Warning: node.wires longer than node.outputs - trimming wires:",node.id," wires:",node.wires.length," outputs:",node.outputs); node.wires = node.wires.slice(0,node.outputs); } for (d in node._def.defaults) { if (node._def.defaults.hasOwnProperty(d)) { node[d] = n[d]; node._config[d] = JSON.stringify(n[d]); } } node._config.x = node.x; node._config.y = node.y; if (node._def.hasOwnProperty('credentials') && n.hasOwnProperty('credentials')) { node.credentials = {}; for (d in node._def.credentials) { if (node._def.credentials.hasOwnProperty(d) && n.credentials.hasOwnProperty(d)) { node.credentials[d] = n.credentials[d]; } } } } } addNode(node); RED.editor.validateNode(node); node_map[n.id] = node; // If an 'unknown' config node, it will not have been caught by the // proper config node handling, so needs adding to new_nodes here if (node.type === "unknown" || node._def.category !== "config") { new_nodes.push(node); } } } } // TODO: make this a part of the node definition so it doesn't have to // be hardcoded here var nodeTypeArrayReferences = { "catch":"scope", "status":"scope", "link in":"links", "link out":"links" } // Remap all wires and config node references for (i=0;i",node_map[wires[w2]].id); } } } } delete n.wires; } for (var d3 in n._def.defaults) { if (n._def.defaults.hasOwnProperty(d3)) { if (n._def.defaults[d3].type && node_map[n[d3]]) { n[d3] = node_map[n[d3]].id; configNode = RED.nodes.node(n[d3]); if (configNode && configNode.users.indexOf(n) === -1) { configNode.users.push(n); } } else if (nodeTypeArrayReferences.hasOwnProperty(n.type) && nodeTypeArrayReferences[n.type] === d3 && n[d3] !== undefined && n[d3] !== null) { for (var j = 0;j 0) { var reimportList = []; replaceNodes.forEach(function(n) { if (configNodes.hasOwnProperty(n.id)) { delete configNodes[n.id]; } else { nodes.splice(nodes.indexOf(n),1); } reimportList.push(convertNode(n)); }); RED.view.redraw(true); var result = importNodes(reimportList,false); var newNodeMap = {}; result[0].forEach(function(n) { newNodeMap[n.id] = n; }); RED.nodes.eachLink(function(l) { if (newNodeMap.hasOwnProperty(l.source.id)) { l.source = newNodeMap[l.source.id]; } if (newNodeMap.hasOwnProperty(l.target.id)) { l.target = newNodeMap[l.target.id]; } }); RED.view.redraw(true); } }); }, registry:registry, setNodeList: registry.setNodeList, getNodeSet: registry.getNodeSet, addNodeSet: registry.addNodeSet, removeNodeSet: registry.removeNodeSet, enableNodeSet: registry.enableNodeSet, disableNodeSet: registry.disableNodeSet, setIconSets: registry.setIconSets, getIconSets: registry.getIconSets, registerType: registry.registerNodeType, getType: registry.getNodeType, convertNode: convertNode, add: addNode, remove: removeNode, clear: clear, addLink: addLink, removeLink: removeLink, addWorkspace: addWorkspace, removeWorkspace: removeWorkspace, getWorkspaceOrder: function() { return workspacesOrder }, setWorkspaceOrder: function(order) { workspacesOrder = order; }, workspace: getWorkspace, addSubflow: addSubflow, removeSubflow: removeSubflow, subflow: getSubflow, subflowContains: subflowContains, eachNode: function(cb) { for (var n=0;n