Commit bc94841c authored by Sven Franck's avatar Sven Franck

allow custom form fields, force portal type in POST, validate against portal_type definition

parent db51c24b
...@@ -27,14 +27,6 @@ ...@@ -27,14 +27,6 @@
**/ **/
map.actions = { map.actions = {
/**
* Create an installation
* @method installa
**/
"install": function () {
util.error({"error":"install: Nothing defined"});
},
/** /**
* Look up single value from dict * Look up single value from dict
* @method translateLookup * @method translateLookup
...@@ -304,7 +296,20 @@ ...@@ -304,7 +296,20 @@
}, },
/** /**
* Submit a form * Create an installation = new service object
* @method install
* @param {object} obj Action Object
**/
"install": function (obj) {
// force new portal type and POST (create new record)
obj.gadget.state.force_type = "Service";
obj.gadget.state.create_new = true;
storage.add(obj);
},
/**
* Add a user to the storage
* @method submit * @method submit
* @param {object} obj Action Object * @param {object} obj Action Object
**/ **/
...@@ -951,7 +956,7 @@ ...@@ -951,7 +956,7 @@
"value": field_value, "value": field_value,
"text": textarea_value, "text": textarea_value,
"readonly": (prevail.properties.editable === false || "readonly": (prevail.properties.editable === false ||
spec.properties.editable === false) ? true : undefined, spec.properties.editable === false) ? "readonly" : undefined,
"options": (prevail.widget.items || spec.widget.items) ? "options": (prevail.widget.items || spec.widget.items) ?
([ ([
prevail.widget.items || spec.widget.items || null, prevail.widget.items || spec.widget.items || null,
...@@ -3631,8 +3636,8 @@ ...@@ -3631,8 +3636,8 @@
text = "search"; text = "search";
} }
// disable IOS auto functionality // disable IOS auto functionality
config.attributes.autocapitalize = "off"; config.attributes.autocapitalize = "none";
config.attributes.autocorrect = "off"; config.attributes.autocorrect = "none";
// update class_list // update class_list
container_class_list = "ui-input-" + text + " ui-body-inherit "; container_class_list = "ui-input-" + text + " ui-body-inherit ";
} }
...@@ -3955,7 +3960,7 @@ ...@@ -3955,7 +3960,7 @@
storage.add = function (config) { storage.add = function (config) {
var property, replace, i, obj, value, form_elements, form_element, pass, var property, replace, i, obj, value, form_elements, form_element, pass,
test_full, test_empty, test_time, form_to_submit, anti_spam, formData, test_full, test_empty, test_time, form_to_submit, anti_spam, formData,
valid, pass_id; valid, pass_id, validate_portal_type_fields;
pass = false; pass = false;
form_to_submit = document.getElementById(config.id); form_to_submit = document.getElementById(config.id);
...@@ -3968,7 +3973,7 @@ ...@@ -3968,7 +3973,7 @@
// spam - thx http://nedbatchelder.com/text/stopbots.html // spam - thx http://nedbatchelder.com/text/stopbots.html
if (anti_spam) { if (anti_spam) {
// fill form in < 1sec = spammer // fill form in < 1sec = spam
if (Date.now() - parseInt(anti_spam.getAttribute("data-created"), 10) if (Date.now() - parseInt(anti_spam.getAttribute("data-created"), 10)
> 1000) { > 1000) {
test_time = true; test_time = true;
...@@ -3999,28 +4004,53 @@ ...@@ -3999,28 +4004,53 @@
replace = form_to_submit.id.split("_")[0] + "_"; replace = form_to_submit.id.split("_")[0] + "_";
obj = {}; obj = {};
// force portal_type
if (config.gadget.state.force_type) {
config.gadget.state.type = config.gadget.state.force_type;
validate_portal_type_fields = true;
}
for (property in valid) { for (property in valid) {
if (valid.hasOwnProperty(property)) { if (valid.hasOwnProperty(property)) {
value = valid[property]; value = valid[property];
// prepare for storage // prepare to store
// TODO: check for portal-type in field name and skip if missing
if (property !== "undefined") { if (property !== "undefined") {
obj[property] = value;
// overwrite portal_type if forced
if (config.gadget.state.force_type
&& property !== "identifier") {
obj[
property.replace(property.split("_")[0],
config.gadget.state.force_type.toLowerCase())
] = value;
} else {
obj[property] = value;
}
} }
// prepare for form submit // prepare for form submit
// NOTE: formData can post CORS! // NOTE: formData can post CORS!
formData.append(property.replace(replace, ""), value); // formData.append(property.replace(replace, ""), value);
} }
} }
// force new object to be created
if (config.gadget.state.create_new) {
delete obj.identifier;
}
util.loader("", "global_dict.status_dict.saving"); util.loader("", "global_dict.status_dict.saving");
// TODO: a post should always include all required fields // TODO: a post should always include all required fields
// TODO: find a way to set default fields, like timestamp etc // TODO: find a way to set default fields, like timestamp etc
app.store({ app.store({
"response": [obj], "response": [obj],
"pass": {"type": config.gadget.state.type, "reply": true} "pass": {
"type": config.gadget.state.type,
"reply": true,
"validate_type": validate_portal_type_fields
}
}) })
.then(function (answer) { .then(function (answer) {
if (answer.response.result === "success") { if (answer.response.result === "success") {
...@@ -5353,7 +5383,7 @@ ...@@ -5353,7 +5383,7 @@
*/ */
app.store = function (reply) { app.store = function (reply) {
var i, obj, key, promises, record, items, store, pass, send_status, var i, obj, key, promises, record, items, store, pass, send_status,
method; method, validate;
pass = reply.pass; pass = reply.pass;
...@@ -5362,53 +5392,110 @@ ...@@ -5362,53 +5392,110 @@
items = util.parseIfNeeded(reply.response); items = util.parseIfNeeded(reply.response);
if (store && items.length > 0) { if (store && items.length > 0) {
promises = [];
// show some status // show some status
util.loader("", "global_dict.status_dict.storing"); util.loader("", "global_dict.status_dict.storing");
// loop over item and create a JIO record // as store will also be called directly and may require field
for (i = 0; i < items.length; i += 1) { // definitions for validating what is being stored, we may fetch
record = items[i]; // field definitions here again. When called in the setContent
obj = {}; // chain, fields are already included in pass
for (key in record) { pass.items = items;
if (record.hasOwnProperty(key)) {
obj[key] = record[key];
}
}
// add portal type
obj.portal_type = pass.type;
// if an object identifier was set in the form, we PUT, else POST
if (obj.identifier) {
method = "put";
obj._id = obj.identifier;
delete obj.identifier;
}
promises[i] = store[method || "post"](obj) // only fetch if validate_type is set!
.then(function (answer) { if (pass.validate_type === undefined) {
return answer; pass.skip = pass.undo_skip = true;
});
} }
return RSVP.all(promises)
.then(function (response) { // TODO: bad way to set attachment value!!
// for single item actions, allow to return a status message return app.fetchConfiguration({
if (response.length === 1 && reply.pass.reply) { "storage": app.default_dict.storage_dict.settings,
send_status = response[0]; "file": app.default_dict.storage_dict.data_type,
"attachment": pass.type.toLowerCase() + "_fieldlist",
"pass": pass
})
.then(function (answer) {
promises = [];
// clean skip && set field validation
if (answer.pass && answer.pass.undo_skip) {
delete answer.pass.skip;
delete answer.pass.undo_skip;
// if we have an answer = field definitions, the record fields
// must be validated before storing, so we don't store a field
// that does not exist
} else {
if (answer.response) {
validate = true;
} else {
util.error({"error":"store: field definintions not found."});
}
} }
return {
"response": send_status, // loop over item and create a JIO record
"pass": pass for (i = 0; i < pass.items.length; i += 1) {
}; record = pass.items[i];
obj = {};
for (key in record) {
if (record.hasOwnProperty(key)) {
if (validate) {
if (answer.response[key]) {
obj[key] = record[key];
} else {
util.error({
"error":"store: field " + key +
" not defined for type =" + pass.type
});
}
} else {
obj[key] = record[key];
}
}
}
// add portal type
obj.portal_type = pass.type;
// if an object identifier was set in the form, we PUT, else POST
if (obj.identifier) {
method = "put";
obj._id = obj.identifier;
delete obj.identifier;
}
// store
promises[i] = store[method || "post"](obj)
.then(function (answer) {
return answer;
})
.fail(util.error);
}
return RSVP.all(promises)
.then(function (response) {
// for single item actions, allow to return a status message
if (response.length === 1 && reply.pass.reply) {
send_status = response[0];
}
return {
"response": send_status,
"pass": pass
};
})
.fail(util.error);
}) })
.fail(util.error); .fail(util.error);
} }
// we may have no sample data! // no sample data!
return { return {
"response": undefined, "response": undefined,
"pass": pass "pass": pass
}; };
} }
// nothing to do
return { return {
"pass": pass "pass": pass
}; };
......
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