Commit 393ece50 authored by Boris Kocherov's avatar Boris Kocherov

support if schema.items is array of schema

parent 8efcc991
...@@ -286,6 +286,21 @@ ...@@ -286,6 +286,21 @@
}); });
} }
function expandItems(g, items, schema_path, minItems) {
if (!(items instanceof Array)) {
return g.expandSchema(items, schema_path, minItems !== 0);
}
var i,
tasks = [];
for (i = 0; i < items.length; i += 1) {
tasks.push(g.expandSchema(items[i], schema_path + '/' + i, i < minItems));
}
return RSVP.Queue()
.push(function () {
return RSVP.all(tasks);
});
}
function expandProperties(g, properties, schema_path, required) { function expandProperties(g, properties, schema_path, required) {
var ret_obj = {}; var ret_obj = {};
return RSVP.Queue() return RSVP.Queue()
...@@ -600,6 +615,7 @@ ...@@ -600,6 +615,7 @@
function render_array(gadget, schema, json_document, div_input, path, schema_path) { function render_array(gadget, schema, json_document, div_input, path, schema_path) {
var input, var input,
is_items_arr = schema.items instanceof Array,
minItems = schema.minItems || 0; minItems = schema.minItems || 0;
if (schema.default === undefined && if (schema.default === undefined &&
json_document === undefined) { json_document === undefined) {
...@@ -622,20 +638,39 @@ ...@@ -622,20 +638,39 @@
// XXX add failback rendering if json_document not array // XXX add failback rendering if json_document not array
// input = render_textarea(schema, default_value, "array"); // input = render_textarea(schema, default_value, "array");
return gadget.expandSchema(schema.items, schema_path + '/items', minItems !== 0) return RSVP.Queue()
.push(function (schema_arr) { .push(function () {
return RSVP.all([
expandItems(gadget, schema.items, schema_path + '/items', minItems),
gadget.expandSchema(schema.additionalItems, schema_path + '/additionalItems', false)
]);
})
.push(function (arr) {
var queue = RSVP.Queue(), var queue = RSVP.Queue(),
i, i,
schema_path_item = schema_path + '/items',
schema_arr_arr = arr[0],
additionalItems = arr[1],
schema_arr = schema_arr_arr,
len = 0; len = 0;
// XXX rewrite loading document for anyOf schema // XXX rewrite loading document for anyOf schema
if (json_document) { if (json_document) {
for (i = 0; i < json_document.length; i = i + 1) { for (i = 0; i < json_document.length; i = i + 1) {
if (is_items_arr) {
if (i < schema_arr_arr.length) {
schema_arr = schema_arr_arr[i];
schema_path_item = schema_path + '/items/' + i;
} else {
schema_arr = additionalItems;
schema_path_item = schema_path + '/additionalItems';
}
}
queue queue
.push( .push(
addSubForm.bind(gadget, { addSubForm.bind(gadget, {
gadget: gadget, gadget: gadget,
parent_type: 'array', parent_type: 'array',
schema_path: schema_path + '/items', schema_path: schema_path_item,
schema_part: schema_arr, schema_part: schema_arr,
default_dict: json_document[i], default_dict: json_document[i],
required: i < minItems required: i < minItems
...@@ -646,34 +681,78 @@ ...@@ -646,34 +681,78 @@
len = json_document.length; len = json_document.length;
} }
if (checkSchemaArrOneChoise(schema_arr) && minItems > len) { if (is_items_arr) {
for (i = 0; i < (minItems - len); i += 1) { if (minItems > len) {
queue for (i; i < (minItems - len); i += 1) {
.push( if (i < schema_arr_arr.length) {
addSubForm.bind(gadget, { schema_arr = schema_arr_arr[i];
gadget: gadget, } else {
parent_type: 'array', schema_arr = additionalItems;
schema_path: schema_arr[0].schema_path, }
schema_part: schema_arr[0].schema, if (!checkSchemaArrOneChoise(schema_arr)) {
required: true break;
}) }
) queue
.push(div_append); .push(
addSubForm.bind(gadget, {
gadget: gadget,
parent_type: 'array',
schema_path: schema_arr[0].schema_path,
schema_part: schema_arr[0].schema,
required: true
})
)
.push(div_append);
}
}
if (i < schema_arr_arr.length) {
schema_arr = schema_arr_arr[i];
} else {
schema_arr = additionalItems;
}
// XXX rerender on next item in schema.items
queue.push(render_schema_selector.bind(gadget,
gadget, "add item to array",
schema_arr, function (value) {
return addSubForm({
gadget: gadget,
parent_type: 'array',
type: value.type,
schema_path: value.schema_path,
schema_part: value.schema
})
.push(element_append);
}));
} else {
if (minItems > len && checkSchemaArrOneChoise(schema_arr)) {
for (i = 0; i < (minItems - len); i += 1) {
queue
.push(
addSubForm.bind(gadget, {
gadget: gadget,
parent_type: 'array',
schema_path: schema_arr[0].schema_path,
schema_part: schema_arr[0].schema,
required: true
})
)
.push(div_append);
}
} }
}
queue.push(render_schema_selector.bind(gadget, queue.push(render_schema_selector.bind(gadget,
gadget, "add item to array", gadget, "add item to array",
schema_arr, function (value) { schema_arr, function (value) {
return addSubForm({ return addSubForm({
gadget: gadget, gadget: gadget,
parent_type: 'array', parent_type: 'array',
type: value.type, type: value.type,
schema_path: value.schema_path, schema_path: value.schema_path,
schema_part: value.schema schema_part: value.schema
}) })
.push(element_append); .push(element_append);
})); }));
}
return queue; return queue;
}) })
.push(function (element) { .push(function (element) {
......
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