UNPKG

armisa-models

Version:
98 lines (84 loc) 2.68 kB
import { BaseSelfControl } from './BaseSelfControl'; import { ValidationChain, ValidationEventArgs } from '../chainOfResponsibility'; import { EnumValidateState } from '../enums'; export class SelfSelect extends BaseSelfControl< number | undefined, number | undefined, number | undefined, HTMLDivElement > { public defaultValueSelected?: number; private validateNormal = (eventArgs: ValidationEventArgs): void => { return; }; private validateRequired = (eventArgs: ValidationEventArgs): void => { if (this.required) { if (typeof this.value !== 'number') { eventArgs.error = 'هیچ موردی انتخاب نشده است'; eventArgs.state = EnumValidateState.empty; eventArgs.cancel = true; } } }; isValueEmpty = () => { return typeof this.#value === 'number'; } isValueNotEmpty = () => { return !this.isValueEmpty(); } validate = (): void => { const validationChain = new ValidationChain(); validationChain.validators.add(this.validateNormal); validationChain.validators.add(this.validateRequired); this.validation = validationChain.validate(); }; public cleaningClassInitializer = (): void => { this.hasChange = false; this.defaultValue = undefined; this.initializeListener = false; this.initializeProperties = false; this.validation = true; }; public refreshHasChange = (): void => { if (this.showHasChangeFlag) { this.hasChange = this.defaultValue !== this.#value; } }; public restartDefaultValue = (): void => { this.defaultValue = this.value; this.refreshHasChange(); }; #value: number | undefined; public cleaner?: () => void; public get value(): number | undefined { return this.#value; } public set value(value: number | undefined) { return; } public setValue = (value: number | undefined): void => { this.#value = value; this.refreshHasChange(); }; public deserialize = (value: number | undefined) => { if (typeof this.defaultValueSelected === 'number' && typeof value !== 'number') { this.#value = this.defaultValueSelected; } else { this.#value = value; } this.restartDefaultValue(); }; constructor(value: number | undefined) { super(); this.#value = value; this.restartDefaultValue(); } static empty(): SelfSelect { return new SelfSelect(undefined); } static deserialize(value?: number | null): SelfSelect { return new SelfSelect( (typeof value === 'number' ? value : undefined) as any ); } }