Files
cms_thermo_active/Step/wwwroot/src/modules/base-components/modal-load-program.ts
T
Lucio Maranta fd9e08a0d2 WIP file editor
2019-08-20 16:40:15 +02:00

368 lines
9.6 KiB
TypeScript

import Vue from "vue";
import Component from "vue-class-component";
import { ModalHelper,Modal } from "src/components/modals";
import { Factory, messageService, awaiter } from "../../_base";
// import { MaintenanceService } from "../services/maintenanceService";
// import { store } from "src/store";
// import { maintenanceActions } from "../store/maintenance.store";
import moment from "moment";
import cardFolderPath from "./cards/card-folder-path.vue";
import cardElementQueue from "src/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;
}
interface FileInfo {
Name: string;
AbsolutePath: string;
CreationDate: string;
LastModDate: string;
Content: Array<any>;
PreviewBase64: any;
CanEdit: boolean;
IsJob: boolean;
}
@Component({
components: {
modal: Modal,
cardFolderPath,
cardElementQueue,
ZoomImage
}
})
export default class ModalLoadProgram extends Vue {
driveList: Array<PathInfo> = [];
currentPath: string = "";
currentDrive: string = null;
lastClickPath: string = "";
breadcrumbs: Array<any> = [];
firstColumnData: Array<PathInfo> = [];
secondColumnData: Array<PathInfo> = [];
isLocalNavigation: boolean = true;
selectedFile: FileInfo = null;
selectedFileMetadata = null;
currentFilterFirst: string = "";
currentFilterSecond: string = "";
navigationDepth: number = 0;
selectedNumberImage: number = 0;
showZoomedImage: boolean = false;
mustCLeanPProgram: boolean = false;
externalEditorOpened: boolean = false;
uploadAllFileInFolder: boolean = false;
onLabel: string = "On";
offLabel: string = "Off";
async mounted() {
if (typeof cmsClient != "undefined") {
this.driveList = JSON.parse(cmsClient.getOSdriveList());
}
}
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;
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 = "";
}
this.calcBreadCrumb(absolutePath);
}
fillArray(destinationArray: Array<any>, sourceArray: Array<any>) {
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);
}
}
get machineInfo() {
return this.$store.state.machineInfo;
}
async getFilesForPath(absolutePath: string, path: string, local: boolean): Promise<Array<any>> {
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<any>): Array<PathInfo> {
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 || i.IsJob
};
});
}
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"){
this.selectedFile = this.toUpperCaseFileModel(
JSON.parse(cmsClient.getProgramInfo(str))
);
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("\\"));
}
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() {
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 (item.IsJob)
this.loadJob(item.AbsolutePath);
else
this.loadProgram(item.AbsolutePath);
}
async loadProgram(path: string) {
if (this.isLocalNavigation && typeof cmsClient != "undefined") {
var resp = cmsClient.uploadAndActivateProgram(path,this.uploadAllFileInFolder);
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);
// var result = await ModalHelper.ShowModalAsync(, jobMetadata);
}
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.endEditor);
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.externalEditorOpened = true;
}
}
endEditor(){
this.externalEditorOpened = false;
}
}