UNPKG

armisa-models

Version:
92 lines (91 loc) 4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.UserBoxFactory = void 0; const chainOfResponsibility_1 = require("../chainOfResponsibility"); const enums_1 = require("../enums"); const ElementFactory_1 = require("../Page/ElementsOfFormFactory/ElementFactory"); const nums2persian_1 = require("nums2persian"); const Cach_1 = require("./Cach"); class UserBoxFactory extends ElementFactory_1.ElementFactory { constructor(mainStateFactory, fieldName, _dispose, caption, placeHolder, tabIndex, type = 'englishName', payLoadKey, responseKey) { super(mainStateFactory, fieldName, _dispose, payLoadKey, responseKey); this.caption = caption; this.placeHolder = placeHolder; this.tabIndex = tabIndex; this.type = type; this.forceUpdate = () => { }; this.value = null; this.setValue = (value) => { if (typeof value !== 'string') { this.value = null; this.forceUpdate(); } else if (this.type === 'code') { this.setValueCodeUserName(value); } else if (this.type === 'englishName') { this.setValueEnglishUserName(value); } else if (this.type === 'farsiName') { this.setValueFarsiUserName(value); } else if (this.type === 'email') { this.setValueEmailUserName(value); } Cach_1.Cach.setValue(this, value); }; this.setValueCodeUserName = (value) => { let newValue = value.split('').map(i => '۱۲۳۴۵۶۷۸۹۰'.includes(i) ? (0, nums2persian_1.persianCharToEnglishNumber)(i).toString() : i).join(''); const match = newValue.match(/[0-9]/g) || []; newValue = match.join(''); this.value = newValue; this.forceUpdate(); }; this.setValueEnglishUserName = (value) => { let newValue = value.split('').map(i => '۱۲۳۴۵۶۷۸۹۰'.includes(i) ? (0, nums2persian_1.persianCharToEnglishNumber)(i).toString() : i).join(''); const match = newValue.match(/[a-zA-Z0-9_]/g) || []; newValue = match.join(''); this.value = newValue; this.forceUpdate(); }; this.setValueEmailUserName = (value) => { let newValue = value.split('').map(i => '۱۲۳۴۵۶۷۸۹۰'.includes(i) ? (0, nums2persian_1.persianCharToEnglishNumber)(i).toString() : i).join(''); const match = newValue.match(/[a-zA-Z0-9@._]/g) || []; newValue = match.join(''); this.value = newValue; this.forceUpdate(); }; this.setValueFarsiUserName = (value) => { this.value = value; this.forceUpdate(); }; this.clearData = () => { Cach_1.Cach.clear(this); }; this.deseriallize = () => { }; this.refreshHasChange = () => { }; this.restartDefaultValue = () => { }; this.validate = () => { var validationChain = new chainOfResponsibility_1.ValidationChain(); validationChain.validators.add(this.validateRequired); this.validation = validationChain.validate(); }; this.validateRequired = (eventArgs) => { if (!this.value) { eventArgs.error = 'اطلاعاتی وارد نشده است'; eventArgs.state = enums_1.EnumValidateState.empty; eventArgs.cancel = true; } }; if (Cach_1.Cach.isCached(this)) { const cach = Cach_1.Cach.getCached(this); this.value = cach.value; this.validation = cach.validation; this._hasChange = cach.hasChange; } } } exports.UserBoxFactory = UserBoxFactory;