Commit fe719311 authored by Boris Kocherov's avatar Boris Kocherov

recursive gadget declaration for complex json document generation

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