Commit 0bcb2e57 authored by Boris Kocherov's avatar Boris Kocherov

fix json_document loading for anyOf contained schema and modify demo.json for demonstrate

parent c1d8f64a
...@@ -14,5 +14,13 @@ ...@@ -14,5 +14,13 @@
"instance-count": 2 "instance-count": 2
} }
}, },
"site-id": "erp5" "site-id": "erp5",
"zodb": [
{
"server": {
"cluster": "super-neo-cluster-uid"
},
"type": "neo"
}
]
} }
\ No newline at end of file
...@@ -348,48 +348,85 @@ ...@@ -348,48 +348,85 @@
}; };
function expandProperties(g, properties, schema_path) { function expandProperties(g, properties, schema_path) {
var ret_obj = {};
return RSVP.Queue() return RSVP.Queue()
.push(function () { .push(function () {
var property_name, var property_name,
arr = []; arr = [];
function addPropertyName(p_name) { function addPropertyName(p_name) {
return function (schema_array) { return function (schema_array) {
var i; ret_obj[p_name] = schema_array;
for (i = 0; i < schema_array.length; i += 1) {
// add propertyName to title
if (schema_array[i].title && schema_array.length > 1) {
schema_array[i].title = p_name + ' /' + schema_array[i].title;
} else {
schema_array[i].title = p_name;
}
// add propertyName to schemaItem
schema_array[i].property_name = p_name;
}
return schema_array;
}; };
} }
for (property_name in properties) { for (property_name in properties) {
if (properties.hasOwnProperty(property_name)) { if (properties.hasOwnProperty(property_name)) {
arr.push( arr.push(
expandSchema(g, properties[property_name], schema_path + expandSchema(g, properties[property_name],
encodeJsonPointer(property_name)) schema_path + encodeJsonPointer(property_name))
.push(addPropertyName(property_name)) .push(addPropertyName(property_name))
); );
} }
} }
return RSVP.all(arr); return RSVP.all(arr);
}) })
.push(function (arr) { .push(function () {
var i, return ret_obj;
z, });
schema_arr = []; }
for (i = 0; i < arr.length; i += 1) {
for (z = 0; z < arr[i].length; z += 1) { function convertExpandedProperties2array(properties) {
schema_arr.push(arr[i][z]); var property_name,
arr = [],
i,
schema_array;
for (property_name in properties) {
if (properties.hasOwnProperty(property_name)) {
schema_array = properties[property_name];
for (i = 0; i < schema_array.length; i += 1) {
// add propertyName to title
if (schema_array[i].title && schema_array.length > 1) {
schema_array[i].title = property_name + ' /' + schema_array[i].title;
} else {
schema_array[i].title = property_name;
} }
// add propertyName to schemaItem
schema_array[i].property_name = property_name;
arr.push(schema_array[i]);
} }
return schema_arr; }
}); }
return arr;
}
function schemaArrFilteredByDocument(schema_arr, json_document) {
var i,
flag,
ret_arr = [],
schema;
if (schema_arr.length === 1) {
return schema_arr[0];
}
if (json_document !== undefined) {
for (i = 0; i < schema_arr.length; i += 1) {
schema = schema_arr[i].schema;
if (schema === true) {
flag = true;
} else if (schema === false) {
flag = false;
} else {
flag = tv4.validate(json_document, schema);
}
if (flag) {
ret_arr.push(schema_arr[i]);
}
}
if (ret_arr.length === 0) {
// XXX find schema more compatible with document
return schema_arr[0];
}
}
// XXX if (ret_arr.length > 1) notify user
return ret_arr[0];
} }
function checkValidityAndNotifyChange(g) { function checkValidityAndNotifyChange(g) {
...@@ -612,6 +649,7 @@ ...@@ -612,6 +649,7 @@
root.appendChild(div); root.appendChild(div);
}); });
// XXX rewrite loading document for anyOf schema
if (default_array) { if (default_array) {
for (i = 0; i < default_array.length; i = i + 1) { for (i = 0; i < default_array.length; i = i + 1) {
queue queue
...@@ -643,6 +681,12 @@ ...@@ -643,6 +681,12 @@
first_path, first_path,
queue = RSVP.Queue(); queue = RSVP.Queue();
if (json_field instanceof Array) {
json_field = schemaArrFilteredByDocument(json_field, default_value);
schema_path = json_field.schema_path;
json_field = json_field.schema;
}
options = options || {}; options = options || {};
type = options.type; type = options.type;
...@@ -784,50 +828,48 @@ ...@@ -784,50 +828,48 @@
}); });
} }
render_object = function (g, json_field, default_dict, root, path, schema_path) { function render_object_additionalProperty(g, title, json_document, path, schema, schema_path, used, element_append) {
var additionalProperties, var div,
key, div_input,
required = json_field.required || [], input;
used_properties = {},
queue = RSVP.Queue(),
selector = {};
g.props.objects[path] = used_properties;
function root_append(child) {
root.appendChild(child);
}
function addAdditional(title, schema, s_path) { div = document.createElement("div");
var div, div.setAttribute("class", "subfield");
div_input, // div.title = json_field.description;
input,
property_name;
div = document.createElement("div"); div_input = document.createElement("div");
div.setAttribute("class", "subfield"); div_input.setAttribute("class", "input");
div.title = json_field.description;
div_input = document.createElement("div"); input = document.createElement("input");
div_input.setAttribute("class", "input"); input.type = "text";
input.placeholder = "name of " + title;
div_input.appendChild(input);
function element_append(child) { return RSVP.Queue()
if (child) { .push(function () {
// insert additionalProperty before selector return expandSchema(g, schema, schema_path);
selector.element.parentNode.insertBefore(child, selector.element); })
.push(function (schema_arr) {
var queue = RSVP.Queue(),
property_name;
for (property_name in json_document) {
if (json_document.hasOwnProperty(property_name) && !used.hasOwnProperty(property_name)) {
used[property_name] = "";
queue
.push(
addSubForm.bind(g, {
gadget: g,
property_name: property_name,
path: path,
schema_path: schema_path,
schema_part: schema_arr,
default_dict: json_document[property_name]
})
)
.push(element_append);
}
} }
} queue.push(function () {
input = document.createElement("input");
input.type = "text";
input.placeholder = "name of " + title;
div_input.appendChild(input);
queue
.push(function () {
return expandSchema(g, schema, s_path);
})
.push(function (schema_arr) {
return render_schema_selector(g, "add " + title, schema_arr, function (value) { return render_schema_selector(g, "add " + title, schema_arr, function (value) {
return addSubForm({ return addSubForm({
gadget: g, gadget: g,
...@@ -838,73 +880,79 @@ ...@@ -838,73 +880,79 @@
}) })
.push(element_append); .push(element_append);
}); });
})
.push(function (input) {
// XXX update on every add/delete item
div_input.appendChild(input);
div.appendChild(div_input);
}); });
return queue;
})
.push(function (input) {
div_input.appendChild(input);
div.appendChild(div_input);
return div;
});
}
for (property_name in default_dict) { render_object = function (g, json_field, default_dict, root, path, schema_path) {
if (default_dict.hasOwnProperty(property_name) && !used_properties.hasOwnProperty(property_name)) { var required = json_field.required || [],
used_properties[property_name] = ""; used_properties = {},
queue properties,
.push( selector = {};
addSubForm.bind(g, {
gadget: g, g.props.objects[path] = used_properties;
property_name: property_name,
path: path, function element_append(child) {
schema_path: s_path, if (child) {
schema_part: schema, // insert additionalProperty before selector
default_dict: default_dict[property_name] selector.element.parentNode.insertBefore(child, selector.element);
})
)
.push(element_append);
}
} }
queue.push(function () {
root.appendChild(div);
});
} }
if (default_dict === undefined) { function root_append(child) {
default_dict = {}; root.appendChild(child);
} }
for (key in json_field.properties) { if (default_dict === undefined) {
if (json_field.properties.hasOwnProperty(key)) { default_dict = {};
if (required.indexOf(key) >= 0) {
used_properties[key] = false;
if (json_field.properties[key].default !== undefined) {
json_field.properties[key].info = '(default = ' + json_field.properties[key].default + ')';
}
queue
.push(render_field.bind(g, g, key, path,
json_field.properties[key], default_dict[key], root, schema_path, {required: true})
);
} else if (default_dict.hasOwnProperty(key)) {
used_properties[key] = "";
queue
.push(
addSubForm.bind(g, {
gadget: g,
property_name: key,
path: path,
schema_path: schema_path + '/properties/' + encodeJsonPointer(key),
schema_part: json_field.properties[key],
default_dict: default_dict[key]
})
)
.push(root_append);
}
}
} }
queue return RSVP.Queue()
.push(function () { .push(function () {
return expandProperties(g, json_field.properties, schema_path + '/properties/'); return expandProperties(g, json_field.properties, schema_path + '/properties/');
}) })
.push(function (schema_arr) { .push(function (ret) {
var schema_arr,
q = RSVP.Queue(),
s_o,
key;
properties = ret;
for (key in properties) {
if (properties.hasOwnProperty(key)) {
schema_arr = properties[key];
s_o = schemaArrFilteredByDocument(schema_arr, default_dict[key]);
if (required.indexOf(key) >= 0 && schema_arr.length === 1) {
used_properties[key] = false;
q.push(render_field.bind(g, g, key, path,
s_o.schema, default_dict[key], root, s_o.schema_path, {required: true})
);
}
if (default_dict.hasOwnProperty(key) && !used_properties.hasOwnProperty(key)) {
used_properties[key] = "";
q.push(
addSubForm.bind(g, {
gadget: g,
property_name: key,
path: path,
schema_path: s_o.schema_path,
schema_part: s_o.schema,
default_dict: default_dict[key]
})
)
.push(root_append);
}
}
}
return q;
})
.push(function () {
var schema_arr = convertExpandedProperties2array(properties);
return render_schema_selector(g, "add property", schema_arr, function (value) { return render_schema_selector(g, "add property", schema_arr, function (value) {
used_properties[value.property_name] = ""; used_properties[value.property_name] = "";
return addSubForm({ return addSubForm({
...@@ -946,45 +994,74 @@ ...@@ -946,45 +994,74 @@
.push(function (element) { .push(function (element) {
selector.element = element; selector.element = element;
return root_append(element); return root_append(element);
}); })
.push(function () {
if (json_field.patternProperties !== undefined) { var queue = RSVP.Queue(),
// XXX need loop on any pattern properties additionalProperties;
if (json_field.patternProperties['.*'] !== undefined) {
addAdditional(".* property", if (json_field.patternProperties !== undefined) {
json_field.patternProperties['.*'], // XXX need loop on any pattern properties
schema_path + '/patternProperties/.*'); if (json_field.patternProperties['.*'] !== undefined) {
} queue
} .push(render_object_additionalProperty.bind(g,
g,
if (json_field.additionalProperties === undefined) { ".* property",
additionalProperties = true; default_dict,
} else { path,
additionalProperties = json_field.additionalProperties; json_field.patternProperties['.*'],
} schema_path + '/patternProperties/.*',
if (additionalProperties !== false) { used_properties,
addAdditional("additional property", additionalProperties, schema_path + '/additionalProperties'); element_append
} ))
.push(root_append);
}
}
for (key in default_dict) { if (json_field.additionalProperties === undefined) {
if (default_dict.hasOwnProperty(key)) { additionalProperties = true;
if (!used_properties.hasOwnProperty(key)) { } else {
additionalProperties = json_field.additionalProperties;
}
if (additionalProperties !== false) {
queue queue
.push( .push(render_object_additionalProperty.bind(g,
addSubForm.bind(g, { g,
gadget: g, "additional property",
property_name: key, default_dict,
path: path, path,
schema_path: "", additionalProperties,
schema_part: undefined, schema_path + '/additionalProperties',
default_dict: default_dict[key] used_properties,
}) element_append
) ))
.push(root_append); .push(root_append);
} }
}
} return queue;
return queue; })
.push(function () {
var key,
queue = RSVP.Queue();
for (key in default_dict) {
if (default_dict.hasOwnProperty(key)) {
if (!used_properties.hasOwnProperty(key)) {
queue
.push(
addSubForm.bind(g, {
gadget: g,
property_name: key,
path: path,
schema_path: "",
schema_part: undefined,
default_dict: default_dict[key]
})
)
.push(root_append);
}
}
}
return queue;
});
}; };
function getFormValuesAsJSONDict(g) { function getFormValuesAsJSONDict(g) {
......
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