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

validation worked with recursive subgadget

parent c487a737
......@@ -8,6 +8,7 @@
<link rel="stylesheet" href="gadget_erp5_nojqm.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="tv4.js" type="text/javascript"></script>
<script src="renderjs.js" type="text/javascript"></script>
<script src="jio.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*/
/*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";
var render_object,
expandSchema;
......@@ -62,6 +62,31 @@
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) {
if (doc instanceof Array) {
return "array";
......@@ -445,6 +470,9 @@
.push(function (value) {
return event(JSON.parse(value[scope]));
})
.push(function () {
return gadget.processValidation();
})
.push(function () {
if (rerender) {
return rerender(g, schema_alternatives);
......@@ -486,7 +514,12 @@
input.type = "button";
gadget.props.add_buttons.push({
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;
});
......@@ -1105,46 +1138,104 @@
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")
.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) {
schema_url = g.options.schema_url;
} else {
return g.processValidationParent(json_dict);
.declareMethod('getElementByPath', function (data_path) {
var g = this,
array,
path,
scope,
key,
slash_count = 0,
slash_count_next,
bingo,
idx,
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) {
json_dict = getFormValuesAsJSONDict(g);
} else {
json_dict = RSVP.Queue()
.push(function () {
return json_dict;
});
return RSVP.Queue()
.push(function () {
return document.getElementById(
g.element.getAttribute("data-gadget-scope") + data_path
);
});
})
.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()
.push(function () {
return RSVP.all([
g.getDeclaredGadget('loadschema'),
json_dict
]);
if (json_document === undefined) {
return getFormValuesAsJSONDict(g);
}
return json_document;
})
.push(function (ret) {
var loadschema_gadget = ret[0],
json_d = ret[1];
return loadschema_gadget.validateJSON(schema_url, json_d);
.push(function (json_d) {
return tv4.validateMultiple(json_d, g.props.schema[""]);
})
.push(function (validation) {
var index,
div,
field_name;
tasks = [];
g.element.querySelectorAll("span.error").forEach(function (span) {
span.textContent = "";
......@@ -1154,26 +1245,42 @@
div.setAttribute("class", "");
});
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) {
if (validation.errors.hasOwnProperty(index)) {
field_name = validation.errors[index].dataPath;
div = g.element.querySelector(".slapos-parameter[name='" + field_name + "']").parentNode;
div.setAttribute("class", "slapos-parameter error-input");
div.querySelector("span.error").textContent = validation.errors[index].message;
tasks.push(
g.getElementByPath(validation.errors[index].dataPath)
.push(print_error(validation.errors[index].message))
);
}
}
for (index in validation.missing) {
if (validation.missing.hasOwnProperty(index)) {
field_name = validation.missing[index].dataPath;
div = g.element.querySelector('.slapos-parameter[name=' + field_name + "']").parentNode;
div.setAttribute("class", "error-input");
div.querySelector("span.error").textContent = validation.missing[index].message;
tasks.push(
g.getElementByPath(validation.missing[index].dataPath)
.push(print_error(validation.missing[index].message))
);
}
}
return "ERROR";
return RSVP.Queue()
.push(function () {
return RSVP.all(tasks);
})
.push(function () {
return "ERROR";
});
});
})
......@@ -1271,6 +1378,12 @@
.push(function (json) {
g.props.schema_map[path] = url;
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")
......@@ -1284,6 +1397,7 @@
// contain map of current normalized schema
// json pointer and corresponding url
// it's need for schema uri computation
g.props.schema = {};
g.props.schema_map = {};
g.props.schema_cache = {};
g.options = options;
......@@ -1307,6 +1421,9 @@
document: options.value
});
})
.push(function () {
return g.processValidation();
})
.push(function () {
return g;
});
......@@ -1334,8 +1451,8 @@
var field_list = this.props.inputs,
i;
for (i = 0; i < field_list.length; i = i + 1) {
if (evt.target === field_list[i].element) {
return this.processValidation.bind(this, this.options.schema_url, undefined)(evt);
if (evt.target === field_list[i]) {
return this.processValidation();
}
}
})
......@@ -1356,4 +1473,4 @@
// });
});
}(window, document, rJS, RSVP, jIO));
\ No newline at end of file
}(window, document, rJS, RSVP, jIO, tv4));
\ 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