Commit d13dae53 authored by Boris Kocherov's avatar Boris Kocherov

use job for listenEvents

parent 47f0cb25
......@@ -5,6 +5,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>ERP5</title>
<link rel="shortcut icon" href="favicon.ico">
<link href="gadget_erp5_page_slap_parameter_form.css" rel="stylesheet" type="text/css"/>
<script src="rsvp.js" type="text/javascript"></script>
<script src="renderjs.js" type="text/javascript"></script>
<script src="jquery.js" type="text/javascript"></script>
......@@ -12,8 +13,6 @@
<script src="gadget_json_generated_form.js" type="text/javascript"></script>
</head>
<body>
<div>
</div>
<div class="loadschema"
data-gadget-url="gadget_erp5_page_slap_load_schema.html"
data-gadget-scope="loadschema" >
......
......@@ -82,7 +82,7 @@
return input;
}
function render_subform(json_field, default_dict, root, path) {
function render_subform(gadget, json_field, default_dict, root, path) {
var div_input,
key,
div,
......@@ -137,6 +137,7 @@
label.setAttribute("class", "slapos-parameter-dict-key");
default_div.appendChild(label);
default_div = render_subform(
gadget,
json_field.patternProperties['.*'],
default_dict[default_value],
default_div,
......@@ -162,7 +163,9 @@
div_input = document.createElement("div");
div_input.setAttribute("class", "input");
if (json_field.properties[key].type === 'object') {
div_input = render_subform(json_field.properties[key],
div_input = render_subform(
gadget,
json_field.properties[key],
default_dict[key],
div_input,
path + "/" + key);
......@@ -170,6 +173,7 @@
input = render_field(json_field.properties[key], default_dict[key]);
input.name = path + "/" + key;
input.setAttribute("class", "slapos-parameter");
gadget.props.inputs.push(input);
div_input.appendChild(input);
}
default_used_list.push(key);
......@@ -216,11 +220,11 @@
return root;
}
function getFormValuesAsJSONDict(element) {
function getFormValuesAsJSONDict(g) {
var json_dict = {},
entry,
multi_level_dict = {};
$(element.querySelectorAll(".slapos-parameter")).each(function (key, input) {
g.props.inputs.forEach(function (input) {
if (input.value !== "") {
if (input.type === 'number') {
json_dict[input.name] = parseInt(input.value, 10);
......@@ -270,7 +274,7 @@
return gadget.processValidation(json_url);
}
function addSubForm(element) {
function addSubForm(g, element) {
var subform_json = JSON.parse(atob(element.value)),
input_text = element.parentNode.querySelector("input[type='text']"),
div = document.createElement("div"),
......@@ -286,7 +290,7 @@
label.setAttribute("class", "slapos-parameter-dict-key");
div.appendChild(label);
div = render_subform(subform_json, {}, div, element.name + "/" + input_text.value);
div = render_subform(g, subform_json, {}, div, element.name + "/" + input_text.value);
element.parentNode.parentNode.insertBefore(div, element.parentNode.parentNode.children[1]);
// element.parentNode.parentNode.appendChild(div);
......@@ -296,7 +300,7 @@
function loadEventList(gadget) {
var g = gadget,
field_list = g.element.querySelectorAll(".slapos-parameter"),
field_list = g.props.inputs,
button_list = g.element.querySelectorAll('button.add-sub-form'),
label_list = g.element.querySelectorAll('label.slapos-parameter-dict-key'),
i,
......@@ -307,7 +311,7 @@
field_list[i],
'change',
false,
validateForm.bind(g, g, g.options.value.parameter.json_url)
validateForm.bind(g, g, g.options.schema_url)
));
}
......@@ -316,23 +320,29 @@
button_list[i],
'click',
false,
addSubForm.bind(g, button_list[i])
addSubForm.bind(g, g, button_list[i])
));
}
for (i = 0; i < label_list.length; i = i + 1) {
promise_list.push(loopEventListener(
label_list[i],
'click',
false,
collapseParameter.bind(g, label_list[i])
));
}
// for (i = 0; i < label_list.length; i = i + 1) {
// promise_list.push(loopEventListener(
// label_list[i],
// 'click',
// false,
// collapseParameter.bind(g, label_list[i])
// ));
// }
return RSVP.all(promise_list);
}
rJS(window)
.ready(function () {
var g = this;
g.props = {};
})
.declareAcquiredMethod("notifyValid", "notifyValid")
.declareAcquiredMethod("notifyInvalid", "notifyInvalid")
.declareMethod("loadJSONSchema", function (url) {
return this.getDeclaredGadget('loadschema')
.push(function (gadget) {
......@@ -342,6 +352,9 @@
.declareMethod('processValidation', function (schema_url, json_dict) {
var g = this;
if (!json_dict) {
json_dict = getFormValuesAsJSONDict(g);
}
return this.getDeclaredGadget('loadschema')
.push(function (gadget) {
return gadget.validateJSON(schema_url, json_dict);
......@@ -386,13 +399,15 @@
})
.declareMethod('renderParameterForm', function (schema, default_dict) {
var g = this;
var parent_element = document.createDocumentFragment();
render_subform(schema, default_dict, parent_element);
var g = this,
parent_element = document.createDocumentFragment();
g.params.inputs = [];
render_subform(g, schema, default_dict, parent_element);
while (g.element.firstChild) {
g.element.removeChild(g.element.firstChild);
}
g.element.appendChild(parent_element);
g.listenEvents();
return g.element;
})
......@@ -494,13 +509,13 @@
});
})
.declareService(function () {
.declareJob('listenEvents', function () {
return loadEventList(this);
})
.declareMethod('getContent', function () {
var g = this,
json_dict = getFormValuesAsJSONDict(g.element);
json_dict = getFormValuesAsJSONDict(g);
return g.processValidation(g.options.schema_url, json_dict)
.push(function (status) {
return {
......@@ -527,4 +542,4 @@
// });
//});
}(window, document, rJS, $));
\ No newline at end of file
}(window, document, rJS, $, loopEventListener));
\ No newline at end of file
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