import Vue from "vue"; import Component from "vue-class-component"; import { ModalHelper,Modal } from "@/components/modals"; import { Factory, messageService, awaiter } from "../../_base"; // import { MaintenanceService } from "../services/maintenanceService"; // import { store } from "@/store"; // import { maintenanceActions } from "../store/maintenance.store"; import moment from "moment"; import cardFolderPath from "./cards/card-folder-path.vue"; import cardElementQueue from "@/app_modules/production/components/card-element-queue.vue"; import * as iziToast from "izitoast"; import { fileService } from "../../services/fileService"; import ZoomImage from './zoom-image.vue' declare var cmsClient: any; interface PathInfo { Name: string; AbsolutePath: string; Path: string; IsDirectory: boolean; IsMain: boolean; FileExist: boolean; } interface FileInfo { Name: string; AbsolutePath: string; CreationDate: string; LastModDate: string; Content: Array; PreviewBase64: any; CanEdit: boolean; IsJob: boolean; } @Component({ components: { modal: Modal, cardFolderPath, cardElementQueue, ZoomImage } }) export default class ModalLoadProgram extends Vue { driveList: Array = []; currentPath: string = ""; currentDrive: string = null; lastClickPath: string = ""; breadcrumbs: Array = []; firstColumnData: Array = []; secondColumnData: Array = []; isLocalNavigation: boolean = true; selectedFile: FileInfo = null; selectedFileMetadata = null; currentFilterFirst: string = ""; currentFilterSecond: string = ""; navigationDepth: number = 0; selectedNumberImage: number = 0; showZoomedImage: boolean = false; mustCLeanPProgram: boolean = false; fileIsChanged: boolean = false; uploadAllFileInFolder: boolean = false; onLabel: string = "On"; offLabel: string = "Off"; recentPath: string = "" thereAreFileEliminated: boolean = false askConfirmLayer: boolean = false loadClicked: boolean = false recentDriveSelected: boolean = false get machineInfo() { return this.$store.state.machineInfo; } get canLoadProgram() { return this.$store.state.process.canLoadProgram; } async mounted() { if (typeof cmsClient != "undefined") { this.driveList = JSON.parse(cmsClient.getOSdriveList()); this.recentPath = cmsClient.RECENT_FOLDER_KEY } this.loadClicked = false } async navigateTo(path: string, absolutePath: string, local: boolean, todepth: number, fromdepth: number) { this.currentPath = absolutePath; this.lastClickPath = absolutePath; this.checkChangeNavigationType(local); this.selectedFile = null; this.selectedFileMetadata = null; var files = await this.getFilesForPath(absolutePath, path, local); // controlla se impostare il currentDrive if (fromdepth == 0) this.currentDrive = absolutePath; // controlla come organizzare le colonne. if (this.navigationDepth == 2 && todepth == 2 && fromdepth == 2) { this.fillArray(this.firstColumnData, this.secondColumnData); } this.navigationDepth = todepth; // Check destination depth if (todepth == 1) { this.fillArray(this.firstColumnData, files); this.secondColumnData.splice(0, this.secondColumnData.length); this.currentFilterFirst = ""; } if (todepth == 2) { this.fillArray(this.secondColumnData, files); this.currentFilterSecond = ""; } // Recent column if(path == this.recentPath) { var main = null; // Parse files files.forEach((element) => { // Check if there is a deleted file if(element.FileExist == false) this.thereAreFileEliminated = true // Return only the main program if(element.IsMain == true) main = element }) // If there is a main program if(main != null) { this.fileInfo(main.AbsolutePath, false, 1) this.uploadAllFileInFolder = true; this.recentDriveSelected = true; } } else { this.uploadAllFileInFolder = false; this.thereAreFileEliminated = false; this.recentDriveSelected = false; } this.calcBreadCrumb(absolutePath); this.cleanFileWatcher(); } fillArray(destinationArray: Array, sourceArray: Array) { destinationArray.splice(0, destinationArray.length); for (const key in sourceArray) { if (sourceArray.hasOwnProperty(key)) { const element = sourceArray[key]; destinationArray.push(element); } } } checkChangeNavigationType(local: boolean) { if (this.isLocalNavigation != local) { this.navigationDepth = 0; this.firstColumnData.splice(0, this.firstColumnData.length); this.secondColumnData.splice(0, this.secondColumnData.length); } } async getFilesForPath(absolutePath: string, path: string, local: boolean): Promise> { this.isLocalNavigation = local; var result = null; if (local) result = JSON.parse(cmsClient.getFileList(absolutePath)); else result = await awaiter(fileService.getFiles(path)); return this.toUpperCaseModel(result).sort(this.compareDirectoriesFirst); } private compareDirectoriesFirst(x, y) { return (x.IsDirectory === y.IsDirectory) ? 0 : x.IsDirectory ? -1 : 1; } toUpperCaseModel(data: Array): Array { return data.map(i => { return { Name: i.name || i.Name, AbsolutePath: i.absolutePath || i.AbsolutePath, Path: i.path || i.Path, IsDirectory: (i.isDirectory != null && i.isDirectory) || (i.IsDirectory != null && i.IsDirectory), IsJob: i.isJob, IsMain: i.IsMain, FileExist: i.FileExist }; }); } calcBreadCrumb(path: string) { this.breadcrumbs.splice(0, this.breadcrumbs.length); if (path.startsWith("\\\\")) { this.breadcrumbs.push({ name: "CN", path: "\\\\" }); } if (path) { var fragments = path.split("\\"); var __p = ""; fragments.forEach(element => { __p = __p + element + "\\"; this.breadcrumbs.push({ name: element, path: __p }); }); } } isInPath(path,isDirectory) { if (this.currentPath) { if(!this.uploadAllFileInFolder || !this.isLocalNavigation) return path == this.currentPath; else return (path.startsWith(this.currentPath) && !isDirectory) || (path == this.currentPath && isDirectory); } return false; } toUpperCaseFileModel(data: any): FileInfo { return { AbsolutePath: data.absolutePath || data.AbsolutePath, Path: data.path || data.Path, Content: data.content || data.Content, CreationDate: data.creationDate || data.CreationDate, LastModDate: data.lastModDate || data.LastModDate, Name: data.name || data.Name, PreviewBase64: data.previewBase64 || data.PreviewBase64, IsJob: data.isJob || data.IsJob, CanEdit: data.canEdit || data.CanEdit } as FileInfo; } async fileInfo(str, isJob, fromdepth: number) { this.lastClickPath = str; if (fromdepth == 1) this.secondColumnData.splice(0, this.secondColumnData.length); if (this.isLocalNavigation && typeof cmsClient != "undefined"){ // Select file this.selectedFile = this.toUpperCaseFileModel( JSON.parse(cmsClient.getProgramInfo(str)) ); // Check if the file it's a job and get data this.selectedFile.IsJob = isJob; if(isJob){ var resp = JSON.parse(cmsClient.readJobMetadata(str)); if(resp.error) { this.selectedFile = null; (iziToast as any).error({ title: "error", message: resp.error, theme: "dark", timeout: 10000, class: "t-error", transitionOut: "fadeOut", }) return } this.selectedFileMetadata = resp.metadata; this.selectedNumberImage = 0; } } if (!this.isLocalNavigation) this.selectedFile = this.toUpperCaseFileModel( await awaiter(fileService.getFileInfo(str))); this.currentPath = this.selectedFile.AbsolutePath.substring(0, this.selectedFile.AbsolutePath.lastIndexOf("\\")); this.cleanFileWatcher(); } beforeMount() { messageService.subscribeToChannel("esc_pressed", args => { this.close(); }); if(!this.machineInfo.isOsai) { awaiter (fileService.isTempFolderOK().then(response => { this.mustCLeanPProgram = !response; })); } } beforeDestroy() { messageService.deleteChannel("esc_pressed"); } close() { this.cleanFileWatcher() messageService.deleteChannel("esc_pressed"); ModalHelper.HideModal(); } reload() { this.driveList = JSON.parse(cmsClient.getOSdriveList()); } getDate(date) { return moment(date).format("L"); } async load(item: FileInfo) { if(!this.thereAreFileEliminated || this.askConfirmLayer) { this.loadClicked = true if (item.IsJob) this.loadJob(item.AbsolutePath); else this.loadProgram(item.AbsolutePath); this.askConfirmLayer = false } else { this.askConfirmLayer = true } this.loadClicked = false } async loadProgram (path: string) { if (this.isLocalNavigation && typeof cmsClient != "undefined") { var resp = cmsClient.uploadAndActivateProgram(path, this.uploadAllFileInFolder, this.recentDriveSelected); if (resp != "") (iziToast as any).error({ title: resp.split(";")[0], message: resp.split(";")[1], theme: "dark", timeout: 10000, class: "t-error", transitionOut: "fadeOut", }) else this.close(); } if (!this.isLocalNavigation) { await awaiter(fileService.activateProgram(path)); this.close(); } } selectNumberImage(value) { this.selectedNumberImage = value; } async loadJob(path: string) { var jobMetadata = cmsClient.readJobMetadata(path); } ToggleShowZoomedImage() { this.showZoomedImage = !this.showZoomedImage; } toggleUploadAllFileInFolder() { this.uploadAllFileInFolder = !this.uploadAllFileInFolder; } async cleanTempfolder() { await awaiter(fileService.cleanTempFolder()); await awaiter (fileService.isTempFolderOK().then(response => { this.mustCLeanPProgram = !response; })); } async backupTempfolder() { await awaiter(fileService.backupTempFolder()); await awaiter (fileService.isTempFolderOK().then(response => { this.mustCLeanPProgram = !response; })); } editprogram(){ var res = cmsClient.editProgram(this.selectedFile.AbsolutePath, this.fileChanged); if (res) { var obj = JSON.parse(res); if (obj.error){ (iziToast as any).error({ title: "error", message: obj.error, theme: "dark", timeout: 10000, class: "t-error", transitionOut: "fadeOut", }) } } else { this.fileIsChanged = false; } } fileChanged() { this.fileIsChanged = true; } reloadProgram() { this.fileInfo(this.selectedFile.AbsolutePath, this.selectedFile.IsJob, this.navigationDepth) this.fileIsChanged = false; } cleanFileWatcher() { this.fileIsChanged = false; cmsClient.cleanFileWatcher() } askConfirmOkClick(){ this.load(this.selectedFile) } askConfirmCancelClick(){ this.askConfirmLayer = false; } }