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

erp5_json_form: update from...

erp5_json_form: update from https://lab.nexedi.com/bk/rjs_json_form/tree/9dafc5c385b6b1de8060fd1b897a50386fabc2cd
parent dd8d76f4
...@@ -121,7 +121,7 @@ ...@@ -121,7 +121,7 @@
return schema; return schema;
} }
function render_enum(g, schema, json_document) { function render_enum(schema, json_document) {
var input = document.createElement("select"), var input = document.createElement("select"),
option, option,
i, i,
...@@ -129,19 +129,12 @@ ...@@ -129,19 +129,12 @@
selected = false, selected = false,
enum_arr = schema['enum']; enum_arr = schema['enum'];
input.size = 1; input.size = 1;
if (schema.default) { option = document.createElement("option");
if (json_document === undefined) { option.value = "";
json_document = schema.default; if (json_document === undefined) {
g.props.changed = true; option.selected = true;
}
} else {
option = document.createElement("option");
option.value = "";
if (json_document === undefined) {
option.selected = true;
}
input.appendChild(option);
} }
input.appendChild(option);
for (i = 0; i < enum_arr.length; i += 1) { for (i = 0; i < enum_arr.length; i += 1) {
if (enum_arr.hasOwnProperty(i)) { if (enum_arr.hasOwnProperty(i)) {
option = document.createElement("option"); option = document.createElement("option");
...@@ -178,7 +171,7 @@ ...@@ -178,7 +171,7 @@
return input; return input;
} }
function render_enum_with_title(g, schema_arr, json_document, selected_schema) { function render_enum_with_title(schema_arr, json_document, selected_schema) {
var input = document.createElement("select"), var input = document.createElement("select"),
option, option,
i, i,
...@@ -188,19 +181,12 @@ ...@@ -188,19 +181,12 @@
if (json_document === undefined && selected_schema !== undefined) { if (json_document === undefined && selected_schema !== undefined) {
json_document = selected_schema.schema.const; json_document = selected_schema.schema.const;
} }
if (schema_arr[0].schema.default) { option = document.createElement("option");
if (json_document === undefined) { option.value = "";
json_document = schema_arr[0].schema.default; if (json_document === undefined) {
g.props.changed = true; option.selected = true;
}
} else {
option = document.createElement("option");
option.value = "";
if (json_document === undefined) {
option.selected = true;
}
input.appendChild(option);
} }
input.appendChild(option);
for (i = 0; i < schema_arr.length; i += 1) { for (i = 0; i < schema_arr.length; i += 1) {
option = document.createElement("option"); option = document.createElement("option");
// XXX use number id for speedup // XXX use number id for speedup
...@@ -237,7 +223,7 @@ ...@@ -237,7 +223,7 @@
return input; return input;
} }
function render_boolean(g, schema, json_document) { function render_boolean(json_document) {
var input, var input,
schema_for_selection = { schema_for_selection = {
type: "boolean", type: "boolean",
...@@ -250,10 +236,7 @@ ...@@ -250,10 +236,7 @@
if (json_document === "false") { if (json_document === "false") {
json_document = false; json_document = false;
} }
if (getDocumentType(schema.default) === "boolean") { input = render_enum(schema_for_selection, json_document);
schema_for_selection.default = schema.default;
}
input = render_enum(g, schema_for_selection, json_document);
input.setAttribute('data-json-type', "boolean"); input.setAttribute('data-json-type', "boolean");
return input; return input;
} }
...@@ -598,9 +581,6 @@ ...@@ -598,9 +581,6 @@
.push(function (value) { .push(function (value) {
return event(schema_alternatives[value[scope]].value); return event(schema_alternatives[value[scope]].value);
}) })
.push(function () {
return gadget.rootNotifyChange();
})
.push(function () { .push(function () {
if (rerender) { if (rerender) {
return rerender(g, schema_alternatives); return rerender(g, schema_alternatives);
...@@ -609,6 +589,9 @@ ...@@ -609,6 +589,9 @@
}) })
.push(function (render_options) { .push(function (render_options) {
return g.render(render_options); return g.render(render_options);
})
.push(function () {
return gadget.rootNotifyChange();
}); });
}, },
rerender: function () { rerender: function () {
...@@ -698,29 +681,22 @@ ...@@ -698,29 +681,22 @@
var input, var input,
is_items_arr = schema.items instanceof Array, is_items_arr = schema.items instanceof Array,
minItems = schema.minItems || 0; minItems = schema.minItems || 0;
if (schema.default === undefined && if (json_document instanceof Array &&
json_document === undefined) { json_document.length === 0) {
div_input.setAttribute("data-undefined", "true"); div_input.setAttribute("data-json-empty-array", "true");
} }
function element_append(child) { function element_append(child) {
if (child) { if (child) {
input.parentNode.insertBefore(child, input); input.parentNode.insertBefore(child, input);
div_input.removeAttribute("data-undefined"); div_input.removeAttribute("data-json-empty-array");
} }
} }
function div_append(child) { function div_append(child) {
if (child) { if (child) {
div_input.appendChild(child); div_input.appendChild(child);
div_input.removeAttribute("data-undefined"); div_input.removeAttribute("data-json-empty-array");
}
}
if (json_document === undefined) {
if (schema.hasOwnProperty('default')) {
json_document = schema.default;
gadget.props.changed = true;
} }
} }
...@@ -923,13 +899,13 @@ ...@@ -923,13 +899,13 @@
// render input begin // render input begin
if (!input && schema_arr[0].is_arr_of_const && schema_arr.length > 1) { if (!input && schema_arr[0].is_arr_of_const && schema_arr.length > 1) {
input = render_enum_with_title(gadget, schema_arr, json_document, options.selected_schema); input = render_enum_with_title(schema_arr, json_document, options.selected_schema);
} }
if (!input && schema.const !== undefined) { if (!input && schema.const !== undefined) {
input = render_const(gadget, schema, json_document); input = render_const(gadget, schema, json_document);
} }
if (!input && schema.enum !== undefined) { if (!input && schema.enum !== undefined) {
input = render_enum(gadget, schema, json_document); input = render_enum(schema, json_document);
// XXX take in account existing type with enum // XXX take in account existing type with enum
type_changed = false; type_changed = false;
} }
...@@ -939,7 +915,7 @@ ...@@ -939,7 +915,7 @@
} }
if (!input && type === "boolean") { if (!input && type === "boolean") {
input = render_boolean(gadget, schema, json_document); input = render_boolean(json_document);
} }
if (!input && ["string", "integer", "number", "null"].indexOf(type) >= 0) { if (!input && ["string", "integer", "number", "null"].indexOf(type) >= 0) {
...@@ -956,10 +932,6 @@ ...@@ -956,10 +932,6 @@
} }
if (type === "integer" || type === "number") { if (type === "integer" || type === "number") {
if (json_document === undefined && typeof schema.default === "number") {
input.value = schema.default;
gadget.props.changed = true;
}
input.setAttribute("data-json-type", type); input.setAttribute("data-json-type", type);
if (json_document === undefined || json_document === null || if (json_document === undefined || json_document === null ||
typeof json_document === "number") { typeof json_document === "number") {
...@@ -991,10 +963,6 @@ ...@@ -991,10 +963,6 @@
input.max = schema.maximum; input.max = schema.maximum;
} }
} else { } else {
if (json_document === undefined && typeof schema.default === "string") {
input.value = schema.default;
gadget.props.changed = true;
}
input.type = "text"; input.type = "text";
if (schema.pattern) { if (schema.pattern) {
input.pattern = schema.pattern; input.pattern = schema.pattern;
...@@ -1012,10 +980,6 @@ ...@@ -1012,10 +980,6 @@
} }
} }
} }
if (schema.default !== undefined) {
input.setAttribute('data-default-value', JSON.stringify(schema.default));
input.placeholder = schema.default;
}
} }
// render input end // render input end
...@@ -1107,6 +1071,9 @@ ...@@ -1107,6 +1071,9 @@
div.setAttribute("data-parent-scope", gadget.element.getAttribute("data-gadget-scope")); div.setAttribute("data-parent-scope", gadget.element.getAttribute("data-gadget-scope"));
div.setAttribute("data-json-path", first_path + '/'); div.setAttribute("data-json-path", first_path + '/');
div.setAttribute("data-json-type", type); div.setAttribute("data-json-type", type);
if (options.required) {
div.setAttribute("data-json-required", "true");
}
} }
if (schema.info !== undefined) { if (schema.info !== undefined) {
...@@ -1326,18 +1293,8 @@ ...@@ -1326,18 +1293,8 @@
root.appendChild(child); root.appendChild(child);
} }
if (JSON.stringify(schema.default) === '{}') {
// save default value as attribute only for empty values
root.parentElement.setAttribute("data-default-value", '{}');
}
if (json_document === undefined) { if (json_document === undefined) {
if (schema.hasOwnProperty('default')) { json_document = {};
json_document = schema.default;
g.props.changed = true;
} else {
json_document = {};
}
} }
return expandProperties(g, schema.properties, schema_path + '/properties/', required) return expandProperties(g, schema.properties, schema_path + '/properties/', required)
...@@ -1525,7 +1482,7 @@ ...@@ -1525,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,
...@@ -1533,6 +1490,7 @@ ...@@ -1533,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) {
...@@ -1544,19 +1502,29 @@ ...@@ -1544,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)) {
d[kk] = {}; if (value !== undefined) {
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 () {
...@@ -1594,25 +1562,51 @@ ...@@ -1594,25 +1562,51 @@
}); });
} }
g.props.inputs.forEach(function (input) {
// set empty object if default value require this 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='" + } else {
g.element.getAttribute("data-gadget-scope") + "']"); if (input.value !== "") {
for (i = 0; i < array.length; i += 1) { var type = input.getAttribute('data-json-type');
path = array[i].getAttribute("data-json-path").slice(0, -1); if (input.tagName === "SELECT" && input.value) {
if (array[i].getAttribute("data-default-value") === "{}") { // selection used for enums
convertOnMultiLevel(multi_level_dict, path, {}); 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)) {
array = options.arrays[path] array = options.arrays[path]
.querySelectorAll("div[data-gadget-parent-scope='" + g.element.getAttribute("data-gadget-scope") + "']"); .querySelectorAll("div[data-gadget-parent-scope='" + g.element.getAttribute("data-gadget-scope") + "']");
len = array.length; len = array.length;
if (len === 0 && if (len === 0 &&
!options.arrays[path].hasAttribute('data-undefined')) { options.arrays[path].hasAttribute('data-json-empty-array')) {
convertOnMultiLevel(multi_level_dict, path.slice(0, -1), []); convertOnMultiLevel(multi_level_dict, path.slice(0, -1), []);
} }
for (i = 0; i < len; i = i + 1) { for (i = 0; i < len; i = i + 1) {
...@@ -1640,47 +1634,26 @@ ...@@ -1640,47 +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, []);
} }
} else if (input.hasAttribute('data-default-value')) {
json_dict[input.name] = JSON.parse(input.getAttribute('data-default-value'));
} }
} }
});
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