UNPKG

armisa-models

Version:
358 lines (357 loc) 17.6 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 _StructrulCodeFactory_code, _StructrulCodeFactory_text; Object.defineProperty(exports, "__esModule", { value: true }); exports.StructrulCodeFactory = void 0; const nums2persian_1 = require("nums2persian"); const enums_1 = require("../../enums"); const ElementFactory_1 = require("../../Page/ElementsOfFormFactory/ElementFactory"); const Cach_1 = require("../Cach"); const TokenInfo_1 = require("../../AmisaAuth/Models/StorageManager/TokenInfo"); const SettingInfo_1 = require("../../AmisaAuth/Models/SettingInfo"); class StructrulCodeFactory extends ElementFactory_1.ElementFactory { get any() { return this; } refreshAllfromOfMainCode(_data) { if (this.useCaseType === 'MainPageCode' && this.mainStateFactory.elementsOfForm.responses && this.mainStateFactory.elementsOfForm.responses.length > 0) { this.mainStateFactory.elementsOfForm.responses.forEach((element) => { element.deseriallize(_data[element.responseKey]); }); const forceUpdate = this.mainStateFactory.any.forceUpdate; forceUpdate && forceUpdate(); } } getNewCodeData(currentCode) { this.mainStateFactory.elementsOfForm.showWaitingFormSpinner(); this.apiAxios.get(`${this.urls.new}/${currentCode}`) .then((response) => { if (response.data.isSuccess) { this.onChangeHandlerCode(response.data.newCode || ''); ///0 = new code this.setValidationState(0); this.childCount = 0; this.validate(); this.setCachedData(); this.refreshAllfromOfMainCode({}); this.codeWaitForLoad = null; this.forceUpdate(); } else { this.mainStateFactory.elementsOfForm.showInvalidArgumentMessageBox(response.data.massageRoot || 'some thing went wrong'); } }) .catch((error) => { console.error(error); this.forceUpdate(); }) .finally(() => { this.mainStateFactory.elementsOfForm.closeWaitingFormSpinner(); }); } get text() { return __classPrivateFieldGet(this, _StructrulCodeFactory_text, "f") || ''; } setText(text) { __classPrivateFieldSet(this, _StructrulCodeFactory_text, text, "f"); this.setCachedData(); this.forceUpdate(); } get value() { return __classPrivateFieldGet(this, _StructrulCodeFactory_code, "f"); } get code() { return __classPrivateFieldGet(this, _StructrulCodeFactory_code, "f") || ''; } setValue(code) { __classPrivateFieldSet(this, _StructrulCodeFactory_code, code, "f"); this.setCachedData(); this.forceUpdate(); } constructor(_mainStateFactory, _fieldName, _dispose, useCaseType, required, onlyLastLevel, caption, typeOfCode, showHasChangeFlag, tabIndex, initialValue, payLoadKey, responseKey, history, onChange) { super(_mainStateFactory, _fieldName, _dispose, payLoadKey, responseKey, history); this.useCaseType = useCaseType; this.required = required; this.onlyLastLevel = onlyLastLevel; this.caption = caption; this.typeOfCode = typeOfCode; this.showHasChangeFlag = showHasChangeFlag; this.tabIndex = tabIndex; this.onChange = onChange; this.forceUpdate = () => { }; this.focusToElement = () => { this.mainStateFactory.elementsOfForm.focuseToThisElement(this); this.element?.focus(); }; this.setValidationState = (result) => { this.codeValueState = result === null ? 'ok' : result === 0 ? 'newCode' : result === 1 ? 'dirty' : result === 2 ? 'withoutParent' : 'clear'; }; this.codeWaitForLoad = null; this.timerForLoad = null; this.clearTimerForLoad = () => { if (this.timerForLoad) { clearTimeout(this.timerForLoad); } }; this.getNewCode = () => { if (this.code === '') { this.mainStateFactory.elementsOfForm.showInvalidArgumentMessageBox('لطفا یک کد در سطح مورد نظر انتخاب کنید'); console.error('code in the same level did not choose'); } else { this.validate(); if (this.validation !== true) { this.mainStateFactory.elementsOfForm.showInvalidArgumentMessageBox(this.validation[0]); console.error(this.validation[0]); } else { this.getNewCodeData(this.code); } } }; this.setCachedData = () => { Cach_1.Cach.setValue(this, { fullName: this.fullName, childCount: this.childCount, name: this.name, id: this.id, codeValueState: this.codeValueState, code: this.code, text: this.text }); }; this.getCodeData = () => { this.clearTimerForLoad(); this.timerForLoad = setTimeout(() => { this.codeWaitForLoad = this.code; this.getCodeDataAxios(); }, 500); }; this.getCodeDataAxios = () => { if (this.codeWaitForLoad === '') { this.clearData(); return; } else if (this.codeValueState === 'dirty') { this.fullName = []; this.childCount = 0; this.codeWaitForLoad = null; this.validate(); this.setCachedData(); this.refreshAllfromOfMainCode({}); this.forceUpdate(); return; } this.mainStateFactory.elementsOfForm.showWaitingFormSpinner(); this.forceUpdateSpinner(); this.apiAxios.get(`${this.urls.get}/${this.codeWaitForLoad}`) .then((response) => { this.fullName = response.data.data.fullName; this.name = response.data.data.name; this.setValidationState(response.data.result); this.childCount = response.data.data.childCount; this.validate(); this.setCachedData(); this.refreshAllfromOfMainCode(response.data.data); this.codeWaitForLoad = null; this.forceUpdate(); }) .catch((error) => { console.error(error); this.forceUpdate(); }) .finally(() => { this.mainStateFactory.elementsOfForm.closeWaitingFormSpinner(); }); }; this.clearData = () => { Cach_1.Cach.clear(this); __classPrivateFieldSet(this, _StructrulCodeFactory_code, null, "f"); __classPrivateFieldSet(this, _StructrulCodeFactory_text, null, "f"); this._defaultValue = null; this.fullName = []; this.codeWaitForLoad = null; this.name = ''; this.codeValueState = 'clear'; this.childCount = 0; this.validation = true; this.refreshAllfromOfMainCode({}); }; this.id = null; this.fullName = []; this.name = ''; ///state that get from server data this.codeValueState = 'clear'; this.childCount = 0; this.validate = () => { if (this.required && !this.code) { // 'namingNotImplement' this.validation = [`${this.caption} وارد نشده است`, enums_1.EnumValidateState.empty]; return this.validation; } else if (this.codeValueState === 'dirty') { this.validation = [`طول ${this.caption} وارد شده معتبر نیست`, enums_1.EnumValidateState.invalidLength]; return this.validation; } else if (this.codeValueState === 'withoutParent') { this.validation = [`${this.caption} وارد شده سرفصل بالاتر ندارد`, enums_1.EnumValidateState.withoutParent]; return this.validation; } else if (this.onlyLastLevel && this.codeValueState === 'newCode') { this.validation = [`${this.caption} وارد شده وجود ندارد`, enums_1.EnumValidateState.withoutParent]; return this.validation; } else if (this.onlyLastLevel && this.childCount > 0) { this.validation = [`${this.caption} وارد شده سرفصل جزء دارد`, enums_1.EnumValidateState.hasChildren]; return this.validation; } this.validation = true; return this.validation; }; _StructrulCodeFactory_code.set(this, null); _StructrulCodeFactory_text.set(this, null); this.deseriallize = (e) => { }; this._defaultValue = null; this.restartDefaultValue = () => { this._defaultValue = this.code; this.refreshHasChange(); }; this.refreshHasChange = () => { if (this.showHasChangeFlag) { this._hasChange = this._defaultValue !== __classPrivateFieldGet(this, _StructrulCodeFactory_code, "f"); } }; this.forceUpdateSpinner = () => { }; this.onChangeHandlerElement = (element) => { const cursor = element.selectionEnd || 0; const valueNotPersian = element.value; this.onChangeHandlerCode(valueNotPersian, cursor); }; this.onChangeHandlerCode = (code, cursorPosition) => { const cursor = cursorPosition || code.length; const value = (0, nums2persian_1.persianStringToEnglishString)(code, false); __classPrivateFieldSet(this, _StructrulCodeFactory_code, value, "f"); this.codeValueState = this.coding.changeEventOfCode(this.code).state; const result = value.split(''); const resultLength = result.length; for (let index = 0; index < resultLength; index++) { if (index < cursor && index < this.coding.maxCodingLen) { if (index === this.coding.untilLevelLen9 && this.coding.levelLen9) { result.splice(this.coding.untilLevelLen9 + 8, 0, '-'); } else if (index === this.coding.untilLevelLen8 && this.coding.levelLen8) { result.splice(this.coding.untilLevelLen8 + 7, 0, '-'); } else if (index === this.coding.untilLevelLen7 && this.coding.levelLen7) { result.splice(this.coding.untilLevelLen7 + 6, 0, '-'); } else if (index === this.coding.untilLevelLen6 && this.coding.levelLen6) { result.splice(this.coding.untilLevelLen6 + 5, 0, '-'); } else if (index === this.coding.untilLevelLen5 && this.coding.levelLen5) { result.splice(this.coding.untilLevelLen5 + 4, 0, '-'); } else if (index === this.coding.untilLevelLen4 && this.coding.levelLen4) { result.splice(this.coding.untilLevelLen4 + 3, 0, '-'); } else if (index === this.coding.untilLevelLen3 && this.coding.levelLen3) { result.splice(this.coding.untilLevelLen3 + 2, 0, '-'); } else if (index === this.coding.untilLevelLen2 && this.coding.levelLen2) { result.splice(this.coding.untilLevelLen2 + 1, 0, '-'); } else if (index === this.coding.untilLevelLen1 && this.coding.levelLen1) { result.splice(this.coding.untilLevelLen1, 0, '-'); } } } this.setText(result.join('')); this.onChange && this.onChange(this); this.getCodeData(); }; this.getPlaceholder = () => { const levels = []; if (this.coding.levelLen1) levels.push(''.padEnd(this.coding.levelLen1, '#')); if (this.coding.levelLen2) levels.push(''.padEnd(this.coding.levelLen2, '#')); if (this.coding.levelLen3) levels.push(''.padEnd(this.coding.levelLen3, '#')); if (this.coding.levelLen4) levels.push(''.padEnd(this.coding.levelLen4, '#')); if (this.coding.levelLen5) levels.push(''.padEnd(this.coding.levelLen5, '#')); if (this.coding.levelLen6) levels.push(''.padEnd(this.coding.levelLen6, '#')); if (this.coding.levelLen7) levels.push(''.padEnd(this.coding.levelLen7, '#')); if (this.coding.levelLen8) levels.push(''.padEnd(this.coding.levelLen8, '#')); if (this.coding.levelLen9) levels.push(''.padEnd(this.coding.levelLen9, '#')); return levels.join(' - '); }; this.settingInfo = (0, SettingInfo_1.useSettingInfo)(); switch (typeOfCode) { case "hesab": this.urls = (0, TokenInfo_1.useUrls)('BaseCode', 'Hesab'); this.coding = this.settingInfo.codingHesab.getStructurCoding(); break; case "kala": this.urls = (0, TokenInfo_1.useUrls)('BaseCode', 'Kala'); this.coding = this.settingInfo.codingKala.getStructurCoding(); break; case "markaz1": this.urls = (0, TokenInfo_1.useUrls)('BaseCode', 'Markaz1'); this.coding = this.settingInfo.codingMarkaz1.getStructurCoding(); break; case "markaz2": this.urls = (0, TokenInfo_1.useUrls)('BaseCode', 'Markaz2'); this.coding = this.settingInfo.codingMarkaz2.getStructurCoding(); break; case "markaz3": this.urls = (0, TokenInfo_1.useUrls)('BaseCode', 'Markaz3'); this.coding = this.settingInfo.codingMarkaz3.getStructurCoding(); break; default: throw new Error(`type of code : '${typeOfCode}' not implemented`); } this.apiAxios = (0, TokenInfo_1.useAxios)(this.urls); if (typeof initialValue === 'object') { this.name = initialValue.name; this.fullName = initialValue.fullName; this.childCount = initialValue.childCount; this.id = initialValue.id || 0; this.codeValueState = initialValue.codeValueState; __classPrivateFieldSet(this, _StructrulCodeFactory_code, initialValue.code, "f"); __classPrivateFieldSet(this, _StructrulCodeFactory_text, initialValue.text, "f"); } this.placeholder = this.getPlaceholder(); this.maxLength = this.getMaxLength(); if (Cach_1.Cach.isCached(this)) { const cach = Cach_1.Cach.getCached(this); this.name = cach.value.name; this.fullName = cach.value.fullName; this.childCount = cach.value.childCount; this.id = cach.value.id; this.codeValueState = cach.value.codeValueState; __classPrivateFieldSet(this, _StructrulCodeFactory_code, cach.value.code, "f"); __classPrivateFieldSet(this, _StructrulCodeFactory_text, cach.value.text, "f"); this.validation = cach.validation; this._hasChange = cach.hasChange; } } getMaxLength() { return this.coding.maxCodingLen + this.coding.maxLevel - 1; } } exports.StructrulCodeFactory = StructrulCodeFactory; _StructrulCodeFactory_code = new WeakMap(), _StructrulCodeFactory_text = new WeakMap();