UNPKG

armisa-models

Version:
121 lines (105 loc) 3.68 kB
import { AxiosInstance } from "axios"; import { EnumValidateState } from "../enums"; import { ElementFactory } from "../Page/ElementsOfFormFactory/ElementFactory"; import { IMainStateFactory } from "../Types"; import { Cach } from "./Cach"; interface ISelectCustomr { type: 0 | 1; code: string; name: string; } export class SelectCustomersFactory extends ElementFactory { public items: ISelectCustomr[] = []; public get any() { return this as any; } public showPotentialListView?: () => void; public showListView?: () => void; public forceUpdate: () => void = () => { }; constructor( _mainStateFactory: IMainStateFactory, _fieldName: string, _dispose: () => void, public required: boolean, public caption: string, public showHasChangeFlag: boolean, public axiosData: { axios: (controllerPath: string) => AxiosInstance; controllerPotentialPath: string; controllerActuallyPath: string; listPotentialViewActionPath?: string | undefined; listActuallyActionPath?: string | undefined; }, public tabIndex?: number, public payLoadKey?: string, responseKey?: string, defaultValue?: [], private onChange?: (e: SelectCustomersFactory) => void, ) { super(_mainStateFactory, _fieldName, _dispose, payLoadKey, responseKey); if (typeof defaultValue === 'number') { this.defaultValue = defaultValue; this._value = defaultValue; } if (Cach.isCached(this)) { const cach = Cach.getCached(this); this._value = cach.value; this.validation = cach.validation; this._hasChange = cach.hasChange; } } public focusToElement = () => { this.mainStateFactory.elementsOfForm.focuseToThisElement(this); }; validate = () => { if (this.required && !this._value) { // 'namingNotImplement' this.validation = [`${this.caption} وارد نشده است`, EnumValidateState.empty]; return this.validation; } this.validation = true; return this.validation; }; private defaultValue: ISelectCustomr[] | undefined | null = null; restartDefaultValue = () => { this.defaultValue = this._value; this.refreshHasChange(); }; refreshHasChange = () => { if (this.showHasChangeFlag) { this._hasChange = this.defaultValue !== this._value; } }; _value: ISelectCustomr[] | undefined | null = null; get value(): ISelectCustomr[] | undefined | null { return this._value; } setValue(value: ISelectCustomr[] | undefined | null) { this._value = value; Cach.setValue(this, value); this.forceUpdate(); if (this.onChange) { setTimeout(() => { this.onChange && this.onChange(this); }, 10); } } public deseriallize = (e?: ISelectCustomr[] | undefined | null) => { this.clearData(); if (e === null) { //do nothing } else if (e === undefined) { //do nothing } else { this._value = e; this.defaultValue = this._value; } }; public clearData = () => { Cach.clear(this); this._value = null; this.defaultValue = null; this.validation = true; this._hasChange = false; }; }