Project

General

Profile

1
import {Component, Input, OnDestroy, OnInit} from "@angular/core";
2
import {Option} from "../../../utils/indicator-utils";
3
import {AbstractControl} from "@angular/forms";
4
import {HelperFunctions} from "../../../openaireLibrary/utils/HelperFunctions.class";
5

    
6
@Component({
7
  selector: '[dashboard-input]',
8
  template: `
9
    <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
      <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
        <mat-option *ngFor="let option of options" [value]="option.value">
17
          {{option.label}}
18
        </mat-option>
19
      </mat-select>
20
    </mat-form-field>
21
  `
22
})
23
export class InputComponent implements OnInit, OnDestroy {
24
  @Input('formInput') formControl: AbstractControl;
25
  @Input('type') type: string = 'text';
26
  @Input('label') label: string;
27
  @Input('rows') rows: number = 3;
28
  @Input('options') options: Option[];
29
  private initValue: any;
30

    
31
  constructor() {
32
  }
33

    
34
  ngOnInit(): void {
35
    this.initValue = HelperFunctions.copy(this.formControl.value);
36
    this.formControl.valueChanges.subscribe(value => {
37
      if(this.initValue === value) {
38
        this.formControl.markAsPristine();
39
      }
40
    });
41
  }
42

    
43
  ngOnDestroy(): void {
44
  }
45

    
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

    
52
  stopPropagation() {
53
    if(event) {
54
      event.stopPropagation();
55
    }
56
  }
57
}
(1-1/2)