Commit 9dafc5c3 authored by Boris Kocherov's avatar Boris Kocherov

change getContent behaviour: set empty object/array for required...

change getContent behaviour: set empty object/array for required properties/items if parent object/array existed

it allow create json document more schema compatible without additional user inputs
parent e1cefbfd
...@@ -1482,7 +1482,7 @@ ...@@ -1482,7 +1482,7 @@
function getFormValuesAsJSONDict(g) { function getFormValuesAsJSONDict(g) {
var multi_level_dict = {"": {}}, var multi_level_dict = {"": {}},
count_of_values = 0, is_empty = true,
scope, scope,
options = g.props, options = g.props,
array, array,
...@@ -1490,6 +1490,7 @@ ...@@ -1490,6 +1490,7 @@
key, key,
i, i,
len, len,
json_dict = {},
queue = RSVP.Queue(); queue = RSVP.Queue();
function convertOnMultiLevel(d, key, value) { function convertOnMultiLevel(d, key, value) {
...@@ -1501,19 +1502,29 @@ ...@@ -1501,19 +1502,29 @@
if (ii === key_list.length - 1) { if (ii === key_list.length - 1) {
if (value !== undefined) { if (value !== undefined) {
d[kk] = value; d[kk] = value;
count_of_values += 1; is_empty = false;
} else { } else {
return d[kk]; return d[kk];
} }
} else { } else {
if (!d.hasOwnProperty(kk)) { if (!d.hasOwnProperty(kk)) {
if (value !== undefined) {
d[kk] = {}; d[kk] = {};
} else {
return;
}
} }
d = d[kk]; d = d[kk];
} }
} }
} }
function check_parent_path_not_empty(path) {
var key_list = path.split("/"),
parent_path = key_list.slice(0, key_list.length - 1).join("/");
return convertOnMultiLevel(multi_level_dict, parent_path) !== undefined;
}
function recursiveGetContent(scope, path) { function recursiveGetContent(scope, path) {
queue queue
.push(function () { .push(function () {
...@@ -1551,21 +1562,43 @@ ...@@ -1551,21 +1562,43 @@
}); });
} }
g.props.inputs.forEach(function (input) {
// set empty object/array for required properties/items if (input.hasAttribute('data-origin-value')) {
array = g.element json_dict[input.name] = JSON.parse(input.getAttribute('data-origin-value'));
.querySelectorAll("div[data-parent-scope='" +
g.element.getAttribute("data-gadget-scope") + "']");
for (i = 0; i < array.length; i += 1) {
path = array[i].getAttribute("data-json-path").slice(0, -1);
if (array[i].hasAttribute("data-json-required")) {
if (array[i].getAttribute("data-json-type") === "object") {
convertOnMultiLevel(multi_level_dict, path, {});
} else { } else {
convertOnMultiLevel(multi_level_dict, path, []); if (input.value !== "") {
var type = input.getAttribute('data-json-type');
if (input.tagName === "SELECT" && input.value) {
// selection used for enums
json_dict[input.name] = JSON.parse(input.value);
} else if (type === 'number') {
json_dict[input.name] = parseFloat(input.value);
} else if (type === "integer") {
json_dict[input.name] = parseInt(input.value, 10);
} else if (type === "boolean") {
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 (path in json_dict) {
if (json_dict.hasOwnProperty(path)) {
convertOnMultiLevel(multi_level_dict, path, json_dict[path]);
}
}
for (path in options.arrays) { for (path in options.arrays) {
if (options.arrays.hasOwnProperty(path)) { if (options.arrays.hasOwnProperty(path)) {
...@@ -1601,45 +1634,26 @@ ...@@ -1601,45 +1634,26 @@
return queue return queue
.push(function () { .push(function () {
var json_dict = {}, // set empty object/array for required properties/items
k; // if parent object/array existed
g.props.inputs.forEach(function (input) { array = g.element
if (input.hasAttribute('data-origin-value')) { .querySelectorAll("div[data-parent-scope='" +
json_dict[input.name] = JSON.parse(input.getAttribute('data-origin-value')); g.element.getAttribute("data-gadget-scope") + "']");
} else { for (i = 0; i < array.length; i += 1) {
if (input.value !== "") { path = array[i].getAttribute("data-json-path").slice(0, -1);
var type = input.getAttribute('data-json-type'); if (check_parent_path_not_empty(path) &&
if (input.tagName === "SELECT" && input.value) { convertOnMultiLevel(multi_level_dict, path) === undefined) {
// selection used for enums if (array[i].hasAttribute("data-json-required")) {
json_dict[input.name] = JSON.parse(input.value); if (array[i].getAttribute("data-json-type") === "object") {
} else if (type === 'number') { convertOnMultiLevel(multi_level_dict, path, {});
json_dict[input.name] = parseFloat(input.value);
} else if (type === "integer") {
json_dict[input.name] = parseInt(input.value, 10);
} else if (type === "boolean") {
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 { } else {
json_dict[input.name] = input.value; convertOnMultiLevel(multi_level_dict, path, []);
} }
} }
} }
});
for (k in json_dict) {
if (json_dict.hasOwnProperty(k)) {
convertOnMultiLevel(multi_level_dict, k, json_dict[k]);
} }
}
if (count_of_values === 0) { if (is_empty) {
switch (g.props.type) { switch (g.props.type) {
case "string": case "string":
return ""; return "";
......
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