Commit a1c5935c authored by Boris Kocherov's avatar Boris Kocherov

fix getContent in subgadget mode and remove leading '//' in input.name

parent 94269625
/*jslint nomen: true, maxlen: 200, indent: 2*/ /*jslint nomen: true, maxlen: 200, indent: 2*/
/*global rJS, console, window, document, RSVP, btoa, atob, $, loopEventListener */ /*global rJS, window, document, RSVP, $, loopEventListener */
(function (window, document, rJS, $, loopEventListener) { (function (window, document, rJS, $, loopEventListener) {
"use strict"; "use strict";
...@@ -49,9 +49,14 @@ ...@@ -49,9 +49,14 @@
span_info, span_info,
span_error, span_error,
input, input,
first_path,
queue = RSVP.Queue(); queue = RSVP.Queue();
// var input = document.createElement("input"); if (path && key) {
first_path = path + key;
} else {
first_path = "";
}
div = document.createElement("div"); div = document.createElement("div");
div.setAttribute("class", "subfield"); div.setAttribute("class", "subfield");
...@@ -87,7 +92,6 @@ ...@@ -87,7 +92,6 @@
} else { } else {
input = document.createElement("input"); input = document.createElement("input");
if (default_value !== undefined) { if (default_value !== undefined) {
console.log(default_value);
input.value = default_value; input.value = default_value;
} }
...@@ -107,7 +111,7 @@ ...@@ -107,7 +111,7 @@
json_field, json_field,
default_value, default_value,
div_input, div_input,
path + "/" + key); first_path + '/');
}); });
} }
...@@ -115,7 +119,7 @@ ...@@ -115,7 +119,7 @@
// object and array excluded from // object and array excluded from
// gadget.props.inputs not contain values // gadget.props.inputs not contain values
gadget.props.inputs.push(input); gadget.props.inputs.push(input);
input.name = path + "/" + key; input.name = first_path;
input.setAttribute("class", "slapos-parameter"); input.setAttribute("class", "slapos-parameter");
div_input.appendChild(input); div_input.appendChild(input);
} }
...@@ -226,37 +230,20 @@ ...@@ -226,37 +230,20 @@
} }
function getFormValuesAsJSONDict(g) { function getFormValuesAsJSONDict(g) {
var json_dict = {}, var multi_level_dict = {},
entry,
multi_level_dict = {},
scope, scope,
subforms = g.props.subforms, subforms = g.props.subforms,
queue = RSVP.Queue(); queue = RSVP.Queue();
g.props.inputs.forEach(function (input) {
if (input.value !== "") {
if (input.type === 'number') {
json_dict[input.name] = parseInt(input.value, 10);
} else if (input.value === "true") {
json_dict[input.name] = true;
} else if (input.value === "false") {
json_dict[input.name] = false;
} else if (input.tagName === "TEXTAREA") {
if (input["data-format"] === "string") {
json_dict[input.name] = input.value;
} else {
json_dict[input.name] = input.value.split('\n');
}
} else {
json_dict[input.name] = input.value;
}
}
});
function convertOnMultiLevel(key, value, d) { function convertOnMultiLevel(d, key, value) {
var i, var orig_d = d,
i,
kk, kk,
key_list = key.split("/"); key_list = key.split("/");
for (i = 2; i < key_list.length; i += 1) { if (key === "") {
return value;
}
for (i = 1; i < key_list.length; i += 1) {
kk = key_list[i]; kk = key_list[i];
if (i === key_list.length - 1) { if (i === key_list.length - 1) {
d[kk] = value; d[kk] = value;
...@@ -267,6 +254,7 @@ ...@@ -267,6 +254,7 @@
d = d[kk]; d = d[kk];
} }
} }
return orig_d;
} }
function recursiveGetContent(gadget, scope) { function recursiveGetContent(gadget, scope) {
...@@ -275,11 +263,7 @@ ...@@ -275,11 +263,7 @@
return gadget.getContent(); return gadget.getContent();
}) })
.push(function (jdict) { .push(function (jdict) {
for (entry in jdict) { convertOnMultiLevel(multi_level_dict, scope, jdict);
if (jdict.hasOwnProperty(entry)) {
convertOnMultiLevel(scope + '/' + entry, jdict[entry], multi_level_dict);
}
}
}); });
} }
...@@ -291,9 +275,30 @@ ...@@ -291,9 +275,30 @@
return queue return queue
.push(function () { .push(function () {
for (entry in json_dict) { var json_dict = {},
if (json_dict.hasOwnProperty(entry)) { key;
convertOnMultiLevel(entry, json_dict[entry], multi_level_dict); g.props.inputs.forEach(function (input) {
if (input.name === "" || input.value !== "") {
if (input.type === 'number') {
json_dict[input.name] = parseInt(input.value, 10);
} else if (input.value === "true") {
json_dict[input.name] = true;
} else if (input.value === "false") {
json_dict[input.name] = false;
} else if (input.tagName === "TEXTAREA") {
if (input["data-format"] === "string") {
json_dict[input.name] = input.value;
} else {
json_dict[input.name] = input.value.split('\n');
}
} else {
json_dict[input.name] = input.value;
}
}
});
for (key in json_dict) {
if (json_dict.hasOwnProperty(key)) {
multi_level_dict = convertOnMultiLevel(multi_level_dict, key, json_dict[key]);
} }
} }
return multi_level_dict; return multi_level_dict;
...@@ -304,7 +309,7 @@ ...@@ -304,7 +309,7 @@
var element = options.element, var element = options.element,
key = options.key || key = options.key ||
element.parentNode.querySelector("input[type='text']").value, element.parentNode.querySelector("input[type='text']").value,
scope = (options.path || element.name) + "/" + key; scope = (options.path || element.name) + key;
if (!key || g.props.subforms.hasOwnProperty(scope)) { if (!key || g.props.subforms.hasOwnProperty(scope)) {
return false; return false;
...@@ -430,7 +435,7 @@ ...@@ -430,7 +435,7 @@
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;
div = $(".slapos-parameter[name='/" + field_name + "']")[0].parentNode; div = $(".slapos-parameter[name='" + field_name + "']")[0].parentNode;
div.setAttribute("class", "slapos-parameter error-input"); div.setAttribute("class", "slapos-parameter error-input");
div.querySelector("span.error").textContent = validation.errors[error_index].message; div.querySelector("span.error").textContent = validation.errors[error_index].message;
} }
...@@ -439,7 +444,7 @@ ...@@ -439,7 +444,7 @@
for (missing_index in validation.missing) { for (missing_index in validation.missing) {
if (validation.missing.hasOwnProperty(missing_index)) { if (validation.missing.hasOwnProperty(missing_index)) {
missing_field_name = validation.missing[missing_index].dataPath; missing_field_name = validation.missing[missing_index].dataPath;
divm = $('.slapos-parameter[name=/' + missing_field_name + "']")[0].parentNode; divm = $('.slapos-parameter[name=' + missing_field_name + "']")[0].parentNode;
divm.setAttribute("class", "error-input"); divm.setAttribute("class", "error-input");
divm.querySelector("span.error").textContent = validation.missing[missing_index].message; divm.querySelector("span.error").textContent = validation.missing[missing_index].message;
} }
...@@ -453,7 +458,7 @@ ...@@ -453,7 +458,7 @@
g.props.inputs = []; g.props.inputs = [];
g.props.add_buttons = []; g.props.add_buttons = [];
g.props.subforms = {}; g.props.subforms = {};
g.props.path = path; g.props.path = path; // self gadget scope
if (key === undefined) { if (key === undefined) {
key = ""; key = "";
} }
......
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