diff --git a/process/ViewTemplateData_lib/process.js b/process/ViewTemplateData_lib/process.js index 0343fcb2078c0b2f52f5ebaf0c73ebb17dfe2f4f..63bde856e266ec8724dfb4aa508934204e2298fc 100644 --- a/process/ViewTemplateData_lib/process.js +++ b/process/ViewTemplateData_lib/process.js @@ -1,6 +1,19 @@ /** - * Object to make the creation of a JSON for the Dynamic Form component simpler + * Object to make the creation of a JSON for the Dynamic Form component simpler. + * + * @example + * + * var form = new DynamicFormDefinition() + * .addField("title", "Title", null, null, true) + * .addField("productcode", "Product code") + * .addField("purchasedate", "Purchase date", "DATE") + * .addField("amount", "Amount", "NUMBER", 1); + * + * if (!vars.get("$field.DESCRIPTION")) + * form.addField("description", "Description", "LONG_TEXT"); + * + * result.string(form.toString()); //.toString() would also be called implicitly so it could be omitted * * @param {String} [pFormDefinition] an already existing form JSON as string */ @@ -12,7 +25,16 @@ function DynamicFormDefinition (pFormDefinition) this.fields = []; } -DynamicFormDefinition.prototype.addField = function (pId, pName, pContentType, pMandatory, pReadOnly, pIsReadable) +/** + * @param {String} pId unique ID of the field + * @param {String} pName title of the field + * @param {String} [pContentType] content type + * @param {String} [pValue] preset value + * @param {boolean} [pMandatory=false] mandatory + * @param {boolean} [pReadOnly=false] readOnly + * @param {boolean} [pIsReadable=true] readable + */ +DynamicFormDefinition.prototype.addField = function (pId, pName, pContentType, pValue, pMandatory, pReadOnly, pIsReadable) { //TODO: check if id is unique this.fields.push({ @@ -21,24 +43,40 @@ DynamicFormDefinition.prototype.addField = function (pId, pName, pContentType, p contentType : pContentType || "TEXT", isReadable : pIsReadable || true, isWritable : !pReadOnly, - isRequired : pMandatory || false + isRequired : pMandatory || false, + value : pValue === undefined ? null : pValue }); return this; } +/** + * + */ DynamicFormDefinition.prototype.removeField = function (pId) +{ + var index = this.indexOfField(pId); + if (index != -1) + this.fields.splice(index, 1); + + return this; +} + +/** + * + */ +DynamicFormDefinition.prototype.indexOfField = function (pId) { for (let i = 0, l = this.fields.length; i < l; i++) { if (this.fields[i].id == pId) - { - this.fields.splice(i, 1); - return this; - } + return i; } - return this; + return -1; } +/** + * + */ DynamicFormDefinition.prototype.toString = function () { return JSON.stringify(this.fields);