UNPKG

armisa-models

Version:
234 lines (233 loc) 12.5 kB
"use strict"; var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) { if (kind === "m") throw new TypeError("Private method is not writable"); if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it"); return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value; }; var _DateBoxFactory_text, _DateBoxFactory_defaultValue, _DateBoxFactory_value; Object.defineProperty(exports, "__esModule", { value: true }); exports.DateBoxFactory = void 0; const nums2persian_1 = require("nums2persian"); const chainOfResponsibility_1 = require("../../chainOfResponsibility"); const enums_1 = require("../../enums"); const ElementFactory_1 = require("../../Page/ElementsOfFormFactory/ElementFactory"); const Cach_1 = require("../Cach"); class DateBoxFactory extends ElementFactory_1.ElementFactory { get any() { return this; } get text() { return __classPrivateFieldGet(this, _DateBoxFactory_text, "f"); } set text(v) { __classPrivateFieldSet(this, _DateBoxFactory_text, v, "f"); } get defaultValue() { return __classPrivateFieldGet(this, _DateBoxFactory_defaultValue, "f"); } get value() { return __classPrivateFieldGet(this, _DateBoxFactory_value, "f") || ''; } setValue(value) { __classPrivateFieldSet(this, _DateBoxFactory_value, value, "f"); if (this.currentTextState === 'shamsi') { __classPrivateFieldSet(this, _DateBoxFactory_text, __classPrivateFieldGet(this, _DateBoxFactory_value, "f"), "f"); } else { __classPrivateFieldSet(this, _DateBoxFactory_text, (0, nums2persian_1.ReturnMiladyDate)(__classPrivateFieldGet(this, _DateBoxFactory_value, "f")), "f"); } this.validate(); Cach_1.Cach.setValue(this, value); this.onChange && this.onChange(this); this.forceUpdate(); } constructor(_mainStateFactory, _fieldName, _dispose, required, showHasChangeFlag, caption, _placeHolder, tabIndex, initialValue, payLoadKey, responseKey, history, onChange) { super(_mainStateFactory, _fieldName, _dispose, payLoadKey, responseKey, history); this.required = required; this.showHasChangeFlag = showHasChangeFlag; this.caption = caption; this.tabIndex = tabIndex; this.onChange = onChange; this.forceUpdate = () => { }; this.focusToElement = () => { this.mainStateFactory.elementsOfForm.focuseToThisElement(this); }; this.validation = true; this.validationState = 'clear'; this.validateNormal = (eventArgs) => { if (typeof this.value === 'string' && this.value !== '') { if (this.value.split('/').length === 3) { if (!(0, nums2persian_1.isValid)(this.value)) { eventArgs.error = 'تاریخ وارد شده معتبر نیست'; eventArgs.state = enums_1.EnumValidateState.invalidValue; eventArgs.cancel = true; } } else { eventArgs.error = 'مقدار تاریخ بدرستی وارد نشده است'; eventArgs.state = enums_1.EnumValidateState.invalidValue; eventArgs.cancel = true; } } }; this.validateRequired = (eventArgs) => { if (this.required) { if (this.value === null || this.value === undefined || (typeof this.value !== 'string' || this.value === '')) { eventArgs.error = 'تاریخ وارد نشده است'; eventArgs.state = enums_1.EnumValidateState.empty; eventArgs.cancel = true; } } }; this.validateMinDate = (eventArgs) => { if (this.minDate) { if (!this.value) { eventArgs.error = `تاریخ وارد نشده است. حداقل تاریخ مجاز: ${this.minDate}`; eventArgs.state = enums_1.EnumValidateState.minValue; eventArgs.cancel = true; } else if (this.value < this.minDate) { eventArgs.error = `تاریخ مقدار وارد شده کمتر از مقدار مجاز میباشد. حداقل تاریخ مجاز: ${this.minDate}`; eventArgs.state = enums_1.EnumValidateState.minValue; eventArgs.cancel = true; } } }; this.validateMaxDate = (eventArgs) => { if (this.maxDate) { if (this.value && this.value > this.maxDate) { eventArgs.error = `تاریخ مقدار وارد شده بیشتر از مقدار مجاز میباشد. حداکثر تاریخ مجاز: ${this.maxDate}`; eventArgs.state = enums_1.EnumValidateState.maxLength; eventArgs.cancel = true; } } }; this.isValueEmpty = () => { return typeof __classPrivateFieldGet(this, _DateBoxFactory_value, "f") === 'string' && __classPrivateFieldGet(this, _DateBoxFactory_value, "f") !== ''; }; this.isValueNotEmpty = () => { return !this.isValueEmpty(); }; this.validate = () => { var validationChain = new chainOfResponsibility_1.ValidationChain(); validationChain.validators.add(this.validateNormal); validationChain.validators.add(this.validateRequired); validationChain.validators.add(this.validateMinDate); validationChain.validators.add(this.validateMaxDate); this.validation = validationChain.validate(); }; this.currentTextState = 'shamsi'; this.changeCurrentTextState = () => { if (this.currentTextState === 'shamsi') { this.currentTextState = 'miladi'; if (__classPrivateFieldGet(this, _DateBoxFactory_value, "f")) { this.text = (0, nums2persian_1.ReturnMiladyDate)(__classPrivateFieldGet(this, _DateBoxFactory_value, "f")); } } else { this.currentTextState = 'shamsi'; this.text = __classPrivateFieldGet(this, _DateBoxFactory_value, "f") || ''; } this.forceUpdate(); }; _DateBoxFactory_text.set(this, ''); _DateBoxFactory_defaultValue.set(this, void 0); this.restartDefaultValue = () => { __classPrivateFieldSet(this, _DateBoxFactory_defaultValue, __classPrivateFieldGet(this, _DateBoxFactory_value, "f"), "f"); this.refreshHasChange(); }; this.refreshHasChange = () => { if (this.showHasChangeFlag) { this._hasChange = __classPrivateFieldGet(this, _DateBoxFactory_defaultValue, "f") !== __classPrivateFieldGet(this, _DateBoxFactory_value, "f"); } }; _DateBoxFactory_value.set(this, void 0); this.deseriallize = (e) => { this.clearData(); if (e === null) { //do nothing } else if (e === undefined) { //do nothing } else if (typeof e === 'object') { __classPrivateFieldSet(this, _DateBoxFactory_value, JSON.stringify(e), "f"); __classPrivateFieldSet(this, _DateBoxFactory_defaultValue, __classPrivateFieldGet(this, _DateBoxFactory_value, "f"), "f"); if (this.currentTextState === 'shamsi') { __classPrivateFieldSet(this, _DateBoxFactory_text, __classPrivateFieldGet(this, _DateBoxFactory_value, "f"), "f"); } else { __classPrivateFieldSet(this, _DateBoxFactory_text, (0, nums2persian_1.ReturnMiladyDate)(__classPrivateFieldGet(this, _DateBoxFactory_value, "f")), "f"); } } else { __classPrivateFieldSet(this, _DateBoxFactory_value, e, "f"); __classPrivateFieldSet(this, _DateBoxFactory_defaultValue, __classPrivateFieldGet(this, _DateBoxFactory_value, "f"), "f"); if (this.currentTextState === 'shamsi') { __classPrivateFieldSet(this, _DateBoxFactory_text, __classPrivateFieldGet(this, _DateBoxFactory_value, "f"), "f"); } else { __classPrivateFieldSet(this, _DateBoxFactory_text, (0, nums2persian_1.ReturnMiladyDate)(__classPrivateFieldGet(this, _DateBoxFactory_value, "f")), "f"); } } Cach_1.Cach.setValue(this, __classPrivateFieldGet(this, _DateBoxFactory_value, "f")); }; this.clearData = () => { Cach_1.Cach.clear(this); __classPrivateFieldSet(this, _DateBoxFactory_value, null, "f"); __classPrivateFieldSet(this, _DateBoxFactory_defaultValue, null, "f"); __classPrivateFieldSet(this, _DateBoxFactory_text, '', "f"); this.validation = true; this._hasChange = false; this.forceUpdate(); }; this.changeHandler = (element) => { if (this.currentTextState === 'miladi') { return; } this.setValue((0, nums2persian_1.onChangeInputDateBoxShamsiHandleValue)(element.value, this.value)); }; if (typeof initialValue === 'string') { __classPrivateFieldSet(this, _DateBoxFactory_defaultValue, initialValue, "f"); __classPrivateFieldSet(this, _DateBoxFactory_value, initialValue, "f"); if (this.currentTextState === 'shamsi') { __classPrivateFieldSet(this, _DateBoxFactory_text, __classPrivateFieldGet(this, _DateBoxFactory_value, "f"), "f"); } else { __classPrivateFieldSet(this, _DateBoxFactory_text, (0, nums2persian_1.ReturnMiladyDate)(__classPrivateFieldGet(this, _DateBoxFactory_value, "f")), "f"); } } else { __classPrivateFieldSet(this, _DateBoxFactory_defaultValue, null, "f"); __classPrivateFieldSet(this, _DateBoxFactory_value, null, "f"); } if (Cach_1.Cach.isCached(this)) { const cach = Cach_1.Cach.getCached(this); __classPrivateFieldSet(this, _DateBoxFactory_value, cach.value, "f"); if (__classPrivateFieldGet(this, _DateBoxFactory_value, "f")) { if (this.currentTextState === 'shamsi') { __classPrivateFieldSet(this, _DateBoxFactory_text, __classPrivateFieldGet(this, _DateBoxFactory_value, "f"), "f"); } else { __classPrivateFieldSet(this, _DateBoxFactory_text, (0, nums2persian_1.ReturnMiladyDate)(__classPrivateFieldGet(this, _DateBoxFactory_value, "f")), "f"); } } this.validation = cach.validation; this._hasChange = cach.hasChange; } if (typeof _placeHolder === 'string') { this.placeHolder = _placeHolder; } else { this.placeHolder = '####/##/##'; } } } exports.DateBoxFactory = DateBoxFactory; _DateBoxFactory_text = new WeakMap(), _DateBoxFactory_defaultValue = new WeakMap(), _DateBoxFactory_value = new WeakMap();