UNPKG

armisa-models

Version:
114 lines (101 loc) 3.65 kB
import { BaseSelfControl } from './BaseSelfControl'; import { ValidationChain, ValidationEventArgs } from '../chainOfResponsibility'; import { EnumValidateState } from '../enums'; export class SelfAmount extends BaseSelfControl< number | null, number | null, number | null, HTMLInputElement > { public minValue: number | undefined; public maxValue: number | undefined; isValueEmpty = () => { return typeof this.value !== 'number'; } isValueNotEmpty = () => { return !this.isValueEmpty(); } private validateNormal = (eventArgs: ValidationEventArgs) => { }; private validateRequired = (eventArgs: ValidationEventArgs) => { if (this.required) { if (typeof this.value !== 'number') { eventArgs.error = 'اطلاعاتی وارد نشده است'; eventArgs.state = EnumValidateState.empty; eventArgs.cancel = true; } } }; private validateMinValue = (eventArgs: ValidationEventArgs) => { if (this.minValue) { if (typeof this.value !== 'number') { eventArgs.error = `هیچ مقداری وارد نشده است. حداقل مقدار مجاز: ${this.minValue}`; eventArgs.state = EnumValidateState.minValue; eventArgs.cancel = true; } else if (this.value < this.minValue) { eventArgs.error = `مقدار وارد شده کمتر از مقدار مجاز میباشد. حداقل مقدار مجاز: ${this.minValue}`; eventArgs.state = EnumValidateState.minValue; eventArgs.cancel = true; } } }; private validateMaxValue = (eventArgs: ValidationEventArgs) => { if (this.maxValue) { if (typeof this.value === 'number' && this.value > this.maxValue) { eventArgs.error = `مقدار وارد شده بیشتر از مقدار مجاز میباشد. حداکثر مقدار مجاز: ${this.maxValue}`; eventArgs.state = EnumValidateState.maxValue; eventArgs.cancel = true; } } }; validate = () => { var validationChain = new ValidationChain(); validationChain.validators.add(this.validateNormal); validationChain.validators.add(this.validateRequired); validationChain.validators.add(this.validateMinValue); validationChain.validators.add(this.validateMaxValue); this.validation = validationChain.validate(); }; public cleaningClassInitializer = () => { this.hasChange = false; this.defaultValue = undefined; this.initializeListener = false; this.initializeProperties = false; this.validation = true; }; public refreshHasChange = () => { if (this.showHasChangeFlag) { this.hasChange = this.defaultValue !== this.#value; } }; public restartDefaultValue = () => { this.defaultValue = this.value; this.refreshHasChange(); }; #value: number | null; public get value() { return this.#value; } public set value(value: any) { } public setValue = (value: number | null) => { this.#value = value; this.refreshHasChange(); }; public deserialize = (value: number | null) => { this.#value = value; this.restartDefaultValue(); }; constructor(value: number | null) { super(); this.#value = value; this.restartDefaultValue(); } static empty(): SelfAmount { return new SelfAmount(null); } static deserialize(value?: number): SelfAmount { return new SelfAmount(typeof value === 'number' ? value : null); } static createCopy(old: SelfAmount) { return new SelfAmount(old.#value); } }