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

Validate config node properties

Fixes #74
This commit is contained in:
Nicholas O'Leary 2013-11-17 17:49:32 +00:00
parent 3797ace89b
commit cbe57aa96c

View File

@ -99,7 +99,11 @@ RED.editor = function() {
valid = node._def.defaults[property].validate.call(node,value); valid = node._def.defaults[property].validate.call(node,value);
} }
if (valid && node._def.defaults[property].type && RED.nodes.getType(node._def.defaults[property].type)) { if (valid && node._def.defaults[property].type && RED.nodes.getType(node._def.defaults[property].type)) {
valid = (value != "_ADD_"); if (!value || value == "_ADD_") {
valid = false;
} else {
valid = RED.nodes.node(value).valid;
}
} }
return valid; return valid;
} }
@ -270,9 +274,11 @@ RED.editor = function() {
function showEditConfigNodeDialog(name,type,id) { function showEditConfigNodeDialog(name,type,id) {
var adding = (id == "_ADD_");
$("#dialog-config-form").html($("script[data-template-name='"+type+"']").html()); $("#dialog-config-form").html($("script[data-template-name='"+type+"']").html());
var node_def = RED.nodes.getType(type); var node_def = RED.nodes.getType(type);
var configNode = RED.nodes.node(id); var configNode = RED.nodes.node(id);
for (var d in node_def.defaults) { for (var d in node_def.defaults) {
var input = $("#node-config-input-"+d); var input = $("#node-config-input-"+d);
if (id == "_ADD_") { if (id == "_ADD_") {
@ -280,6 +286,21 @@ RED.editor = function() {
} else { } else {
input.val(configNode[d]); input.val(configNode[d]);
} }
$("#node-config-input-"+d).change(function() {
var n = configNode;
if (adding) {
n = {_def:node_def};
}
var property = d;
return function() {
if (!validateNodeProperty(n, property,this.value)) {
$(this).addClass("input-error");
} else {
$(this).removeClass("input-error");
}
};
}());
$("#node-config-input-"+d).change();
} }
var buttons = $( "#node-config-dialog" ).dialog("option","buttons"); var buttons = $( "#node-config-dialog" ).dialog("option","buttons");
if (id == "_ADD_") { if (id == "_ADD_") {
@ -325,7 +346,6 @@ RED.editor = function() {
} }
$( "#node-config-dialog" ).dialog("option","buttons",buttons); $( "#node-config-dialog" ).dialog("option","buttons",buttons);
var adding = (id == "_ADD_");
if (adding) { if (adding) {
id = (1+Math.random()*4294967295).toString(16); id = (1+Math.random()*4294967295).toString(16);
} }
@ -365,7 +385,7 @@ RED.editor = function() {
}); });
select.append('<option value="_ADD_"'+(value==""?" selected":"")+'>Add new '+type+'...</option>'); select.append('<option value="_ADD_"'+(value==""?" selected":"")+'>Add new '+type+'...</option>');
select.change(); window.setTimeout(function() { select.change();},50);
} }
$( "#node-config-dialog" ).dialog({ $( "#node-config-dialog" ).dialog({
@ -381,22 +401,23 @@ RED.editor = function() {
var configType = $(this).dialog('option','node-type'); var configType = $(this).dialog('option','node-type');
var configAdding = $(this).dialog('option','node-adding'); var configAdding = $(this).dialog('option','node-adding');
var configTypeDef = RED.nodes.getType(configType); var configTypeDef = RED.nodes.getType(configType);
var configNode;
if (configAdding) { if (configAdding) {
var nn = {type:configType,id:configId,users:[]}; configNode = {type:configType,id:configId,users:[]};
for (var d in configTypeDef.defaults) { for (var d in configTypeDef.defaults) {
var input = $("#node-config-input-"+d); var input = $("#node-config-input-"+d);
nn[d] = input.val(); configNode[d] = input.val();
} }
nn.label = configTypeDef.label; configNode.label = configTypeDef.label;
nn._def = configTypeDef; configNode._def = configTypeDef;
//console.log(nn.id,nn.label()); //console.log(nn.id,nn.label());
RED.nodes.add(nn); RED.nodes.add(configNode);
updateConfigNodeSelect(configProperty,configType,nn.id); updateConfigNodeSelect(configProperty,configType,configNode.id);
} else { } else {
configNode = RED.nodes.node(configId);
for (var d in configTypeDef.defaults) { for (var d in configTypeDef.defaults) {
var input = $("#node-config-input-"+d); var input = $("#node-config-input-"+d);
var configNode = RED.nodes.node(configId);
configNode[d] = input.val(); configNode[d] = input.val();
} }
updateConfigNodeSelect(configProperty,configType,configId); updateConfigNodeSelect(configProperty,configType,configId);
@ -405,6 +426,7 @@ RED.editor = function() {
if (configTypeDef.oneditsave) { if (configTypeDef.oneditsave) {
configTypeDef.oneditsave.call(RED.nodes.node(configId)); configTypeDef.oneditsave.call(RED.nodes.node(configId));
} }
validateNode(configNode);
RED.view.dirty(true); RED.view.dirty(true);
$( this ).dialog( "close" ); $( this ).dialog( "close" );