|
|
@ -15,9 +15,17 @@ |
|
|
///
|
|
|
///
|
|
|
|
|
|
|
|
|
import { Component, forwardRef, Input, OnDestroy, OnInit } from '@angular/core'; |
|
|
import { Component, forwardRef, Input, OnDestroy, OnInit } from '@angular/core'; |
|
|
import { ControlValueAccessor, FormBuilder, NG_VALUE_ACCESSOR, UntypedFormGroup, Validators } from '@angular/forms'; |
|
|
import { |
|
|
|
|
|
ControlValueAccessor, |
|
|
|
|
|
FormBuilder, |
|
|
|
|
|
FormGroup, |
|
|
|
|
|
NG_VALIDATORS, |
|
|
|
|
|
NG_VALUE_ACCESSOR, |
|
|
|
|
|
ValidationErrors, |
|
|
|
|
|
Validator, |
|
|
|
|
|
Validators |
|
|
|
|
|
} from '@angular/forms'; |
|
|
import { coerceBooleanProperty } from '@angular/cdk/coercion'; |
|
|
import { coerceBooleanProperty } from '@angular/cdk/coercion'; |
|
|
import { getTimezoneInfo } from '@shared/models/time/time.models'; |
|
|
|
|
|
import { TimeService } from '@core/services/time.service'; |
|
|
import { TimeService } from '@core/services/time.service'; |
|
|
import { takeUntil } from 'rxjs/operators'; |
|
|
import { takeUntil } from 'rxjs/operators'; |
|
|
import { Subject } from 'rxjs'; |
|
|
import { Subject } from 'rxjs'; |
|
|
@ -26,15 +34,22 @@ import { Subject } from 'rxjs'; |
|
|
selector: 'tb-datapoints-limit', |
|
|
selector: 'tb-datapoints-limit', |
|
|
templateUrl: './datapoints-limit.component.html', |
|
|
templateUrl: './datapoints-limit.component.html', |
|
|
styleUrls: ['./datapoints-limit.component.scss'], |
|
|
styleUrls: ['./datapoints-limit.component.scss'], |
|
|
providers: [{ |
|
|
providers: [ |
|
|
|
|
|
{ |
|
|
provide: NG_VALUE_ACCESSOR, |
|
|
provide: NG_VALUE_ACCESSOR, |
|
|
useExisting: forwardRef(() => DatapointsLimitComponent), |
|
|
useExisting: forwardRef(() => DatapointsLimitComponent), |
|
|
multi: true |
|
|
multi: true |
|
|
}] |
|
|
}, |
|
|
|
|
|
{ |
|
|
|
|
|
provide: NG_VALIDATORS, |
|
|
|
|
|
useExisting: forwardRef(() => DatapointsLimitComponent), |
|
|
|
|
|
multi: true |
|
|
|
|
|
} |
|
|
|
|
|
] |
|
|
}) |
|
|
}) |
|
|
export class DatapointsLimitComponent implements ControlValueAccessor, OnInit, OnDestroy { |
|
|
export class DatapointsLimitComponent implements ControlValueAccessor, Validator, OnInit, OnDestroy { |
|
|
|
|
|
|
|
|
datapointsLimitFormGroup: UntypedFormGroup; |
|
|
datapointsLimitFormGroup: FormGroup; |
|
|
|
|
|
|
|
|
modelValue: number | null; |
|
|
modelValue: number | null; |
|
|
|
|
|
|
|
|
@ -71,7 +86,7 @@ export class DatapointsLimitComponent implements ControlValueAccessor, OnInit, O |
|
|
|
|
|
|
|
|
ngOnInit() { |
|
|
ngOnInit() { |
|
|
this.datapointsLimitFormGroup = this.fb.group({ |
|
|
this.datapointsLimitFormGroup = this.fb.group({ |
|
|
limit: [null] |
|
|
limit: [null, [Validators.min(this.minDatapointsLimit()), Validators.max(this.maxDatapointsLimit())]] |
|
|
}); |
|
|
}); |
|
|
this.datapointsLimitFormGroup.get('limit').valueChanges.pipe( |
|
|
this.datapointsLimitFormGroup.get('limit').valueChanges.pipe( |
|
|
takeUntil(this.destroy$) |
|
|
takeUntil(this.destroy$) |
|
|
@ -82,10 +97,11 @@ export class DatapointsLimitComponent implements ControlValueAccessor, OnInit, O |
|
|
|
|
|
|
|
|
updateValidators() { |
|
|
updateValidators() { |
|
|
if (this.datapointsLimitFormGroup) { |
|
|
if (this.datapointsLimitFormGroup) { |
|
|
this.datapointsLimitFormGroup.get('limit').setValidators(this.required |
|
|
if (this.required) { |
|
|
? [Validators.required, Validators.min(this.minDatapointsLimit()), |
|
|
this.datapointsLimitFormGroup.get('limit').addValidators(Validators.required); |
|
|
Validators.max(this.maxDatapointsLimit())] |
|
|
} else { |
|
|
: []); |
|
|
this.datapointsLimitFormGroup.get('limit').removeValidators(Validators.required); |
|
|
|
|
|
} |
|
|
this.datapointsLimitFormGroup.get('limit').updateValueAndValidity(); |
|
|
this.datapointsLimitFormGroup.get('limit').updateValueAndValidity(); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
@ -122,6 +138,12 @@ export class DatapointsLimitComponent implements ControlValueAccessor, OnInit, O |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
validate(): ValidationErrors { |
|
|
|
|
|
return this.datapointsLimitFormGroup.get('limit').valid ? null : { |
|
|
|
|
|
datapointsLimitFormGroup: false, |
|
|
|
|
|
}; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
minDatapointsLimit() { |
|
|
minDatapointsLimit() { |
|
|
return this.timeService.getMinDatapointsLimit(); |
|
|
return this.timeService.getMinDatapointsLimit(); |
|
|
} |
|
|
} |
|
|
|