Commit fe719311 authored by Boris Kocherov's avatar Boris Kocherov

recursive gadget declaration for complex json document generation

parent 9ba0bebd
......@@ -226,7 +226,10 @@
function getFormValuesAsJSONDict(g) {
var json_dict = {},
entry,
multi_level_dict = {};
multi_level_dict = {},
scope,
subforms = g.props.subforms,
queue = RSVP.Queue();
g.props.inputs.forEach(function (input) {
if (input.value !== "") {
if (input.type === 'number') {
......@@ -264,13 +267,35 @@
}
}
for (entry in json_dict) {
if (json_dict.hasOwnProperty(entry)) {
convertOnMultiLevel(entry, json_dict[entry], multi_level_dict);
function recursiveGetContent(gadget, scope) {
queue
.push(function () {
return gadget.getContent();
})
.push(function (jdict) {
for (entry in jdict) {
if (jdict.hasOwnProperty(entry)) {
convertOnMultiLevel(scope + '/' + entry, jdict[entry], multi_level_dict);
}
}
});
}
for (scope in subforms) {
if (subforms.hasOwnProperty(scope)) {
recursiveGetContent(subforms[scope], scope);
}
}
return multi_level_dict;
return queue
.push(function () {
for (entry in json_dict) {
if (json_dict.hasOwnProperty(entry)) {
convertOnMultiLevel(entry, json_dict[entry], multi_level_dict);
}
}
return multi_level_dict;
});
}
function validateForm(gadget, json_url) {
......@@ -279,27 +304,30 @@
function addSubForm(g, options) {
var element = options.element,
subform_json = options.schema_part,
input_text = element.parentNode.querySelector("input[type='text']"),
div = document.createElement("div"),
label;
key = element.parentNode.querySelector("input[type='text']").value,
scope = element.name + "/" + key;
if (input_text.value === "") {
if (!key || g.props.subforms.hasOwnProperty(scope)) {
return false;
}
div.setAttribute("class", "slapos-parameter-dict-key");
label = document.createElement("label");
label.textContent = input_text.value;
label.setAttribute("class", "slapos-parameter-dict-key");
div.appendChild(label);
return g.declareGadget('gadget_json_generated_form.html', {scope: scope})
.push(function (form_gadget) {
var div = document.createElement("div"),
label;
div = render_subform(g, subform_json, {}, div, element.name + "/" + input_text.value);
g.props.subforms[scope] = form_gadget;
element.parentNode.parentNode.insertBefore(div, element.parentNode.parentNode.children[1]);
// element.parentNode.parentNode.appendChild(div);
div.setAttribute("class", "slapos-parameter-dict-key");
label = document.createElement("label");
label.textContent = key;
label.setAttribute("class", "slapos-parameter-dict-key");
div.appendChild(label);
return div;
div.appendChild(form_gadget.element);
element.parentNode.parentNode.insertBefore(div, element.parentNode.parentNode.children[1]);
return form_gadget.renderParameterForm(options.schema_part, {}, scope);
});
}
function loadEventList(gadget) {
......@@ -344,6 +372,7 @@
.ready(function () {
var g = this;
g.props = {};
g.options = {};
})
.declareAcquiredMethod("notifyValid", "notifyValid")
.declareAcquiredMethod("notifyInvalid", "notifyInvalid")
......@@ -354,14 +383,38 @@
});
})
.declareAcquiredMethod("processValidationParent", "processValidation")
.allowPublicAcquisition("processValidation", function (json_dict) {
return this.processValidation(undefined, json_dict);
})
.declareMethod('processValidation', function (schema_url, json_dict) {
var g = this;
if (!schema_url) {
if (!g.options.schema_url) {
return g.processValidationParent(json_dict);
} else {
schema_url = g.options.schema_url;
}
}
if (!json_dict) {
json_dict = getFormValuesAsJSONDict(g);
} else {
json_dict = RSVP.Queue()
.push(function () {
return json_dict;
});
}
return this.getDeclaredGadget('loadschema')
.push(function (gadget) {
return gadget.validateJSON(schema_url, json_dict);
return RSVP.Queue()
.push(function () {
return RSVP.all([
g.getDeclaredGadget('loadschema'),
json_dict
])
})
.push(function (ret) {
var loadschema_gadget = ret[0],
json_dict = ret[1];
return loadschema_gadget.validateJSON(schema_url, json_dict);
})
.push(function (validation) {
var error_index,
......@@ -402,11 +455,13 @@
});
})
.declareMethod('renderParameterForm', function (schema, default_dict) {
.declareMethod('renderParameterForm', function (schema, default_dict, path) {
var g = this,
parent_element = document.createDocumentFragment();
g.props.inputs = [];
g.props.add_buttons = [];
g.props.subforms = {};
g.props.path = path;
render_subform(g, schema, default_dict, parent_element);
while (g.element.firstChild) {
g.element.removeChild(g.element.firstChild);
......@@ -519,15 +574,15 @@
})
.declareMethod('getContent', function () {
var g = this,
json_dict = getFormValuesAsJSONDict(g);
return g.processValidation(g.options.schema_url, json_dict)
.push(function (status) {
return {
value: json_dict,
status: status
};
});
var g = this;
return getFormValuesAsJSONDict(g);
// return g.processValidation(g.options.schema_url, json_dict)
// .push(function (status) {
// return {
// value: json_dict,
// status: status
// };
// });
});
//.declareService(function () {
......
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