|
|
|
@ -23,6 +23,7 @@ import { |
|
|
|
NG_VALUE_ACCESSOR, |
|
|
|
ValidationErrors, |
|
|
|
Validator, |
|
|
|
ValidatorFn, |
|
|
|
Validators |
|
|
|
} from '@angular/forms'; |
|
|
|
import { TimeUnit, timeUnitTranslations } from '@home/components/rule-node/rule-node-config.models'; |
|
|
|
@ -79,6 +80,13 @@ export class TimeUnitInputComponent implements ControlValueAccessor, Validator, |
|
|
|
@Input() |
|
|
|
maxErrorText: string; |
|
|
|
|
|
|
|
@Input() |
|
|
|
@coerceNumber() |
|
|
|
stepMultipleOf: number; |
|
|
|
|
|
|
|
@Input() |
|
|
|
stepMultipleOfErrorText: string; |
|
|
|
|
|
|
|
@Input() |
|
|
|
subscriptSizing: SubscriptSizing = 'fixed'; |
|
|
|
|
|
|
|
@ -93,6 +101,9 @@ export class TimeUnitInputComponent implements ControlValueAccessor, Validator, |
|
|
|
@coerceBoolean() |
|
|
|
sameWidthInputs: boolean = false; |
|
|
|
|
|
|
|
@Input() |
|
|
|
containerClass: string | string[] | Record<string, boolean | undefined | null> = "flex gap-4"; |
|
|
|
|
|
|
|
timeUnits = Object.values(TimeUnit).filter(item => item !== TimeUnit.MILLISECONDS) as TimeUnit[]; |
|
|
|
|
|
|
|
timeUnitTranslations = timeUnitTranslations; |
|
|
|
@ -128,7 +139,7 @@ export class TimeUnitInputComponent implements ControlValueAccessor, Validator, |
|
|
|
this.timeUnits = this.timeUnits.filter(item => item !== TimeUnit.DAYS); |
|
|
|
} |
|
|
|
} |
|
|
|
if(this.required || this.maxTime) { |
|
|
|
if (this.required || this.maxTime || isDefinedAndNotNull(this.minTime) || this.stepMultipleOf) { |
|
|
|
const timeControl = this.timeInputForm.get('time'); |
|
|
|
const validators = [Validators.pattern(/^\d*$/)]; |
|
|
|
if (this.required) { |
|
|
|
@ -145,6 +156,10 @@ export class TimeUnitInputComponent implements ControlValueAccessor, Validator, |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
if (isDefinedAndNotNull(this.stepMultipleOf) && this.stepMultipleOf > 0) { |
|
|
|
validators.push(this.createStepMultipleOfValidator()); |
|
|
|
} |
|
|
|
|
|
|
|
timeControl.setValidators(validators); |
|
|
|
timeControl.updateValueAndValidity({ emitEvent: false }); |
|
|
|
} |
|
|
|
@ -170,6 +185,8 @@ export class TimeUnitInputComponent implements ControlValueAccessor, Validator, |
|
|
|
return this.minErrorText; |
|
|
|
} else if (this.timeInputForm.get('time').hasError('max') && this.maxErrorText) { |
|
|
|
return this.maxErrorText; |
|
|
|
} else if (this.timeInputForm.get('time').hasError('stepMultipleOf') && this.stepMultipleOfErrorText) { |
|
|
|
return this.stepMultipleOfErrorText; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@ -232,4 +249,34 @@ export class TimeUnitInputComponent implements ControlValueAccessor, Validator, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private createStepMultipleOfValidator(): ValidatorFn { |
|
|
|
return (control: AbstractControl): ValidationErrors | null => { |
|
|
|
const time = control.value; |
|
|
|
if (!isDefinedAndNotNull(time) || !isNumeric(time)) { |
|
|
|
return null; |
|
|
|
} |
|
|
|
const numericTime = Number(time); |
|
|
|
if (numericTime === 0) { |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
const timeUnit = control.parent?.get('timeUnit')?.value as TimeUnit; |
|
|
|
if (!timeUnit) { |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
const unitInSec = this.timeIntervalsInSec.get(timeUnit); |
|
|
|
const totalTimeInSec = numericTime * unitInSec; |
|
|
|
const multipleOfVal = this.stepMultipleOf; |
|
|
|
|
|
|
|
let isValid: boolean; |
|
|
|
if (totalTimeInSec < multipleOfVal) { |
|
|
|
isValid = (multipleOfVal % totalTimeInSec === 0); |
|
|
|
} else { |
|
|
|
isValid = (totalTimeInSec % multipleOfVal === 0); |
|
|
|
} |
|
|
|
return isValid ? null : { stepMultipleOf: true }; |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|