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 () {
return ret_obj;
});
}
function convertExpandedProperties2array(properties) {
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 arr;
}
function schemaArrFilteredByDocument(schema_arr, json_document) {
var i, var i,
z, flag,
schema_arr = []; ret_arr = [],
for (i = 0; i < arr.length; i += 1) { schema;
for (z = 0; z < arr[i].length; z += 1) { if (schema_arr.length === 1) {
schema_arr.push(arr[i][z]); 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];
} }
return schema_arr; }
}); // 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,
key,
required = json_field.required || [],
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) {
var div, var div,
div_input, div_input,
input, input;
property_name;
div = document.createElement("div"); div = document.createElement("div");
div.setAttribute("class", "subfield"); div.setAttribute("class", "subfield");
div.title = json_field.description; // div.title = json_field.description;
div_input = document.createElement("div"); div_input = document.createElement("div");
div_input.setAttribute("class", "input"); div_input.setAttribute("class", "input");
function element_append(child) {
if (child) {
// insert additionalProperty before selector
selector.element.parentNode.insertBefore(child, selector.element);
}
}
input = document.createElement("input"); input = document.createElement("input");
input.type = "text"; input.type = "text";
input.placeholder = "name of " + title; input.placeholder = "name of " + title;
div_input.appendChild(input); div_input.appendChild(input);
queue return RSVP.Queue()
.push(function () { .push(function () {
return expandSchema(g, schema, s_path); return expandSchema(g, schema, schema_path);
}) })
.push(function (schema_arr) { .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 () {
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,60 +880,68 @@ ...@@ -838,60 +880,68 @@
}) })
.push(element_append); .push(element_append);
}); });
});
return queue;
}) })
.push(function (input) { .push(function (input) {
// XXX update on every add/delete item
div_input.appendChild(input); div_input.appendChild(input);
div.appendChild(div_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); function root_append(child) {
}); root.appendChild(child);
} }
if (default_dict === undefined) { if (default_dict === undefined) {
default_dict = {}; default_dict = {};
} }
for (key in json_field.properties) { return RSVP.Queue()
if (json_field.properties.hasOwnProperty(key)) { .push(function () {
if (required.indexOf(key) >= 0) { return expandProperties(g, json_field.properties, schema_path + '/properties/');
})
.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; used_properties[key] = false;
if (json_field.properties[key].default !== undefined) { q.push(render_field.bind(g, g, key, path,
json_field.properties[key].info = '(default = ' + json_field.properties[key].default + ')'; s_o.schema, default_dict[key], root, s_o.schema_path, {required: true})
}
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)) { }
if (default_dict.hasOwnProperty(key) && !used_properties.hasOwnProperty(key)) {
used_properties[key] = ""; used_properties[key] = "";
queue q.push(
.push(
addSubForm.bind(g, { addSubForm.bind(g, {
gadget: g, gadget: g,
property_name: key, property_name: key,
path: path, path: path,
schema_path: schema_path + '/properties/' + encodeJsonPointer(key), schema_path: s_o.schema_path,
schema_part: json_field.properties[key], schema_part: s_o.schema,
default_dict: default_dict[key] default_dict: default_dict[key]
}) })
) )
...@@ -899,12 +949,10 @@ ...@@ -899,12 +949,10 @@
} }
} }
} }
return q;
queue
.push(function () {
return expandProperties(g, json_field.properties, schema_path + '/properties/');
}) })
.push(function (schema_arr) { .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,14 +994,26 @@ ...@@ -946,14 +994,26 @@
.push(function (element) { .push(function (element) {
selector.element = element; selector.element = element;
return root_append(element); return root_append(element);
}); })
.push(function () {
var queue = RSVP.Queue(),
additionalProperties;
if (json_field.patternProperties !== undefined) { if (json_field.patternProperties !== undefined) {
// XXX need loop on any pattern properties // XXX need loop on any pattern properties
if (json_field.patternProperties['.*'] !== undefined) { if (json_field.patternProperties['.*'] !== undefined) {
addAdditional(".* property", queue
.push(render_object_additionalProperty.bind(g,
g,
".* property",
default_dict,
path,
json_field.patternProperties['.*'], json_field.patternProperties['.*'],
schema_path + '/patternProperties/.*'); schema_path + '/patternProperties/.*',
used_properties,
element_append
))
.push(root_append);
} }
} }
...@@ -963,9 +1023,25 @@ ...@@ -963,9 +1023,25 @@
additionalProperties = json_field.additionalProperties; additionalProperties = json_field.additionalProperties;
} }
if (additionalProperties !== false) { if (additionalProperties !== false) {
addAdditional("additional property", additionalProperties, schema_path + '/additionalProperties'); queue
.push(render_object_additionalProperty.bind(g,
g,
"additional property",
default_dict,
path,
additionalProperties,
schema_path + '/additionalProperties',
used_properties,
element_append
))
.push(root_append);
} }
return queue;
})
.push(function () {
var key,
queue = RSVP.Queue();
for (key in default_dict) { for (key in default_dict) {
if (default_dict.hasOwnProperty(key)) { if (default_dict.hasOwnProperty(key)) {
if (!used_properties.hasOwnProperty(key)) { if (!used_properties.hasOwnProperty(key)) {
...@@ -985,6 +1061,7 @@ ...@@ -985,6 +1061,7 @@
} }
} }
return queue; 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