Commit 9b56a686 authored by Rafael Monnerat's avatar Rafael Monnerat

slapos_jio: Fixup required support on parameter editor

parent a9ed4da5
...@@ -40,11 +40,12 @@ ...@@ -40,11 +40,12 @@
); );
} }
function render_selection(json_field, default_value) { function render_selection(json_field, default_value, is_required) {
var option_list = [domsugar('option', { var property_dict = {size: 1},
value: "", option_list = [domsugar('option', {
selected: (default_value === undefined) value: "",
})], selected: (default_value === undefined)
})],
option_index, option_index,
selected, selected,
is_selected = (default_value === undefined), is_selected = (default_value === undefined),
...@@ -77,16 +78,18 @@ ...@@ -77,16 +78,18 @@
value: default_value, value: default_value,
text: default_value, text: default_value,
"data-format": data_format, "data-format": data_format,
selected: selected selected: true
})); }));
} }
return domsugar('select', {
size: 1, property_dict["data-format"] = data_format;
"data-format": data_format if (is_required) {
}, option_list); property_dict.required = true;
}
return domsugar('select', property_dict, option_list);
} }
function render_selection_oneof(json_field, default_value) { function render_selection_oneof(json_field, default_value, is_required) {
var option_list = [domsugar('option', { var option_list = [domsugar('option', {
value: "", value: "",
selected: (default_value === undefined) selected: (default_value === undefined)
...@@ -114,7 +117,7 @@ ...@@ -114,7 +117,7 @@
}, option_list); }, option_list);
} }
function render_textarea(json_field, default_value, data_format) { function render_textarea(json_field, default_value, data_format, is_required) {
var value = ''; var value = '';
if (default_value !== undefined) { if (default_value !== undefined) {
if (default_value instanceof Array) { if (default_value instanceof Array) {
...@@ -129,14 +132,14 @@ ...@@ -129,14 +132,14 @@
}); });
} }
function render_field(json_field, default_value) { function render_field(json_field, default_value, is_required) {
var data_format, domsugar_input_dict = {}; var data_format, domsugar_input_dict = {};
if (json_field['enum'] !== undefined) { if (json_field['enum'] !== undefined) {
return render_selection(json_field, default_value); return render_selection(json_field, default_value, is_required);
} }
if (json_field.oneOf !== undefined) { if (json_field.oneOf !== undefined) {
return render_selection_oneof(json_field, default_value); return render_selection_oneof(json_field, default_value, is_required);
} }
if (json_field.type === "boolean") { if (json_field.type === "boolean") {
...@@ -147,7 +150,7 @@ ...@@ -147,7 +150,7 @@
if (default_value === "false") { if (default_value === "false") {
default_value = false; default_value = false;
} }
return render_selection(json_field, default_value); return render_selection(json_field, default_value, is_required);
} }
if (json_field.type === "array") { if (json_field.type === "array") {
...@@ -157,11 +160,11 @@ ...@@ -157,11 +160,11 @@
data_format = "array-number"; data_format = "array-number";
} }
} }
return render_textarea(json_field, default_value, data_format); return render_textarea(json_field, default_value, data_format, is_required);
} }
if (json_field.type === "string" && json_field.textarea === true) { if (json_field.type === "string" && json_field.textarea === true) {
return render_textarea(json_field, default_value, "string"); return render_textarea(json_field, default_value, "string", is_required);
} }
if (default_value !== undefined) { if (default_value !== undefined) {
...@@ -179,6 +182,10 @@ ...@@ -179,6 +182,10 @@
domsugar_input_dict.type = "text"; domsugar_input_dict.type = "text";
} }
if (is_required) {
domsugar_input_dict.required = true;
}
return domsugar('input', domsugar_input_dict); return domsugar('input', domsugar_input_dict);
} }
...@@ -192,7 +199,8 @@ ...@@ -192,7 +199,8 @@
default_used_list = [], default_used_list = [],
default_div, default_div,
span_error, span_error,
span_info; span_info,
is_required;
if (default_dict === undefined) { if (default_dict === undefined) {
default_dict = {}; default_dict = {};
...@@ -271,6 +279,10 @@ ...@@ -271,6 +279,10 @@
div.appendChild(label); div.appendChild(label);
div_input = document.createElement("div"); div_input = document.createElement("div");
div_input.setAttribute("class", "input"); div_input.setAttribute("class", "input");
is_required = false;
if ((Array.isArray(json_field.required)) && (json_field.required.includes(key))) {
is_required = true;
}
if (json_field.properties[key].type === 'object') { if (json_field.properties[key].type === 'object') {
label.setAttribute("class", "slapos-parameter-dict-key"); label.setAttribute("class", "slapos-parameter-dict-key");
div_input = render_subform(json_field.properties[key], div_input = render_subform(json_field.properties[key],
...@@ -278,7 +290,8 @@ ...@@ -278,7 +290,8 @@
div_input, div_input,
path + "/" + key); path + "/" + key);
} else { } else {
input = render_field(json_field.properties[key], default_dict[key]); input = render_field(
json_field.properties[key], default_dict[key], is_required);
input.name = path + "/" + key; input.name = path + "/" + key;
input.setAttribute("class", "slapos-parameter"); input.setAttribute("class", "slapos-parameter");
input.setAttribute("placeholder", " "); input.setAttribute("placeholder", " ");
...@@ -317,7 +330,7 @@ ...@@ -317,7 +330,7 @@
div.appendChild(label); div.appendChild(label);
div_input = document.createElement("div"); div_input = document.createElement("div");
div_input.setAttribute("class", "input"); div_input.setAttribute("class", "input");
input = render_field({"type": "string", "textarea": true}, default_dict[key]); input = render_field({"type": "string", "textarea": true}, default_dict[key], false);
input.name = path + "/" + key; input.name = path + "/" + key;
input.setAttribute("class", "slapos-parameter"); input.setAttribute("class", "slapos-parameter");
input.setAttribute("placeholder", " "); input.setAttribute("placeholder", " ");
...@@ -592,14 +605,27 @@ ...@@ -592,14 +605,27 @@
for (error_index in validation.errors) { for (error_index in validation.errors) {
if (validation.errors.hasOwnProperty(error_index)) { if (validation.errors.hasOwnProperty(error_index)) {
field_name = validation.errors[error_index].dataPath; field_name = validation.errors[error_index].dataPath;
input_field = g.element.querySelector(".slapos-parameter[name='/" + field_name + "']"); if (field_name !== "") {
if (input_field === null) {
field_name = field_name.split("/").slice(0, -1).join("/");
input_field = g.element.querySelector(".slapos-parameter[name='/" + field_name + "']"); input_field = g.element.querySelector(".slapos-parameter[name='/" + field_name + "']");
if (input_field === null) {
field_name = field_name.split("/").slice(0, -1).join("/");
input_field = g.element.querySelector(".slapos-parameter[name='/" + field_name + "']");
}
div = input_field.parentNode;
div.setAttribute("class", "slapos-parameter error-input");
div.querySelector("span.error").textContent = validation.errors[error_index].message;
} else if (validation.errors[error_index].code == "302") {
// OBJECT_REQUIRED use case
field_name = "/" + validation.errors[error_index].params.key;
input_field = g.element.querySelector(".slapos-parameter[name='/" + field_name + "']");
if (input_field === null) {
field_name = field_name.split("/").slice(0, -1).join("/");
input_field = g.element.querySelector(".slapos-parameter[name='/" + field_name + "']");
}
div = input_field.parentNode;
div.setAttribute("class", "slapos-parameter error-input");
div.querySelector("span.error").textContent = validation.errors[error_index].message;
} }
div = input_field.parentNode;
div.setAttribute("class", "slapos-parameter error-input");
div.querySelector("span.error").textContent = validation.errors[error_index].message;
} }
} }
......
...@@ -280,7 +280,7 @@ ...@@ -280,7 +280,7 @@
</item> </item>
<item> <item>
<key> <string>serial</string> </key> <key> <string>serial</string> </key>
<value> <string>1000.42210.9826.36164</string> </value> <value> <string>1003.46178.61021.55398</string> </value>
</item> </item>
<item> <item>
<key> <string>state</string> </key> <key> <string>state</string> </key>
...@@ -298,7 +298,7 @@ ...@@ -298,7 +298,7 @@
</tuple> </tuple>
<state> <state>
<tuple> <tuple>
<float>1656947623.15</float> <float>1666027007.74</float>
<string>UTC</string> <string>UTC</string>
</tuple> </tuple>
</state> </state>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment