SUPPORT-9194: add script getting value depend on parent value last digit
This commit is contained in:
parent
e47098649d
commit
db71ddab3a
1 changed files with 49 additions and 0 deletions
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue