c70109fc5c1fe0c04925e9981f31f911ee9042fe
[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 {PackageCreationUtils} from '../package-creation.utils';
5 import 'ace-builds/src-noconflict/ace';
6 import 'ace-builds/webpack-resolver';
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
20     constructor(private packageCreationStore: PackageCreationStore, private packageCreationUtils: PackageCreationUtils) {
21         this.packageCreationStore.state$.subscribe(cbaPackage => {
22             if (cbaPackage.scripts && cbaPackage.scripts.files && cbaPackage.scripts.files.size > 0) {
23                 this.scriptsFiles = cbaPackage.scripts.files;
24             }
25         });
26     }
27
28
29     ngOnInit() {
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(fileIndex: number) {
47         console.log(this.uploadedFiles[fileIndex]);
48         const filename = 'Scripts/' + this.uploadedFiles[fileIndex].name;
49         this.packageCreationStore.removeFileFromState(filename);
50         this.uploadedFiles.splice(fileIndex, 1);
51     }
52
53     public fileOver(event) {
54         console.log(event);
55     }
56
57     public fileLeave(event) {
58         console.log(event);
59     }
60
61
62     setFilesToStore() {
63         for (const droppedFile of this.uploadedFiles) {
64             droppedFile.file((file: File) => {
65                 const fileReader = new FileReader();
66                 fileReader.onload = (e) => {
67                     this.packageCreationStore.addScripts('Scripts/' + droppedFile.name,
68                         fileReader.result.toString());
69                 };
70                 fileReader.readAsText(file);
71             });
72
73         }
74     }
75
76     resetTheUploadedFiles() {
77         this.uploadedFiles = [];
78     }
79
80     textChanges(code: any, key: string) {
81         this.packageCreationStore.addScripts(key, code);
82     }
83 }