Files
raven-client/ayanova/src/api/form-custom-template.js
2020-04-02 14:07:02 +00:00

84 lines
2.7 KiB
JavaScript

/*Xeslint-disable */
///Add data key names which make the custom fields control work more easily
///Since the names can be inferred from the data that comes from the server it saves bandwidth to do it here at the client
function addDataKeyNames(obj) {
//iterate the array of objects
//if it has a "type" property then it's a custom field so add its data key name
for (let i = 0; i < obj.length; i++) {
if (obj[i].type) {
obj[i]["dataKey"] = "c" + parseInt(obj[i].fld.replace(/^\D+/g, ""));
}
}
//return the whole thing again now translated
return obj;
}
export default {
// cache the form customization data if it's not already present
//cache invalidation is hard, this needs it...hmmm....
//for now will rely on logout and back in to clear up any customization issues
get(formKey) {
return new Promise(function getFormTemplate(resolve) {
if (
!window.$gz._.has(window.$gz.store.state.formCustomTemplate, formKey)
) {
//fetch and populate the store
window.$gz.api.get("formcustom/" + formKey).then(res => {
if (res.error) {
throw res.error;
}
window.$gz.store.commit("setFormCustomTemplateItem", {
formKey: formKey,
concurrencyToken: res.data.concurrencyToken,
value: addDataKeyNames(JSON.parse(res.data.template))
});
resolve();
});
} else {
resolve();
}
});
},
set(formKey, token, template) {
window.$gz.store.commit("setFormCustomTemplateItem", {
formKey: formKey,
concurrencyToken: token,
value: addDataKeyNames(JSON.parse(template))
});
},
getFieldTemplateValue(formKey, fieldKey) {
if (fieldKey === undefined) {
throw "ERROR form-custom-template::getFieldTemplateValue -> fieldKey not specified for template for form [" +
formKey +
"]";
}
let template = window.$gz.store.state.formCustomTemplate[formKey];
if (template === undefined) {
throw "ERROR form-custom-template::getFieldTemplateValue -> Store is missing form template for [" +
formKey +
"]";
}
//_https://lodash.com/docs#find
//Note that not every field being requested will exist so it's valid to return undefined
let templateItem = window.$gz._.find(template, ["fld", fieldKey]);
return templateItem;
},
getTemplateConcurrencyToken(formKey) {
let tok =
window.$gz.store.state.formCustomTemplate[formKey + "_concurrencyToken"];
if (tok === undefined) {
throw "ERROR form-custom-template::getTemplateConcurrencyToken -> Store is missing concurrency token for [" +
formKey +
"]";
}
return tok;
}
};