Commit 2e2a05eb authored by Boris Kocherov's avatar Boris Kocherov

use one code for array.element schema selection and object.property

parent 2a7ca697
......@@ -337,119 +337,161 @@
});
}
function render_select_type(gadget, title, schema_arr, event) {
var scope = Math.random().toString(36).substr(2, 9),
type_arr = [],
schema_item,
description,
i,
z,
type,
schema_path;
if (schema_arr.length === 1 && schema_arr[0].schema === true) {
type_arr = [
"string",
"number",
"boolean",
"array",
"object",
"null"
];
schema_path = schema_arr[0].schema_path;
} else {
for (i = 0; i < schema_arr.length; i += 1) {
schema_item = schema_arr[i];
description = schema_item.title;
if (getDocumentType(schema_item.schema.type) === "array") {
description = description || schema_item.schema.description;
for (z = 0; z < schema_item.schema.type.length; z += 1) {
type = schema_item.schema.type[z];
function render_select_type(gadget, title, schema_arr, event, rerender) {
return RSVP.Queue()
.push(function () {
var type_arr = [],
schema_item,
description,
i,
z,
type;
function generateItemsForAny(property_name, schema_path) {
var desc,
types = [
"string",
"number",
"boolean",
"array",
"object",
"null"
],
ii;
if (property_name) {
desc = property_name + " # ";
} else {
desc = "";
}
for (ii = 0; ii < types.length; ii += 1) {
type_arr.push({
title: description + ' # ' + type,
title: desc + types[ii],
value: {
property_name: property_name,
schema: { type: types[ii] },
schema_path: schema_path
}
});
}
}
for (i = 0; i < schema_arr.length; i += 1) {
schema_item = schema_arr[i];
description = schema_item.title;
if (schema_item.schema === true) {
generateItemsForAny(schema_item.property_name, schema_item.schema_path);
} else if (getDocumentType(schema_item.schema.type) === "array") {
description = description || schema_item.schema.description;
for (z = 0; z < schema_item.schema.type.length; z += 1) {
type = schema_item.schema.type[z];
type_arr.push({
title: description + ' # ' + type,
value: {
type: type,
property_name: schema_item.property_name,
schema_path: schema_item.schema_path,
schema: schema_item.schema
}
});
}
} else {
description = description ||
schema_item.schema.type ||
schema_item.schema.description;
type_arr.push({
title: description,
value: {
type: type,
property_name: schema_item.property_name,
schema_path: schema_item.schema_path,
schema: schema_item.schema
}
});
}
} else {
description = description ||
schema_item.schema.type ||
schema_item.schema.description;
type_arr.push({
title: description,
value: {
property_name: schema_item.property_name,
schema_path: schema_item.schema_path,
schema: schema_item.schema
}
});
}
}
}
if (type_arr.length > 1) {
return gadget.declareGadget("gadget_html5_select.html", {scope: scope})
.push(function (g) {
var x,
item_list = [[title, title]],
render_options,
item,
jsonify = type_arr[0].title;
for (x = 0; x < type_arr.length; x += 1) {
item = type_arr[x];
if (jsonify) {
item_list.push([item.title, JSON.stringify(item.value)]);
} else {
item_list.push([item, item]);
}
}
render_options = {
name: scope,
editable: true,
hidden: item_list.length <= 1,
value: item_list[0][1],
item_list: item_list
};
gadget.props.add_custom_data[scope] = {
element: g.element,
event: function () {
return g.getContent()
.push(function (value) {
if (jsonify) {
return event(JSON.parse(value[scope]));
return type_arr;
})
.push(function (schema_alternatives) {
var scope = Math.random().toString(36).substr(2, 9);
if (schema_alternatives.length > 1) {
return gadget.declareGadget("gadget_html5_select.html", {scope: scope})
.push(function (g) {
return RSVP.Queue()
.push(function () {
var x,
item_list = [[title, title]],
item;
if (rerender) {
return rerender(g, schema_alternatives);
}
return event({
schema_path: schema_path,
schema: { type: value[scope] }
});
for (x = 0; x < schema_alternatives.length; x += 1) {
item = schema_alternatives[x];
// need speedup by using array index
item_list.push([item.title, JSON.stringify(item.value)]);
}
return {
name: scope,
editable: true,
hidden: item_list.length === 0,
value: item_list[0][1],
item_list: item_list
};
})
.push(function () {
.push(function (render_options) {
gadget.props.add_custom_data[scope] = {
element: g.element,
event: function () {
return g.getContent()
.push(function (value) {
return event(JSON.parse(value[scope]));
})
.push(function () {
if (rerender) {
return rerender(g, schema_alternatives);
}
return render_options;
})
.push(function (render_options) {
return g.render(render_options);
});
},
rerender: function () {
return RSVP.Queue()
.push(function () {
if (rerender) {
return rerender(g, schema_alternatives);
}
return render_options;
})
.push(function (render_options) {
return g.render(render_options);
});
}
};
return g.render(render_options);
})
//not need if gadget_html5_select.render return element
.push(function () {
return g.element;
});
}
};
return g.render(render_options)
//not need if gadget_html5_select.render return element
});
}
if (schema_alternatives.length === 1) {
return RSVP.Queue()
.push(function () {
return g.element;
var input = document.createElement("button");
input.setAttribute("class",
"ui-shadow-inset ui-btn ui-btn-inline ui-corner-all" +
" ui-btn-icon-notext ui-icon-btn ui-icon-plus ui-input-btn");
input.type = "button";
gadget.props.add_buttons.push({
element: input,
event: event.bind(gadget, schema_alternatives[0].value)
});
return input;
});
});
}
return RSVP.Queue()
.push(function () {
var input = document.createElement("button");
input.setAttribute("class",
"ui-shadow-inset ui-btn ui-btn-inline ui-corner-all" +
" ui-btn-icon-notext ui-icon-btn ui-icon-plus ui-input-btn");
input.type = "button";
gadget.props.add_buttons.push({
element: input,
event: event.bind(gadget, schema_arr[0])
});
return input;
}
return RSVP.Queue()
.push(function () {
return document.createElement("div");
});
});
}
......@@ -669,89 +711,10 @@
var additionalProperties,
key,
required = json_field.required || [],
properties = json_field.properties,
used_properties = {},
queue = RSVP.Queue(),
scope = "property_add_" + Math.random().toString(36).substr(2, 9),
selector = {
event: function () {
return g.getDeclaredGadget(scope)
.push(function (z) {
return z.getContent()
.push(function (value) {
var property_name_array = value[scope].split('/'),
property_name,
schema,
schema_path_local,
anyOfidx = property_name_array[1];
property_name = decodeJsonPointer(property_name_array[0]);
schema_path_local = schema_path + '/properties/' +
encodeJsonPointer(property_name);
if (property_name_array.length > 1) {
schema = json_field.properties[property_name].anyOf[anyOfidx];
schema_path_local += '/' + anyOfidx;
} else {
schema = json_field.properties[property_name];
}
used_properties[property_name] = "";
return addSubForm({
gadget: g,
property_name: property_name,
path: path,
schema_path: schema_path_local,
schema_part: schema
});
})
.push(function (element) {
z.element.parentNode.insertBefore(element, z.element);
});
})
.push(selector.rerender);
},
rerender: function () {
return g.getDeclaredGadget(scope)
.push(function (g) {
var property_name,
i,
anyOf,
description,
item_list = [["add property", "add property"]];
for (property_name in properties) {
if (properties.hasOwnProperty(property_name) &&
!used_properties.hasOwnProperty(property_name)) {
anyOf = properties[property_name].anyOf;
if (anyOf) {
for (i = 0; i < anyOf.length; i += 1) {
description = anyOf[i].$ref ||
anyOf[i].title ||
anyOf[i].type ||
anyOf[i].description;
item_list.push([
property_name + '/' + description,
encodeJsonPointer(property_name) + '/' + i.toString()
]);
}
} else {
item_list.push([property_name, property_name]);
}
}
}
return g.render({
name: scope,
editable: true,
hidden: item_list.length <= 1,
value: item_list[0][1],
item_list: item_list
})
//not need if gadget_html5_select.render return element
.push(function () {
return g.element;
});
});
}
};
selector = {};
g.props.add_property_selections[scope] = selector;
g.props.objects[path] = used_properties;
function root_append(child) {
......@@ -861,9 +824,47 @@
queue
.push(function () {
return g.declareGadget("gadget_html5_select.html", {scope: scope});
return expandProperties(g, json_field.properties, schema_path + '/properties/');
})
.push(function (schema_arr) {
return render_select_type(g, "add property", schema_arr, function (value) {
used_properties[value.property_name] = "";
return addSubForm({
gadget: g,
property_name: value.property_name,
path: path,
type: value.type,
schema_path: value.schema_path,
schema_part: value.schema
})
.push(function (element) {
var s_e = selector.element;
if (s_e) {
s_e.parentNode.insertBefore(element, s_e);
}
});
},
function (gadget_s, schema_alternatives) {
var x,
item_list = [["add property", "add property"]],
item;
if (schema_alternatives) {
for (x = 0; x < schema_alternatives.length; x += 1) {
item = schema_alternatives[x];
if (!used_properties.hasOwnProperty(item.value.property_name)) {
item_list.push([item.title, JSON.stringify(item.value)]);
}
}
return {
name: gadget_s.element.getAttribute('data-gadget-scope'),
editable: true,
hidden: item_list.length === 0,
value: item_list[0][1],
item_list: item_list
};
}
});
})
.push(selector.rerender)
.push(function (element) {
selector.element = element;
return root_append(element);
......@@ -1094,11 +1095,9 @@
}
}
element.parentNode.removeChild(element);
for (key in g.props.add_property_selections) {
if (g.props.add_property_selections.hasOwnProperty(key)) {
tasks.push(g.props
.add_property_selections[key]
.rerender());
for (key in g.props.add_custom_data) {
if (g.props.add_custom_data.hasOwnProperty(key)) {
tasks.push(g.props.add_custom_data[key].rerender());
}
}
return RSVP.All(tasks);
......@@ -1180,8 +1179,7 @@
var g = this,
opt = arr[0],
event_object;
event_object = g.props.add_property_selections[sub_scope];
event_object = event_object || g.props.add_custom_data[sub_scope];
event_object = g.props.add_custom_data[sub_scope];
if (opt.type === "change" && event_object) {
return event_object.event();
}
......@@ -1195,7 +1193,6 @@
g.props.inputs = [];
g.props.add_buttons = [];
g.props.add_custom_data = {};
g.props.add_property_selections = {};
g.props.arrays = {};
g.props.objects = {};
g.props.path = options.path; // self gadget scope
......
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