1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

fixed comments from @knolleary

This commit is contained in:
Hiroyasu Nishiyama 2019-01-28 22:14:08 +09:00
parent 2b43e3ee23
commit f88a4b1791
8 changed files with 76 additions and 97 deletions

View File

@ -902,7 +902,7 @@
"properties": "Properties", "properties": "Properties",
"description": "Description", "description": "Description",
"appearance": "Appearance", "appearance": "Appearance",
"env": "Env Var", "env": "Environment Variables",
"name": "name" "name": "name"
} }
} }

View File

@ -611,40 +611,7 @@ RED.editor = (function() {
} }
function isSameEnv(env0, env1) { function isSameEnv(env0, env1) {
function isSameInfo(info0, info1) { return (JSON.stringify(env0) === JSON.stringify(env1));
if (info0 && info1 &&
(info0.name === info1.name) &&
(info0.label === info1.label) &&
(info0.value === info1.value) &&
(info0.type === info1.type) &&
(info0.target_type === info1.target_type) &&
(info0.target === info1.target)) {
return true;
}
return false;
}
function inEnv(env, item) {
for(var i = 0; i < env.length; i++) {
var item1 = env[i];
if ((item1.name !== item.name) ||
(item1.type !== item.type) ||
(item1.value !== item.value) ||
!isSameInfo(item1.info, item.info)) {
return false;
}
}
return true;
}
if (env0 && env1 && (env0.length === env1.length)) {
for (var i = 0; i < env0.length; i++) {
var item0 = env0[i];
if (!inEnv(env1, item0)) {
return false;
}
}
return true;
}
return false;
} }
function buildEditForm(container,formId,type,ns,node) { function buildEditForm(container,formId,type,ns,node) {

View File

@ -19,13 +19,13 @@ RED.subflow = (function() {
var _subflowEditTemplate = '<script type="text/x-red" data-template-name="subflow">'+ 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>'+ '<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>'+
'<div class="form-row"><label data-i18n="[append]editor:editor-tab.env"><i class="fa fa-th-list"></i> </label></div>'+ '<div class="form-row" style="margin-bottom: 0px;"><label style="width: auto;" data-i18n="[append]editor:editor-tab.env"><i class="fa fa-th-list"></i> </label></div>'+
'<div class="form-row node-input-env-container-row"><ol id="node-input-env-container"></ol></div>'+ '<div class="form-row node-input-env-container-row"><ol id="node-input-env-container"></ol></div>'+
'</script>'; '</script>';
var _subflowTemplateEditTemplate = '<script type="text/x-red" data-template-name="subflow-template">'+ 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"><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"><i class="fa fa-folder-o"></i> <label for="subflow-input-category" data-i18n="editor:subflow.category"></label><select style="width: 250px;" id="subflow-input-category"></select><input style="display:none; margin-left: 10px; width:calc(100% - 250px)" type="text" id="subflow-input-custom-category"></div>'+ '<div class="form-row"><i class="fa fa-folder-o"></i> <label for="subflow-input-category" data-i18n="editor:subflow.category"></label><select style="width: 250px;" id="subflow-input-category"></select><input style="display:none; margin-left: 10px; width:calc(100% - 250px)" type="text" id="subflow-input-custom-category"></div>'+
'<div class="form-row"><label data-i18n="[append]editor:editor-tab.env"><i class="fa fa-th-list"></i> </label></div>'+ '<div class="form-row" style="margin-bottom: 0px;"><label style="width: auto;" data-i18n="[append]editor:editor-tab.env"><i class="fa fa-th-list"></i> </label></div>'+
'<div class="form-row node-input-env-container-row"><ol id="node-input-env-container"></ol></div>'+ '<div class="form-row node-input-env-container-row"><ol id="node-input-env-container"></ol></div>'+
'<div class="form-row form-tips" id="subflow-dialog-user-count"></div>'+ '<div class="form-row form-tips" id="subflow-dialog-user-count"></div>'+
'</script>'; '</script>';

View File

@ -158,7 +158,13 @@ module.exports = function(RED) {
}, },
env: { env: {
get: function(envVar) { get: function(envVar) {
return node.getenv(envVar); if (node && node._flow) {
var flow = node._flow;
if (flow) {
return flow.getSetting(envVar);
}
}
return process.env[envVar];
} }
}, },
setTimeout: function () { setTimeout: function () {

View File

@ -239,22 +239,6 @@ Node.prototype.receive = function(msg) {
} }
}; };
Node.prototype.getenv = function(name) {
var flow = this._flow;
if (flow) {
var val = flow.getSetting(name);
return val;
}
return undefined;
};
Node.prototype.setenv = function(name, val) {
var flow = this._flow;
if (flow) {
flow.setSetting(name, val);
}
};
function log_helper(self, level, msg) { function log_helper(self, level, msg) {
var o = { var o = {
level: level, level: level,

View File

@ -462,9 +462,6 @@ class Flow {
console.log("==================") console.log("==================")
} }
getenv(name) {
return process.env[name];
}
} }
/** /**

View File

@ -23,34 +23,6 @@ const flowUtil = require("./util");
var Log; var Log;
/**
* Convert env var definition of subflow template and instance to env var dic
* @param {Hash} env1 env var definition of template
* @param {Hash} env2 env var definition of instance
* @return {Hash} env var dic
*/
function env2Dic(env0, env1) {
var dic = {};
function add(env) {
for(var i = 0; i < env.length; i++) {
var item = env[i];
var name = item.name;
var info = Object.assign({}, item.info);
info.name = name;
dic[name] = item;
dic[name+"_type"] = { value: item.type };
dic[name+"_info"] = { value: info };
}
}
if (env0) {
add(env0);
}
if (env1) {
add(env1);
}
return dic;
}
/** /**
* This class represents a subflow - which is handled as a special type of Flow * This class represents a subflow - which is handled as a special type of Flow
*/ */
@ -133,14 +105,12 @@ class Subflow extends Flow {
var self = this; var self = this;
// Create a subflow node to accept inbound messages and route appropriately // Create a subflow node to accept inbound messages and route appropriately
var Node = require("../Node"); var Node = require("../Node");
var env = env2Dic(this.subflowDef.env, this.subflowInstance.env);
var subflowInstanceConfig = { var subflowInstanceConfig = {
id: this.subflowInstance.id, id: this.subflowInstance.id,
type: this.subflowInstance.type, type: this.subflowInstance.type,
z: this.subflowInstance.z, z: this.subflowInstance.z,
name: this.subflowInstance.name, name: this.subflowInstance.name,
wires: [], wires: [],
env: env,
_flow: this _flow: this
} }
if (this.subflowDef.in) { if (this.subflowDef.in) {
@ -229,11 +199,63 @@ class Subflow extends Flow {
* @return {Object} val value of env var * @return {Object} val value of env var
*/ */
getSetting(name) { getSetting(name) {
function lookup(env, name) {
for(var i = 0; i < env.length; i++) {
var item = env[i];
if (item.name === name) {
return item;
}
}
return null;
}
function lookupFlow0(env, name) {
if (flow.subflowInstance) {
var vi = lookup(flow.subflowInstance.env, name);
if (vi) {
return vi;
}
}
if (flow.subflowDef) {
var vt = lookup(flow.subflowDef.env, name);
if (vt) {
return vt;
}
}
return null;
}
function lookupFlow(env, name) {
if (name.endsWith("_type")) {
var vname = name.substring(0, 5); // len("_type")==5
var val = lookupFlow0(env, vname);
if (val) {
return val.type;
}
}
else if (name.endsWith("_info")) {
var vname = name.substring(0, 5); // len("_info")==5
var val = lookupFlow0(env, vname);
if (val) {
return val.info;
}
}
else {
var val = lookupFlow0(env, name);
if (val) {
return val.value;
}
}
return null;
}
var node = this.node; var node = this.node;
if (node) { if (node) {
var env = node.env; var flow = node._flow;
if (env && env.hasOwnProperty(name)) { if (flow) {
return env[name].value; var v = lookupFlow(flow, name);
if (v) {
return v;
}
} }
} }
var parent = this.parent; var parent = this.parent;

View File

@ -419,9 +419,12 @@ function setObjectProperty(msg,prop,value,createMissing) {
* @param {String} name - name of variable * @param {String} name - name of variable
* @return {String} value of env var * @return {String} value of env var
*/ */
function getenv(node, name) { function getSetting(node, name) {
if (node && node.getenv) { if (node && node._flow) {
return node.getenv(name); var flow = node._flow;
if (flow) {
return flow.getSetting(name);
}
} }
return process.env[name]; return process.env[name];
} }
@ -442,16 +445,16 @@ function evaluateEnvProperty(value, node) {
if (/^\${[^}]+}$/.test(value)) { if (/^\${[^}]+}$/.test(value)) {
// ${ENV_VAR} // ${ENV_VAR}
var name = value.substring(2,value.length-1); var name = value.substring(2,value.length-1);
var val = getenv(node, name); var val = getSetting(node, name);
return val ? val : ""; return val ? val : "";
} else if (!/\${\S+}/.test(value)) { } else if (!/\${\S+}/.test(value)) {
// ENV_VAR // ENV_VAR
var val = getenv(node, value); var val = getSetting(node, value);
return val ? val : ""; return val ? val : "";
} else { } else {
// FOO${ENV_VAR}BAR // FOO${ENV_VAR}BAR
return value.replace(/\${([^}]+)}/g, function(match, name) { return value.replace(/\${([^}]+)}/g, function(match, name) {
var val = getenv(node, name); var val = getSetting(node, name);
return (val ? val : ""); return (val ? val : "");
}); });
} }
@ -558,7 +561,7 @@ function prepareJSONataExpression(value,node) {
return node.context().global.get(val); return node.context().global.get(val);
}); });
expr.assign('env', function(name) { expr.assign('env', function(name) {
var val = getenv(node, name); var val = getSetting(node, name);
return (val ? val : ""); return (val ? val : "");
}) })
expr.registerFunction('clone', cloneMessage, '<(oa)-:o>'); expr.registerFunction('clone', cloneMessage, '<(oa)-:o>');