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