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

100% Statements 78/78
100% Branches 20/20
100% Functions 42/42
100% Lines 74/74

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                                                      66x           66x 66x 34x   66x 66x 66x 66x     1x   157x 3x 49x   4x   5x 5x     28x 28x 28x 28x       9x 9x 9x       1x       1x       1x       1x 1x       1x       9x 9x       28x 28x 2x         28x       28x       28x       28x       28x       28x       28x       28x       28x       28x       66x 58x   66x       66x 66x       66x 66x 66x 4x 4x 1x   1x     62x       63x       66x 4x   62x       27x 27x       11x 11x       2x 1x 1x         9x 1x     8x 8x 1x 1x   8x      
import { Subscription } from 'rxjs';
import { debounceTime, tap } from 'rxjs/operators';
import { DynamicFormAction } from '../dynamic-form-action/dynamic-form-action';
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 { FormControlBase } from '../dynamic-form-field/dynamic-form-field-control';
import { dynamicFormFieldDefaultDebounce } from '../dynamic-form-field/dynamic-form-field-settings';
import { DynamicFormFieldType } from '../dynamic-form-field/dynamic-form-field-type';
import { DynamicFormInput } from '../dynamic-form-input/dynamic-form-input';
import { DynamicForm } from '../dynamic-form/dynamic-form';
import { DynamicFormBuilder } from '../dynamic-form/dynamic-form.builder';
import { DynamicFormControlAddOn, DynamicFormControlDefinition } from './dynamic-form-control-definition';
import { DynamicFormControlEvaluator } from './dynamic-form-control-evaluator';
import { DynamicFormControlTemplate } from './dynamic-form-control-template';
import { DynamicFormControlAsyncValidator, DynamicFormControlValidator } from './dynamic-form-control-validator';
 
export class DynamicFormControl<
  Value = any,
  Input extends DynamicFormInput<Value> = DynamicFormInput<Value>,
  Template extends DynamicFormControlTemplate<Value, Input> = DynamicFormControlTemplate<Value, Input>,
  Definition extends DynamicFormControlDefinition<Value, Input, Template> = DynamicFormControlDefinition<Value, Input, Template>,
  Type extends DynamicFormFieldType = DynamicFormFieldType
> extends DynamicFormField<Value, Value, FormControlBase<Value>, Template, Definition, Type> {
 
  private _valueChanging: boolean;
  protected _valueSubscription: Subscription;
  protected _evaluators: DynamicFormControlEvaluator[] = [];
 
  protected _prefixAddOn: DynamicFormControlAddOn;
  protected _suffixAddOn: DynamicFormControlAddOn;
 
  constructor(builder: DynamicFormBuilder, root: DynamicForm, parent: DynamicFormElement, definition: Definition, type: Type) {
    super(builder, root, parent, definition, type);
    if (!this.template.input) {
      this.template.input = {} as Input;
    }
    this._model = this.createModel();
    this._control = this.createControl();
    this._valueSubscription = this.createValueSubscription();
    this.extendExpressionData({ input: () => this.input });
  }
 
  get fieldClassType(): DynamicFormFieldClassType { return 'control'; }
 
  get input(): Input { return this.template.input; }
  get inputId(): string { return this.id || this.path; }
  get inputType(): string { return this.input.type; }
 
  get evaluators(): DynamicFormControlEvaluator[] { return this._evaluators; }
 
  get prefixAddOn(): DynamicFormControlAddOn { return this._prefixAddOn; }
  get suffixAddOn(): DynamicFormControlAddOn { return this._suffixAddOn; }
 
  override init(): void {
    super.init();
    this.initEvaluators();
    this.initPrefixAddOn();
    this.initSuffixAddOn();
  }
 
  check(): void {
    this.checkValue();
    this.checkControl();
    this.checkValidators();
  }
 
  destroy(): void {
    this._valueSubscription.unsubscribe();
  }
 
  reset(): void {
    this._control.reset(null);
  }
 
  resetEmpty(): void {
    this._control.reset(null);
  }
 
  resetDefault(): void {
    const defaultValue = this.defaultValue;
    this._control.reset(defaultValue);
  }
 
  validate(): void {
    this._control.markAsTouched();
  }
 
  patchModel(model: any): void {
    this.setModel(model);
    this.setValue(model, false);
  }
 
  protected override afterInitExpressions(): void {
    const keys = Object.keys(this.expressions);
    if (keys.includes('input.defaultValue')) {
      this.checkDefaultValue();
    }
  }
 
  protected getChildren(): DynamicFormElement[] {
    return undefined;
  }
 
  protected override getHeaderActions(): DynamicFormAction[] {
    return undefined;
  }
 
  protected override getFooterActions(): DynamicFormAction[] {
    return undefined;
  }
 
  protected getValidators(): (DynamicFormControlValidator | DynamicFormControlAsyncValidator)[] {
    return this._builder.createControlValidators(this);
  }
 
  protected getEvaluators(): DynamicFormControlEvaluator[] {
    return this._builder.createControlEvaluators(this);
  }
 
  protected initEvaluators(): void {
    this._evaluators = this.getEvaluators();
  }
 
  protected getPrefixAddOn(): DynamicFormControlAddOn {
    return this._builder.createFormControlAddOn(this.root, this, this.definition.prefixAddOn);
  }
 
  protected initPrefixAddOn(): void {
    this._prefixAddOn = this.getPrefixAddOn();
  }
 
  protected getSuffixAddOn(): DynamicFormControlAddOn {
    return this._builder.createFormControlAddOn(this.root, this, this.definition.suffixAddOn);
  }
 
  protected initSuffixAddOn(): void {
    this._suffixAddOn = this.getSuffixAddOn();
  }
 
  private createModel(): any {
    if (this.parentField.model[this.key] === undefined) {
      this.parentField.model[this.key] = this.defaultValue;
    }
    return this.parentField.model[this.key];
  }
 
  private createControl(): FormControlBase<Value> {
    const options = { updateOn: this.getUpdateOn() };
    return new FormControlBase<Value>(this._model, options);
  }
 
  private createValueSubscription(): Subscription {
    const valueChanges = this._control.valueChanges;
    const observer = { next: value => this.setModel(value) };
    if (this.settings.updateType === 'debounce') {
      const debounce = this.settings.updateDebounce || dynamicFormFieldDefaultDebounce;
      return valueChanges.pipe(
        tap(() => this._valueChanging = true),
        debounceTime(debounce),
        tap(() => this._valueChanging = false),
      ).subscribe(observer);
    }
    return valueChanges.subscribe(observer);
  }
 
  protected override get defaultValue(): any {
    return this.input?.defaultValue !== undefined ? this.input.defaultValue : super.defaultValue || null;
  }
 
  private getUpdateOn(): 'change' | 'blur' | 'submit' {
    if (this.settings.updateType === 'debounce') {
      return 'change';
    }
    return this.settings.updateType;
  }
 
  private setModel(value: any): void {
    this.parentField.model[this.key] = value;
    this._model = this.parentField.model[this.key];
  }
 
  private setValue(value: any, markAsTouched: boolean): void {
    this._control.setValue(value, { onlySelf: true, emitEvent: false });
    return markAsTouched && this._control.markAsTouched();
  }
 
  private checkDefaultValue(): void {
    if (this.model !== this.defaultValue) {
      this.setModel(this.defaultValue);
      this.setValue(this.defaultValue, false);
    }
  }
 
  private checkValue(): void {
    if (this._valueChanging) {
      return;
    }
 
    const value = this.parentField.model[this.key];
    if (this._control.value !== value || this._model !== value) {
      this.setModel(value);
      this.setValue(value, true);
    }
    this._evaluators.filter(e => !!e.enabled).forEach(e => e.func(this));
  }
}