Commit 23f03d0a authored by Boris Kocherov's avatar Boris Kocherov

make code better understandable

default_dict, default_value renamed to json_document
json_field renamed to schema
parent d6a7ebde
...@@ -275,7 +275,7 @@ ...@@ -275,7 +275,7 @@
delete_button: options.delete_button, delete_button: options.delete_button,
schema: options.schema_part, schema: options.schema_part,
schema_path: options.schema_path, schema_path: options.schema_path,
document: options.default_dict, document: options.json_document,
display_label: options.parent_type !== "array", display_label: options.parent_type !== "array",
saveOrigValue: g.props.saveOrigValue, saveOrigValue: g.props.saveOrigValue,
scope: scope scope: scope
...@@ -682,7 +682,7 @@ ...@@ -682,7 +682,7 @@
parent_type: 'array', parent_type: 'array',
schema_path: schema_path_item, schema_path: schema_path_item,
schema_part: schema_arr, schema_part: schema_arr,
default_dict: json_document[i], json_document: json_document[i],
required: i < minItems required: i < minItems
}) })
) )
...@@ -775,7 +775,7 @@ ...@@ -775,7 +775,7 @@
}); });
} }
function render_field(gadget, key, path, json_field, default_value, root, schema_path, options) { function render_field(gadget, key, path, schema, json_document, root, schema_path, options) {
var type, var type,
div, div,
delete_button, delete_button,
...@@ -789,10 +789,10 @@ ...@@ -789,10 +789,10 @@
type_changed, type_changed,
queue = RSVP.Queue(); queue = RSVP.Queue();
if (json_field instanceof Array) { if (schema instanceof Array) {
json_field = schemaArrFilteredByDocument(json_field, default_value); schema = schemaArrFilteredByDocument(schema, json_document);
schema_path = json_field.schema_path; schema_path = schema.schema_path;
json_field = json_field.schema; schema = schema.schema;
} }
options = options || {}; options = options || {};
...@@ -804,44 +804,44 @@ ...@@ -804,44 +804,44 @@
first_path = ""; first_path = "";
} }
if (json_field === undefined) { if (schema === undefined) {
json_field = getDocumentSchema(default_value); schema = getDocumentSchema(json_document);
} }
if (getDocumentType(json_field.type) === "string") { if (getDocumentType(schema.type) === "string") {
type = json_field.type; type = schema.type;
} else if (type === undefined && } else if (type === undefined &&
default_value === undefined && json_document === undefined &&
getDocumentType(json_field.type) === "array") { getDocumentType(schema.type) === "array") {
type = json_field.type[0]; type = schema.type[0];
} }
if (["object", "array"].indexOf(type) >= 0 && if (["object", "array"].indexOf(type) >= 0 &&
!(path !== "" && default_value === undefined) && !(path !== "" && json_document === undefined) &&
getDocumentType(default_value) !== type) { getDocumentType(json_document) !== type) {
if (gadget.props.saveOrigValue) { if (gadget.props.saveOrigValue) {
// XXX is not useful for user // XXX is not useful for user
// only for tests // only for tests
json_field = { schema = {
const: default_value const: json_document
}; };
} else { } else {
gadget.props.changed = true; gadget.props.changed = true;
} }
} }
if (type === undefined && default_value !== undefined) { if (type === undefined && json_document !== undefined) {
type = getDocumentType(default_value); type = getDocumentType(json_document);
} }
if (typeof type === "string") { if (typeof type === "string") {
// it's only for simple types so we not use // it's only for simple types so we not use
// complex type detection // complex type detection
type_changed = default_value !== undefined && type_changed = json_document !== undefined &&
typeof default_value !== type; typeof json_document !== type;
} }
div = document.createElement("div"); div = document.createElement("div");
div.setAttribute("class", "jsonformfield ui-field-contain"); div.setAttribute("class", "jsonformfield ui-field-contain");
div.title = json_field.description; div.title = schema.description;
// if (key && !first_path) { // if (key && !first_path) {
if (options.delete_button === true) { if (options.delete_button === true) {
delete_button = createElement("span", delete_button = createElement("span",
...@@ -862,7 +862,7 @@ ...@@ -862,7 +862,7 @@
} }
} }
label_text = [key, json_field.title] label_text = [key, schema.title]
.filter(function (v) { return v; }) .filter(function (v) { return v; })
.join(" ") .join(" ")
// use non-breaking hyphen // use non-breaking hyphen
...@@ -884,49 +884,49 @@ ...@@ -884,49 +884,49 @@
div_input.setAttribute("id", gadget.element.getAttribute("data-gadget-scope") + first_path + '/'); div_input.setAttribute("id", gadget.element.getAttribute("data-gadget-scope") + first_path + '/');
div_input.setAttribute("class", "input"); div_input.setAttribute("class", "input");
if (json_field.const !== undefined) { if (schema.const !== undefined) {
input = render_const(gadget, json_field, default_value); input = render_const(gadget, schema, json_document);
} else if (json_field.enum !== undefined) { } else if (schema.enum !== undefined) {
input = render_enum(gadget, json_field, default_value); input = render_enum(gadget, schema, json_document);
// XXX take in account existing type with enum // XXX take in account existing type with enum
type_changed = false; type_changed = false;
} }
if (!input && type === "null") { if (!input && type === "null") {
input = render_const(gadget, {const: null}, default_value); input = render_const(gadget, {const: null}, json_document);
} }
if (!input && type === "boolean") { if (!input && type === "boolean") {
input = render_boolean(gadget, json_field, default_value); input = render_boolean(gadget, schema, json_document);
} }
if (!input && ["string", "integer", "number", "null"].indexOf(type) >= 0) { if (!input && ["string", "integer", "number", "null"].indexOf(type) >= 0) {
if (json_field.contentMediaType === "text/plain") { if (schema.contentMediaType === "text/plain") {
input = render_textarea(default_value, "string"); input = render_textarea(json_document, "string");
} else { } else {
input = document.createElement("input"); input = document.createElement("input");
if (default_value !== undefined) { if (json_document !== undefined) {
if (typeof default_value === "object") { if (typeof json_document === "object") {
input.value = JSON.stringify(default_value); input.value = JSON.stringify(json_document);
} else { } else {
input.value = default_value; input.value = json_document;
} }
} }
if (type === "integer" || type === "number") { if (type === "integer" || type === "number") {
if (default_value === undefined && typeof json_field.default === "number") { if (json_document === undefined && typeof schema.default === "number") {
input.value = json_field.default; input.value = schema.default;
gadget.props.changed = true; gadget.props.changed = true;
} }
input.setAttribute("data-json-type", type); input.setAttribute("data-json-type", type);
if (default_value === undefined || default_value === null || if (json_document === undefined || json_document === null ||
typeof default_value === "number") { typeof json_document === "number") {
input.type = "number"; input.type = "number";
} }
if (type === "integer") { if (type === "integer") {
input.setAttribute("step", "1"); input.setAttribute("step", "1");
if (typeof default_value === "number" && if (typeof json_document === "number" &&
parseInt(default_value, 10) !== default_value) { parseInt(json_document, 10) !== json_document) {
// original json_document contain float schema // original json_document contain float schema
// limit integer we can save original document // limit integer we can save original document
type_changed = true; type_changed = true;
...@@ -935,36 +935,36 @@ ...@@ -935,36 +935,36 @@
if (type === "number") { if (type === "number") {
input.setAttribute("step", "any"); input.setAttribute("step", "any");
} }
if (json_field.multipleOf && json_field.multipleOf >= 0) { if (schema.multipleOf && schema.multipleOf >= 0) {
input.step = json_field.multipleOf; input.step = schema.multipleOf;
} }
if (json_field.minimum && if (schema.minimum &&
// step work from min value so we can't // step work from min value so we can't
// use min if min not multipleOf step // use min if min not multipleOf step
!(json_field.multipleOf && !(schema.multipleOf &&
(json_field.minimum % json_field.multipleOf) !== 0)) { (schema.minimum % schema.multipleOf) !== 0)) {
input.min = json_field.minimum; input.min = schema.minimum;
} }
if (json_field.maximum) { if (schema.maximum) {
input.max = json_field.maximum; input.max = schema.maximum;
} }
} else { } else {
if (default_value === undefined && typeof json_field.default === "string") { if (json_document === undefined && typeof schema.default === "string") {
input.value = json_field.default; input.value = schema.default;
gadget.props.changed = true; gadget.props.changed = true;
} }
input.type = "text"; input.type = "text";
if (json_field.pattern) { if (schema.pattern) {
input.pattern = json_field.pattern; input.pattern = schema.pattern;
} else if (json_field.minLength) { } else if (schema.minLength) {
// minLength absent in html5 so // minLength absent in html5 so
// use pattern for this task // use pattern for this task
input.pattern = ".{" + json_field.minLength + ",}"; input.pattern = ".{" + schema.minLength + ",}";
} }
if (json_field.maxLength) { if (schema.maxLength) {
input.maxLength = json_field.maxLength; input.maxLength = schema.maxLength;
} }
if (json_field.format === 'uri') { if (schema.format === 'uri') {
input.type = "url"; input.type = "url";
input.spellcheck = false; input.spellcheck = false;
} }
...@@ -975,8 +975,8 @@ ...@@ -975,8 +975,8 @@
if (!input && type === "array") { if (!input && type === "array") {
queue = render_array( queue = render_array(
gadget, gadget,
json_field, schema,
default_value, json_document,
div_input, div_input,
first_path + '/', first_path + '/',
schema_path schema_path
...@@ -988,8 +988,8 @@ ...@@ -988,8 +988,8 @@
.push(function () { .push(function () {
return render_object( return render_object(
gadget, gadget,
json_field, schema,
default_value, json_document,
div_input, div_input,
first_path + '/', first_path + '/',
schema_path schema_path
...@@ -1004,7 +1004,7 @@ ...@@ -1004,7 +1004,7 @@
input.name = first_path; input.name = first_path;
input.required = options.required; input.required = options.required;
if (type_changed) { if (type_changed) {
input.setAttribute('data-origin-value', JSON.stringify(default_value)); input.setAttribute('data-origin-value', JSON.stringify(json_document));
} }
// XXX for gui // XXX for gui
//input.setAttribute("class", "slapos-parameter"); //input.setAttribute("class", "slapos-parameter");
...@@ -1014,9 +1014,9 @@ ...@@ -1014,9 +1014,9 @@
div.setAttribute("data-json-type", type); div.setAttribute("data-json-type", type);
} }
if (json_field.info !== undefined) { if (schema.info !== undefined) {
span_info = document.createElement("span"); span_info = document.createElement("span");
span_info.textContent = json_field.info; span_info.textContent = schema.info;
div_input.appendChild(span_info); div_input.appendChild(span_info);
} }
error_message = document.createElement("span"); error_message = document.createElement("span");
...@@ -1039,7 +1039,7 @@ ...@@ -1039,7 +1039,7 @@
div = document.createElement("div"); div = document.createElement("div");
div.setAttribute("class", "jsonformfield"); div.setAttribute("class", "jsonformfield");
// div.title = json_field.description; // div.title = schema.description;
div_input = document.createElement("div"); div_input = document.createElement("div");
div_input.setAttribute("class", "input"); div_input.setAttribute("class", "input");
...@@ -1064,7 +1064,7 @@ ...@@ -1064,7 +1064,7 @@
path: path, path: path,
schema_path: schema_path, schema_path: schema_path,
schema_part: schema_arr, schema_part: schema_arr,
default_dict: json_document[property_name] json_document: json_document[property_name]
}) })
) )
.push(element_append); .push(element_append);
...@@ -1212,9 +1212,9 @@ ...@@ -1212,9 +1212,9 @@
return true; return true;
} }
render_object = function (g, json_field, default_dict, root, path, schema_path) { render_object = function (g, schema, json_document, root, path, schema_path) {
var required = json_field.required || [], var required = schema.required || [],
schema_editor = checkSchemaIsMetaSchema(json_field), schema_editor = checkSchemaIsMetaSchema(schema),
used_properties = {}, used_properties = {},
properties, properties,
selector = {}; selector = {};
...@@ -1232,16 +1232,16 @@ ...@@ -1232,16 +1232,16 @@
root.appendChild(child); root.appendChild(child);
} }
if (default_dict === undefined) { if (json_document === undefined) {
if (json_field.hasOwnProperty('default')) { if (schema.hasOwnProperty('default')) {
default_dict = json_field.default; json_document = schema.default;
g.props.changed = true; g.props.changed = true;
} else { } else {
default_dict = {}; json_document = {};
} }
} }
return expandProperties(g, json_field.properties, schema_path + '/properties/', required) return expandProperties(g, schema.properties, schema_path + '/properties/', required)
.push(function (ret) { .push(function (ret) {
var schema_arr, var schema_arr,
q = RSVP.Queue(), q = RSVP.Queue(),
...@@ -1251,13 +1251,13 @@ ...@@ -1251,13 +1251,13 @@
for (key in properties) { for (key in properties) {
if (properties.hasOwnProperty(key)) { if (properties.hasOwnProperty(key)) {
schema_arr = properties[key]; schema_arr = properties[key];
s_o = schemaArrFilteredByDocument(schema_arr, default_dict[key]); s_o = schemaArrFilteredByDocument(schema_arr, json_document[key]);
// XXX need schema merge with patternProperties passed key // XXX need schema merge with patternProperties passed key
if (checkSchemaArrOneChoise(schema_arr)) { if (checkSchemaArrOneChoise(schema_arr)) {
if (required.indexOf(key) >= 0) { if (required.indexOf(key) >= 0) {
used_properties[key] = false; used_properties[key] = false;
q.push(render_field.bind(g, g, key, path, q.push(render_field.bind(g, g, key, path,
s_o.schema, default_dict[key], root, s_o.schema_path, {required: true}) s_o.schema, json_document[key], root, s_o.schema_path, {required: true})
); );
} }
if (!used_properties.hasOwnProperty(key) && if (!used_properties.hasOwnProperty(key) &&
...@@ -1266,14 +1266,14 @@ ...@@ -1266,14 +1266,14 @@
) { ) {
used_properties[key] = false; used_properties[key] = false;
q.push(render_field.bind(g, g, key, path, q.push(render_field.bind(g, g, key, path,
s_o.schema, default_dict[key], root, s_o.schema_path, { s_o.schema, json_document[key], root, s_o.schema_path, {
required: false, required: false,
delete_button: false delete_button: false
})); }));
} }
} }
if (!used_properties.hasOwnProperty(key) && if (!used_properties.hasOwnProperty(key) &&
default_dict.hasOwnProperty(key)) { json_document.hasOwnProperty(key)) {
used_properties[key] = ""; used_properties[key] = "";
q.push( q.push(
addSubForm.bind(g, { addSubForm.bind(g, {
...@@ -1282,7 +1282,7 @@ ...@@ -1282,7 +1282,7 @@
path: path, path: path,
schema_path: s_o.schema_path, schema_path: s_o.schema_path,
schema_part: s_o.schema, schema_part: s_o.schema,
default_dict: default_dict[key] json_document: json_document[key]
}) })
) )
.push(root_append); .push(root_append);
...@@ -1348,9 +1348,9 @@ ...@@ -1348,9 +1348,9 @@
// XXX for pattern properties needs schemas merge for // XXX for pattern properties needs schemas merge for
// all passed patterns // all passed patterns
if (json_field.patternProperties !== undefined) { if (schema.patternProperties !== undefined) {
for (key in json_field.patternProperties) { for (key in schema.patternProperties) {
if (json_field.patternProperties.hasOwnProperty(key)) { if (schema.patternProperties.hasOwnProperty(key)) {
if (key === ".*" || if (key === ".*" ||
key === "^.*$" || key === "^.*$" ||
key === ".*$" || key === ".*$" ||
...@@ -1363,9 +1363,9 @@ ...@@ -1363,9 +1363,9 @@
.push(render_object_additionalProperty.bind(g, .push(render_object_additionalProperty.bind(g,
g, g,
key + " property", key + " property",
default_dict, json_document,
path, path,
json_field.patternProperties[key], schema.patternProperties[key],
schema_path + '/patternProperties/' + key, schema_path + '/patternProperties/' + key,
used_properties, used_properties,
element_append element_append
...@@ -1376,10 +1376,10 @@ ...@@ -1376,10 +1376,10 @@
} }
if (additionalProperties === undefined) { if (additionalProperties === undefined) {
if (json_field.additionalProperties === undefined) { if (schema.additionalProperties === undefined) {
additionalProperties = true; additionalProperties = true;
} else { } else {
additionalProperties = json_field.additionalProperties; additionalProperties = schema.additionalProperties;
} }
} }
if (additionalProperties !== false) { if (additionalProperties !== false) {
...@@ -1387,7 +1387,7 @@ ...@@ -1387,7 +1387,7 @@
.push(render_object_additionalProperty.bind(g, .push(render_object_additionalProperty.bind(g,
g, g,
"additional property", "additional property",
default_dict, json_document,
path, path,
additionalProperties, additionalProperties,
schema_path + '/additionalProperties', schema_path + '/additionalProperties',
...@@ -1402,8 +1402,8 @@ ...@@ -1402,8 +1402,8 @@
.push(function () { .push(function () {
var key, var key,
queue = RSVP.Queue(); queue = RSVP.Queue();
for (key in default_dict) { for (key in json_document) {
if (default_dict.hasOwnProperty(key)) { if (json_document.hasOwnProperty(key)) {
if (!used_properties.hasOwnProperty(key)) { if (!used_properties.hasOwnProperty(key)) {
queue queue
.push( .push(
...@@ -1413,7 +1413,7 @@ ...@@ -1413,7 +1413,7 @@
path: path, path: path,
schema_path: "", schema_path: "",
schema_part: undefined, schema_part: undefined,
default_dict: default_dict[key] json_document: json_document[key]
}) })
) )
.push(root_append); .push(root_append);
......
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