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

Allow a node to specify a filter for the config nodes it can pick from

This commit is contained in:
Nick O'Leary 2021-09-30 19:44:34 +01:00
parent ec27e19e3f
commit 253c489a33
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9

View File

@ -242,7 +242,7 @@ RED.editor = (function() {
* @param property - the name of the field * @param property - the name of the field
* @param type - the type of the config-node * @param type - the type of the config-node
*/ */
function prepareConfigNodeSelect(node,property,type,prefix) { function prepareConfigNodeSelect(node,property,type,prefix,filter) {
var input = $("#"+prefix+"-"+property); var input = $("#"+prefix+"-"+property);
if (input.length === 0 ) { if (input.length === 0 ) {
return; return;
@ -265,12 +265,12 @@ RED.editor = (function() {
select.css({ select.css({
'flex-grow': 1 'flex-grow': 1
}); });
updateConfigNodeSelect(property,type,node[property],prefix); updateConfigNodeSelect(property,type,node[property],prefix,filter);
$('<a id="'+prefix+'-lookup-'+property+'" class="red-ui-button"><i class="fa fa-pencil"></i></a>') $('<a id="'+prefix+'-lookup-'+property+'" class="red-ui-button"><i class="fa fa-pencil"></i></a>')
.css({"margin-left":"10px"}) .css({"margin-left":"10px"})
.appendTo(outerWrap); .appendTo(outerWrap);
$('#'+prefix+'-lookup-'+property).on("click", function(e) { $('#'+prefix+'-lookup-'+property).on("click", function(e) {
showEditConfigNodeDialog(property,type,select.find(":selected").val(),prefix); showEditConfigNodeDialog(property,type,select.find(":selected").val(),prefix,node);
e.preventDefault(); e.preventDefault();
}); });
var label = ""; var label = "";
@ -304,7 +304,7 @@ RED.editor = (function() {
} }
button.on("click", function(e) { button.on("click", function(e) {
showEditConfigNodeDialog(property,type,input.val()||"_ADD_",prefix); showEditConfigNodeDialog(property,type,input.val()||"_ADD_",prefix,node);
e.preventDefault(); e.preventDefault();
}); });
} }
@ -476,7 +476,7 @@ RED.editor = (function() {
if (configTypeDef.exclusive) { if (configTypeDef.exclusive) {
prepareConfigNodeButton(node,d,definition.defaults[d].type,prefix); prepareConfigNodeButton(node,d,definition.defaults[d].type,prefix);
} else { } else {
prepareConfigNodeSelect(node,d,definition.defaults[d].type,prefix); prepareConfigNodeSelect(node,d,definition.defaults[d].type,prefix,definition.defaults[d].filter);
} }
} else { } else {
console.log("Unknown type:", definition.defaults[d].type); console.log("Unknown type:", definition.defaults[d].type);
@ -694,7 +694,7 @@ RED.editor = (function() {
return 0; return 0;
} }
function updateConfigNodeSelect(name,type,value,prefix) { function updateConfigNodeSelect(name,type,value,prefix,filter) {
// if prefix is null, there is no config select to update // if prefix is null, there is no config select to update
if (prefix) { if (prefix) {
var button = $("#"+prefix+"-edit-"+name); var button = $("#"+prefix+"-edit-"+name);
@ -717,13 +717,17 @@ RED.editor = (function() {
} }
var configNodes = []; var configNodes = [];
if (typeof filter !== 'function') {
filter = null;
}
RED.nodes.eachConfig(function(config) { RED.nodes.eachConfig(function(config) {
if (config.type == type && (!config.z || config.z === activeWorkspace.id)) { if (config.type == type && (!config.z || config.z === activeWorkspace.id)) {
if (!filter || filter.call(null,config)) {
var label = RED.utils.getNodeLabel(config,config.id); var label = RED.utils.getNodeLabel(config,config.id);
config.__label__ = label+(config.d?" ["+RED._("workspace.disabled")+"]":""); config.__label__ = label+(config.d?" ["+RED._("workspace.disabled")+"]":"");
configNodes.push(config); configNodes.push(config);
} }
}
}); });
var configSortFn = defaultConfigNodeSort; var configSortFn = defaultConfigNodeSort;
if (typeof node_def.sort == "function") { if (typeof node_def.sort == "function") {
@ -1038,8 +1042,9 @@ RED.editor = (function() {
* type - type of config node * type - type of config node
* id - id of config node to edit. _ADD_ for a new one * id - id of config node to edit. _ADD_ for a new one
* prefix - the input prefix of the parent property * prefix - the input prefix of the parent property
* editContext - the node that was being edited that triggered editing this node
*/ */
function showEditConfigNodeDialog(name,type,id,prefix) { function showEditConfigNodeDialog(name,type,id,prefix,editContext) {
if (buildingEditDialog) { return } if (buildingEditDialog) { return }
buildingEditDialog = true; buildingEditDialog = true;
var adding = (id == "_ADD_"); var adding = (id == "_ADD_");
@ -1342,7 +1347,13 @@ RED.editor = (function() {
RED.events.emit("nodes:change",editing_config_node); RED.events.emit("nodes:change",editing_config_node);
} }
RED.tray.close(function() { RED.tray.close(function() {
updateConfigNodeSelect(configProperty,configType,editing_config_node.id,prefix); var filter = null;
if (editContext && typeof editContext._def.defaults[configProperty].filter === 'function') {
filter = function(n) {
return editContext._def.defaults[configProperty].filter.call(editContext,n);
}
}
updateConfigNodeSelect(configProperty,configType,editing_config_node.id,prefix,filter);
}); });
} }
} }
@ -1399,7 +1410,13 @@ RED.editor = (function() {
RED.view.redraw(true); RED.view.redraw(true);
RED.history.push(historyEvent); RED.history.push(historyEvent);
RED.tray.close(function() { RED.tray.close(function() {
updateConfigNodeSelect(configProperty,configType,"",prefix); var filter = null;
if (editContext && typeof editContext._def.defaults[configProperty].filter === 'function') {
filter = function(n) {
return editContext._def.defaults[configProperty].filter.call(editContext,n);
}
}
updateConfigNodeSelect(configProperty,configType,"",prefix,filter);
}); });
} }
}); });