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';
7 selector: 'app-scripts-tab',
8 templateUrl: './scripts-tab.component.html',
9 styleUrls: ['./scripts-tab.component.css']
11 export class ScriptsTabComponent implements OnInit {
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();
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;
30 public dropped(files: NgxFileDropEntry[]) {
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);
44 removeFile(fileIndex: number) {
45 console.log(this.uploadedFiles[fileIndex]);
46 this.packageCreationStore.removeFromState(this.uploadedFiles[fileIndex].name, 'scripts');
47 this.uploadedFiles.splice(fileIndex, 1);
50 public fileOver(event) {
54 public fileLeave(event) {
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(droppedFile.name,
65 this.packageCreationUtils.transformToJson(fileReader.result));
67 fileReader.readAsText(file);
73 resetTheUploadedFiles() {
74 this.uploadedFiles = [];