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

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

parent 2a7ca697
...@@ -337,30 +337,48 @@ ...@@ -337,30 +337,48 @@
}); });
} }
function render_select_type(gadget, title, schema_arr, event) { function render_select_type(gadget, title, schema_arr, event, rerender) {
var scope = Math.random().toString(36).substr(2, 9), return RSVP.Queue()
type_arr = [], .push(function () {
var type_arr = [],
schema_item, schema_item,
description, description,
i, i,
z, z,
type, type;
schema_path; function generateItemsForAny(property_name, schema_path) {
if (schema_arr.length === 1 && schema_arr[0].schema === true) { var desc,
type_arr = [ types = [
"string", "string",
"number", "number",
"boolean", "boolean",
"array", "array",
"object", "object",
"null" "null"
]; ],
schema_path = schema_arr[0].schema_path; ii;
if (property_name) {
desc = property_name + " # ";
} else { } else {
desc = "";
}
for (ii = 0; ii < types.length; ii += 1) {
type_arr.push({
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) { for (i = 0; i < schema_arr.length; i += 1) {
schema_item = schema_arr[i]; schema_item = schema_arr[i];
description = schema_item.title; description = schema_item.title;
if (getDocumentType(schema_item.schema.type) === "array") { 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; description = description || schema_item.schema.description;
for (z = 0; z < schema_item.schema.type.length; z += 1) { for (z = 0; z < schema_item.schema.type.length; z += 1) {
type = schema_item.schema.type[z]; type = schema_item.schema.type[z];
...@@ -388,56 +406,74 @@ ...@@ -388,56 +406,74 @@
}); });
} }
} }
} return type_arr;
})
if (type_arr.length > 1) { .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}) return gadget.declareGadget("gadget_html5_select.html", {scope: scope})
.push(function (g) { .push(function (g) {
return RSVP.Queue()
.push(function () {
var x, var x,
item_list = [[title, title]], item_list = [[title, title]],
render_options, item;
item, if (rerender) {
jsonify = type_arr[0].title; return rerender(g, schema_alternatives);
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]);
} }
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)]);
} }
render_options = { return {
name: scope, name: scope,
editable: true, editable: true,
hidden: item_list.length <= 1, hidden: item_list.length === 0,
value: item_list[0][1], value: item_list[0][1],
item_list: item_list item_list: item_list
}; };
})
.push(function (render_options) {
gadget.props.add_custom_data[scope] = { gadget.props.add_custom_data[scope] = {
element: g.element, element: g.element,
event: function () { event: function () {
return g.getContent() return g.getContent()
.push(function (value) { .push(function (value) {
if (jsonify) {
return event(JSON.parse(value[scope])); return event(JSON.parse(value[scope]));
})
.push(function () {
if (rerender) {
return rerender(g, schema_alternatives);
} }
return event({ return render_options;
schema_path: schema_path,
schema: { type: value[scope] }
});
}) })
.push(function (render_options) {
return g.render(render_options);
});
},
rerender: function () {
return RSVP.Queue()
.push(function () { .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);
}); });
} }
}; };
return g.render(render_options) return g.render(render_options);
})
//not need if gadget_html5_select.render return element //not need if gadget_html5_select.render return element
.push(function () { .push(function () {
return g.element; return g.element;
}); });
}); });
} }
if (schema_alternatives.length === 1) {
return RSVP.Queue() return RSVP.Queue()
.push(function () { .push(function () {
var input = document.createElement("button"); var input = document.createElement("button");
...@@ -447,11 +483,17 @@ ...@@ -447,11 +483,17 @@
input.type = "button"; input.type = "button";
gadget.props.add_buttons.push({ gadget.props.add_buttons.push({
element: input, element: input,
event: event.bind(gadget, schema_arr[0]) event: event.bind(gadget, schema_alternatives[0].value)
}); });
return input; return input;
}); });
} }
return RSVP.Queue()
.push(function () {
return document.createElement("div");
});
});
}
function render_array(gadget, json_field, default_array, root, path, schema_path) { function render_array(gadget, json_field, default_array, root, path, schema_path) {
var queue = RSVP.Queue(), var queue = RSVP.Queue(),
...@@ -669,89 +711,10 @@ ...@@ -669,89 +711,10 @@
var additionalProperties, var additionalProperties,
key, key,
required = json_field.required || [], required = json_field.required || [],
properties = json_field.properties,
used_properties = {}, used_properties = {},
queue = RSVP.Queue(), queue = RSVP.Queue(),
scope = "property_add_" + Math.random().toString(36).substr(2, 9), selector = {};
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;
});
});
}
};
g.props.add_property_selections[scope] = selector;
g.props.objects[path] = used_properties; g.props.objects[path] = used_properties;
function root_append(child) { function root_append(child) {
...@@ -861,9 +824,47 @@ ...@@ -861,9 +824,47 @@
queue queue
.push(function () { .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) { .push(function (element) {
selector.element = element; selector.element = element;
return root_append(element); return root_append(element);
...@@ -1094,11 +1095,9 @@ ...@@ -1094,11 +1095,9 @@
} }
} }
element.parentNode.removeChild(element); element.parentNode.removeChild(element);
for (key in g.props.add_property_selections) { for (key in g.props.add_custom_data) {
if (g.props.add_property_selections.hasOwnProperty(key)) { if (g.props.add_custom_data.hasOwnProperty(key)) {
tasks.push(g.props tasks.push(g.props.add_custom_data[key].rerender());
.add_property_selections[key]
.rerender());
} }
} }
return RSVP.All(tasks); return RSVP.All(tasks);
...@@ -1180,8 +1179,7 @@ ...@@ -1180,8 +1179,7 @@
var g = this, var g = this,
opt = arr[0], opt = arr[0],
event_object; event_object;
event_object = g.props.add_property_selections[sub_scope]; event_object = g.props.add_custom_data[sub_scope];
event_object = event_object || g.props.add_custom_data[sub_scope];
if (opt.type === "change" && event_object) { if (opt.type === "change" && event_object) {
return event_object.event(); return event_object.event();
} }
...@@ -1195,7 +1193,6 @@ ...@@ -1195,7 +1193,6 @@
g.props.inputs = []; g.props.inputs = [];
g.props.add_buttons = []; g.props.add_buttons = [];
g.props.add_custom_data = {}; g.props.add_custom_data = {};
g.props.add_property_selections = {};
g.props.arrays = {}; g.props.arrays = {};
g.props.objects = {}; g.props.objects = {};
g.props.path = options.path; // self gadget scope 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