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