36cccc88c057450eb0411233d4ae48daf933eddb
[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
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         /* this.packageStore.state$.subscribe(res => {
34              //  this.scriptsFiles =
35              console.log('from scripts');
36              console.log(res.scripts);
37              this.scriptsFiles = res.scripts.files;
38          });*/
39     }
40
41     public dropped(files: NgxFileDropEntry[]) {
42         this.files = files;
43         for (const droppedFile of files) {
44             // Is it a file & Not added before ?
45             if (droppedFile.fileEntry.isFile && !this.fileNames.has(droppedFile.fileEntry.name)) {
46                 const fileEntry = droppedFile.fileEntry as FileSystemFileEntry;
47                 this.uploadedFiles.push(fileEntry);
48                 console.log(fileEntry.name);
49                 this.fileNames.add(fileEntry.name);
50
51             }
52         }
53     }
54
55     removeFile(fileIndex: number) {
56         console.log(this.uploadedFiles[fileIndex]);
57         const filename = 'Scripts/' + this.uploadedFiles[fileIndex].name;
58         this.packageCreationStore.removeFileFromState(filename);
59         this.uploadedFiles.splice(fileIndex, 1);
60     }
61
62     public fileOver(event) {
63         console.log(event);
64     }
65
66     public fileLeave(event) {
67         console.log(event);
68     }
69
70
71     setFilesToStore() {
72         for (const droppedFile of this.uploadedFiles) {
73             droppedFile.file((file: File) => {
74                 const fileReader = new FileReader();
75                 fileReader.onload = (e) => {
76                     this.packageCreationStore.addScripts('Scripts/' + droppedFile.name,
77                         fileReader.result.toString());
78                 };
79                 fileReader.readAsText(file);
80             });
81
82         }
83     }
84
85     resetTheUploadedFiles() {
86         this.uploadedFiles = [];
87     }
88
89     textChanges(code: any, key: string) {
90         this.packageCreationStore.addScripts(key, code);
91     }
92 }