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

100% Statements 85/85
100% Branches 16/16
100% Functions 32/32
100% Lines 76/76

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                                      38x 38x 38x     1x   16x     1x 1x 1x       2x 2x 1x 1x 1x 1x         2x 1x 1x 3x 3x   1x 1x 1x         2x 2x 2x 1x 1x 1x 1x 1x         2x 1x 1x 1x 1x 1x 1x 1x 1x 1x         2x 1x 1x 1x 1x 1x 1x 1x 1x 1x         1x 1x 2x       2x       2x       2x 1x 1x 1x       2x 1x 1x 1x       2x 1x       24x       24x 24x 36x         23x       40x 40x       38x       37x 1x   36x      
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 { FormArrayBase } 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 { DynamicFormArrayDefinition } from './dynamic-form-array-definition';
import { DynamicFormArrayTemplate } from './dynamic-form-array-template';
import { DynamicFormArrayAsyncValidator, DynamicFormArrayValidator } from './dynamic-form-array-validator';
 
export class DynamicFormArray<
  Value = any, Model extends Value = Value,
  Template extends DynamicFormArrayTemplate = DynamicFormArrayTemplate,
  Definition extends DynamicFormArrayDefinition<Value, Template> = DynamicFormArrayDefinition<Value, Template>,
  Type extends DynamicFormFieldType = DynamicFormFieldType
> extends DynamicFormField<Value[], Model[], FormArrayBase<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 FormArrayBase<Value>([]));
    this.initModel(this.getModel());
    this.extendExpressionData({ length: () => this.length });
  }
 
  get fieldClassType(): DynamicFormFieldClassType { return 'array'; }
 
  get length(): number { return this._children.length; }
 
  pushField(element: DynamicFormField<Value, Model>): void {
    this._children.push(element);
    this._control.push(element.control);
    this._control.markAsTouched();
  }
 
  popField(): void {
    const length = this.length;
    if (length > 0) {
      this._children.pop().destroy();
      this._model.pop();
      this._control.removeAt(length - 1);
      this._control.markAsTouched();
    }
  }
 
  removeField(index: number): void {
    if (index >= 0 && index < this.length) {
      this._children.splice(index, 1).forEach(field => field.destroy());
      this._children.forEach((field, idx) => {
        field.definition.key = `${idx}`;
        field.definition.index = idx;
      });
      this._model.splice(index, 1);
      this._control.removeAt(index);
      this._control.markAsTouched();
    }
  }
 
  clearFields(): void {
    const length = this.length;
    if (length > 0) {
      this._children.forEach(field => field.destroy());
      this._control.clear();
      this._children = [];
      this._model = [];
      this.parentField.model[this.key] = this._model;
      this._control.markAsTouched();
    }
  }
 
  moveFieldDown(index: number): void {
    if (index >= 0 && index < this.length - 1) {
      const field = this._children.splice(index, 1)[0];
      this._children.splice(index + 1, 0, field);
      this._children[index].definition.index = index;
      this._children[index + 1].definition.index = index + 1;
      const value = this._model.splice(index, 1)[0];
      this._model.splice(index + 1, 0, value);
      this._control.removeAt(index);
      this._control.insert(index + 1, field.control);
      this._control.markAsTouched();
    }
  }
 
  moveFieldUp(index: number): void {
    if (index >= 1 && index < this.length) {
      const field = this._children.splice(index, 1)[0];
      this._children.splice(index - 1, 0, field);
      this._children[index - 1].definition.index = index - 1;
      this._children[index].definition.index = index;
      const value = this._model.splice(index, 1)[0];
      this._model.splice(index - 1, 0, value);
      this._control.removeAt(index);
      this._control.insert(index - 1, field.control);
      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());
    this._children = [];
    this._control.clear();
    this.initModel([]);
  }
 
  resetDefault(): void {
    this._children.forEach((field) => field.destroy());
    this._control.clear();
    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.createFormArrayElements(this);
  }
 
  protected override initChildren(): void {
    super.initChildren();
    this._children.forEach((field, index) => {
      this._control.insert(index, field.control);
    });
  }
 
  protected getValidators(): (DynamicFormArrayValidator | DynamicFormArrayAsyncValidator)[] {
    return this._builder.createArrayValidators(this);
  }
 
  private initModel(model: any): void {
    this.parentField.model[this.key] = model;
    this._model = this.parentField.model[this.key];
  }
 
  private getModel(): any {
    return this.parentField.model[this.key] || this.getDefaultModel();
  }
 
  private getDefaultModel(): any {
    if (this.defaultValue) {
      return this.cloneObject(this.defaultValue);
    }
    return Array.from({ length: this.definition.defaultLength || 0 });
  }
}