/*e slint-disable */ import store from "../store"; import gzapi from "./gzapi"; import _ from "../libs/lodash.min.js"; ///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 (var 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 { get(formKey) { return new Promise(function getFormTemplate(resolve) { if (!_.has(store.state.formCustomTemplate, formKey)) { //fetch and populate the store gzapi.get("formcustom/" + formKey).then(res => { if (res.error) { throw res.error; } store.commit("addFormCustomTemplateItem", { formKey: formKey, value: addDataKeyNames(JSON.parse(res.data.template)) }); resolve(); }); } }); } };