SUPPORT-9194: add script getting value depend on parent value last digit

This commit is contained in:
gulnaz 2025-05-23 15:40:05 +03:00
parent e47098649d
commit db71ddab3a

View file

@ -0,0 +1,49 @@
import {AnalyticalScope, Behavior, NotNull, ObjectRef, Text} from "@webbpm/base-package";
import {Subscription} from "rxjs";
import {OnDestroy} from "@angular/core";
@AnalyticalScope(Text)
export class DigitDependentValue extends Behavior implements OnDestroy {
@ObjectRef()
@NotNull()
public parent: Text;
@NotNull()
public oneDigitValue: string;
@NotNull()
public fromTwoToFourDigitValue: string;
@NotNull()
public otherDigitValue: string;
private text: Text;
private subscription: Subscription;
private excludedNumbers: number[] = [11, 12, 13, 14];
initialize() {
super.initialize();
this.text = this.getScript(Text);
this.subscription = this.parent.valueChangeEvent.subscribe(value => {
if (value) {
this.text.setValue(this.getValueDependOn(value));
}
});
}
private getValueDependOn(parentValue: string) {
let number = Number.parseInt(parentValue);
let lastDigit = Math.abs(number) % 10;
let lastDigits = Math.abs(number) % 100;
return this.excludedNumbers.includes(lastDigits)
? this.otherDigitValue
: lastDigit == 1
? this.oneDigitValue
: lastDigit > 1 && lastDigit < 5
? this.fromTwoToFourDigitValue
: this.otherDigitValue;
}
ngOnDestroy() {
this.subscription.unsubscribe();
}
}