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 @@
function getFormValuesAsJSONDict(g) {
var multi_level_dict = {"": {}},
count_of_values = 0,
is_empty = true,
scope,
options = g.props,
array,
......@@ -1490,6 +1490,7 @@
key,
i,
len,
json_dict = {},
queue = RSVP.Queue();
function convertOnMultiLevel(d, key, value) {
......@@ -1501,19 +1502,29 @@
if (ii === key_list.length - 1) {
if (value !== undefined) {
d[kk] = value;
count_of_values += 1;
is_empty = false;
} else {
return d[kk];
}
} else {
if (!d.hasOwnProperty(kk)) {
if (value !== undefined) {
d[kk] = {};
} else {
return;
}
}
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) {
queue
.push(function () {
......@@ -1551,21 +1562,43 @@
});
}
// set empty object/array for required properties/items
array = g.element
.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, {});
g.props.inputs.forEach(function (input) {
if (input.hasAttribute('data-origin-value')) {
json_dict[input.name] = JSON.parse(input.getAttribute('data-origin-value'));
} 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) {
if (options.arrays.hasOwnProperty(path)) {
......@@ -1601,45 +1634,26 @@
return queue
.push(function () {
var json_dict = {},
k;
g.props.inputs.forEach(function (input) {
if (input.hasAttribute('data-origin-value')) {
json_dict[input.name] = JSON.parse(input.getAttribute('data-origin-value'));
} else {
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');
}
// set empty object/array for required properties/items
// if parent object/array existed
array = g.element
.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 (check_parent_path_not_empty(path) &&
convertOnMultiLevel(multi_level_dict, path) === undefined) {
if (array[i].hasAttribute("data-json-required")) {
if (array[i].getAttribute("data-json-type") === "object") {
convertOnMultiLevel(multi_level_dict, path, {});
} 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) {
case "string":
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