Commit ed7ec9ad authored by Boris Kocherov's avatar Boris Kocherov

draft

parent faa7d8af
...@@ -14,6 +14,28 @@ ...@@ -14,6 +14,28 @@
return _str.replace(/~/g,'~0').replace(/\//g,'~1'); return _str.replace(/~/g,'~0').replace(/\//g,'~1');
} }
function getDocumentType(doc) {
if (doc instanceof Array) {
return "array";
}
return typeof doc;
}
function getDocumentSchema(doc) {
var type = getDocumentType(doc),
schema = {
type: type
};
if (type === "array") {
schema.maxItems = 0;
} else if (type === "object") {
schema.additionalProperties = false;
} else {
schema.readOnly = true;
}
return schema;
}
function render_selection(json_field, default_value) { function render_selection(json_field, default_value) {
var input = document.createElement("select"), var input = document.createElement("select"),
option = document.createElement("option"), option = document.createElement("option"),
...@@ -58,15 +80,23 @@ ...@@ -58,15 +80,23 @@
div_input, div_input,
input, input,
item_schema, item_schema,
i; i,
if (json_field.items.type) { maxItems = json_field.maxItems;
item_schema = json_field.items; div = document.createElement("div");
div = document.createElement("div"); div.setAttribute("class", "subfield");
div.setAttribute("class", "subfield"); div.title = json_field.description;
div.title = json_field.description;
div_input = document.createElement("div"); div_input = document.createElement("div");
div_input.setAttribute("class", "input"); div_input.setAttribute("class", "input");
function element_append(child) {
if (child) {
div_input.appendChild(child);
}
}
if (maxItems === undefined || default_array.length < maxItems) {
item_schema = json_field.items;
input = document.createElement("button"); input = document.createElement("button");
input.setAttribute("class", input.setAttribute("class",
...@@ -75,35 +105,48 @@ ...@@ -75,35 +105,48 @@
input.type = "button"; input.type = "button";
input.name = path; input.name = path;
gadget.props.add_buttons.push({ gadget.props.add_buttons.push({
parent_type: 'array',
element: input, element: input,
schema_part: item_schema event: function () {
return addSubForm({
gadget: gadget,
parent_type: 'array',
element: input,
schema_part: item_schema
})
.push(element_append);
}
}); });
div_input.appendChild(input); } else {
input = document.createElement("div");
input.setAttribute("class", "input");
}
div.appendChild(div_input); div_input.appendChild(input);
if (default_array) { div.appendChild(div_input);
for (i = 0; i < default_array.length; i++) {
queue if (default_array) {
.push( for (i = 0; i < default_array.length; i++) {
addSubForm.bind(gadget, gadget, { queue
parent_type: 'array', .push(
path: path, addSubForm.bind(gadget, {
element: input, gadget: gadget,
schema_part: item_schema, parent_type: 'array',
default_dict: default_array[i] path: path,
}) element: input,
); schema_part: item_schema,
} default_dict: default_array[i]
})
)
.push(element_append);
} }
root.appendChild(div);
} }
root.appendChild(div);
// todo add failback rendering if default_array not array // todo add failback rendering if default_array not array
// input = render_textarea(json_field, default_value, "array"); // input = render_textarea(json_field, default_value, "array");
return queue; return queue;
} }
function render_field(gadget, key, path, json_field, default_value, root) { function render_field(gadget, key, path, json_field, default_value, root, editable_label) {
var div, var div,
label, label,
div_input, div_input,
...@@ -119,10 +162,18 @@ ...@@ -119,10 +162,18 @@
first_path = ""; first_path = "";
} }
if (json_field === undefined) {
json_field = getDocumentSchema(default_value);
} else if (json_field.type === undefined) {
json_field.type = getDocumentType(default_value);
}
div = document.createElement("div"); div = document.createElement("div");
div.setAttribute("class", "subfield"); div.setAttribute("class", "subfield");
div.title = json_field.description; div.title = json_field.description;
if (key && !first_path) { // if (key && !first_path) {
if (false) {
// XXX;
label = document.createElement("input"); label = document.createElement("input");
label.value = key; label.value = key;
gadget.props.property_name_edit = label; gadget.props.property_name_edit = label;
...@@ -134,23 +185,25 @@ ...@@ -134,23 +185,25 @@
div_input = document.createElement("div"); div_input = document.createElement("div");
div_input.setAttribute("class", "input"); div_input.setAttribute("class", "input");
if (json_field['enum'] !== undefined) { if (json_field.enum !== undefined) {
input = render_selection(json_field, default_value); input = render_selection(json_field, default_value);
} }
if (json_field.type === "boolean") { if (json_field.type === "boolean") {
json_field['enum'] = [true, false];
if (default_value === "true") { if (default_value === "true") {
default_value = true; default_value = true;
} }
if (default_value === "false") { if (default_value === "false") {
default_value = false; default_value = false;
} }
input = render_selection(json_field, default_value); input = render_selection({
type: "boolean",
enum: [true, false]
}, default_value);
} }
if (["string", "integer", "number"].indexOf(json_field.type) >= 0) { if (!input && ["string", "integer", "number"].indexOf(json_field.type) >= 0) {
if (json_field.textarea === true) { if (json_field.contentMediaType === "text/plain") {
input = render_textarea(json_field, default_value, "string"); input = render_textarea(json_field, default_value, "string");
} else { } else {
input = document.createElement("input"); input = document.createElement("input");
...@@ -217,10 +270,19 @@ ...@@ -217,10 +270,19 @@
} }
function render_object(g, json_field, default_dict, root, path) { function render_object(g, json_field, default_dict, root, path) {
var key, var additionalProperties,
key,
required = json_field.required || [],
used_properties = {}, used_properties = {},
unused_properties = [],
queue = RSVP.Queue(); queue = RSVP.Queue();
g.props.objects[path] = used_properties;
function root_append(child) {
root.appendChild(child);
}
function addAdditional(schema) { function addAdditional(schema) {
var div, var div,
div_input, div_input,
...@@ -233,6 +295,12 @@ ...@@ -233,6 +295,12 @@
div_input = document.createElement("div"); div_input = document.createElement("div");
div_input.setAttribute("class", "input"); div_input.setAttribute("class", "input");
function element_append(child) {
if (child) {
div_input.appendChild(child);
}
}
input = document.createElement("input"); input = document.createElement("input");
input.type = "text"; input.type = "text";
div_input.appendChild(input); div_input.appendChild(input);
...@@ -245,7 +313,14 @@ ...@@ -245,7 +313,14 @@
input.name = path; input.name = path;
g.props.add_buttons.push({ g.props.add_buttons.push({
element: input, element: input,
schema_part: schema event: function () {
return addSubForm({
gadget: g,
element: input,
schema_part: schema
})
.push(element_append);
}
}); });
div_input.appendChild(input); div_input.appendChild(input);
div.appendChild(div_input); div.appendChild(div_input);
...@@ -256,14 +331,16 @@ ...@@ -256,14 +331,16 @@
used_properties[key] = ""; used_properties[key] = "";
queue queue
.push( .push(
addSubForm.bind(g, g, { addSubForm.bind(g, {
gadget: g,
property_name: key, property_name: key,
path: path, path: path,
element: input, element: input,
schema_part: schema, schema_part: schema,
default_dict: default_dict[key] default_dict: default_dict[key]
}) })
); )
.push(element_append);
} }
} }
queue.push(function () { queue.push(function () {
...@@ -275,39 +352,69 @@ ...@@ -275,39 +352,69 @@
default_dict = {}; default_dict = {};
} }
function generate_property_selection(properties) {
var input;
if (properties.length > 0) {
input = render_selection({
enum: properties
});
}
if (input) {
root_append(input);
}
}
for (key in json_field.properties) { for (key in json_field.properties) {
if (json_field.properties.hasOwnProperty(key)) { if (json_field.properties.hasOwnProperty(key)) {
used_properties[key] = false; if (required.indexOf(key) >= 0) {
if (json_field.properties[key].default !== undefined) { used_properties[key] = false;
json_field.properties[key].info = '(default = ' + json_field.properties[key].default + ')'; if (json_field.properties[key].default !== undefined) {
json_field.properties[key].info = '(default = ' + json_field.properties[key].default + ')';
}
queue
.push(render_field.bind(g, g, key, path,
json_field.properties[key], default_dict[key], root)
);
} else if (default_dict.hasOwnProperty(key)) {
used_properties[key] = "";
queue
.push(
addSubForm.bind(g, {
gadget: g,
property_name: key,
path: path,
schema_part: json_field.properties[key],
default_dict: default_dict[key]
})
)
.push(root_append);
} else {
unused_properties.push(key);
} }
queue
.push(render_field.bind(g, g, key, path,
json_field.properties[key], default_dict[key], root)
);
} }
} }
generate_property_selection(unused_properties);
if (json_field.patternProperties !== undefined) { if (json_field.patternProperties !== undefined) {
if (json_field.patternProperties['.*'] !== undefined) { if (json_field.patternProperties['.*'] !== undefined) {
addAdditional(json_field.patternProperties['.*']); addAdditional(json_field.patternProperties['.*']);
} }
g.props.objects[path] = used_properties;
} }
if (json_field.additionalProperties !== undefined && json_field.additionalProperties.type) { if (json_field.additionalProperties === undefined) {
addAdditional(json_field.additionalProperties); additionalProperties = true;
g.props.objects[path] = used_properties; } else {
additionalProperties = json_field.additionalProperties;
}
if (additionalProperties.type) {
addAdditional(additionalProperties);
} }
for (key in default_dict) { for (key in default_dict) {
if (default_dict.hasOwnProperty(key)) { if (default_dict.hasOwnProperty(key)) {
if (!used_properties.hasOwnProperty(key)) { if (!used_properties.hasOwnProperty(key)) {
queue queue
.push(render_field.bind(g, g, key, path, { .push(render_field.bind(g, g, key, path, undefined, default_dict[key], root));
"type": "string",
"info": "(Not part of the schema)"
}, default_dict[key], root)
);
} }
} }
} }
...@@ -437,8 +544,9 @@ ...@@ -437,8 +544,9 @@
}); });
} }
function addSubForm(g, options) { function addSubForm(options) {
var element = options.element, var element = options.element,
g = options.gadget,
property_name, property_name,
parent_path = options.path || element.name, parent_path = options.path || element.name,
scope, scope,
...@@ -454,12 +562,12 @@ ...@@ -454,12 +562,12 @@
if (!property_name) { if (!property_name) {
// TODO notify user // TODO notify user
// you can't create property without property_name // you can't create property without property_name
return false; return RSVP.Queue();
} }
if (g.props.objects[parent_path].hasOwnProperty(property_name) && g.props.objects[parent_path][property_name] !== "") { if (g.props.objects[parent_path].hasOwnProperty(property_name) && g.props.objects[parent_path][property_name] !== "") {
// TODO notify user // TODO notify user
// you can't create property with existed property_name // you can't create property with existed property_name
return false; return RSVP.Queue();
} }
if (input_element) { if (input_element) {
input_element.value = ""; input_element.value = "";
...@@ -476,7 +584,7 @@ ...@@ -476,7 +584,7 @@
form_gadget.element.setAttribute("data-json-property-name", property_name); form_gadget.element.setAttribute("data-json-property-name", property_name);
} }
// add to end of list // add to end of list
element.parentNode.appendChild(form_gadget.element); // element.parentNode.appendChild(form_gadget.element);
return form_gadget.renderForm({ return form_gadget.renderForm({
schema: options.schema_part, schema: options.schema_part,
document: options.default_dict, document: options.default_dict,
...@@ -651,9 +759,6 @@ ...@@ -651,9 +759,6 @@
if (!property_name || !options.display_label) { if (!property_name || !options.display_label) {
property_name = ""; property_name = "";
} }
if (schema.type === undefined) {
schema.type = "object";
}
root = g.element.querySelector('[data-json-path="/"]'); root = g.element.querySelector('[data-json-path="/"]');
if (!root) { if (!root) {
root = g.element; root = g.element;
...@@ -749,7 +854,7 @@ ...@@ -749,7 +854,7 @@
button_list[i].element, button_list[i].element,
'click', 'click',
false, false,
addSubForm.bind(g, g, button_list[i]) button_list[i].event
)); ));
} }
......
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