c3704365cc8a92c11e7848924d851634eb38abf3
[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) {
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
48     removeInitFile(index) {
49         this.uploadedFiles.splice(index, 1);
50     }
51
52     initDelete(file) {
53         this.fileToDelete = file;
54     }
55     removeFile(filePath: string, FileIndex: number) {
56         const filename = filePath.split('/')[2] || '';
57         //  const filename = 'Scripts/' + this.getFileType(this.uploadedFiles[fileIndex].name) + '/' + this.uploadedFiles[fileIndex].name;
58         this.packageCreationStore.removeFileFromState(filePath);
59         // remove from upload files array
60         // tslint:disable-next-line: prefer-for-of
61         for (let i = 0; i < this.uploadedFiles.length; i++) {
62             if (this.uploadedFiles[i].name === filename) {
63                 this.uploadedFiles.splice(i, 1);
64                 break;
65             }
66         }
67     }
68
69     public fileOver(event) {
70         console.log(event);
71     }
72
73     public fileLeave(event) {
74         console.log(event);
75     }
76
77
78     setFilesToStore() {
79         for (const droppedFile of this.uploadedFiles) {
80             droppedFile.file((file: File) => {
81                 const fileReader = new FileReader();
82                 fileReader.onload = (e) => {
83                     this.packageCreationStore.addScripts('Scripts/' + this.getFileType(droppedFile.name) + '/' + droppedFile.name,
84                         fileReader.result.toString());
85                 };
86                 fileReader.readAsText(file);
87             });
88
89         }
90     }
91
92     getFileType(filename: string): string {
93         let fileType = '';
94         const fileExtension = filename.substring(filename.lastIndexOf('.') + 1);
95         if (fileExtension === 'py') {
96             fileType = 'python';
97         } else if (fileExtension === 'kt') {
98             fileType = 'kotlin';
99         }
100         return fileType;
101     }
102
103     resetTheUploadedFiles() {
104         this.uploadedFiles = [];
105     }
106
107     textChanges(code: any, key: string) {
108         this.packageCreationStore.addScripts(key, code);
109     }
110
111     changeDivShow(mapIndex: number) {
112         const divElement = document.getElementById('id-script-' + mapIndex) as HTMLElement;
113         if (divElement.getAttribute('class').includes('show')) {
114             divElement.setAttribute('class', 'collapse');
115         } else {
116             divElement.setAttribute('class', 'collapse show');
117         }
118     }
119 }