Added more comments

This commit is contained in:
Giuseppe Digilio
2019-03-20 19:57:07 +01:00
parent 825464bb9c
commit 6335d61dda
26 changed files with 1505 additions and 123 deletions

View File

@@ -156,7 +156,7 @@ describe('SectionFormOperationsService test suite', () => {
}
};
expect(service.isPartOfArrayOfGroup(model)).toBeTruthy();
expect(service.isPartOfArrayOfGroup(model as any)).toBeTruthy();
});
it('should return false when parent element doesn\'t belong to an array group element', () => {
@@ -164,7 +164,7 @@ describe('SectionFormOperationsService test suite', () => {
parent: null
};
expect(service.isPartOfArrayOfGroup(model)).toBeFalsy();
expect(service.isPartOfArrayOfGroup(model as any)).toBeFalsy();
});
});

View File

@@ -21,12 +21,35 @@ import { FormFieldMetadataValueObject } from '../../../shared/form/builder/model
import { DynamicQualdropModel } from '../../../shared/form/builder/ds-dynamic-form-ui/models/ds-dynamic-qualdrop.model';
import { DynamicRelationGroupModel } from '../../../shared/form/builder/ds-dynamic-form-ui/models/relation-group/dynamic-relation-group.model';
/**
* The service handling all form section operations
*/
@Injectable()
export class SectionFormOperationsService {
constructor(private formBuilder: FormBuilderService, private operationsBuilder: JsonPatchOperationsBuilder) {
/**
* Initialize service variables
*
* @param {FormBuilderService} formBuilder
* @param {JsonPatchOperationsBuilder} operationsBuilder
*/
constructor(
private formBuilder: FormBuilderService,
private operationsBuilder: JsonPatchOperationsBuilder) {
}
/**
* Dispatch properly method based on form operation type
*
* @param pathCombiner
* the [[JsonPatchOperationPathCombiner]] object for the specified operation
* @param event
* the [[DynamicFormControlEvent]] for the specified operation
* @param previousValue
* the [[FormFieldPreviousValueObject]] for the specified operation
* @param hasStoredValue
* representing if field value related to the specified operation has stored value
*/
public dispatchOperationsFromEvent(pathCombiner: JsonPatchOperationPathCombiner,
event: DynamicFormControlEvent,
previousValue: FormFieldPreviousValueObject,
@@ -43,6 +66,14 @@ export class SectionFormOperationsService {
}
}
/**
* Return index if specified field is part of fields array
*
* @param event
* the [[DynamicFormControlEvent]] for the specified operation
* @return number
* the array index is part of array, zero otherwise
*/
public getArrayIndexFromEvent(event: DynamicFormControlEvent): number {
let fieldIndex: number;
if (isNotEmpty(event)) {
@@ -60,7 +91,15 @@ export class SectionFormOperationsService {
return isNotUndefined(fieldIndex) ? fieldIndex : 0;
}
public isPartOfArrayOfGroup(model: any): boolean {
/**
* Check if specified model is part of array of group
*
* @param model
* the [[DynamicFormControlModel]] model
* @return boolean
* true if is part of array, false otherwise
*/
public isPartOfArrayOfGroup(model: DynamicFormControlModel): boolean {
return (isNotNull(model.parent)
&& (model.parent as any).type === DYNAMIC_FORM_CONTROL_TYPE_GROUP
&& (model.parent as any).parent
@@ -68,7 +107,15 @@ export class SectionFormOperationsService {
&& (model.parent as any).parent.context.type === DYNAMIC_FORM_CONTROL_TYPE_ARRAY);
}
public getQualdropValueMap(event): Map<string, any> {
/**
* Return a map for the values of a Qualdrop field
*
* @param event
* the [[DynamicFormControlEvent]] for the specified operation
* @return Map<string, any>
* the map of values
*/
public getQualdropValueMap(event: DynamicFormControlEvent): Map<string, any> {
const metadataValueMap = new Map();
const context = this.formBuilder.isQualdropGroup(event.model)
@@ -87,12 +134,28 @@ export class SectionFormOperationsService {
return metadataValueMap;
}
/**
* Return the absolute path for the field interesting in the specified operation
*
* @param event
* the [[DynamicFormControlEvent]] for the specified operation
* @return string
* the field path
*/
public getFieldPathFromEvent(event: DynamicFormControlEvent): string {
const fieldIndex = this.getArrayIndexFromEvent(event);
const fieldId = this.getFieldPathSegmentedFromChangeEvent(event);
return (isNotUndefined(fieldIndex)) ? fieldId + '/' + fieldIndex : fieldId;
}
/**
* Return the absolute path for the Qualdrop field interesting in the specified operation
*
* @param event
* the [[DynamicFormControlEvent]] for the specified operation
* @return string
* the field path
*/
public getQualdropItemPathFromEvent(event: DynamicFormControlEvent): string {
const fieldIndex = this.getArrayIndexFromEvent(event);
const metadataValueMap = new Map();
@@ -117,6 +180,14 @@ export class SectionFormOperationsService {
return path;
}
/**
* Return the segmented path for the field interesting in the specified change operation
*
* @param event
* the [[DynamicFormControlEvent]] for the specified operation
* @return string
* the field path
*/
public getFieldPathSegmentedFromChangeEvent(event: DynamicFormControlEvent): string {
let fieldId;
if (this.formBuilder.isQualdropGroup(event.model as DynamicFormControlModel)) {
@@ -129,6 +200,14 @@ export class SectionFormOperationsService {
return fieldId;
}
/**
* Return the value of the field interesting in the specified change operation
*
* @param event
* the [[DynamicFormControlEvent]] for the specified operation
* @return any
* the field value
*/
public getFieldValueFromChangeEvent(event: DynamicFormControlEvent): any {
let fieldValue;
const value = (event.model as any).value;
@@ -142,12 +221,12 @@ export class SectionFormOperationsService {
if ((event.model as DsDynamicInputModel).hasAuthority) {
if (Array.isArray(value)) {
value.forEach((authority, index) => {
authority = Object.assign(new AuthorityValue(), authority, {language});
authority = Object.assign(new AuthorityValue(), authority, { language });
value[index] = authority;
});
fieldValue = value;
} else {
fieldValue = Object.assign(new AuthorityValue(), value, {language});
fieldValue = Object.assign(new AuthorityValue(), value, { language });
}
} else {
// Language without Authority (input, textArea)
@@ -162,6 +241,14 @@ export class SectionFormOperationsService {
return fieldValue;
}
/**
* Return a map for the values of an array of field
*
* @param items
* the list of items
* @return Map<string, any>
* the map of values
*/
public getValueMap(items: any[]): Map<string, any> {
const metadataValueMap = new Map();
@@ -177,6 +264,16 @@ export class SectionFormOperationsService {
return metadataValueMap;
}
/**
* Handle form remove operations
*
* @param pathCombiner
* the [[JsonPatchOperationPathCombiner]] object for the specified operation
* @param event
* the [[DynamicFormControlEvent]] for the specified operation
* @param previousValue
* the [[FormFieldPreviousValueObject]] for the specified operation
*/
protected dispatchOperationsFromRemoveEvent(pathCombiner: JsonPatchOperationPathCombiner,
event: DynamicFormControlEvent,
previousValue: FormFieldPreviousValueObject): void {
@@ -189,6 +286,18 @@ export class SectionFormOperationsService {
}
}
/**
* Handle form change operations
*
* @param pathCombiner
* the [[JsonPatchOperationPathCombiner]] object for the specified operation
* @param event
* the [[DynamicFormControlEvent]] for the specified operation
* @param previousValue
* the [[FormFieldPreviousValueObject]] for the specified operation
* @param hasStoredValue
* representing if field value related to the specified operation has stored value
*/
protected dispatchOperationsFromChangeEvent(pathCombiner: JsonPatchOperationPathCombiner,
event: DynamicFormControlEvent,
previousValue: FormFieldPreviousValueObject,
@@ -243,6 +352,18 @@ export class SectionFormOperationsService {
}
}
/**
* Handle form operations interesting a field with a map as value
*
* @param valueMap
* map of values
* @param pathCombiner
* the [[JsonPatchOperationPathCombiner]] object for the specified operation
* @param event
* the [[DynamicFormControlEvent]] for the specified operation
* @param previousValue
* the [[FormFieldPreviousValueObject]] for the specified operation
*/
protected dispatchOperationsFromMap(valueMap: Map<string, any>,
pathCombiner: JsonPatchOperationPathCombiner,
event: DynamicFormControlEvent,

View File

@@ -42,46 +42,55 @@ export class FormSectionComponent extends SectionModelComponent {
/**
* The form id
* @type {string}
*/
public formId;
public formId: string;
/**
* The form model
* @type {DynamicFormControlModel[]}
*/
public formModel: DynamicFormControlModel[];
/**
* A boolean representing if this section is updating
* @type {boolean}
*/
public isUpdating = false;
/**
* A boolean representing if this section is loading
* @type {boolean}
*/
public isLoading = true;
/**
* The form config
* @type {SubmissionFormsModel}
*/
protected formConfig: SubmissionFormsModel;
/**
* The form data
* @type {any}
*/
protected formData: any = Object.create({});
/**
* The [JsonPatchOperationPathCombiner] object
* @type {JsonPatchOperationPathCombiner}
*/
protected pathCombiner: JsonPatchOperationPathCombiner;
/**
* The [FormFieldPreviousValueObject] object
* @type {FormFieldPreviousValueObject}
*/
protected previousValue: FormFieldPreviousValueObject = new FormFieldPreviousValueObject();
/**
* The list of Subscription
* @type {Array}
*/
protected subs: Subscription[] = [];
@@ -92,6 +101,7 @@ export class FormSectionComponent extends SectionModelComponent {
/**
* Initialize instance variables
*
* @param {ChangeDetectorRef} cdr
* @param {FormBuilderService} formBuilderService
* @param {SectionFormOperationsService} formOperationsService
@@ -158,6 +168,9 @@ export class FormSectionComponent extends SectionModelComponent {
/**
* Get section status
*
* @return Observable<boolean>
* the section status
*/
protected getSectionStatus(): Observable<boolean> {
return this.formService.isValid(this.formId);
@@ -290,6 +303,9 @@ export class FormSectionComponent extends SectionModelComponent {
/**
* Method called when a form dfChange event is fired.
* Dispatch form operations based on changes.
*
* @param event
* the [[DynamicFormControlEvent]] emitted
*/
onChange(event: DynamicFormControlEvent): void {
this.formOperationsService.dispatchOperationsFromEvent(
@@ -308,6 +324,9 @@ export class FormSectionComponent extends SectionModelComponent {
/**
* Method called when a form dfFocus event is fired.
* Initialize [FormFieldPreviousValueObject] instance.
*
* @param event
* the [[DynamicFormControlEvent]] emitted
*/
onFocus(event: DynamicFormControlEvent): void {
const value = this.formOperationsService.getFieldValueFromChangeEvent(event);
@@ -324,6 +343,9 @@ export class FormSectionComponent extends SectionModelComponent {
/**
* Method called when a form remove event is fired.
* Dispatch form operations based on changes.
*
* @param event
* the [[DynamicFormControlEvent]] emitted
*/
onRemove(event: DynamicFormControlEvent): void {
this.formOperationsService.dispatchOperationsFromEvent(