a85ccf146f393a704921f612d34ead36b1e94b1f
[ccsdk/cds.git] /
1 import { Component, OnInit } from '@angular/core';
2 import { FileSystemFileEntry, NgxFileDropEntry } from 'ngx-file-drop';
3 import { PackageCreationStore } from '../package-creation.store';
4 import 'ace-builds/src-noconflict/ace';
5 import 'ace-builds/webpack-resolver';
6 declare var $: any;
7
8 @Component({
9     selector: 'app-scripts-tab',
10     templateUrl: './scripts-tab.component.html',
11     styleUrls: ['./scripts-tab.component.css']
12 })
13 export class ScriptsTabComponent implements OnInit {
14
15     public scriptsFiles: Map<string, string> = new Map<string, string>();
16     public uploadedFiles: FileSystemFileEntry[] = [];
17     public files: NgxFileDropEntry[] = [];
18     private fileNames: Set<string> = new Set();
19     fileToDelete: any = {};
20
21     constructor(
22         private packageCreationStore: PackageCreationStore,
23     ) { }
24
25
26     ngOnInit() {
27         this.packageCreationStore.state$.subscribe(cbaPackage => {
28             if (cbaPackage.scripts && cbaPackage.scripts.files && cbaPackage.scripts.files.size > 0) {
29                 this.scriptsFiles = cbaPackage.scripts.files;
30             }
31         });
32     }
33
34     public dropped(files: NgxFileDropEntry[]) {
35         this.files = files;
36         for (const droppedFile of files) {
37             // Is it a file & Not added before ?
38             if (droppedFile.fileEntry.isFile && !this.fileNames.has(droppedFile.fileEntry.name)) {
39                 const fileEntry = droppedFile.fileEntry as FileSystemFileEntry;
40                 this.uploadedFiles.push(fileEntry);
41                 console.log(fileEntry.name);
42                 this.fileNames.add(fileEntry.name);
43
44             }
45         }
46     }
47     initDelete(file) {
48         this.fileToDelete = file;
49     }
50     removeFile(filePath: string, FileIndex: number) {
51         const filename = filePath.split('/')[2] || '';
52         //  const filename = 'Scripts/' + this.getFileType(this.uploadedFiles[fileIndex].name) + '/' + this.uploadedFiles[fileIndex].name;
53         this.packageCreationStore.removeFileFromState(filePath);
54         // remove from upload files array
55         // tslint:disable-next-line: prefer-for-of
56         for (let i = 0; i < this.uploadedFiles.length; i++) {
57             if (this.uploadedFiles[i].name === filename) {
58                 this.uploadedFiles.splice(i, 1);
59                 break;
60             }
61         }
62     }
63
64     public fileOver(event) {
65         console.log(event);
66     }
67
68     public fileLeave(event) {
69         console.log(event);
70     }
71
72
73     setFilesToStore() {
74         for (const droppedFile of this.uploadedFiles) {
75             droppedFile.file((file: File) => {
76                 const fileReader = new FileReader();
77                 fileReader.onload = (e) => {
78                     this.packageCreationStore.addScripts('Scripts/' + this.getFileType(droppedFile.name) + '/' + droppedFile.name,
79                         fileReader.result.toString());
80                 };
81                 fileReader.readAsText(file);
82             });
83
84         }
85     }
86
87     getFileType(filename: string): string {
88         let fileType = '';
89         const fileExtension = filename.substring(filename.lastIndexOf('.') + 1);
90         if (fileExtension === 'py') {
91             fileType = 'python';
92         } else if (fileExtension === 'kt') {
93             fileType = 'kotlin';
94         }
95         return fileType;
96     }
97
98     resetTheUploadedFiles() {
99         this.uploadedFiles = [];
100     }
101
102     textChanges(code: any, key: string) {
103         this.packageCreationStore.addScripts(key, code);
104     }
105
106     changeDivShow(mapIndex: number) {
107         const divElement = document.getElementById('id-script-' + mapIndex) as HTMLElement;
108         if (divElement.getAttribute('class').includes('show')) {
109             divElement.setAttribute('class', 'collapse');
110         } else {
111             divElement.setAttribute('class', 'collapse show');
112         }
113     }
114 }