Commit 0707fc55 authored by Rafael Monnerat's avatar Rafael Monnerat

slapos_jio: Include support for editable on parameter form gadget

   Render a "read only" version of the parameter form, similar to the other gadgets.

   There are 2 ocasions that it is required:

      - On Software Instance/Slave Instance view for parameter_xml
      - On Whenever display the connection parameters.
parent 1ac3ec59
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
function jsonDictToParameterXML(json) { function jsonDictToParameterXML(json) {
var parameter_id, var parameter_id,
xml_output = $($.parseXML('<?xml version="1.0" encoding="UTF-8" ?><instance />')); xml_output = $($.parseXML('<?xml version="1.0" encoding="UTF-8" ?>\n<instance />'));
// Used by serialisation XML // Used by serialisation XML
for (parameter_id in json) { for (parameter_id in json) {
if (json.hasOwnProperty(parameter_id)) { if (json.hasOwnProperty(parameter_id)) {
...@@ -28,7 +28,7 @@ ...@@ -28,7 +28,7 @@
} }
function jsonDictToParameterJSONInXML(json) { function jsonDictToParameterJSONInXML(json) {
var xml_output = $($.parseXML('<?xml version="1.0" encoding="UTF-8" ?><instance />')); var xml_output = $($.parseXML('<?xml version="1.0" encoding="UTF-8" ?>\n<instance />'));
// Used by serialisation XML // Used by serialisation XML
$('instance', xml_output).append( $('instance', xml_output).append(
$('<parameter />', xml_output) $('<parameter />', xml_output)
...@@ -129,6 +129,23 @@ ...@@ -129,6 +129,23 @@
}); });
} }
function render_field_readonly(json_field, default_value) {
var domsugar_input_dict = {
text: default_value
};
if (json_field.type === "array") {
domsugar('pre', domsugar_input_dict);
}
if (json_field.type === "string" && json_field.textarea === true) {
return domsugar('pre', domsugar_input_dict);
}
return domsugar('span', domsugar_input_dict);
}
function render_field(json_field, default_value) { function render_field(json_field, default_value) {
var data_format, domsugar_input_dict = {}; var data_format, domsugar_input_dict = {};
if (json_field['enum'] !== undefined) { if (json_field['enum'] !== undefined) {
...@@ -182,7 +199,7 @@ ...@@ -182,7 +199,7 @@
return domsugar('input', domsugar_input_dict); return domsugar('input', domsugar_input_dict);
} }
function render_subform(json_field, default_dict, root, path) { function render_subform(json_field, default_dict, root, path, editable) {
var div_input, var div_input,
key, key,
div, div,
...@@ -202,7 +219,7 @@ ...@@ -202,7 +219,7 @@
path = "/"; path = "/";
} }
if (json_field.patternProperties !== undefined) { if (editable && json_field.patternProperties !== undefined) {
if (json_field.patternProperties['.*'] !== undefined) { if (json_field.patternProperties['.*'] !== undefined) {
div = domsugar("div", { div = domsugar("div", {
...@@ -210,7 +227,6 @@ ...@@ -210,7 +227,6 @@
title: json_field.description title: json_field.description
}); });
div_input = domsugar("div", { div_input = domsugar("div", {
"class": "input" "class": "input"
}, [ }, [
...@@ -250,7 +266,8 @@ ...@@ -250,7 +266,8 @@
json_field.patternProperties['.*'], json_field.patternProperties['.*'],
default_dict[default_value], default_dict[default_value],
default_div, default_div,
path + "/" + default_value path + "/" + default_value,
editable
)); ));
} }
} }
...@@ -262,72 +279,85 @@ ...@@ -262,72 +279,85 @@
for (key in json_field.properties) { for (key in json_field.properties) {
if (json_field.properties.hasOwnProperty(key)) { if (json_field.properties.hasOwnProperty(key)) {
div = document.createElement("div"); if (editable || default_dict[key] !== undefined) {
div.setAttribute("class", "subfield"); div = domsugar("div", {
div.title = json_field.properties[key].description; 'class': 'subfield',
/* console.log(key); */ "title": json_field.properties[key].description
label = document.createElement("label"); });
label.textContent = json_field.properties[key].title;
div.appendChild(label); /* console.log(key); */
div_input = document.createElement("div"); label = domsugar("label", {
div_input.setAttribute("class", "input"); text: json_field.properties[key].title
if (json_field.properties[key].type === 'object') { })
label.setAttribute("class", "slapos-parameter-dict-key"); div.appendChild(label);
div_input = render_subform(json_field.properties[key], div_input = domsugar("div", {'class': 'input'});
default_dict[key], if (json_field.properties[key].type === 'object') {
div_input, label.setAttribute("class", "slapos-parameter-dict-key");
path + "/" + key); div_input = render_subform(json_field.properties[key],
} else { default_dict[key],
input = render_field(json_field.properties[key], default_dict[key]); div_input,
input.name = path + "/" + key; path + "/" + key,
input.setAttribute("class", "slapos-parameter"); editable);
input.setAttribute("placeholder", " "); } else {
div_input.appendChild(input); if (!editable) {
} input = render_field_readonly(json_field.properties[key], default_dict[key]);
default_used_list.push(key); } else {
if (json_field.properties[key]['default'] !== undefined) { input = render_field(json_field.properties[key], default_dict[key]);
span_info = document.createElement("span"); }
span_info.textContent = '(default = ' + json_field.properties[key]['default'] + ')'; input.name = path + "/" + key;
div_input.appendChild(span_info); input.setAttribute("class", "slapos-parameter");
input.setAttribute("placeholder", " ");
div_input.appendChild(input);
}
default_used_list.push(key);
if (editable && (json_field.properties[key]['default'] !== undefined)) {
span_info = domsugar('span', {
text: '(default = ' + json_field.properties[key]['default'] + ')'
});
div_input.appendChild(span_info);
}
div_input.appendChild(domsugar("span", {
class: "error"
}));
div.appendChild(div_input);
root.appendChild(div);
} }
span_error = document.createElement("span");
span_error.setAttribute("class", "error");
div_input.appendChild(span_error);
div.appendChild(div_input);
root.appendChild(div);
} }
} }
for (key in default_dict) { for (key in default_dict) {
if (default_dict.hasOwnProperty(key)) { if (default_dict.hasOwnProperty(key)) {
if (default_used_list.indexOf(key) < 0) { if (default_used_list.indexOf(key) < 0) {
div = document.createElement("div"); div = domsugar("div", {title: key});
div.title = key;
if (typeof default_dict[key] === 'object') { if (typeof default_dict[key] === 'object') {
div_input = document.createElement("div"); div_input = domsugar("div", {'class': 'input'});
div_input.setAttribute("class", "input");
label.setAttribute("class", "slapos-parameter-dict-key"); label.setAttribute("class", "slapos-parameter-dict-key");
div_input = render_subform({}, div_input = render_subform({},
default_dict[key], default_dict[key],
div_input, div_input,
path + "/" + key); path + "/" + key,
editable);
} else { } else {
if (!editable) {
input = render_field_readonly({"type": "string", "textarea": true}, default_dict[key]);
} else {
input = render_field({"type": "string", "textarea": true}, default_dict[key]);
input.name = path + "/" + key;
input.setAttribute("class", "slapos-parameter");
input.setAttribute("placeholder", " ");
}
div.setAttribute("class", "subfield"); div.setAttribute("class", "subfield");
label = document.createElement("label"); div.appendChild(domsugar("label", {text: key}));
label.textContent = key; div_input = domsugar("div",
div.appendChild(label); {"class": "input"},
div_input = document.createElement("div"); [
div_input.setAttribute("class", "input"); input,
input = render_field({"type": "string", "textarea": true}, default_dict[key]); domsugar("span", {
input.name = path + "/" + key; text: '(Not part of the schema)'
input.setAttribute("class", "slapos-parameter"); }),
input.setAttribute("placeholder", " "); domsugar("span", {
div_input.appendChild(input); class: "error"
span_info = document.createElement("span"); })
span_info.textContent = '(Not part of the schema)'; ]);
div_input.appendChild(span_info);
span_error = document.createElement("span");
span_error.setAttribute("class", "error");
div_input.appendChild(span_error);
} }
default_used_list.push(key); default_used_list.push(key);
div.appendChild(div_input); div.appendChild(div_input);
...@@ -451,7 +481,11 @@ ...@@ -451,7 +481,11 @@
text: input_text.value text: input_text.value
})]); })]);
div = render_subform(subform_json, {}, div, element.name + "/" + input_text.value); div = render_subform(subform_json,
{},
div,
element.name + "/" + input_text.value,
gadget.state.editable);
element.parentNode.parentNode.insertBefore(div, element.parentNode.parentNode.children[1]); element.parentNode.parentNode.insertBefore(div, element.parentNode.parentNode.children[1]);
// element.parentNode.parentNode.appendChild(div); // element.parentNode.parentNode.appendChild(div);
...@@ -625,6 +659,7 @@ ...@@ -625,6 +659,7 @@
var fieldset, var fieldset,
fieldset_list = g.element.querySelectorAll('fieldset'), fieldset_list = g.element.querySelectorAll('fieldset'),
div_error, div_error,
raw_parameter_input,
show_raw_button = g.element.querySelector("button.slapos-show-raw-parameter"), show_raw_button = g.element.querySelector("button.slapos-show-raw-parameter"),
show_form_button = g.element.querySelector("button.slapos-show-form"); show_form_button = g.element.querySelector("button.slapos-show-form");
...@@ -657,16 +692,24 @@ ...@@ -657,16 +692,24 @@
} else { } else {
div_error = domsugar('div'); div_error = domsugar('div');
} }
if (!g.state.editable) {
raw_parameter_input = domsugar('pre', {
name: "text_content",
text: g.state.parameter_xml
});
} else {
raw_parameter_input = domsugar('textarea', {
rows: "10",
cols: "80",
name: "text_content",
text: g.state.parameter_xml
});
}
fieldset = domsugar('fieldset', [ fieldset = domsugar('fieldset', [
domsugar('div', { domsugar('div', {
'class': 'field' 'class': 'field'
}, [ }, [
domsugar('textarea', { raw_parameter_input
rows: "10",
cols: "80",
name: "text_content",
text: g.state.parameter_xml
})
]), ]),
// div error // div error
div_error div_error
...@@ -688,6 +731,7 @@ ...@@ -688,6 +731,7 @@
shared = gadget.state.shared, shared = gadget.state.shared,
softwaretype = gadget.state.softwaretype, softwaretype = gadget.state.softwaretype,
softwareindex = gadget.state.softwareindex, softwareindex = gadget.state.softwareindex,
editable = gadget.state.editable,
to_hide = gadget.element.querySelector("button.slapos-show-form"), to_hide = gadget.element.querySelector("button.slapos-show-form"),
to_show = gadget.element.querySelector("button.slapos-show-raw-parameter"); to_show = gadget.element.querySelector("button.slapos-show-raw-parameter");
...@@ -882,7 +926,7 @@ ...@@ -882,7 +926,7 @@
var fieldset_list = gadget.element.querySelectorAll('fieldset'), var fieldset_list = gadget.element.querySelectorAll('fieldset'),
fieldset = document.createElement("fieldset"); fieldset = document.createElement("fieldset");
fieldset = render_subform(json, parameter_dict, fieldset, undefined); fieldset = render_subform(json, parameter_dict, fieldset, undefined, editable);
$(fieldset_list[1]).replaceWith(fieldset); $(fieldset_list[1]).replaceWith(fieldset);
return fieldset_list; return fieldset_list;
}); });
...@@ -955,7 +999,6 @@ ...@@ -955,7 +999,6 @@
return this.changeState({ return this.changeState({
// Not used parameters // Not used parameters
// editable: options.editable,
// hidden: options.hidden, // hidden: options.hidden,
// key: options.key, // key: options.key,
serialisation: options.serialisation, serialisation: options.serialisation,
...@@ -965,6 +1008,7 @@ ...@@ -965,6 +1008,7 @@
shared: options.value.parameter.shared, shared: options.value.parameter.shared,
softwaretype: options.value.parameter.softwaretype, softwaretype: options.value.parameter.softwaretype,
softwareindex: options.value.parameter.softwareindex, softwareindex: options.value.parameter.softwareindex,
editable: options.editable,
// Force refresh in any case // Force refresh in any case
render_timestamp: new Date().getTime() render_timestamp: new Date().getTime()
}); });
...@@ -1060,6 +1104,9 @@ ...@@ -1060,6 +1104,9 @@
gadget.state.shared = 1; gadget.state.shared = 1;
content_dict.shared = 1; content_dict.shared = 1;
} }
if (!gadget.state.editable) {
return gadget.state.parameter_xml;
}
if (text_content !== null) { if (text_content !== null) {
// Don't provide blank string since the parameter will not able to load // Don't provide blank string since the parameter will not able to load
// itself. If the user removed the values, provide an empty parameter default. // itself. If the user removed the values, provide an empty parameter default.
......
...@@ -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.18290.33085.1843</string> </value> <value> <string>1000.19711.33506.41574</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>1652729515.46</float> <float>1652829218.23</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