|
6 | 6 | <!-- TODO is it better to have the formio div in the <form> element or outside of it? --> |
7 | 7 | <div id="formio"></div> |
8 | 8 |
|
9 | | - <script cam-script type="text/form-script"> |
| 9 | + <script cam-script type= "text/form-script"> |
10 | 10 | <!-- <script>--> |
11 | 11 |
|
12 | 12 |
|
13 | 13 | let submissionSuffix = '_submission' |
14 | | - let startFormVariableName = 'startForm' + submissionSuffix; |
| 14 | + let startFormVariableName = 'startForm' + submissionSuffix |
15 | 15 | let formLocationDeploymentParam = 'deployment' |
16 | | - let formLocationLocalPathParam = "path" |
17 | | - let fetchVariableKey = 'fetchVariable'; |
18 | | - let camVariableNameKey = "camVariableName" |
19 | | - let stringifyKey = "stringify" |
20 | | - let formioDivId = "formio" |
21 | | - let submitAsTransientVariableParam = "transient" |
| 16 | + let formLocationLocalPathParam = 'path' |
| 17 | + let fetchVariableKey = 'fetchVariable' |
| 18 | + let camVariableNameKey = 'camVariableName' |
| 19 | + let stringifyKey = 'stringify' |
| 20 | + let formioDivId = 'formio' |
| 21 | + let submitAsTransientVariableParam = 'transient' |
22 | 22 | let submitAsTransientVariableDefault = false |
23 | | - let submissionVariableNameParam = "var" |
| 23 | + let submissionVariableNameParam = 'var' |
24 | 24 | let removePrivateValidations = true |
25 | | - let firstSubmit = true; |
| 25 | + let firstSubmit = true |
| 26 | + let subFormKey = 'subform' |
| 27 | + let subFormHideButtonsKey = 'hideButtons' |
26 | 28 |
|
27 | 29 | function getParameterByName(name, url) { |
28 | 30 | if (!url) url = window.location.href; |
|
34 | 36 | return decodeURIComponent(results[2].replace(/\+/g, ' ')); |
35 | 37 | } |
36 | 38 |
|
| 39 | + /** |
| 40 | + * Removes private/secret marked validations from the provided schema. |
| 41 | + */ |
37 | 42 | function cleanPrivateValidations(schema) { |
38 | | - FormioUtils.eachComponent(schema.components, component => { |
39 | | - if (component.validate && component.validate.customPrivate) { |
40 | | - delete component.validate.custom; |
41 | | - } |
42 | | - }) |
| 43 | + if (removePrivateValidations){ |
| 44 | + FormioUtils.eachComponent(schema.components, component => { |
| 45 | + if (component.validate && component.validate.customPrivate) { |
| 46 | + delete component.validate.custom; |
| 47 | + } |
| 48 | + }) |
| 49 | + } |
43 | 50 | } |
44 | 51 |
|
45 | | - function getSchema() { |
| 52 | + /** |
| 53 | + * Returns a formio schema object |
| 54 | + * @returns {Promise<formioShchema>} |
| 55 | + */ |
| 56 | + function getSchema(processDefId, formUrl) { |
46 | 57 | return new Promise(resolve => { |
47 | 58 | inject(['$http', 'Uri', function ($http, Uri) { |
48 | | - let processDefId = camForm.processDefinitionId; |
| 59 | + // let processDefId = camForm.processDefinitionId; |
49 | 60 |
|
50 | 61 | $http.get(Uri.appUri('engine://engine/:engine/process-definition/' + processDefId)).then(function (response) { |
51 | 62 |
|
52 | | - let formNameDeployment = getParameterByName(formLocationDeploymentParam, camForm.options.formUrl); |
| 63 | + // let formNameDeployment = getParameterByName(formLocationDeploymentParam, camForm.options.formUrl); |
| 64 | + let formNameDeployment = getParameterByName(formLocationDeploymentParam, formUrl); |
53 | 65 | let formLocDeployment = function () { |
54 | 66 | return formNameDeployment !== null; |
55 | 67 | } |
56 | 68 |
|
57 | | - let formNamePath = getParameterByName(formLocationLocalPathParam, camForm.options.formUrl); |
| 69 | + // let formNamePath = getParameterByName(formLocationLocalPathParam, camForm.options.formUrl); |
| 70 | + let formNamePath = getParameterByName(formLocationLocalPathParam, formUrl); |
58 | 71 | let formLocPath = function () { |
59 | 72 | return formNamePath !== null; |
60 | 73 | } |
61 | 74 |
|
62 | 75 | if (formLocDeployment() === false && formLocPath() === false) { |
63 | | - console.error("No valid form json location was found for formio.") |
| 76 | + //@TODO bubble this to formio errors |
| 77 | + console.error("No valid form json location was found for formio. (could not deployment or path params in form key)") |
64 | 78 | } |
65 | 79 |
|
66 | 80 | let formName = formLocDeployment() === true ? formNameDeployment : formNamePath |
|
103 | 117 |
|
104 | 118 |
|
105 | 119 | function getVariables(varNames) { |
| 120 | + //@TODO Add error handling when variables are not found. Should be bubbled to formio form errors view. |
106 | 121 | return new Promise(resolve => { |
107 | 122 | if (camForm.taskId != null) { |
108 | 123 | inject(['$http', 'Uri', function ($http, Uri) { |
|
139 | 154 | function getDefaultValues(variablesStore, submissionObject, schema){ |
140 | 155 | let defaultValues = {data: {}} |
141 | 156 | FormioUtils.eachComponent(schema.components, component => { |
142 | | - if (component.hasOwnProperty("properties")){ |
| 157 | + if (component.hasOwnProperty('properties')){ |
143 | 158 | if (component.properties.hasOwnProperty(camVariableNameKey)) { |
144 | 159 | let defaultValue = _.get(variablesStore, component.properties[camVariableNameKey]) |
145 | 160 | let key = component.key |
146 | 161 |
|
147 | | - if (component.properties[stringifyKey] === "true"){ |
| 162 | + if (component.properties[stringifyKey] === 'true'){ |
148 | 163 | // @TODO do a check if the value is a object! |
149 | 164 | defaultValue = JSON.stringify(defaultValue) |
150 | 165 | } |
|
192 | 207 | variableManager.createVariable(submissionVariable); |
193 | 208 | } |
194 | 209 |
|
| 210 | + /** |
| 211 | + * Used to clean buttons in schemas / typically subforms |
| 212 | + * Hides Submit buttons |
| 213 | + * Hides all buttons if hideAllButtons equals 'true' (string) |
| 214 | + * |
| 215 | + */ |
| 216 | + function cleanButtons(schema, hideAllButtons){ |
| 217 | + FormioUtils.eachComponent(schema.components, component => { |
| 218 | + if (component.type === 'button') { |
| 219 | + if (hideAllButtons === 'true') { |
| 220 | + component.hidden = true |
| 221 | + } else { |
| 222 | + if (component.action === 'submit' || !component.action) { |
| 223 | + component.hidden = true |
| 224 | + } |
| 225 | + } |
| 226 | + } |
| 227 | + }) |
| 228 | + } |
| 229 | + |
| 230 | + /** |
| 231 | + * Injects subForm components into container form components that have the subForm property |
| 232 | + * @param schema |
| 233 | + * @returns {Promise<undefined>} |
| 234 | + */ |
| 235 | + function setupContainerSubForms(schema){ |
| 236 | + return new Promise(resolve => { |
| 237 | + let promises = [] |
| 238 | + FormioUtils.eachComponent(schema.components, component => { |
| 239 | + if (component.type === 'container' && _.has(component.properties, subFormKey)){ |
| 240 | + let sub = getSchema(camForm.processDefinitionId, '?' + component.properties[subFormKey]).then(subSchema => { |
| 241 | + cleanButtons(subSchema, component.properties[subFormHideButtonsKey]) |
| 242 | + component.components = component.components.concat(subSchema.components) |
| 243 | + }) |
| 244 | + promises.push(sub) |
| 245 | + } |
| 246 | + }, true) |
| 247 | + Promise.all(promises).then(() => { |
| 248 | + resolve() |
| 249 | + }) |
| 250 | + }) |
| 251 | + } |
| 252 | + |
195 | 253 |
|
196 | 254 | camForm.on('form-loaded', function () { |
197 | 255 | $scope.options.hideCompleteButton = true |
198 | | - getSchema().then(schema => { |
| 256 | + getSchema(camForm.processDefinitionId, camForm.options.formUrl).then(schema => { |
199 | 257 | let varNames = [] |
200 | 258 | FormioUtils.eachComponent(schema.components, component => { |
201 | 259 | if (_.has(component.properties, fetchVariableKey)){ |
|
213 | 271 | return details |
214 | 272 | }) |
215 | 273 |
|
| 274 | + }).then(details => { |
| 275 | + return setupContainerSubForms(details.schema).then(() => { |
| 276 | + return details |
| 277 | + }) |
216 | 278 | }).then(details => { |
217 | 279 | // TODO add a loading text |
218 | 280 | try { |
|
0 commit comments