Commit eca3154f authored by Boris Kocherov's avatar Boris Kocherov

make code better understandable and use is_arr_of_const marker

checkSchemaSimpleType() use schema_arr option instead schema
schemaArrFilteredByDocument() return schema_arr instead schema_object
parent b2f97275
...@@ -594,7 +594,20 @@ ...@@ -594,7 +594,20 @@
required_stack = []; required_stack = [];
} }
g.props.schema_required_urls[schema_path] = required_stack; g.props.schema_required_urls[schema_path] = required_stack;
return expandSchema(g, schema, schema_path); return expandSchema(g, schema, schema_path)
.push(function (schema_arr) {
var i;
for (i = 0; i < schema_arr.length; i += 1) {
if (!schema_arr[i].schema.hasOwnProperty('const')) {
schema_arr[0].is_arr_of_const = false;
break;
}
if (i === schema_arr.length - 1) {
schema_arr[0].is_arr_of_const = true;
}
}
return schema_arr;
});
} }
rJS(window) rJS(window)
......
...@@ -345,17 +345,24 @@ ...@@ -345,17 +345,24 @@
} }
return true; return true;
} }
if (schema_arr[0].is_arr_of_const) {
return true;
}
return false; return false;
} }
function checkSchemaSimpleType(schema) { function checkSchemaSimpleType(schema_arr) {
return [ // return true if rendering are not recursive
'string', var schema = schema_arr[0].schema;
'integer', return schema_arr[0].is_arr_of_const ||
'number', schema.hasOwnProperty('const') ||
'boolean', [
'null' 'string',
].indexOf(schema.type) >= 0; 'integer',
'number',
'boolean',
'null'
].indexOf(schema.type) >= 0;
} }
function convertExpandedProperties2array(properties) { function convertExpandedProperties2array(properties) {
...@@ -385,10 +392,12 @@ ...@@ -385,10 +392,12 @@
function schemaArrFilteredByDocument(schema_arr, json_document) { function schemaArrFilteredByDocument(schema_arr, json_document) {
var i, var i,
flag, flag,
circular = schema_arr[0].circular,
ret_arr = [], ret_arr = [],
schema; schema;
if (schema_arr.length === 1) { if (schema_arr.length === 1 ||
return schema_arr[0]; schema_arr[0].is_arr_of_const) {
return schema_arr;
} }
if (json_document !== undefined) { if (json_document !== undefined) {
for (i = 0; i < schema_arr.length; i += 1) { for (i = 0; i < schema_arr.length; i += 1) {
...@@ -406,11 +415,12 @@ ...@@ -406,11 +415,12 @@
} }
if (ret_arr.length === 0) { if (ret_arr.length === 0) {
// XXX find schema more compatible with document // XXX find schema more compatible with document
return schema_arr[0]; return schema_arr;
} }
ret_arr[0].circular = circular;
return ret_arr;
} }
// XXX if (ret_arr.length > 1) notify user return schema_arr;
return ret_arr[0];
} }
function render_schema_selector(gadget, title, schema_arr, event, rerender) { function render_schema_selector(gadget, title, schema_arr, event, rerender) {
...@@ -782,7 +792,8 @@ ...@@ -782,7 +792,8 @@
type_changed, type_changed,
queue = RSVP.Queue(); queue = RSVP.Queue();
schema = schemaArrFilteredByDocument(schema_arr, json_document); // XXX if (ret_arr.length > 1) notify user
schema = schemaArrFilteredByDocument(schema_arr, json_document)[0];
schema_path = schema.schema_path; schema_path = schema.schema_path;
schema = schema.schema; schema = schema.schema;
...@@ -1238,28 +1249,28 @@ ...@@ -1238,28 +1249,28 @@
.push(function (ret) { .push(function (ret) {
var schema_arr, var schema_arr,
q = RSVP.Queue(), q = RSVP.Queue(),
s_o, filtered_schema_arr,
key; key;
properties = ret; properties = ret;
for (key in properties) { for (key in properties) {
if (properties.hasOwnProperty(key)) { if (properties.hasOwnProperty(key)) {
schema_arr = properties[key]; schema_arr = properties[key];
s_o = schemaArrFilteredByDocument(schema_arr, json_document[key]); filtered_schema_arr = schemaArrFilteredByDocument(schema_arr, json_document[key]);
// XXX need schema merge with patternProperties passed key // XXX need schema merge with patternProperties passed key
if (checkSchemaArrOneChoise(schema_arr)) { if (checkSchemaArrOneChoise(schema_arr)) {
if (required.indexOf(key) >= 0) { if (required.indexOf(key) >= 0) {
used_properties[key] = false; used_properties[key] = false;
q.push(render_field.bind(g, g, key, path, q.push(render_field.bind(g, g, key, path,
[s_o], json_document[key], root, {required: true}) filtered_schema_arr, json_document[key], root, {required: true})
); );
} }
if (!used_properties.hasOwnProperty(key) && if (!used_properties.hasOwnProperty(key) &&
!schema_editor && !schema_editor &&
(checkSchemaSimpleType(s_o.schema) || !s_o.circular) (checkSchemaSimpleType(filtered_schema_arr) || !filtered_schema_arr[0].circular)
) { ) {
used_properties[key] = false; used_properties[key] = false;
q.push(render_field.bind(g, g, key, path, q.push(render_field.bind(g, g, key, path,
[s_o], json_document[key], root, { filtered_schema_arr, json_document[key], root, {
required: false, required: false,
delete_button: false delete_button: false
})); }));
...@@ -1273,7 +1284,7 @@ ...@@ -1273,7 +1284,7 @@
gadget: g, gadget: g,
property_name: key, property_name: key,
path: path, path: path,
schema_arr: [s_o], schema_arr: filtered_schema_arr,
json_document: json_document[key] json_document: json_document[key]
}) })
) )
......
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