armisa-models
Version:
models of armisa!
124 lines (108 loc) • 3.78 kB
text/typescript
import { ValidationChain, ValidationEventArgs } from "../chainOfResponsibility";
import { ElementFactory } from "../Page/ElementsOfFormFactory/ElementFactory";
import { IMainStateFactory } from "../Types";
import { Cach } from "./Cach";
export class CheckBoxFactory extends ElementFactory {
public forceUpdate = () => { };
#value: boolean;
public get value() {
return this.#value;
}
public setValue = (value: boolean) => {
this.mainStateFactory.any.cach[this.factoryFieldName] = value;
this.#value = value;
this.refreshHasChange();
Cach.setValue(this, value);
this.forceUpdate();
};
public deseriallize = (e: any) => {
this.clearData();
if (e === null) {
//do nothing
} else if (e === undefined) {
//do nothing
} else if (typeof e === 'boolean') {
this.#value = e;
this.#defaultValue = e;
} else if (typeof e === 'number') {
this.#value = e > 0;
this.#defaultValue = this.#value;
} else if (typeof e === 'string') {
const isTrue = (): boolean => {
if (e.toLowerCase() === 'true') {
return true;
} else if (e === '1') {
return true;
}
return false;
}
this.#value = isTrue();
this.#defaultValue = this.#value;
}
Cach.setValue(this, this.#value);
}
public clearData = () => {
Cach.clear(this);
this.#value = false;
this.#defaultValue = false;
this.validation = true;
this._hasChange = false;
}
#defaultValue: boolean;
public get defaultValue() {
return this.#defaultValue;
}
public focusToElement = () => {
this.mainStateFactory.elementsOfForm.focuseToThisElement(this);
}
constructor(
_mainStateFactory: IMainStateFactory,
_fieldName: string,
_dispose: () => void,
public caption: string,
public disabled: boolean,
public readonly: boolean,
public showHasChangeFlag: boolean,
public dirLeftToRight: boolean,
public tabIndex?: number,
initialValue?: boolean,
payLoadKey?: string,
responseKey?: string,
) {
super(_mainStateFactory, _fieldName, _dispose, payLoadKey, responseKey);
if (typeof initialValue === 'boolean') {
this.#defaultValue = initialValue;
this.#value = initialValue;
} else {
this.#defaultValue = false;
this.#value = false;
}
if (Cach.isCached(this)) {
const cach = Cach.getCached(this);
this.#value = cach.value;
this.validation = cach.validation;
this._hasChange = cach.hasChange;
}
}
public restartDefaultValue = () => {
this.#defaultValue = this.value;
this.refreshHasChange();
};
public refreshHasChange = () => {
if (this.showHasChangeFlag) {
this._hasChange = this.defaultValue !== this.#value;
}
};
public validate = () => {
var validationChain = new ValidationChain();
validationChain.validators.add(this.validateNormal);
this.validation = validationChain.validate();
}
private validateNormal = (_eventArgs: ValidationEventArgs) => { };
isValueEmpty = () => {
return typeof this.#value === 'string' && this.#value !== '';
}
isValueNotEmpty = () => {
return !this.isValueEmpty();
}
}