All files / lib/dynamic-form-field dynamic-form-field.ts

100% Statements 84/84
100% Branches 42/42
100% Functions 59/59
100% Lines 84/84

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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226                                                                          417x   417x 417x                   418x 417x 417x 417x     178x   489x 21x 161x   18x 18x   799x   662x 28x 7x 6x 510x   2x 119x   1x 9x   12x   144x 141x 138x   277x 287x     133x 132x 132x 132x                 1x 1x                 88x         88x       124x       133x       133x 133x                 132x 132x 132x       92x       132x       92x       132x       58x 58x 4x         58x 58x 3x 3x 3x         5x       417x 417x 3x 3x 2x 1x 1x 3x 1x 1x 1x   417x       135x 16x 10x       135x 16x 1x       58x 5x 4x       417x       417x 417x 417x 417x 417x      
import { DynamicFormAction } from '../dynamic-form-action/dynamic-form-action';
import { DynamicFormClassType } from '../dynamic-form-config/dynamic-form-class-type';
import { DynamicFormElement } from '../dynamic-form-element/dynamic-form-element';
import { assignExpressionData } from '../dynamic-form-expression/dynamic-form-expression-helpers';
import { DynamicFormValidationErrors } from '../dynamic-form-validation/dynamic-form-validation-errors';
import { DynamicForm } from '../dynamic-form/dynamic-form';
import { cloneObject } from '../dynamic-form/dynamic-form-helpers';
import { DynamicFormBuilder } from '../dynamic-form/dynamic-form.builder';
import { DynamicFormFieldClassType } from './dynamic-form-field-class-type';
import { DynamicFormFieldControl } from './dynamic-form-field-control';
import { DynamicFormFieldDefinition } from './dynamic-form-field-definition';
import { DynamicFormFieldExpressionData } from './dynamic-form-field-expression-data';
import { DynamicFormFieldExpressions } from './dynamic-form-field-expressions';
import { DynamicFormFieldSettings } from './dynamic-form-field-settings';
import { DynamicFormFieldTemplate } from './dynamic-form-field-template';
import { DynamicFormFieldType } from './dynamic-form-field-type';
import {
  DynamicFormFieldValidatorBase, DynamicFormFieldAsyncValidatorFn, DynamicFormFieldValidatorFn,
} from './dynamic-form-field-validator';
 
export abstract class DynamicFormField<
  Value = any, Model extends Value = Value,
  Control extends DynamicFormFieldControl<Value> = DynamicFormFieldControl<Value>,
  Template extends DynamicFormFieldTemplate = DynamicFormFieldTemplate,
  Definition extends DynamicFormFieldDefinition<Value, Template> = DynamicFormFieldDefinition<Value, Template>,
  Type extends DynamicFormFieldType = DynamicFormFieldType,
  Child extends DynamicFormElement = DynamicFormElement
> extends DynamicFormElement<Template, Definition, Child, DynamicFormFieldExpressionData, DynamicFormFieldExpressions, Type> {
 
  protected _settings: DynamicFormFieldSettings;
 
  protected _depth: number;
  protected _model: Model;
  protected _parameters: any;
 
  protected _control: Control;
 
  protected _validators: DynamicFormFieldValidatorBase[] = [];
 
  protected _headerActions: DynamicFormAction[] = [];
  protected _footerActions: DynamicFormAction[] = [];
 
  constructor(
    builder: DynamicFormBuilder,
    root: DynamicForm,
    parent: DynamicFormElement,
    definition: Definition,
    type: Type,
    control?: Control,
  ) {
    super(builder, root, parent, definition, type);
    this._control = control;
    this._depth = this.getDepth();
    this._settings = this.createSettings();
  }
 
  override get classType(): DynamicFormClassType { return 'field'; }
 
  get key(): string { return this.definition.key; }
  get index(): number { return this.definition.index; }
  get depth(): number { return this._depth; }
  get path(): string {
    const parentPath = this.parentField && this.parentField.path;
    return parentPath ? `${parentPath}.${this.key}` : this.key || null;
  }
  get settings(): DynamicFormFieldSettings { return this._settings; }
 
  get model(): Model { return this._model; }
  get value(): Value { return this._control.value; }
  get valid(): boolean { return this._control.valid; }
  get status(): string { return this._control.status; }
  get control(): Control { return this._control; }
 
  get disabled(): boolean { return this.control.disabled; }
  get readonly(): boolean { return this.template.readonly || this.parentField.readonly || false; }
 
  get wrappers(): string[] { return this.definition.wrappers; }
  get unregistered(): boolean { return this.definition.unregistered; }
 
  get validators(): DynamicFormFieldValidatorBase[] { return this._validators; }
 
  get errors(): DynamicFormValidationErrors { return this.control.errors; }
  get hasErrors(): boolean { return (this.errors || false) && true; }
  get showErrors(): boolean { return this.hasErrors && this.control.touched; }
 
  get headerActions(): DynamicFormAction[] { return this._headerActions; }
  get footerActions(): DynamicFormAction[] { return this._footerActions; }
 
  override init(): void {
    super.init();
    this.initValidators();
    this.initHeaderActions();
    this.initFooterActions();
  }
 
  abstract get fieldClassType(): DynamicFormFieldClassType;
 
  abstract check(): void;
  abstract destroy(): void;
 
  clear(): void {
     this.resetEmpty();
     this.validate();
  }
 
  abstract reset(): void;
  abstract resetEmpty(): void;
  abstract resetDefault(): void;
  abstract validate(): void;
 
  protected getId(): string {
    return this._builder.getFieldId(this);
  }
 
 
  protected override initId(): void {
    this.definition.id = this.getId();
  }
 
  protected get defaultValue(): Value {
    return this.definition.defaultValue;
  }
 
  protected override getExpressions(): DynamicFormFieldExpressions {
    return this._builder.createFieldExpressions(this);
  }
 
  protected override initExpressions(): void {
    super.initExpressions();
    this.afterInitExpressions();
  }
 
  protected afterInitExpressions(): void {}
 
  protected abstract override getChildren(): Child[];
  protected abstract getValidators(): DynamicFormFieldValidatorBase[];
 
  protected initValidators(): void {
    this._validators = this.getValidators() || [];
    this._control.setValidators(this.getValidatorFunctions());
    this._control.setAsyncValidators(this.getAsyncValidatorFunctions());
  }
 
  protected getHeaderActions(): DynamicFormAction[] {
    return this._builder.createFormActions(this.root, this, this.definition.headerActions);
  }
 
  protected initHeaderActions(): void {
    this._headerActions = this.getHeaderActions() || [];
  }
 
  protected getFooterActions(): DynamicFormAction[] {
    return this._builder.createFormActions(this.root, this, this.definition.footerActions);
  }
 
  protected initFooterActions(): void {
    this._footerActions = this.getFooterActions() || [];
  }
 
  protected checkControl(): void {
    const disabled = (this.parentField && this.parentField.control.disabled) || this.template.disabled || false;
    if (this.control.disabled !== disabled) {
      return disabled ? this.control.disable() : this.control.enable();
    }
  }
 
  protected checkValidators(): void {
    const validatorsChanged = this.validatorsChanged();
    if (validatorsChanged) {
      this._control.setValidators(this.getValidatorFunctions());
      this._control.setAsyncValidators(this.getAsyncValidatorFunctions());
      this._control.updateValueAndValidity();
    }
  }
 
  protected cloneObject<T>(obj: T): T {
    return cloneObject(obj);
  }
 
  protected override createExpressionData(): DynamicFormFieldExpressionData {
    const expressionData = super.createExpressionData() as DynamicFormFieldExpressionData;
    assignExpressionData(expressionData, {
      key: () => this.key,
      index: () => this.index,
      depth: () => this.depth,
      disabled: () => this.disabled,
      readonly: () => this.readonly,
      model: () => this.model,
      value: () => this.value,
      valid: () => this.valid,
      status: () => this.status,
    });
    return expressionData;
  }
 
  private getValidatorFunctions(): DynamicFormFieldValidatorFn[] {
    return this._validators
      .filter(validator => !!validator.validatorFn && !validator.async)
      .map(validator => validator.validatorFn as DynamicFormFieldValidatorFn);
  }
 
  private getAsyncValidatorFunctions(): DynamicFormFieldAsyncValidatorFn[] {
    return this._validators
      .filter(validator => !!validator.validatorFn && validator.async)
      .map(validator => validator.validatorFn as DynamicFormFieldAsyncValidatorFn);
  }
 
  private validatorsChanged(): boolean {
    return this._validators
      .map(validator => validator.checkChanges())
      .some(change => !!change);
  }
 
  private getDepth(): number {
    return this.parentField ? this.parentField.depth + 1 : 0;
  }
 
  private createSettings(): DynamicFormFieldSettings {
    const defaultSettings = { autoGeneratedId: false, updateType: 'change' } as DynamicFormFieldSettings;
    const rootSettings = this.root && this.root.settings || {};
    const parentSettings = this.parentField && this.parentField.settings || {};
    const options = this.definition.settings || {};
    return { ...defaultSettings, ...rootSettings, ...parentSettings, ...options };
  }
}