All files / src/app/superadmin/settings edit-custom-text.component.ts

5.55% Statements 1/18
0% Branches 0/5
0% Functions 0/5
5.55% Lines 1/18

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                                                          1x                                                                        
import {
  Component, Input, Output, OnDestroy, OnInit, EventEmitter
} from '@angular/core';
import { FormControl, FormGroup } from '@angular/forms';
import { Subscription } from 'rxjs';
 
@Component({
  selector: 'tc-custom-text',
  template: `
    <div class="flex-row"  [style.align-items]="'center'" [style.margin-bottom.px]="10">
      <div class="flex-column" [style.width.%]="40">
        <p>{{ctLabel}}</p>
        <em [style.font-size]="'smaller'">({{ctKey}})</em>
      </div>
      <mat-form-field [style.width.%]="50">
        <textarea matInput cdkTextareaAutosize [formControl]="inputControl">
        </textarea>
      </mat-form-field>
      <button mat-icon-button matTooltip="Auf Standard setzen"
              [style.width.%]="10"
              [disabled]="inputControl.value === ctDefaultValue"
              (click)="setToDefault()">
        <mat-icon>undo</mat-icon>
      </button>
    </div>
    <mat-divider></mat-divider>
    `
})
 
export class EditCustomTextComponent implements OnInit, OnDestroy {
  @Input() parentForm!: FormGroup;
  @Input() ctKey!: string;
  @Input() ctLabel!: string;
  @Input() ctDefaultValue!: string;
  @Input() ctInitialValue!: string;
  @Output() valueChange = new EventEmitter<EditCustomTextComponent>();
  inputControl = new FormControl();
  valueChanged = false;
  value: string = '';
  valueChangeSubscription: Subscription | null = null;
 
  ngOnInit(): void {
    this.inputControl.setValue(this.ctInitialValue ? this.ctInitialValue : this.ctDefaultValue);
    this.parentForm.addControl(this.ctKey, this.inputControl);
    this.valueChangeSubscription = this.inputControl.valueChanges.subscribe(() => {
      this.value = this.inputControl.value;
      Iif (!this.value) {
        this.inputControl.setValue(this.ctDefaultValue, { emitEvent: false });
        this.value = this.ctDefaultValue;
      }
      this.valueChanged = this.ctInitialValue ?
        (this.value !== this.ctInitialValue) : (this.value !== this.ctDefaultValue);
      this.valueChange.emit(this);
    });
  }
 
  setToDefault(): void {
    this.inputControl.setValue(this.ctDefaultValue);
  }
 
  ngOnDestroy(): void {
    this.valueChangeSubscription?.unsubscribe();
    this.parentForm.removeControl(this.ctKey);
  }
}