UNPKG

armisa-models

Version:
293 lines (292 loc) 13.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ElementsOfFormFactory = void 0; const AttachedFilesFactory_1 = require("../../ComponentFactory/AttachedFilesFactory"); const ToolStripFactory_1 = require("../../ComponentFactory/ToolStrip/ToolStripFactory"); const Types_1 = require("../../Types"); const Argument_1 = require("./Argument"); const Tabbing_1 = require("./Tabbing"); ; class ElementsOfFormFactory { get headPayLoad() { const convertOtherToObject = () => { const initialValue = {}; return this.otherPayLoad?.reduce((obj, item) => { return { ...obj, [item.payLoadKey]: item.object.getProp(item.fieldName), }; }, initialValue); }; const other = convertOtherToObject(); const convertPayloadToObject = () => { const initialValue = {}; return this.payLoad?.reduce((obj, item) => { return { ...obj, [item.payLoadKey]: item.value, }; }, initialValue); }; const payload = convertPayloadToObject(); return { ...other, ...payload, ...this.customPayLoad }; } get searchParameter() { return { ...this.filters, ...this.pageParameter, ...this.headPayLoad }; } get hasChange() { return this.elements.some(i => i.hasChange); } constructor(mainStateFactory, initializeCustomPayLoad) { this.mainStateFactory = mainStateFactory; this.filters = {}; this.initializeElements = () => { this.elements.forEach(element => { element.clearData(); }); }; this.addResponseElement = (element) => { if (this.responses === undefined) { this.responses = []; } this.responses.push(element); }; this.deserializedResponse = (data) => { if (this.responses && this.responses.length > 0) { this.responses.forEach((element) => { element.deseriallize(data.getProp(element.responseKey)); }); } }; this.otherPayLoad = []; this.addOtherPayLoadKey = (object, payLoadKey, fieldName) => { if (typeof payLoadKey === 'string' && !this.otherPayLoad.find(i => i.payLoadKey === payLoadKey)) { this.otherPayLoad.push({ object, payLoadKey, fieldName }); } }; this.addPayloadElement = (element) => { if (this.payLoad === undefined) { this.payLoad = []; } this.payLoad.push(element); }; this.gotoPage = (page) => { if (typeof page === 'number' && page > 0 && typeof this.pageParameter.totalPages === 'number' && page <= this.pageParameter.totalPages) { this.pageParameter.currentPage = page; } else { this.pageParameter.currentPage = 1; } }; this.goNextPage = () => { if (typeof this.pageParameter.totalPages === 'number' && this.pageParameter.currentPage < this.pageParameter.totalPages) { this.pageParameter.currentPage = this.pageParameter.currentPage + 1; } }; this.goPreviousPage = () => { if (this.pageParameter.currentPage > 1) { this.pageParameter.currentPage = this.pageParameter.currentPage - 1; } }; this.goLastPage = () => { if (typeof this.pageParameter.totalPages === 'number') { this.pageParameter.currentPage = this.pageParameter.totalPages; } }; this.goFirstPage = () => { }; this.showSendByLetterForm = (type, object) => { if (!object) { this.showInvalidArgumentMessageBox('اطلاعاتی به درستی برای ارسال نامه تشخیص داده نشد'); console.error('invalid object to add letter', object); return; } const id = object.getProp('id'); if (!(typeof id === 'number' && id > 0)) { this.showInvalidArgumentMessageBox('شناسه به درستی تشخیص داده نشده'); console.error('invalid field id in object to add letter', object); return; } switch (type) { case Types_1.OfficialAttacheFormsType.AnbarHead: case Types_1.OfficialAttacheFormsType.OfficialAppointment: case Types_1.OfficialAttacheFormsType.OfficialCall: case Types_1.OfficialAttacheFormsType.OfficialEmail: case Types_1.OfficialAttacheFormsType.OfficialLetter: case Types_1.OfficialAttacheFormsType.OfficialNote: case Types_1.OfficialAttacheFormsType.OfficialSms: case Types_1.OfficialAttacheFormsType.OfficialTask: break; default: const yearId = object.getProp('yearId'); if (!(typeof yearId === 'number' && yearId > 0)) { this.showInvalidArgumentMessageBox('سال مالی به درستی تشخیص داده نشده'); console.error('invalid field yearid in object to add letter', object); return; } break; } this.mainStateManager.showSendByLetterForm(type, object); }; this.closeSendByLetterForm = () => { this.mainStateFactory.mainStateManager.sendByLetterForm?.close(); }; this.showPopup = (children, element, onClosedPopup) => { if (this.mainStateFactory.elementsOfForm.popup) { return; } this.mainStateManager.showPopup(this.mainStateFactory, children, element, onClosedPopup); }; this.closePopup = () => { this.popup?.close(); }; this.showModal = (children, onClosedModal, argument, customPayload, finedInRows, ExtraStackMenuComponent, typeOfForm, cssClasses) => { if (this.mainStateFactory.elementsOfForm.childModal) { return; } let argumentClass; if (typeof argument === 'number') { argumentClass = Argument_1.Argument.Id(argument, finedInRows); } else if (typeof argument === 'string') { argumentClass = Argument_1.Argument.Code(argument, finedInRows); } else if (typeof argument === 'object') { argumentClass = Argument_1.Argument.Object(argument, finedInRows); } else { argumentClass = Argument_1.Argument.Object({}, false); } argumentClass.typeOfForm = typeOfForm; argumentClass.ExtraStackMenuComponent = ExtraStackMenuComponent; argumentClass.customPayload = customPayload; this.mainStateManager.showModal(this.mainStateFactory, children, false, onClosedModal, argumentClass, cssClasses); }; this.closeModal = () => { this.childModal?.close(); }; this.showFullScreenWaittingSpinner = (onClosedModal) => { this.mainStateManager.showFullScreenWaittingSpinner(this.mainStateFactory, onClosedModal); }; this.closeFullScreenWaittingSpinner = () => { this.childModal?.close(); }; this.showSomeThingWentWrong = (onClosedModal) => { this.mainStateManager.showSomeThingWentWrong(this.mainStateFactory, onClosedModal); }; this.showErrorMessageBox = (error, onClosedModal) => { this.mainStateManager.showErrorMessageBox(this.mainStateFactory, error, onClosedModal); }; this.showInvalidArgumentMessageBox = (description, onClosedModal) => { this.mainStateManager.showInvalidArgumentMessageBox(this.mainStateFactory, description, onClosedModal); }; this.showSuccessFullMessageBox = (caption, text, description, onClosedModal) => { this.mainStateManager.showSuccessFullMessageBox(this.mainStateFactory, caption, text, description, onClosedModal); }; this.showAreYouSureDeleteMessageBox = (onClickDelete, caption, text, description) => { this.mainStateManager.showAreYouSureDeleteMessageBox(this.mainStateFactory, onClickDelete, caption, text, description); }; this.showThereAreSomeErrorYouCanNot = (countOfError, onClosedModal) => { this.mainStateManager.showThereAreSomeErrorYouCanNot(this.mainStateFactory, countOfError, onClosedModal); }; this.showThereIsNotAnyChangeForSave = (onClosedModal) => { this.mainStateManager.showThereIsNotAnyChangeForSave(this.mainStateFactory, onClosedModal); }; this.showAttachedFiles = (argument, axiosData, caption, onClosedModal) => { this.mainStateManager.showAttachedFiles(this.mainStateFactory, argument, axiosData, caption, onClosedModal); }; this.showWaitingFormSpinner = () => { this.isShowWaitingFormSpinner = true; const forceUpdate = this.mainStateFactory.any.forceUpdate; if (typeof forceUpdate === 'function') { forceUpdate(); } }; this.closeWaitingFormSpinner = () => { this.isShowWaitingFormSpinner = false; const forceUpdate = this.mainStateFactory.any.forceUpdate; if (typeof forceUpdate === 'function') { forceUpdate(); } }; this.showWaitingTableSpinner = () => { this.isShowWaitingTableSpinner = true; const forceUpdate = this.mainStateFactory.any.forceUpdate; if (typeof forceUpdate === 'function') { forceUpdate(); } }; this.closeWaitingTableSpinner = () => { this.isShowWaitingTableSpinner = false; const forceUpdate = this.mainStateFactory.any.forceUpdate; if (typeof forceUpdate === 'function') { forceUpdate(); } }; //** return number of error */ this.validate = () => { let numberofError = 0; this.elements.forEach(element => { element.validate(); if (!(typeof element.validation === 'boolean' && element.validation === true)) { numberofError += 1; } }); if (this.mainStateFactory.any.forceUpdate) { this.mainStateFactory.any.forceUpdate(); } return numberofError; }; this.addElement = (element) => { this.elements.push(element); if (typeof element.tabIndex === 'number') { this.tabbing.elementTabbing.focusAbleElements.push(element); this.tabbing.elementTabbing.focusAbleElements.sort((a, b) => a.tabIndex - b.tabIndex); } }; this.removeElement = (element) => { delete element.mainStateFactory.any[element.factoryFieldName]; this.elements = this.elements.filter(i => i !== element); if (typeof element.tabIndex === 'number') { this.tabbing.elementTabbing.focusAbleElements = this.tabbing.elementTabbing.focusAbleElements.filter(i => i !== element); this.tabbing.elementTabbing.focusAbleElements.sort((a, b) => a.tabIndex - b.tabIndex); } }; this.focuseToThisElement = (factory) => { if (typeof factory.tabIndex === 'number' && factory.element) { this.tabbing.elementTabbing.focusToElementByHTMLElement(factory.element); } }; this.saveAttachedFiles = async (id) => { const find = this.elements.find(i => i instanceof AttachedFilesFactory_1.AttachedFilesFactory); if (find) { await find.save(id); } }; this.close = () => { if (this.subPageItem) { this.subPageItem.close(); } else if (this.childModal) { this.childModal.mainStateFactory.any.dispose && this.childModal.mainStateFactory.any.dispose(); this.childModal.close(); } else if (this.subPageFactory && this.subPageFactory.subPages.length === 0) { this.mainStateFactory.any.dispose && this.mainStateFactory.any.dispose(); this.mainStateManager.history && this.mainStateManager.history.push('/home'); } }; this.mainStateManager = this.mainStateFactory.mainStateManager; this.toolStripFactory = new ToolStripFactory_1.ToolStripFactory(this); this.tabbing = new Tabbing_1.Tabbing(this); this.elements = []; this.pageParameter = { currentPage: 1, itemsPerPage: 50, totalPages: 1, }; this.customPayLoad = initializeCustomPayLoad ? initializeCustomPayLoad : {}; } } exports.ElementsOfFormFactory = ElementsOfFormFactory;