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