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

100% Statements 67/67
100% Branches 11/11
100% Functions 34/34
100% Lines 56/56

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                                        32x 32x 32x     1x   10x     4x 2x 1x   1x   2x 2x       2x 2x 1x 1x 1x 1x         2x 2x 1x 2x 2x   1x 1x 1x 1x         1x 1x 2x       2x       2x       2x 1x 2x   1x 1x       2x 1x 2x   1x 1x       2x 1x       19x       19x 31x 31x         18x       34x 34x       32x       31x 1x   30x 2x 2x        
import { DynamicFormElement } from '../dynamic-form-element/dynamic-form-element';
import { DynamicFormField } from '../dynamic-form-field/dynamic-form-field';
import { DynamicFormFieldClassType } from '../dynamic-form-field/dynamic-form-field-class-type';
import { FormRecordBase } from '../dynamic-form-field/dynamic-form-field-control';
import { DynamicFormFieldType } from '../dynamic-form-field/dynamic-form-field-type';
import { DynamicForm } from '../dynamic-form/dynamic-form';
import { DynamicFormBuilder } from '../dynamic-form/dynamic-form.builder';
import { DynamicFormDictionaryDefinition } from './dynamic-form-dictionary-definition';
import { DynamicFormDictionaryTemplate } from './dynamic-form-dictionary-template';
import { DynamicFormDictionaryAsyncValidator, DynamicFormDictionaryValidator } from './dynamic-form-dictionary-validator';
 
export class DynamicFormDictionary<
  Value = any, Model extends Value = Value,
  Template extends DynamicFormDictionaryTemplate = DynamicFormDictionaryTemplate,
  Definition extends DynamicFormDictionaryDefinition<Value, Template> = DynamicFormDictionaryDefinition<Value, Template>,
  Type extends DynamicFormFieldType = DynamicFormFieldType
> extends DynamicFormField<{ [key: string]: Value }, { [key: string]: Model }, FormRecordBase<Value>,
    Template, Definition, Type, DynamicFormField<Value, Model>> {
 
  constructor(builder: DynamicFormBuilder, root: DynamicForm, parent: DynamicFormElement, definition: Definition, type: Type) {
    super(builder, root, parent, definition, type, new FormRecordBase({}));
    this.initModel(this.getModel());
    this.extendExpressionData({ length: () => this.length });
  }
 
  get fieldClassType(): DynamicFormFieldClassType { return 'dictionary'; }
 
  get length(): number { return this._children.length; }
 
  registerField(field: DynamicFormField<Value, Model>): void {
    const index = this._children.findIndex(f => f.key === field.key);
    if (index >= 0) {
      this._children[index] = field;
    } else {
      this._children.push(field);
    }
    this._control.registerControl(field.key, field.control);
    this._control.markAsTouched();
  }
 
  removeField(key: string): void {
    const index = this._children.findIndex(field => field.key === key);
    if (index >= 0 && index < this.length) {
      this._children.splice(index, 1).forEach(field => field.destroy());
      delete this._model[key];
      this._control.removeControl(key);
      this._control.markAsTouched();
    }
  }
 
  clearFields(): void {
    const length = this.length;
    if (length > 0) {
      this._children.forEach(field => {
        field.destroy();
        this._control.removeControl(field.key);
      });
      this._children = [];
      this._model = {};
      this.parentField.model[this.key] = this._model;
      this._control.markAsTouched();
    }
  }
 
  check(): void {
    this.checkControl();
    this.checkValidators();
    this._children.forEach(field => field.check());
  }
 
  destroy(): void {
    this._children.forEach(field => field.destroy());
  }
 
  reset(): void {
    this._children.forEach(field => field.reset());
  }
 
  resetEmpty(): void {
    this._children.forEach(field => field.destroy());
    Object.keys(this._control.controls).forEach((key) => {
      this._control.removeControl(key);
    });
    this.initModel({});
    this._children = [];
  }
 
  resetDefault(): void {
    this._children.forEach((field) => field.destroy());
    Object.keys(this._control.controls).forEach((key) => {
      this._control.removeControl(key);
    });
    this.initModel(this.getDefaultModel());
    this.initChildren();
  }
 
  validate(): void {
    this._children.forEach(field => field.validate());
    this._control.markAsTouched();
  }
 
  protected getChildren(): DynamicFormField<Value, Model>[] {
    return this._builder.createFormDictionaryElements(this);
  }
 
  protected override initChildren(): void {
    super.initChildren();
    this._children.filter(field => !field.unregistered).forEach(field => {
      this._control.registerControl(field.definition.key, field.control);
    });
  }
 
  protected getValidators(): (DynamicFormDictionaryValidator | DynamicFormDictionaryAsyncValidator)[] {
    return this._builder.createDictionaryValidators(this);
  }
 
  private initModel(model: any): void {
    this.parentField.model[this.definition.key] = model;
    this._model = this.parentField.model[this.definition.key];
  }
 
  private getModel(): any {
    return this.parentField.model[this.definition.key] || this.getDefaultModel();
  }
 
  private getDefaultModel(): any {
    if (this.definition.defaultValue) {
      return this.cloneObject(this.definition.defaultValue);
    }
    return (this.definition.defaultKeys || []).reduce((result, key) => {
      result[key] = undefined;
      return result;
    }, {});
  }
}