|
@@ -17,7 +17,7 @@
|
|
|
*
|
|
|
*/
|
|
|
|
|
|
-import { FormBuilder, FormGroup, FormControl, ValidatorFn, AsyncValidatorFn } from '@angular/forms';
|
|
|
+import { FormBuilder, FormGroup, FormArray, FormControl, ValidatorFn, AsyncValidatorFn } from '@angular/forms';
|
|
|
import { reduce } from 'lodash/fp';
|
|
|
import * as fmdModels from '../models/field.model';
|
|
|
|
|
@@ -31,17 +31,20 @@ interface AbstractControlOptions {
|
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------
|
|
|
-// Raw Model (or Mapped Raw Model) to Automatic Metadata
|
|
|
+// AutoMeta: Raw Model (or Mapped Raw Model) to Automatic Metadata
|
|
|
// ---------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
|
const isScalar = val => typeof val === 'boolean' || typeof val === 'number' || typeof val === 'string';
|
|
|
+const isArray = val => Array.isArray(val);
|
|
|
|
|
|
const keyValPairToMeta = (val, key) => ({ name: key, [isScalar(val) ? 'value' : 'meta']: val });
|
|
|
const keyValPairToMetaRecursive = ( [key, val] ) => {
|
|
|
- const innerVal = isScalar(val) ? val : autoMeta(val);
|
|
|
+ const innerVal = isScalar(val) ? val : (isArray(val) ? arrayToMeta(val) : autoMeta(val));
|
|
|
const metaVal = keyValPairToMeta(innerVal, key);
|
|
|
return [key, metaVal];
|
|
|
};
|
|
|
+const arrayToMeta = arrVals => arrVals.map(val => ({ name: val, 'value' : val }));
|
|
|
+
|
|
|
const autoMeta = model => Object.entries(model)
|
|
|
.map(keyValPairToMetaRecursive)
|
|
|
.reduce((res, [key, val]) => addProp(res, key, val), {});
|
|
@@ -121,14 +124,18 @@ const buildFormGroupFunctionFactory = (fb: FormBuilder): (meta) => FormGroup =>
|
|
|
});
|
|
|
const buildFormControl = metaF => new FormControl(buildControlState(metaF), buildValidators(metaF));
|
|
|
|
|
|
- // Build Form Group Member
|
|
|
+ // Build Form Array
|
|
|
+ const buildFormArray = (metaG): FormArray => fb.array(metaG.map(metaF => buildFormControl(metaF)));
|
|
|
+
|
|
|
+ // Build Form Group Member - builds a FormControl, FormArray, or another FormGroup which can contain any of these
|
|
|
const buildFormGroupMember = metaFoG => isGroup(metaFoG) ?
|
|
|
- _buildFormGroup(metaFoG.meta) :
|
|
|
+ (isArray(metaFoG.meta) ? buildFormArray(metaFoG.meta) : _buildFormGroup(metaFoG.meta)) :
|
|
|
buildFormControl(metaFoG);
|
|
|
|
|
|
const buildFormGroupReducerIteree = (res, metaFoG) => Object.assign(res, { [metaFoG.name]: buildFormGroupMember(metaFoG) });
|
|
|
const _buildFormGroup = _metaG => fb.group(reduce(buildFormGroupReducerIteree, {}, _metaG));
|
|
|
|
|
|
+ // The main function - builds FormGroups containing other FormGroups, FormArrays and FormControls
|
|
|
const buildFormGroup = metaG => {
|
|
|
// Ensure that we have Field-Specific Metadata, not raw Objects
|
|
|
const metaWithNameKeys = addMissingNames(metaG);
|
|
@@ -161,7 +168,7 @@ const addNameIfMissing = (metaFoG, key) => metaFoG.name ? metaFoG : addProp(meta
|
|
|
const addNameToSelfAndChildren = ( [key, metaFoG] ) => {
|
|
|
metaFoG = addNameIfMissing(metaFoG, key);
|
|
|
if (isGroup(metaFoG)) {
|
|
|
- metaFoG.meta = Array.isArray(metaFoG.meta) ? Object.values(addMissingNames(metaFoG.meta)) : addMissingNames(metaFoG.meta); // Recursion
|
|
|
+ metaFoG.meta = isArray(metaFoG.meta) ? Object.values(addMissingNames(metaFoG.meta)) : addMissingNames(metaFoG.meta); // Recursion
|
|
|
}
|
|
|
return [key, metaFoG];
|
|
|
};
|