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 @@ ...@@ -337,119 +337,161 @@
}); });
} }
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 () {
schema_item, var type_arr = [],
description, schema_item,
i, description,
z, i,
type, z,
schema_path; type;
if (schema_arr.length === 1 && schema_arr[0].schema === true) { function generateItemsForAny(property_name, schema_path) {
type_arr = [ var desc,
"string", types = [
"number", "string",
"boolean", "number",
"array", "boolean",
"object", "array",
"null" "object",
]; "null"
schema_path = schema_arr[0].schema_path; ],
} else { ii;
for (i = 0; i < schema_arr.length; i += 1) { if (property_name) {
schema_item = schema_arr[i]; desc = property_name + " # ";
description = schema_item.title; } else {
if (getDocumentType(schema_item.schema.type) === "array") { desc = "";
description = description || schema_item.schema.description; }
for (z = 0; z < schema_item.schema.type.length; z += 1) { for (ii = 0; ii < types.length; ii += 1) {
type = schema_item.schema.type[z];
type_arr.push({ 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: { value: {
type: type,
property_name: schema_item.property_name, property_name: schema_item.property_name,
schema_path: schema_item.schema_path, schema_path: schema_item.schema_path,
schema: schema_item.schema 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
}
});
} }
} return type_arr;
} })
.push(function (schema_alternatives) {
if (type_arr.length > 1) { var scope = Math.random().toString(36).substr(2, 9);
return gadget.declareGadget("gadget_html5_select.html", {scope: scope}) if (schema_alternatives.length > 1) {
.push(function (g) { return gadget.declareGadget("gadget_html5_select.html", {scope: scope})
var x, .push(function (g) {
item_list = [[title, title]], return RSVP.Queue()
render_options, .push(function () {
item, var x,
jsonify = type_arr[0].title; item_list = [[title, title]],
for (x = 0; x < type_arr.length; x += 1) { item;
item = type_arr[x]; if (rerender) {
if (jsonify) { return rerender(g, schema_alternatives);
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 event({ for (x = 0; x < schema_alternatives.length; x += 1) {
schema_path: schema_path, item = schema_alternatives[x];
schema: { type: value[scope] } // 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); return g.render(render_options);
})
//not need if gadget_html5_select.render return element
.push(function () {
return g.element;
}); });
} });
}; }
return g.render(render_options) if (schema_alternatives.length === 1) {
//not need if gadget_html5_select.render return element return RSVP.Queue()
.push(function () { .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()
return RSVP.Queue() .push(function () {
.push(function () { return document.createElement("div");
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;
}); });
} }
...@@ -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