Project

General

Profile

1 57673 k.triantaf
import {Component, Input, OnDestroy, OnInit} from "@angular/core";
2
import {Option} from "../../../utils/indicator-utils";
3 57768 k.triantaf
import {AbstractControl} from "@angular/forms";
4 57735 k.triantaf
import {HelperFunctions} from "../../../openaireLibrary/utils/HelperFunctions.class";
5 57673 k.triantaf
6
@Component({
7
  selector: '[dashboard-input]',
8
  template: `
9 57697 k.triantaf
    <mat-form-field class="uk-width-1-1 uk-padding-remove">
10
      <input *ngIf="type === 'text'" matInput [placeholder]="label"
11
             [formControl]="formControl" [required]="required">
12 57777 k.triantaf
      <textarea *ngIf="type === 'textarea'" [rows]="rows" matInput
13
                [placeholder]="label" [formControl]="formControl"></textarea>
14
      <mat-select *ngIf="type === 'select'" [placeholder]="label"
15
                  (openedChange)="stopPropagation()" [formControl]="formControl" [disableOptionCentering]="true">
16 57697 k.triantaf
        <mat-option *ngFor="let option of options" [value]="option.value">
17 57673 k.triantaf
          {{option.label}}
18 57697 k.triantaf
        </mat-option>
19
      </mat-select>
20
    </mat-form-field>
21
  `
22 57673 k.triantaf
})
23
export class InputComponent implements OnInit, OnDestroy {
24 57768 k.triantaf
  @Input('formInput') formControl: AbstractControl;
25 57673 k.triantaf
  @Input('type') type: string = 'text';
26
  @Input('label') label: string;
27
  @Input('rows') rows: number = 3;
28
  @Input('options') options: Option[];
29 57735 k.triantaf
  private initValue: any;
30 57673 k.triantaf
31
  constructor() {
32
  }
33
34
  ngOnInit(): void {
35 57735 k.triantaf
    this.initValue = HelperFunctions.copy(this.formControl.value);
36
    this.formControl.valueChanges.subscribe(value => {
37 57770 k.triantaf
      if(this.initValue === value) {
38 57735 k.triantaf
        this.formControl.markAsPristine();
39
      }
40
    });
41 57673 k.triantaf
  }
42
43
  ngOnDestroy(): void {
44
  }
45 57697 k.triantaf
46
  public get required(): boolean {
47
    return this.formControl && this.formControl.validator
48
      && this.formControl.validator(this.formControl)
49
      && this.formControl.validator(this.formControl).required;
50
  }
51 57708 k.triantaf
52
  stopPropagation() {
53
    if(event) {
54
      event.stopPropagation();
55
    }
56
  }
57 57673 k.triantaf
}