UNPKG

armisa-models

Version:
215 lines (214 loc) 8.89 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.StructuralCodeListViewFactory = void 0; const ElementsOfFormFactory_1 = require("../../Page/ElementsOfFormFactory"); class StructuralCodeListViewFactory { get any() { return this; } constructor(mainStateFactory, selfStructuralCode, onlyLastLevel, initialFilterValue) { this.mainStateFactory = mainStateFactory; this.selfStructuralCode = selfStructuralCode; this.onlyLastLevel = onlyLastLevel; this.forceUpdate = () => { }; this.selectedRowId = null; this.currentFilterWaitForLoad = ''; this.rows = []; this.code = ''; this.searchParameter = { currentPage: 1, itemsPerPage: 20, totalPages: 0 }; this.previousRow = () => { this.selectPreviousRow(); }; this.nextRow = () => { this.selectNextRow(); }; this.selectPreviousRow = () => { for (let index = 0; index < this.rows.length; index++) { const row = this.rows[index]; if (row.id === this.selectedRowId && index > 0) { this.selectThisRow(this.rows[index - 1]); return; } } if (this.rows && this.rows.length > 0) { this.selectThisRow(this.rows[this.rows.length - 1]); } }; this.selectNextRow = () => { for (let index = 0; index < this.rows.length; index++) { const row = this.rows[index]; if (row.id === this.selectedRowId && index < this.rows.length - 1) { this.selectThisRow(this.rows[index + 1]); return; } } if (this.rows && this.rows.length > 0) { this.selectThisRow(this.rows[0]); } }; this.previousPage = () => { this.selectByPageup(); }; this.nextPage = () => { this.selectByPagedown(); }; this.selectedRow = () => { return this.rows.find((i) => i.id > 0 && i.id === this.selectedRowId); }; this.currentRow = () => { return this.rows.find((i) => i.id > 0 && i.id === this.selectedRowId); }; this.setCurrentPage = (page) => { this.searchParameter.currentPage = page; this.code = ''; this.searchListView(); }; this.selectThisRow = (row) => { this.selectedRowId = row.id; this.forceUpdate(); }; this._selectCurrentRowByCode = () => { if (this.rows && this.rows.length) { if (this.code === '' && this.rows && this.rows.length) { this.selectedRowId = this.rows[0].id; } else { const findRow = this.rows.find((i) => i.code === this.code); if (findRow) { this.selectedRowId = findRow.id; } } } }; this.exportToExcel = () => { if (this.selfStructuralCode.urls.excel) { this.downloadFile(this.selfStructuralCode.urls.excel, this.searchParameter, this.afterExportToExcelSuccess); } }; this.afterExportToExcelSuccess = (_response) => { // UtilityFile.blobExcel(response.data); }; this.downloadFile = (path, searchParametr, afterSuccess) => { // this.pageData.Eventing.trigger('form.waitLoading'); // const header: AxiosRequestConfig = { responseType: 'arraybuffer', headers: { Authorization: `bearer ${this.mainStateManager.Usering.accessToken}` } }; this.selfStructuralCode.apiAxios .post(path, searchParametr) .then((response) => { afterSuccess(response); this.forceUpdate(); }) .catch((error) => { console.error(error); this.forceUpdate(); }); }; this.searchListView = () => { this.elementsOfForm.showWaitingFormSpinner(); this.forceUpdate(); const searchParameter = { ...this.elementsOfForm.searchParameter, ...this.searchParameter }; if (this.filter !== '') { // searchParameter.filter = this.filter; searchParameter.code = ''; // searchParameter.currentPage = 1; } else { searchParameter.code = this.code; // searchParameter.filter = ''; } this.selfStructuralCode.apiAxios.get(this.selfStructuralCode.urls.search, { params: searchParameter }) .then((response) => { this.searchParameter = { ...this.searchParameter, totalItems: response.data.searchParameter.totalItems, itemsPerPage: response.data.searchParameter.itemsPerPage, currentPage: response.data.searchParameter.currentPage, totalPages: response.data.searchParameter.totalPages, needTotalCount: response.data.searchParameter.needTotalCount, showAll: response.data.searchParameter.showAll, }; this.rows = response.data.rows; this._selectCurrentRowByCode(); this.elementsOfForm.closeWaitingFormSpinner(); this.forceUpdate(); }) .catch((error) => { this.elementsOfForm.closeWaitingFormSpinner(); this.rows = []; this.elementsOfForm.showErrorMessageBox(error); this.forceUpdate(); }); }; this.accept = () => { this.acceptCurrentRow(); }; this.acceptCurrentRow = () => { const currentRow = this.currentRow(); if (currentRow) { this.mainStateFactory.elementsOfForm.closeModal(); this.selfStructuralCode.focusToElement(); this.selfStructuralCode.onChangeHandlerCode(currentRow.code); this.selfStructuralCode.getCodeData(); } }; this.close = () => { this.mainStateFactory.elementsOfForm.closeModal(); this.selfStructuralCode.focusToElement(); }; this.cancelForm = () => { this.mainStateFactory.elementsOfForm.closeModal(); this.selfStructuralCode.focusToElement(); }; this.mainStateManager = this.mainStateFactory.mainStateManager; this.elementsOfForm = new ElementsOfFormFactory_1.ElementsOfFormFactory(this.mainStateFactory); this.code = this.selfStructuralCode.code; this.filter = initialFilterValue || ''; } selectByPageup() { const selectedRow = this.rows.find((i) => i.id === this.selectedRowId); if (selectedRow) { const selectedRowIndex = this.rows.indexOf(selectedRow); if (selectedRowIndex - 10 < 0) { this.selectThisRow(this.rows[0]); return; } else { this.selectThisRow(this.rows[selectedRowIndex - 10]); return; } } } selectByPagedown() { const selectedRow = this.rows.find((i) => i.id === this.selectedRowId); if (selectedRow) { const selectedRowIndex = this.rows.indexOf(selectedRow); if (selectedRowIndex + 10 <= this.rows.length - 1) { this.selectThisRow(this.rows[selectedRowIndex + 10]); return; } else if (this.rows.length + 1 > selectedRowIndex && this.rows.length - 1 < selectedRowIndex + 10) { this.selectThisRow(this.rows[this.rows.length - 1]); return; } } if (this.rows.length >= 10) { this.selectThisRow(this.rows[10]); return; } else if (this.rows.length > 0 && this.rows.length < 10) { this.selectThisRow(this.rows[this.rows.length]); return; } } selectFirst() { if (this.rows.length > 0) { this.selectThisRow(this.rows[0]); } } selectLast() { if (this.rows.length > 0) { this.selectThisRow(this.rows[this.rows.length - 1]); } } } exports.StructuralCodeListViewFactory = StructuralCodeListViewFactory;