634e5fc84b3fb052c661f908a1c651847cb7f4e6
[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
6 @Component({
7     selector: 'app-scripts-tab',
8     templateUrl: './scripts-tab.component.html',
9     styleUrls: ['./scripts-tab.component.css']
10 })
11 export class ScriptsTabComponent implements OnInit {
12
13     public scriptsFiles: Map<string, string> = new Map<string, string>();
14     public uploadedFiles: FileSystemFileEntry[] = [];
15     public files: NgxFileDropEntry[] = [];
16     private fileNames: Set<string> = new Set();
17
18     constructor(private packageCreationStore: PackageCreationStore, private packageCreationUtils: PackageCreationUtils) {
19         this.packageCreationStore.state$.subscribe(cbaPackage => {
20             if (cbaPackage.scripts && cbaPackage.scripts.files && cbaPackage.scripts.files.size > 0) {
21                 this.scriptsFiles = cbaPackage.scripts.files;
22             }
23         });
24     }
25
26
27     ngOnInit() {
28     }
29
30     public dropped(files: NgxFileDropEntry[]) {
31         this.files = files;
32         for (const droppedFile of files) {
33             // Is it a file & Not added before ?
34             if (droppedFile.fileEntry.isFile && !this.fileNames.has(droppedFile.fileEntry.name)) {
35                 const fileEntry = droppedFile.fileEntry as FileSystemFileEntry;
36                 this.uploadedFiles.push(fileEntry);
37                 console.log(fileEntry.name);
38                 this.fileNames.add(fileEntry.name);
39
40             }
41         }
42     }
43
44     removeFile(fileIndex: number) {
45         console.log(this.uploadedFiles[fileIndex]);
46         this.packageCreationStore.removeFileFromState(this.uploadedFiles[fileIndex].name);
47         this.uploadedFiles.splice(fileIndex, 1);
48     }
49
50     public fileOver(event) {
51         console.log(event);
52     }
53
54     public fileLeave(event) {
55         console.log(event);
56     }
57
58
59     setFilesToStore() {
60         for (const droppedFile of this.uploadedFiles) {
61             droppedFile.file((file: File) => {
62                 const fileReader = new FileReader();
63                 fileReader.onload = (e) => {
64                     this.packageCreationStore.addScripts('Scripts/' + droppedFile.name,
65                         fileReader.result.toString());
66                 };
67                 fileReader.readAsText(file);
68             });
69
70         }
71     }
72
73     resetTheUploadedFiles() {
74         this.uploadedFiles = [];
75     }
76 }