|
@@ -26,7 +26,6 @@ import { FormBuilder, FormGroup, FormArray, FormControl, AbstractControlOptions
|
|
|
import { cloneDeep, omit, reduce } from 'lodash/fp';
|
|
|
import * as fmdModels from '../models/field.model';
|
|
|
|
|
|
-
|
|
|
// ---------------------------------------------------------------------------------------------------------------------
|
|
|
// AutoMeta: Generate Automatic Metadata from a model
|
|
|
// ---------------------------------------------------------------------------------------------------------------------
|
|
@@ -160,61 +159,68 @@ const getRCBaseObjectWithAllKeys = (metaFoG, extraMeta, createFromExtra = false)
|
|
|
// Build Form-Field-Type-Specific Metadata (using the field models in dynaform/models)
|
|
|
// ---------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
|
-const resolveType = (metaFoG: StringMap<any>): string => {
|
|
|
- if (metaFoG.type) {
|
|
|
- return metaFoG.type;
|
|
|
- }
|
|
|
- if (isContainer(metaFoG)) {
|
|
|
- return 'container';
|
|
|
- }
|
|
|
- if (isRepeatingContainer(metaFoG)) {
|
|
|
- return 'repeatingContainer';
|
|
|
- }
|
|
|
- return 'text';
|
|
|
-}
|
|
|
+// MAYBE CHANGE INTO A MODULE SO WE CAN USE CLOSURE BUT ALSO METHODS INSIDE IT
|
|
|
+// More Elegant But Not Urgent
|
|
|
|
|
|
-const buildFieldClassName = (t: string): string => {
|
|
|
- const start = t[0].toUpperCase() + t.slice(1);
|
|
|
- if (start === 'Container' || start === 'RepeatingContainer' || start === 'Heading' || t.slice(-5) === 'Group') {
|
|
|
- return start;
|
|
|
- }
|
|
|
- return start + 'Field';
|
|
|
-};
|
|
|
+const buildModeledFieldGroupMember = metaFog => metaFog; // THIS MAY BREAK THINGS NOW WE'VE MOVED FUNCTION INTO CLOSURE BELOW
|
|
|
|
|
|
-const buildModeledField = (metaFoG, context) => {
|
|
|
- const type = resolveType(metaFoG);
|
|
|
- const className = buildFieldClassName(type);
|
|
|
- if (!fmdModels[className]) {
|
|
|
- throw new Error(`No metadata model "${className}" for type "${type}"`);
|
|
|
- }
|
|
|
- return new fmdModels[className](metaFoG, context);
|
|
|
-};
|
|
|
+const buildFieldSpecificMetaInClosure = (metaG, context) => {
|
|
|
|
|
|
-// Build Form Group Member
|
|
|
-const buildModeledFieldGroupMember = (metaFoG, context = {}) => {
|
|
|
- const modeledGroupMember = buildModeledField(metaFoG, context);
|
|
|
- if (isContainer(metaFoG)) {
|
|
|
- modeledGroupMember.meta = _buildFieldSpecificMeta(modeledGroupMember.meta);
|
|
|
- } else if (isRepeatingContainer(metaFoG)) {
|
|
|
- modeledGroupMember.meta = modeledGroupMember.meta.map(rcMem => ({ ...rcMem, meta: _buildFieldSpecificMeta(rcMem.meta) }));
|
|
|
- modeledGroupMember.__defaultContainer = {
|
|
|
- ...modeledGroupMember.meta[0],
|
|
|
- meta: _buildFieldSpecificMeta(modeledGroupMember.__defaultContainer),
|
|
|
- name: '__defaultContainer',
|
|
|
- button: ''
|
|
|
- };
|
|
|
+ const resolveType = (metaFoG: StringMap<any>): string => {
|
|
|
+ if (metaFoG.type) {
|
|
|
+ return metaFoG.type;
|
|
|
+ }
|
|
|
+ if (isContainer(metaFoG)) {
|
|
|
+ return 'container';
|
|
|
+ }
|
|
|
+ if (isRepeatingContainer(metaFoG)) {
|
|
|
+ return 'repeatingContainer';
|
|
|
+ }
|
|
|
+ return 'text';
|
|
|
}
|
|
|
- return modeledGroupMember;
|
|
|
-};
|
|
|
-
|
|
|
-// Build Form Group
|
|
|
- const buildModeledFieldGroupReducerIteree = (res, metaFoG) => ({ ...res, [metaFoG.name]: buildModeledFieldGroupMember(metaFoG) });
|
|
|
-const _buildFieldSpecificMeta = metaG => isRepeatingContainer(metaG) ?
|
|
|
- metaG.map(rcMem => _buildFieldSpecificMeta(rcMem)) :
|
|
|
- reduce(buildModeledFieldGroupReducerIteree, {}, metaG);
|
|
|
-const buildFieldSpecificMeta = metaG => {
|
|
|
- const withNames = addMissingNames(metaG);
|
|
|
- return _buildFieldSpecificMeta(addMissingNames(metaG));
|
|
|
+
|
|
|
+ const buildFieldClassName = (t: string): string => {
|
|
|
+ const start = t[0].toUpperCase() + t.slice(1);
|
|
|
+ if (start === 'Container' || start === 'RepeatingContainer' || start === 'Heading' || t.slice(-5) === 'Group') {
|
|
|
+ return start;
|
|
|
+ }
|
|
|
+ return start + 'Field';
|
|
|
+ };
|
|
|
+
|
|
|
+ const buildModeledField = (metaFoG) => {
|
|
|
+ const type = resolveType(metaFoG);
|
|
|
+ const className = buildFieldClassName(type);
|
|
|
+ if (!fmdModels[className]) {
|
|
|
+ throw new Error(`No metadata model "${className}" for type "${type}"`);
|
|
|
+ }
|
|
|
+ return new fmdModels[className](metaFoG, context);
|
|
|
+ };
|
|
|
+
|
|
|
+ // Build Form Group Member
|
|
|
+ const buildModeledFieldGroupMember = (metaFoG) => {
|
|
|
+ const modeledGroupMember = buildModeledField(metaFoG);
|
|
|
+ if (isContainer(metaFoG)) {
|
|
|
+ modeledGroupMember.meta = _buildFieldSpecificMeta(modeledGroupMember.meta);
|
|
|
+ } else if (isRepeatingContainer(metaFoG)) {
|
|
|
+ modeledGroupMember.meta = modeledGroupMember.meta.map(rcMem => ({ ...rcMem, meta: _buildFieldSpecificMeta(rcMem.meta) }));
|
|
|
+ modeledGroupMember.__defaultContainer = {
|
|
|
+ ...modeledGroupMember.meta[0],
|
|
|
+ meta: _buildFieldSpecificMeta(modeledGroupMember.__defaultContainer),
|
|
|
+ name: '__defaultContainer',
|
|
|
+ button: ''
|
|
|
+ };
|
|
|
+ }
|
|
|
+ return modeledGroupMember;
|
|
|
+ };
|
|
|
+
|
|
|
+ // Build Form Group
|
|
|
+ const buildModeledFieldGroupReducerIteree = (res, metaFoG) => ({ ...res, [metaFoG.name]: buildModeledFieldGroupMember(metaFoG) });
|
|
|
+ const _buildFieldSpecificMeta = metaG => isRepeatingContainer(metaG) ?
|
|
|
+ metaG.map(rcMem => _buildFieldSpecificMeta(rcMem)) :
|
|
|
+ reduce(buildModeledFieldGroupReducerIteree, {}, metaG);
|
|
|
+ const buildFieldSpecificMeta = metaG => _buildFieldSpecificMeta(addMissingNames(metaG));
|
|
|
+
|
|
|
+ return buildFieldSpecificMeta(metaG);
|
|
|
}
|
|
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------
|
|
@@ -383,7 +389,8 @@ const buildFormGroupFunctionFactory = (fb: FormBuilder): (meta) => FormGroup =>
|
|
|
// Ensure that we have Field-Specific Metadata, not raw Objects
|
|
|
const metaWithNameKeys = addMissingNames(metaG); // <!--- DO WE REALLY HAVE TO CALL addMissingManes again here - it should have been done already?
|
|
|
// MAYBE only run this if first entry isn't right, for reasons of efficiency
|
|
|
- const fieldModeledMeta = addMissingFieldSpecificMeta(metaWithNameKeys);
|
|
|
+ // const fieldModeledMeta = addMissingFieldSpecificMeta(metaWithNameKeys);
|
|
|
+ const fieldModeledMeta = metaWithNameKeys;
|
|
|
return _buildFormGroup(fieldModeledMeta);
|
|
|
};
|
|
|
return buildFormGroup;
|
|
@@ -454,6 +461,13 @@ const generateNewModel = (originalModel, updates) => {
|
|
|
|
|
|
const updateObject = (obj, updates, createAdditionalKeys = false) => {
|
|
|
// THIS DOES NOT MUTATE obj, instead returning a new object
|
|
|
+ if (typeof obj !== 'object') {
|
|
|
+ obj = {};
|
|
|
+ }
|
|
|
+ console.log('obj is', obj, typeof obj);
|
|
|
+ if (Object.keys(obj).length === 0) {
|
|
|
+ createAdditionalKeys = true;
|
|
|
+ }
|
|
|
const shallowClone = { ...obj };
|
|
|
Object.entries(updates).forEach(([key, val]) => safeSet(shallowClone, key, val, createAdditionalKeys));
|
|
|
return shallowClone;
|
|
@@ -468,8 +482,9 @@ const safeSet = (obj, key, val, createAdditionalKeys = false) => {
|
|
|
if (val === currentVal) {
|
|
|
return;
|
|
|
}
|
|
|
- if (nullOrScaler(currentVal)) {
|
|
|
- console.log('safeSet nullOrScaler', key, val);
|
|
|
+
|
|
|
+ if (undefinedNullOrScalar(currentVal)) {
|
|
|
+ console.log('safeSet undefinedNullOrScalar', key, val);
|
|
|
obj[key] = val;
|
|
|
} else {
|
|
|
if (Array.isArray(currentVal)) {
|
|
@@ -492,8 +507,8 @@ const safeSet = (obj, key, val, createAdditionalKeys = false) => {
|
|
|
}
|
|
|
};
|
|
|
|
|
|
-const nullOrScaler = val => {
|
|
|
- if (val === null) { return true; }
|
|
|
+const undefinedNullOrScalar = val => {
|
|
|
+ if (val === null || val === undefined) { return true; }
|
|
|
const t = typeof val;
|
|
|
return t === 'number' || t === 'string' || t === 'boolean';
|
|
|
};
|
|
@@ -557,6 +572,6 @@ const addMissingFieldSpecificMeta = metaG => Object.entries(metaG)
|
|
|
|
|
|
export {
|
|
|
autoMeta, combineModelWithMeta, combineExtraMeta, execMetaReorderingInstructions,
|
|
|
- buildFieldSpecificMeta, extractFieldMappings, buildFormGroupFunctionFactory,
|
|
|
+ buildFieldSpecificMetaInClosure, extractFieldMappings, buildFormGroupFunctionFactory,
|
|
|
generateNewModel
|
|
|
};
|