2653d739c879ad1279353afabbd234274e53d887
[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
28         this.packageCreationStore.state$.subscribe(cbaPackage => {
29             if (cbaPackage.scripts && cbaPackage.scripts.files && cbaPackage.scripts.files.size > 0) {
30                 this.scriptsFiles = cbaPackage.scripts.files;
31             }
32         });
33     }
34
35     public dropped(files: NgxFileDropEntry[]) {
36         this.files = files;
37         for (const droppedFile of files) {
38             // Is it a file & Not added before ?
39             if (droppedFile.fileEntry.isFile) {
40                 const fileEntry = droppedFile.fileEntry as FileSystemFileEntry;
41                 this.uploadedFiles.push(fileEntry);
42                 console.log(fileEntry.name);
43                 this.fileNames.add(fileEntry.name);
44
45             }
46         }
47     }
48
49     removeInitFile(index) {
50         this.uploadedFiles.splice(index, 1);
51     }
52
53     initDelete(file) {
54         this.fileToDelete = file;
55     }
56     removeFile(filePath: string, FileIndex: number) {
57         const filename = filePath.split('/')[2] || '';
58         //  const filename = 'Scripts/' + this.getFileType(this.uploadedFiles[fileIndex].name) + '/' + this.uploadedFiles[fileIndex].name;
59         this.packageCreationStore.removeFileFromState(filePath);
60         // remove from upload files array
61         // tslint:disable-next-line: prefer-for-of
62         for (let i = 0; i < this.uploadedFiles.length; i++) {
63             if (this.uploadedFiles[i].name === filename) {
64                 this.uploadedFiles.splice(i, 1);
65                 break;
66             }
67         }
68     }
69
70     public fileOver(event) {
71         console.log(event);
72     }
73
74     public fileLeave(event) {
75         console.log(event);
76     }
77
78
79     setFilesToStore() {
80         for (const droppedFile of this.uploadedFiles) {
81             droppedFile.file((file: File) => {
82                 const fileReader = new FileReader();
83                 fileReader.onload = (e) => {
84                     this.packageCreationStore.addScripts('Scripts/' + this.getFileType(droppedFile.name) + '/' + droppedFile.name,
85                         fileReader.result.toString());
86                 };
87                 fileReader.readAsText(file);
88             });
89
90         }
91     }
92
93     getFileType(filename: string): string {
94         let fileType = '';
95         const fileExtension = filename.substring(filename.lastIndexOf('.') + 1);
96         if (fileExtension === 'py') {
97             fileType = 'python';
98         } else if (fileExtension === 'kt') {
99             fileType = 'kotlin';
100         }
101         return fileType;
102     }
103
104     resetTheUploadedFiles() {
105         this.uploadedFiles = [];
106     }
107
108     textChanges(code: any, key: string) {
109         this.packageCreationStore.addScripts(key, code);
110     }
111
112     changeDivShow(mapIndex: number) {
113         const divElement = document.getElementById('id-script-' + mapIndex) as HTMLElement;
114         if (divElement.getAttribute('class').includes('show')) {
115             divElement.setAttribute('class', 'collapse');
116         } else {
117             divElement.setAttribute('class', 'collapse show');
118         }
119     }
120 }