|
@@ -1,6 +1,7 @@
|
|
import { FormBuilder, FormControl } from '@angular/forms';
|
|
import { FormBuilder, FormControl } from '@angular/forms';
|
|
import { reduce } from 'lodash/fp';
|
|
import { reduce } from 'lodash/fp';
|
|
import * as fmdModels from './../models';
|
|
import * as fmdModels from './../models';
|
|
|
|
+import { Container } from '@angular/compiler/src/i18n/i18n_ast';
|
|
|
|
|
|
/*
|
|
/*
|
|
* FORM UTILITIES
|
|
* FORM UTILITIES
|
|
@@ -31,6 +32,10 @@ const addProp = (obj, key, val) => { obj[key] = val; return obj; };
|
|
// Helper function to distinguish group from field
|
|
// Helper function to distinguish group from field
|
|
const isGroup = metaFoG => metaFoG.meta;
|
|
const isGroup = metaFoG => metaFoG.meta;
|
|
|
|
|
|
|
|
+// Is Container
|
|
|
|
+// Helper function to distinguish container group
|
|
|
|
+const isContainer = metaFoG => metaFoG.type === 'container';
|
|
|
|
+
|
|
// ---------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------
|
|
// Add Missing Names
|
|
// Add Missing Names
|
|
// Helper function to add any missing 'name' properties to Fields and Groups using property's key, recursively
|
|
// Helper function to add any missing 'name' properties to Fields and Groups using property's key, recursively
|
|
@@ -76,25 +81,43 @@ const combineExtraMeta = (metaG, metaExtra) => {
|
|
combineMetaForField(metaG[key], val);
|
|
combineMetaForField(metaG[key], val);
|
|
}
|
|
}
|
|
});
|
|
});
|
|
- return {...metaG, ...combinedMeta};
|
|
|
|
|
|
+ return { ...metaG, ...combinedMeta };
|
|
};
|
|
};
|
|
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------
|
|
// Build Modelled Metadata - Form Metadata Factory - UNDERWAY
|
|
// Build Modelled Metadata - Form Metadata Factory - UNDERWAY
|
|
|
|
|
|
|
|
+const buildClassName = (t = 'text') => {
|
|
|
|
+ const start = t[0].toUpperCase() + t.slice(1);
|
|
|
|
+ if (start === 'Container' || t.slice(-5) === 'Group') {
|
|
|
|
+ return start;
|
|
|
|
+ }
|
|
|
|
+ return start + 'Field';
|
|
|
|
+};
|
|
|
|
+
|
|
const buildModeledField = metaF => {
|
|
const buildModeledField = metaF => {
|
|
// TODO: Add validity check
|
|
// TODO: Add validity check
|
|
- const modeledMetaF = new fmdModels[metaF.type](metaF);
|
|
|
|
|
|
+ const fieldType = buildClassName(metaF.type);
|
|
|
|
+ if (!fmdModels[fieldType]) {
|
|
|
|
+ throw new Error('Unknown Field Type');
|
|
|
|
+ }
|
|
|
|
+ const modeledMetaF = new fmdModels[fieldType](metaF);
|
|
return modeledMetaF;
|
|
return modeledMetaF;
|
|
};
|
|
};
|
|
|
|
|
|
// Build Form Group Member
|
|
// Build Form Group Member
|
|
-const buildModeledGroupMember = metaFoG => isGroup(metaFoG) ? _buildModeledGroup(metaFoG.meta) : buildModeledField(metaFoG);
|
|
|
|
|
|
+const buildModeledGroupMember = metaFoG => {
|
|
|
|
+ const modeledGroupMember = buildModeledField(metaFoG);
|
|
|
|
+ if (isContainer(metaFoG)) {
|
|
|
|
+ modeledGroupMember.meta = _buildModeledGroup(metaFoG.meta);
|
|
|
|
+ }
|
|
|
|
+ return modeledGroupMember;
|
|
|
|
+};
|
|
|
|
|
|
// Build Form Group
|
|
// Build Form Group
|
|
const buildModeledGroupReducerIteree = (res, metaFoG) => Object.assign(res, { [metaFoG.name]: buildModeledGroupMember(metaFoG) });
|
|
const buildModeledGroupReducerIteree = (res, metaFoG) => Object.assign(res, { [metaFoG.name]: buildModeledGroupMember(metaFoG) });
|
|
const _buildModeledGroup = metaG => reduce(buildModeledGroupReducerIteree, {}, metaG);
|
|
const _buildModeledGroup = metaG => reduce(buildModeledGroupReducerIteree, {}, metaG);
|
|
-const buildModedMeta = metaG => _buildModeledGroup(metaG);
|
|
|
|
|
|
+const buildModeledMeta = metaG => _buildModeledGroup(metaG);
|
|
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------
|
|
// Functions which build FormControls and FormGroups
|
|
// Functions which build FormControls and FormGroups
|
|
@@ -115,9 +138,9 @@ const buildFormGroupMember = metaFoG => isGroup(metaFoG) ? _buildFormGroup(metaF
|
|
// Build Form Group
|
|
// Build Form Group
|
|
const buildFormGroupReducerIteree = (res, metaFoG) => Object.assign(res, { [metaFoG.name]: buildFormGroupMember(metaFoG) });
|
|
const buildFormGroupReducerIteree = (res, metaFoG) => Object.assign(res, { [metaFoG.name]: buildFormGroupMember(metaFoG) });
|
|
const _buildFormGroup = metaG => fb.group(reduce(buildFormGroupReducerIteree, {}, metaG));
|
|
const _buildFormGroup = metaG => fb.group(reduce(buildFormGroupReducerIteree, {}, metaG));
|
|
-const buildFormGroup = metaG => _buildFormGroup(metaG);
|
|
|
|
|
|
+const buildFormGroup = metaG => _buildFormGroup(addMissingNames(metaG));
|
|
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------
|
|
// Exports
|
|
// Exports
|
|
|
|
|
|
-export { autoMeta, combineExtraMeta, buildModedMeta, buildFormGroup };
|
|
|
|
|
|
+export { autoMeta, combineExtraMeta, buildModeledMeta, buildFormGroup };
|