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