All files / lib/dynamic-form dynamic-form-component.factory.ts

100% Statements 49/49
100% Branches 16/16
100% Functions 15/15
100% Lines 48/48

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124                                        1x 27x     9x   5x   1x   1x   2x 1x         6x       4x       2x       14x 14x 2x 1x   12x           6x 6x 6x           16x 15x 4x 4x 4x 4x 4x   11x 11x 11x           2x 2x 2x           4x 6x 6x 6x 6x 6x   4x           16x 16x   8x 8x 2x 1x   6x   7x       6x      
import { Injectable, ViewContainerRef } from '@angular/core';
import { DynamicFormAction } from '../dynamic-form-action/dynamic-form-action';
import { DynamicFormActionBase } from '../dynamic-form-action/dynamic-form-action-base';
import { DynamicFormActionType } from '../dynamic-form-action/dynamic-form-action-type';
import { DynamicFormConfigService } from '../dynamic-form-config/dynamic-form-config.service';
import { DynamicFormControl } from '../dynamic-form-control/dynamic-form-control';
import { DynamicFormElement } from '../dynamic-form-element/dynamic-form-element';
import { DynamicFormElementBase } from '../dynamic-form-element/dynamic-form-element-base';
import { DynamicFormElementType } from '../dynamic-form-element/dynamic-form-element-type';
import { DynamicFormError } from '../dynamic-form-error/dynamic-form-error';
import { DynamicFormErrorType } from '../dynamic-form-error/dynamic-form-error-type';
import { DynamicFormErrorHandler } from '../dynamic-form-error/dynamic-form-error.handler';
import { DynamicFormField } from '../dynamic-form-field/dynamic-form-field';
import { DynamicFormFieldBase } from '../dynamic-form-field/dynamic-form-field-base';
import { DynamicFormFieldType } from '../dynamic-form-field/dynamic-form-field-type';
import { DynamicFormFieldWrapperBase } from '../dynamic-form-field/dynamic-form-field-wrapper-base';
import { DynamicFormFieldWrapperType } from '../dynamic-form-field/dynamic-form-field-wrapper-type';
import { DynamicFormInputType } from '../dynamic-form-input/dynamic-form-input-type';
 
@Injectable()
export class DynamicFormComponentFactory {
  constructor(private configService: DynamicFormConfigService, private errorHandler: DynamicFormErrorHandler) {}
 
  createComponent(ref: ViewContainerRef, element: DynamicFormElement): DynamicFormElementBase | undefined {
    switch (element.classType) {
      case 'element':
        return this.createElementComponent(ref, element);
      case 'field':
        return this.createFieldComponent(ref, element as DynamicFormField);
      case 'action':
        return this.createActionComponent(ref, element as DynamicFormAction);
        default:
          this.handleError(DynamicFormErrorType.ClassType, `Class type ${ element.classType } is not defined`);
          return undefined;
    }
  }
 
  createElementComponent(ref: ViewContainerRef, element: DynamicFormElement): DynamicFormElementBase {
    return this.createElementComponentForType(ref, element, element.type);
  }
 
  createFieldComponent(ref: ViewContainerRef, field: DynamicFormField): DynamicFormFieldBase {
    return this.createFieldComponentForType(ref, field, field.type);
  }
 
  createActionComponent(ref: ViewContainerRef, action: DynamicFormAction): DynamicFormActionBase {
    return this.createActionComponentForType(ref, action, action.type);
  }
 
  createInputComponent(ref: ViewContainerRef, field: DynamicFormControl): DynamicFormFieldBase | undefined {
    const type = this.configService.getInputType(field.inputType);
    if (!type) {
      this.handleError(DynamicFormErrorType.InputType, `Input type ${ field.inputType } is not defined`);
      return undefined;
    }
    return this.createFieldComponentForType(ref, field, type, true);
  }
 
  private createElementComponentForType(
    ref: ViewContainerRef, element: DynamicFormElement, type: DynamicFormElementType,
  ): DynamicFormElementBase {
    const component = ref.createComponent(type.component).instance;
    component.element = element;
    return component;
  }
 
  private createFieldComponentForType(
    ref: ViewContainerRef, field: DynamicFormField, type: DynamicFormFieldType | DynamicFormInputType, isInput: boolean = false,
  ): DynamicFormFieldBase {
    const wrapperTypes = this.getWrapperTypes(field, type, isInput);
    if (wrapperTypes.length > 0) {
      const wrapperComponents = this.createWrapperComponents(ref, field, wrapperTypes);
      const wrapperComponent = wrapperComponents[wrapperComponents.length - 1];
      wrapperComponent.component = wrapperComponent.ref.createComponent(type.component).instance;
      wrapperComponent.component.field = field;
      return wrapperComponents[0];
    }
    const component = ref.createComponent(type.component).instance;
    component.field = field;
    return component;
  }
 
  private createActionComponentForType(
    ref: ViewContainerRef, action: DynamicFormAction, type: DynamicFormActionType,
  ): DynamicFormActionBase {
    const component = ref.createComponent(type.component).instance;
    component.action = action;
    return component;
  }
 
  private createWrapperComponents(
    ref: ViewContainerRef, field: DynamicFormField, types: DynamicFormFieldWrapperType[],
  ): DynamicFormFieldWrapperBase[] {
    const wrappers = types.reduce((result, type) => {
      const parentComponent = result[result.length - 1];
      const component = parentComponent.ref.createComponent(type.component).instance;
      parentComponent.component = component;
      component.field = field;
      return [ ...result, component ];
    }, [ { ref } ] as DynamicFormFieldWrapperBase[]);
    return wrappers.slice(1);
  }
 
  private getWrapperTypes(
    field: DynamicFormField, type: DynamicFormFieldType | DynamicFormInputType, isInput: boolean,
  ): DynamicFormFieldWrapperType[] {
    const wrappers = !isInput ? (field.wrappers || []).concat(type.wrappers || []) : type.wrappers || [];
    return wrappers
      .map(wrapper => {
        const wrapperType = this.configService.getFieldWrapperType(wrapper);
        if (!wrapperType) {
          this.handleError(DynamicFormErrorType.WrapperType, `Wrapper type ${ wrapper } is not defined`);
          return undefined;
        }
        return wrapperType;
      })
      .filter(wrapperType => !!wrapperType);
  }
 
  private handleError<ErrorType extends DynamicFormErrorType = DynamicFormErrorType>(type: ErrorType, message: string): void {
    this.errorHandler.handle(new DynamicFormError<ErrorType>(type, message));
  }
}