mirror of
https://github.com/node-red/node-red.git
synced 2025-03-01 10:36:34 +00:00
add global environment variable feature
This commit is contained in:
@@ -1204,5 +1204,10 @@
|
||||
"node": "Node",
|
||||
"junction": "Junction",
|
||||
"linkNodes": "Link Nodes"
|
||||
},
|
||||
"env-var": {
|
||||
"environment": "Environment",
|
||||
"header": "Global Environment Variables",
|
||||
"revert": "Revert"
|
||||
}
|
||||
}
|
||||
|
@@ -1349,5 +1349,10 @@
|
||||
"show-version-control-tab": "バージョンコントロールタブを表示",
|
||||
"start-flows": "フローを開始",
|
||||
"stop-flows": "フローを停止"
|
||||
},
|
||||
"env-var": {
|
||||
"environment": "環境変数",
|
||||
"header": "大域環境変数",
|
||||
"revert": "破棄"
|
||||
}
|
||||
}
|
||||
|
@@ -775,6 +775,7 @@ var RED = (function() {
|
||||
RED.deploy.init(RED.settings.theme("deployButton",null));
|
||||
|
||||
RED.keyboard.init(buildMainMenu);
|
||||
RED.envVar.init();
|
||||
|
||||
RED.nodes.init();
|
||||
RED.runtime.init()
|
||||
|
@@ -421,7 +421,7 @@ RED.deploy = (function() {
|
||||
|
||||
const unusedConfigNodes = [];
|
||||
RED.nodes.eachConfig(function (node) {
|
||||
if ((node._def.hasUsers !== false) && (node.users.length === 0)) {
|
||||
if ((node._def.hasUsers !== false) && (node.users.length === 0) && (node.type !== "global-config")) {
|
||||
unusedConfigNodes.push(getNodeInfo(node));
|
||||
hasUnusedConfig = true;
|
||||
}
|
||||
|
190
packages/node_modules/@node-red/editor-client/src/js/ui/env-var.js
vendored
Normal file
190
packages/node_modules/@node-red/editor-client/src/js/ui/env-var.js
vendored
Normal file
@@ -0,0 +1,190 @@
|
||||
/**
|
||||
* 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.envVar = (function() {
|
||||
function saveEnvList(list) {
|
||||
const items = list.editableList("items")
|
||||
const new_env = [];
|
||||
items.each(function (i,el) {
|
||||
var data = el.data('data');
|
||||
var item;
|
||||
if (data.nameField && data.valueField) {
|
||||
item = {
|
||||
name: data.nameField.val(),
|
||||
value: data.valueField.typedInput("value"),
|
||||
type: data.valueField.typedInput("type")
|
||||
};
|
||||
new_env.push(item);
|
||||
}
|
||||
});
|
||||
return new_env;
|
||||
}
|
||||
|
||||
function getGlobalConf(create) {
|
||||
var gconf = null;
|
||||
RED.nodes.eachConfig(function (conf) {
|
||||
if (conf.type === "global-config") {
|
||||
gconf = conf;
|
||||
}
|
||||
});
|
||||
if ((gconf === null) && create) {
|
||||
var cred = {
|
||||
_ : {},
|
||||
map: {}
|
||||
};
|
||||
gconf = {
|
||||
id: RED.nodes.id(),
|
||||
type: "global-config",
|
||||
env: [],
|
||||
name: "global-config",
|
||||
label: "",
|
||||
hasUsers: false,
|
||||
users: [],
|
||||
credentials: cred,
|
||||
_def: RED.nodes.getType("global-config"),
|
||||
};
|
||||
RED.nodes.add(gconf);
|
||||
}
|
||||
return gconf;
|
||||
}
|
||||
|
||||
function applyChanges(list) {
|
||||
var gconf = getGlobalConf(false);
|
||||
var new_env = [];
|
||||
var items = list.editableList('items');
|
||||
var credentials = gconf ? gconf.credentials : null;
|
||||
|
||||
if (!credentials) {
|
||||
credentials = {
|
||||
_ : {},
|
||||
map: {}
|
||||
};
|
||||
}
|
||||
items.each(function (i,el) {
|
||||
var data = el.data('data');
|
||||
if (data.nameField && data.valueField) {
|
||||
var item = {
|
||||
name: data.nameField.val(),
|
||||
value: data.valueField.typedInput("value"),
|
||||
type: data.valueField.typedInput("type")
|
||||
};
|
||||
if (item.name.trim() !== "") {
|
||||
new_env.push(item);
|
||||
if ((item.type === "cred") && (item.value !== "__PWRD__")) {
|
||||
credentials.map[item.name] = item.value;
|
||||
credentials.map["has_"+item.name] = (item.value !== "");
|
||||
item.value = "__PWRD__";
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
if (gconf === null) {
|
||||
gconf = getGlobalConf(true);
|
||||
}
|
||||
if ((JSON.stringify(new_env) !== JSON.stringify(gconf.env)) ||
|
||||
(JSON.stringify(credentials) !== JSON.stringify(gconf.credentials))) {
|
||||
gconf.env = new_env;
|
||||
gconf.credentials = credentials;
|
||||
RED.nodes.dirty(true);
|
||||
}
|
||||
}
|
||||
|
||||
function getSettingsPane() {
|
||||
var gconf = getGlobalConf(false);
|
||||
var env = gconf ? gconf.env : [];
|
||||
var cred = gconf ? gconf.credentials : null;
|
||||
if (!cred) {
|
||||
cred = {
|
||||
_ : {},
|
||||
map: {}
|
||||
};
|
||||
}
|
||||
|
||||
var pane = $("<div/>", {
|
||||
id: "red-ui-settings-tab-envvar"
|
||||
});
|
||||
var content = $("<div/>", {
|
||||
class: "form-row node-input-env-container-row"
|
||||
}).css({
|
||||
"margin": "10px"
|
||||
}).appendTo(pane);
|
||||
|
||||
var label = $("<label></label>").css({
|
||||
width: "100%"
|
||||
}).appendTo(content);
|
||||
$("<i/>", {
|
||||
class: "fa fa-list"
|
||||
}).appendTo(label);
|
||||
$("<span/>").text(" "+RED._("env-var.header")).appendTo(label);
|
||||
|
||||
var list = $("<ol/>", {
|
||||
id: "node-input-env-container"
|
||||
}).appendTo(content);
|
||||
var node = {
|
||||
type: "",
|
||||
env: env,
|
||||
credentials: cred.map,
|
||||
};
|
||||
RED.editor.envVarList.create(list, node);
|
||||
|
||||
var buttons = $("<div/>").css({
|
||||
"text-align": "right",
|
||||
}).appendTo(content);
|
||||
var revertButton = $("<button/>", {
|
||||
class: "red-ui-button"
|
||||
}).css({
|
||||
}).text(RED._("env-var.revert")).appendTo(buttons);
|
||||
|
||||
var items = saveEnvList(list);
|
||||
revertButton.on("click", function (ev) {
|
||||
list.editableList("empty");
|
||||
list.editableList("addItems", items);
|
||||
});
|
||||
|
||||
return pane;
|
||||
}
|
||||
|
||||
function init(done) {
|
||||
if (!RED.user.hasPermission("settings.write")) {
|
||||
RED.notify(RED._("user.errors.settings"),"error");
|
||||
return;
|
||||
}
|
||||
RED.userSettings.add({
|
||||
id:'envvar',
|
||||
title: RED._("env-var.environment"),
|
||||
get: getSettingsPane,
|
||||
focus: function() {
|
||||
var height = $("#red-ui-settings-tab-envvar").parent().height();
|
||||
$("#node-input-env-container").editableList("height", (height -100));
|
||||
},
|
||||
close: function() {
|
||||
var list = $("#node-input-env-container");
|
||||
try {
|
||||
applyChanges(list);
|
||||
}
|
||||
catch (e) {
|
||||
console.log(e);
|
||||
console.log(e.stack);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
init: init,
|
||||
};
|
||||
|
||||
})();
|
@@ -5603,7 +5603,24 @@ RED.view = (function() {
|
||||
if (activeSubflow) {
|
||||
activeSubflowChanged = activeSubflow.changed;
|
||||
}
|
||||
var result = RED.nodes.import(nodesToImport,{generateIds:options.generateIds, addFlow: addNewFlow, importMap: options.importMap});
|
||||
var filteredNodesToImport = nodesToImport;
|
||||
var globalConfig = null;
|
||||
var gconf = null;
|
||||
|
||||
RED.nodes.eachConfig(function (conf) {
|
||||
if (conf.type === "global-config") {
|
||||
gconf = conf;
|
||||
}
|
||||
});
|
||||
if (gconf) {
|
||||
filteredNodesToImport = nodesToImport.filter(function (n) {
|
||||
return (n.type !== "global-config");
|
||||
});
|
||||
globalConfig = nodesToImport.find(function (n) {
|
||||
return (n.type === "global-config");
|
||||
});
|
||||
}
|
||||
var result = RED.nodes.import(filteredNodesToImport,{generateIds:options.generateIds, addFlow: addNewFlow, importMap: options.importMap});
|
||||
if (result) {
|
||||
var new_nodes = result.nodes;
|
||||
var new_links = result.links;
|
||||
@@ -5735,6 +5752,50 @@ RED.view = (function() {
|
||||
}
|
||||
}
|
||||
|
||||
if (globalConfig) {
|
||||
// merge global env to existing global-config
|
||||
var env0 = gconf.env;
|
||||
var env1 = globalConfig.env;
|
||||
var newEnv = Array.from(env0);
|
||||
var changed = false;
|
||||
|
||||
env1.forEach(function (item1) {
|
||||
var index = newEnv.findIndex(function (item0) {
|
||||
return (item0.name === item1.name);
|
||||
});
|
||||
if (index >= 0) {
|
||||
var item0 = newEnv[index];
|
||||
if ((item0.type !== item1.type) ||
|
||||
(item0.value !== item1.value)) {
|
||||
newEnv[index] = item1;
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
else {
|
||||
newEnv.push(item1);
|
||||
changed = true;
|
||||
}
|
||||
});
|
||||
if(changed) {
|
||||
gconf.env = newEnv;
|
||||
var replaceEvent = {
|
||||
t: "edit",
|
||||
node: gconf,
|
||||
changed: true,
|
||||
changes: {
|
||||
env: env0
|
||||
}
|
||||
};
|
||||
historyEvent = {
|
||||
t:"multi",
|
||||
events: [
|
||||
replaceEvent,
|
||||
historyEvent,
|
||||
]
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
RED.history.push(historyEvent);
|
||||
|
||||
updateActiveNodes();
|
||||
|
Reference in New Issue
Block a user