Commit 0d190623 authored by Boris Kocherov's avatar Boris Kocherov

validation worked with recursive subgadget

parent c487a737
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
<link rel="stylesheet" href="gadget_erp5_nojqm.css"> <link rel="stylesheet" href="gadget_erp5_nojqm.css">
<link href="gadget_erp5_page_slap_parameter_form.css" rel="stylesheet" type="text/css"/> <link href="gadget_erp5_page_slap_parameter_form.css" rel="stylesheet" type="text/css"/>
<script src="rsvp.js" type="text/javascript"></script> <script src="rsvp.js" type="text/javascript"></script>
<script src="tv4.js" type="text/javascript"></script>
<script src="renderjs.js" type="text/javascript"></script> <script src="renderjs.js" type="text/javascript"></script>
<script src="jio.js" type="text/javascript"></script> <script src="jio.js" type="text/javascript"></script>
<script src="gadget_json_generated_form.js" type="text/javascript"></script> <script src="gadget_json_generated_form.js" type="text/javascript"></script>
......
/*jslint nomen: true, maxlen: 200, indent: 2, maxerr: 100*/ /*jslint nomen: true, maxlen: 200, indent: 2, maxerr: 100*/
/*global window, document, URL, rJS, RSVP, jIO */ /*global window, document, URL, rJS, RSVP, jIO, tv4 */
(function (window, document, rJS, RSVP, jIO) { (function (window, document, rJS, RSVP, jIO, tv4) {
"use strict"; "use strict";
var render_object, var render_object,
expandSchema; expandSchema;
...@@ -62,6 +62,31 @@ ...@@ -62,6 +62,31 @@
return schema; return schema;
} }
function schemaPushSchemaPart(schema, schema_path, schema_part) {
var i,
k,
key_list;
if (schema_path === "/") {
schema_path = "";
}
key_list = schema_path.split("/");
for (i = 0; i < key_list.length; i += 1) {
k = decodeJsonPointer(key_list[i]);
if (i === key_list.length - 1) {
if (schema_part !== undefined) {
schema[k] = schema_part;
} else {
return schema[k];
}
} else {
if (!schema.hasOwnProperty(k)) {
schema[k] = {};
}
schema = schema[k];
}
}
}
function getDocumentType(doc) { function getDocumentType(doc) {
if (doc instanceof Array) { if (doc instanceof Array) {
return "array"; return "array";
...@@ -445,6 +470,9 @@ ...@@ -445,6 +470,9 @@
.push(function (value) { .push(function (value) {
return event(JSON.parse(value[scope])); return event(JSON.parse(value[scope]));
}) })
.push(function () {
return gadget.processValidation();
})
.push(function () { .push(function () {
if (rerender) { if (rerender) {
return rerender(g, schema_alternatives); return rerender(g, schema_alternatives);
...@@ -486,7 +514,12 @@ ...@@ -486,7 +514,12 @@
input.type = "button"; input.type = "button";
gadget.props.add_buttons.push({ gadget.props.add_buttons.push({
element: input, element: input,
event: event.bind(gadget, schema_alternatives[0].value) event: function () {
return event(schema_alternatives[0].value)
.push(function () {
return gadget.processValidation();
});
}
}); });
return input; return input;
}); });
...@@ -1105,46 +1138,104 @@ ...@@ -1105,46 +1138,104 @@
tasks.push(g.props.add_custom_data[key].rerender()); tasks.push(g.props.add_custom_data[key].rerender());
} }
} }
return RSVP.All(tasks); tasks.push(g.processValidation());
return RSVP.Queue()
.push(function () {
return RSVP.all(tasks);
});
}) })
.declareAcquiredMethod("processValidationParent", "processValidation") .declareMethod('getElementByPath', function (data_path) {
.allowPublicAcquisition("processValidation", function (json_dict) { var g = this,
return this.processValidation(undefined, json_dict); array,
}) path,
.declareMethod('processValidation', function (schema_url, json_dict) { scope,
var g = this; key,
if (!schema_url) { slash_count = 0,
if (g.options.schema_url) { slash_count_next,
schema_url = g.options.schema_url; bingo,
} else { idx,
return g.processValidationParent(json_dict); options = g.props;
if (data_path !== "/") {
for (path in options.arrays) {
if (options.arrays.hasOwnProperty(path) && data_path.startsWith(path)) {
array = options.arrays[path]
.querySelectorAll("div[data-gadget-parent-scope='" + g.element.getAttribute("data-gadget-scope") + "']");
data_path = data_path.slice(path.length).split("/");
idx = data_path[0];
data_path = "/" + data_path.slice(1).join("/");
bingo = array[idx].getAttribute('data-gadget-scope');
}
}
if (bingo) {
return g.getDeclaredGadget(bingo)
.push(function (gadget) {
return gadget.getElementByPath(data_path);
});
}
for (path in options.objects) {
if (options.objects.hasOwnProperty(path) && data_path.startsWith(path)) {
slash_count_next = path.split("/").length - 1;
if (slash_count_next > slash_count) {
bingo = path;
slash_count = slash_count_next;
}
}
}
if (bingo) {
path = options.objects[bingo];
for (key in path) {
if (path.hasOwnProperty(key)) {
if (data_path.startsWith(bingo + encodeJsonPointer(key))) {
data_path = data_path.slice(bingo.length + encodeJsonPointer(key).length);
if (!data_path) {
data_path = "/";
}
scope = path[key];
}
}
}
}
if (scope) {
return g.getDeclaredGadget(scope)
.push(function (gadget) {
return gadget.getElementByPath(data_path);
});
} }
} }
if (!json_dict) { return RSVP.Queue()
json_dict = getFormValuesAsJSONDict(g); .push(function () {
} else { return document.getElementById(
json_dict = RSVP.Queue() g.element.getAttribute("data-gadget-scope") + data_path
.push(function () { );
return json_dict; });
}); })
.declareAcquiredMethod("processValidationParent", "processValidationTop")
.allowPublicAcquisition("processValidationTop", function (arr) {
return this.processValidation(arr[0]);
})
.declareMethod('processValidation', function (json_document) {
// XXX need use local schema and local json document
// in every subgadget to take into account user anyOf choice
// and so more precisely point to issue
var g = this;
if (!g.props.toplevel) {
return g.processValidationParent(json_document);
} }
return RSVP.Queue() return RSVP.Queue()
.push(function () { .push(function () {
return RSVP.all([ if (json_document === undefined) {
g.getDeclaredGadget('loadschema'), return getFormValuesAsJSONDict(g);
json_dict }
]); return json_document;
}) })
.push(function (ret) { .push(function (json_d) {
var loadschema_gadget = ret[0], return tv4.validateMultiple(json_d, g.props.schema[""]);
json_d = ret[1];
return loadschema_gadget.validateJSON(schema_url, json_d);
}) })
.push(function (validation) { .push(function (validation) {
var index, var index,
div, tasks = [];
field_name;
g.element.querySelectorAll("span.error").forEach(function (span) { g.element.querySelectorAll("span.error").forEach(function (span) {
span.textContent = ""; span.textContent = "";
...@@ -1154,26 +1245,42 @@ ...@@ -1154,26 +1245,42 @@
div.setAttribute("class", ""); div.setAttribute("class", "");
}); });
if (validation.valid) { if (validation.valid) {
return "VALID"; return RSVP.Queue()
.push(function () {
return "VALID";
});
}
function print_error(message) {
return function (element) {
var id = element.id;
element.setAttribute("class", "error-input");
element.querySelector("#" + id.replace("/", "\\/") + " > span.error").textContent = message;
};
} }
for (index in validation.errors) { for (index in validation.errors) {
if (validation.errors.hasOwnProperty(index)) { if (validation.errors.hasOwnProperty(index)) {
field_name = validation.errors[index].dataPath; tasks.push(
div = g.element.querySelector(".slapos-parameter[name='" + field_name + "']").parentNode; g.getElementByPath(validation.errors[index].dataPath)
div.setAttribute("class", "slapos-parameter error-input"); .push(print_error(validation.errors[index].message))
div.querySelector("span.error").textContent = validation.errors[index].message; );
} }
} }
for (index in validation.missing) { for (index in validation.missing) {
if (validation.missing.hasOwnProperty(index)) { if (validation.missing.hasOwnProperty(index)) {
field_name = validation.missing[index].dataPath; tasks.push(
div = g.element.querySelector('.slapos-parameter[name=' + field_name + "']").parentNode; g.getElementByPath(validation.missing[index].dataPath)
div.setAttribute("class", "error-input"); .push(print_error(validation.missing[index].message))
div.querySelector("span.error").textContent = validation.missing[index].message; );
} }
} }
return "ERROR"; return RSVP.Queue()
.push(function () {
return RSVP.all(tasks);
})
.push(function () {
return "ERROR";
});
}); });
}) })
...@@ -1271,6 +1378,12 @@ ...@@ -1271,6 +1378,12 @@
.push(function (json) { .push(function (json) {
g.props.schema_map[path] = url; g.props.schema_map[path] = url;
return resolveLocalReference(json, hash); return resolveLocalReference(json, hash);
})
.push(function (schema_part) {
// console.log(path);
schemaPushSchemaPart(g.props.schema, path, schema_part);
// console.log(g.props.schema[""]);
return schema_part;
}); });
}) })
.declareAcquiredMethod("loadJSONSchemaParent", "loadJSONSchemaTop") .declareAcquiredMethod("loadJSONSchemaParent", "loadJSONSchemaTop")
...@@ -1284,6 +1397,7 @@ ...@@ -1284,6 +1397,7 @@
// contain map of current normalized schema // contain map of current normalized schema
// json pointer and corresponding url // json pointer and corresponding url
// it's need for schema uri computation // it's need for schema uri computation
g.props.schema = {};
g.props.schema_map = {}; g.props.schema_map = {};
g.props.schema_cache = {}; g.props.schema_cache = {};
g.options = options; g.options = options;
...@@ -1307,6 +1421,9 @@ ...@@ -1307,6 +1421,9 @@
document: options.value document: options.value
}); });
}) })
.push(function () {
return g.processValidation();
})
.push(function () { .push(function () {
return g; return g;
}); });
...@@ -1334,8 +1451,8 @@ ...@@ -1334,8 +1451,8 @@
var field_list = this.props.inputs, var field_list = this.props.inputs,
i; i;
for (i = 0; i < field_list.length; i = i + 1) { for (i = 0; i < field_list.length; i = i + 1) {
if (evt.target === field_list[i].element) { if (evt.target === field_list[i]) {
return this.processValidation.bind(this, this.options.schema_url, undefined)(evt); return this.processValidation();
} }
} }
}) })
...@@ -1356,4 +1473,4 @@ ...@@ -1356,4 +1473,4 @@
// }); // });
}); });
}(window, document, rJS, RSVP, jIO)); }(window, document, rJS, RSVP, jIO, tv4));
\ No newline at end of file \ 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