Skip to content

Commit c75bda3

Browse files
committed
Implemented fields validation for Arduino nodes
1 parent 594fd64 commit c75bda3

File tree

1 file changed

+58
-29
lines changed

1 file changed

+58
-29
lines changed

arduino-cloud.html

+58-29
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
<script type="text/javascript">
2+
function validator(v) {
3+
debugger;
4+
return (v !== null && v !== "" && v !== undefined);
5+
}
26
function setupNode(nodeName, ins, outs) {
37
RED.nodes.registerType(nodeName,{
48
category: 'Arduino IoT Cloud',
59
color: '#00979d',
610
defaults: {
7-
connection: {type: "arduino-connection", required: true},
8-
thing: {value:"",required:true},
9-
property: {value:"",required:true},
10-
name: {value:"",required:true},
11+
connection: {type: "arduino-connection", validate: validator},
12+
thing: {value:"", validate: validator},
13+
property: {value:"", validate: validator},
14+
name: {value:"", validate: validator},
1115
propname: {value:""},
1216
prevconnection: {value:""}
1317
},
@@ -39,21 +43,26 @@
3943
if (connection !== "_ADD_") {
4044
initThings(this.connection, this.thing, this.property);
4145
}
46+
$("#node-input-thing").trigger("change");
4247
});
4348
$("#node-input-thing").change( () => {
4449

4550
const thing_id = $( "#node-input-thing" ).val();
46-
if (thing_id && thing_id !== "0") {
51+
if (thing_id) {
4752
initProperties(this.connection, thing_id, undefined, outs);
48-
} else
53+
} else {
4954
$("select#node-input-property").empty();
55+
}
56+
$("#node-input-property").trigger("change");
5057
});
5158
$("#node-input-property").change( () => {
5259
const property_name = $("#node-input-property").find('option:selected').text();
53-
if (property_name && property_name !== "") {
60+
const property_value = $("#node-input-property").find('option:selected').val();
61+
if (property_name !=="" && property_value !== "" && property_value !== undefined) {
5462
this.propname = property_name;
5563
$( "#node-input-name" ).val(property_name);
5664
}
65+
$("#node-input-name").trigger("change");
5766
});
5867
},
5968
oneditsave: function () {
@@ -117,28 +126,33 @@
117126
<script type="text/javascript">
118127
function initThings(connection, thing_id) {
119128
$.getJSON(`things?connectionid=${connection}` , things => {
120-
$("<option value='" + 0 + "'> " + "Select a thing" + "</option>").appendTo("#node-input-thing");
129+
$("<option value='" + "" + "'> " + "Select a thing" + "</option>").appendTo("#node-input-thing");
121130
for (const t of things) {
122131
$("<option value='" + t.id + "'>" + t.name + "</option>").appendTo("#node-input-thing");
123132
}
124-
if (thing_id) {
133+
if (thing_id !== undefined) {
125134
$("#node-input-thing").val(thing_id);
135+
$("#node-input-property").val("");
126136
}
137+
$("#node-input-property").trigger("change");
127138
});
128139
}
129140
function initProperties(connection, thing_id, property_id, outs) {
130141
if (thing_id === "" || thing_id === "0")
131142
return;
132143
$("#node-input-property").html("");
144+
133145
$.getJSON(`properties?connectionid=${connection}&thing_id=${thing_id}` , properties => {
134-
$("<option value='" + 0 + "'> " + "Select a property" + "</option>").appendTo("#node-input-property");
146+
$("<option value='" + "" + "'> " + "Select a property" + "</option>").appendTo("#node-input-property");
147+
135148
for (const p of properties) {
136149
if (outs > 0 || p.permission === "READ_WRITE")
137150
$("<option value='" + p.id + "'>" + p.name + "</option>").appendTo("#node-input-property");
138151
}
139-
if (property_id) {
152+
if (property_id !== undefined) {
140153
$("#node-input-property").val(property_id);
141154
}
155+
$("#node-input-name").trigger("change");
142156
});
143157
}
144158
</script>
@@ -188,11 +202,11 @@
188202
category: 'Arduino IoT Cloud',
189203
color: '#00979d',
190204
defaults: {
191-
connection: {type: "arduino-connection", required: true},
192-
thing: {value:"",required:true},
193-
property: {value:"",required:true},
205+
connection: {type: "arduino-connection", validate: validator},
206+
thing: {value:"",validate: validator},
207+
property: {value:"",validate: validator},
194208
propid: {value:""},
195-
name: {value:"",required:true},
209+
name: {value:"",validate: validator},
196210
timeWindowCount: {value: 1, validate:RED.validators.number(), required:true},
197211
timeWindowUnit: {value: '3600', required:true},
198212
prevconnection: {value:""},
@@ -227,20 +241,25 @@
227241
if (connection !== "_ADD_") {
228242
initThings(this.connection, this.thing, this.property);
229243
}
244+
$("#node-input-thing").trigger("change");
230245
});
231246
$("#node-input-thing").change( () => {
232247
const thing_id = $( "#node-input-thing" ).val();
233248
if (thing_id && thing_id !== "0") {
234249
initProperties(this.connection, thing_id);
235-
} else
250+
} else {
236251
$("select#node-input-property").empty();
252+
}
253+
$("#node-input-property").trigger("change");
237254
});
238255
$("#node-input-property").change( () => {
239256
const property_name = $("#node-input-property").find('option:selected').text();
240-
if (property_name && property_name !== "") {
257+
const property_value = $("#node-input-property").find('option:selected').val();
258+
if (property_name !=="" && property_value !== "" && property_value !== undefined) {
241259
this.propname = property_name;
242260
$( "#node-input-name" ).val(property_name);
243261
}
262+
$("#node-input-name").trigger("change");
244263
});
245264
},
246265
oneditsave: function () {
@@ -293,11 +312,11 @@
293312
category: 'Arduino IoT Cloud',
294313
color: '#00979d',
295314
defaults: {
296-
connection: {type: "arduino-connection", required: true},
297-
thing: {value:"",required:true},
298-
property: {value:"",required:true},
315+
connection: {type: "arduino-connection", validate: validator},
316+
thing: {value:"", validate: validator},
317+
property: {value:"", validate: validator},
299318
propid: {value:""},
300-
name: {value:"",required:true},
319+
name: {value:"", validate: validator},
301320
timeWindowCount: {value: 1, validate:RED.validators.number(), required:true},
302321
timeWindowUnit: {value: '3600', required:true},
303322
prevconnection: {value:""},
@@ -332,20 +351,25 @@
332351
if (connection !== "_ADD_") {
333352
initThings(this.connection, this.thing, this.property);
334353
}
354+
$("#node-input-thing").trigger("change");
335355
});
336356
$("#node-input-thing").change( () => {
337357
const thing_id = $( "#node-input-thing" ).val();
338358
if (thing_id && thing_id !== "0") {
339359
initProperties(this.connection, thing_id);
340-
} else
360+
} else {
341361
$("select#node-input-property").empty();
362+
}
363+
$("#node-input-property").trigger("change");
342364
});
343365
$("#node-input-property").change( () => {
344366
const property_name = $("#node-input-property").find('option:selected').text();
345-
if (property_name && property_name !== "") {
367+
const property_value = $("#node-input-property").find('option:selected').val();
368+
if (property_name !=="" && property_value !== "" && property_value !== undefined) {
346369
this.propname = property_name;
347370
$( "#node-input-name" ).val(property_name);
348371
}
372+
$("#node-input-name").trigger("change");
349373
});
350374
},
351375
oneditsave: function () {
@@ -397,11 +421,11 @@
397421
category: 'Arduino IoT Cloud',
398422
color: '#00979d',
399423
defaults: {
400-
connection: {type: "arduino-connection", required: true},
401-
thing: {value:"",required:true},
402-
property: {value:"",required:true},
424+
connection: {type: "arduino-connection", validate: validator},
425+
thing: {value:"",validate: validator},
426+
property: {value:"",validate: validator},
403427
propid: {value:""},
404-
name: {value:"",required:true},
428+
name: {value:"",validate: validator},
405429
prevconnection: {value:""},
406430
propname: {value:""}
407431
},
@@ -434,20 +458,25 @@
434458
if (connection !== "_ADD_") {
435459
initThings(this.connection, this.thing, this.property);
436460
}
461+
$("#node-input-thing").trigger("change");
437462
});
438463
$("#node-input-thing").change( () => {
439464
const thing_id = $( "#node-input-thing" ).val();
440465
if (thing_id && thing_id !== "0") {
441466
initProperties(this.connection, thing_id);
442-
} else
467+
} else {
443468
$("select#node-input-property").empty();
469+
}
470+
$("#node-input-property").trigger("change");
444471
});
445472
$("#node-input-property").change( () => {
446473
const property_name = $("#node-input-property").find('option:selected').text();
447-
if (property_name && property_name !== "") {
474+
const property_value = $("#node-input-property").find('option:selected').val();
475+
if (property_name !=="" && property_value !== "" && property_value !== undefined) {
448476
this.propname = property_name;
449477
$( "#node-input-name" ).val(property_name);
450478
}
479+
$("#node-input-name").trigger("change");
451480
});
452481
},
453482
oneditsave: function () {

0 commit comments

Comments
 (0)