2019-11-24 17:30:04 +01:00
|
|
|
import {Component, Input, OnDestroy, OnInit} from "@angular/core";
|
|
|
|
import {Option} from "../../../utils/indicator-utils";
|
2019-11-29 13:14:49 +01:00
|
|
|
import {AbstractControl} from "@angular/forms";
|
2019-11-28 11:30:56 +01:00
|
|
|
import {HelperFunctions} from "../../../openaireLibrary/utils/HelperFunctions.class";
|
2019-11-24 17:30:04 +01:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: '[dashboard-input]',
|
|
|
|
template: `
|
2019-12-20 12:48:35 +01:00
|
|
|
<mat-form-field *ngIf="type != 'checkbox'" class="uk-width-1-1 uk-padding-remove">
|
2019-11-26 20:11:24 +01:00
|
|
|
<input *ngIf="type === 'text'" matInput [placeholder]="label"
|
|
|
|
[formControl]="formControl" [required]="required">
|
2019-12-02 12:27:12 +01:00
|
|
|
<textarea *ngIf="type === 'textarea'" [rows]="rows" matInput
|
|
|
|
[placeholder]="label" [formControl]="formControl"></textarea>
|
|
|
|
<mat-select *ngIf="type === 'select'" [placeholder]="label"
|
|
|
|
(openedChange)="stopPropagation()" [formControl]="formControl" [disableOptionCentering]="true">
|
2019-11-26 20:11:24 +01:00
|
|
|
<mat-option *ngFor="let option of options" [value]="option.value">
|
2019-11-24 17:30:04 +01:00
|
|
|
{{option.label}}
|
2019-11-26 20:11:24 +01:00
|
|
|
</mat-option>
|
|
|
|
</mat-select>
|
2019-12-20 12:48:35 +01:00
|
|
|
|
2019-11-26 20:11:24 +01:00
|
|
|
</mat-form-field>
|
2019-12-20 12:48:35 +01:00
|
|
|
<mat-checkbox *ngIf="type === 'checkbox'" [formControl]="formControl" >{{label}}</mat-checkbox>
|
2019-11-26 20:11:24 +01:00
|
|
|
`
|
2019-11-24 17:30:04 +01:00
|
|
|
})
|
|
|
|
export class InputComponent implements OnInit, OnDestroy {
|
2019-11-29 13:14:49 +01:00
|
|
|
@Input('formInput') formControl: AbstractControl;
|
2019-11-24 17:30:04 +01:00
|
|
|
@Input('type') type: string = 'text';
|
|
|
|
@Input('label') label: string;
|
|
|
|
@Input('rows') rows: number = 3;
|
|
|
|
@Input('options') options: Option[];
|
2019-11-28 11:30:56 +01:00
|
|
|
private initValue: any;
|
2019-11-24 17:30:04 +01:00
|
|
|
|
|
|
|
constructor() {
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnInit(): void {
|
2019-11-28 11:30:56 +01:00
|
|
|
this.initValue = HelperFunctions.copy(this.formControl.value);
|
|
|
|
this.formControl.valueChanges.subscribe(value => {
|
2019-11-29 14:35:13 +01:00
|
|
|
if(this.initValue === value) {
|
2019-11-28 11:30:56 +01:00
|
|
|
this.formControl.markAsPristine();
|
|
|
|
}
|
|
|
|
});
|
2019-11-24 17:30:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ngOnDestroy(): void {
|
|
|
|
}
|
2019-11-26 20:11:24 +01:00
|
|
|
|
|
|
|
public get required(): boolean {
|
|
|
|
return this.formControl && this.formControl.validator
|
|
|
|
&& this.formControl.validator(this.formControl)
|
|
|
|
&& this.formControl.validator(this.formControl).required;
|
|
|
|
}
|
2019-11-27 13:14:14 +01:00
|
|
|
|
|
|
|
stopPropagation() {
|
|
|
|
if(event) {
|
|
|
|
event.stopPropagation();
|
|
|
|
}
|
|
|
|
}
|
2019-11-24 17:30:04 +01:00
|
|
|
}
|