import * as widgets from "oasi-widgets"; import { SortingType } from "../linkedDocuments/search-result-sorting"; import * as _ from "lodash"; import { IZepdFile, ZeptRestService } from 'isogd-file-rest'; import { DocumentIsogd } from "isogd-document-rest"; class LogFilesController { static $inject = ['$state', '$timeout', 'zeptRestService', '$filter', 'blockUI', '$uibModal', 'toastr']; private document: DocumentIsogd; private myBlockUI: any; private files: IZepdFile[]; private loadingStatus: widgets.LoadingStatus = widgets.LoadingStatus.LOADING; private status: boolean; private loadedFilesNumber: number; private allFilesNumber: number; private summLoadedFilesSize: number; private summAllFilesSize: number; private sortings: SortingType[]; private firstTime: boolean; private editor: boolean; constructor(private $state: ng.ui.IStateService, private $timeout: ng.ITimeoutService, private zeptService: ZeptRestService, private $filter: ng.IFilterService, private blockUI: any, private $uibModal: angular.ui.bootstrap.IModalService, private toastr: Toastr) { } $onInit() { this.sortings = [ new SortingType('lfileName', 'none', ''), new SortingType('lfileSize', 'none', ''), new SortingType('lfolderName', 'asc', ''), new SortingType('ldateOper', 'none', ''), new SortingType('lstatus', 'none', '') ]; this.myBlockUI = this.blockUI.instances.get('logFiles'); this.zeptService.get(this.document.documentID).then(response => { this.files = response; this.calculate(); this.loadingStatus = widgets.LoadingStatus.SUCCESS; }).catch(error => { this.loadingStatus = widgets.LoadingStatus.ERROR; }); } private orderBy(files: any, sortings: SortingType[]): any[] { let result: any[] = files; sortings.slice().reverse().forEach(sorting => { switch (sorting.value) { case 'desc': result = this.$filter('orderBy')(result, sorting.name, true); break; case 'asc': result = this.$filter('orderBy')(result, sorting.name, false); break; default: break; } }); return result; } change(sorting: SortingType) { this.update(sorting.name); this.files = this.orderBy(this.files, this.sortings); } private update(columnName: string) { this.sortings.forEach(v => { if (v.name === columnName) { switch (v.value) { case 'desc': v.value = 'asc'; break; default: v.value = 'desc'; break; } } else { v.value = 'none'; } }); } private calculate(): void { this.firstTime = !_.every(this.files, f => { return f.status === "SUCCESS" }); this.files.forEach(f => { (f).lfileName = f.fileName; (f).lstatus = f.status; (f).lfileSize = f.fileSize; (f).lfolderName = (f).folderName; (f).ldateOper = (f).dateOper; (f).lfileId = f.id; (f).lfolderId = f.guidFolder; (f).lurlPart = f.urlPart; (f).lattempt = f.attempt; }); this.files = this.orderBy(this.files, this.sortings); let result: boolean = true; this.loadedFilesNumber = 0; this.allFilesNumber = this.files.length; this.summLoadedFilesSize = 0; this.summAllFilesSize = 0; this.files.forEach(f => { this.summAllFilesSize += (f.fileSize === null) ? 0 : parseInt(f.fileSize); if (f.status !== "SUCCESS") { result = false; } else { this.loadedFilesNumber++; this.summLoadedFilesSize += (f.fileSize === null) ? 0 : parseInt(f.fileSize); } }); this.summLoadedFilesSize = this.summLoadedFilesSize / (1024 * 1024); this.summAllFilesSize = this.summAllFilesSize / (1024 * 1024); this.status = result; } $onDestroy() { this.myBlockUI.reset(); } showHistory(file: IZepdFile) { let modalInstance = this.$uibModal.open({ template: require('./logFileDlg.html'), controller: 'LogFileGldController as ctrl', size: "lg", backdrop: 'static', keyboard: false, resolve: { logs: () => { return file.zepdLogs } } }); modalInstance.result.then((result: any) => { }).catch(error => { }); } upload() { this.myBlockUI.start(); this.zeptService.post(this.document.documentID).then(response => { this.zeptService.get(this.document.documentID).then(response => { this.files = response; this.calculate(); this.myBlockUI.stop(); }).catch(error => { this.myBlockUI.stop(); this.toastr.error("Ошибка при дозагрузке файлов!"); }); }).catch(error => { this.myBlockUI.stop(); this.toastr.error("Ошибка при дозагрузке файлов!"); }); } } export const LogFilesComponent: angular.IComponentOptions = { controller: LogFilesController, template: require('./logFiles.html'), bindings: { document: '<', editor: '<' } };