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 import {ToastrService} from 'ngx-toastr';
11 selector: 'app-scripts-tab',
12 templateUrl: './scripts-tab.component.html',
13 styleUrls: ['./scripts-tab.component.css']
15 export class ScriptsTabComponent implements OnInit {
17 public scriptsFiles: Map<string, string> = new Map<string, string>();
18 public uploadedFiles: FileSystemFileEntry[] = [];
19 public files: NgxFileDropEntry[] = [];
20 private fileNames: Set<string> = new Set();
21 fileToDelete: any = {};
24 private packageCreationStore: PackageCreationStore,
25 private toastService: ToastrService
32 this.packageCreationStore.state$.subscribe(cbaPackage => {
33 if (cbaPackage.scripts && cbaPackage.scripts.files && cbaPackage.scripts.files.size > 0) {
34 this.scriptsFiles = cbaPackage.scripts.files;
39 public dropped(files: NgxFileDropEntry[]) {
41 for (const droppedFile of files) {
42 // Is it a file & Not added before ?
43 if (droppedFile.fileEntry.isFile) {
44 const fileEntry = droppedFile.fileEntry as FileSystemFileEntry;
45 this.uploadedFiles.push(fileEntry);
46 console.log(fileEntry.name);
47 this.fileNames.add(fileEntry.name);
53 removeInitFile(index) {
54 this.uploadedFiles.splice(index, 1);
58 this.fileToDelete = file;
61 removeFile(filePath: string, FileIndex: number) {
62 const filename = filePath.split('/')[2] || '';
63 // const filename = 'Scripts/' + this.getFileType(this.uploadedFiles[fileIndex].name) + '/' + this.uploadedFiles[fileIndex].name;
64 this.packageCreationStore.removeFileFromState(filePath);
65 // remove from upload files array
66 // tslint:disable-next-line: prefer-for-of
67 for (let i = 0; i < this.uploadedFiles.length; i++) {
68 if (this.uploadedFiles[i].name === filename) {
69 this.uploadedFiles.splice(i, 1);
75 public fileOver(event) {
79 public fileLeave(event) {
85 for (const droppedFile of this.uploadedFiles) {
86 droppedFile.file((file: File) => {
87 const fileReader = new FileReader();
88 fileReader.onload = (e) => {
89 this.packageCreationStore.addScripts('Scripts/' + this.getFileType(droppedFile.name) + '/' + droppedFile.name,
90 fileReader.result.toString());
92 fileReader.readAsText(file);
98 getFileType(filename: string): string {
100 const fileExtension = filename.substring(filename.lastIndexOf('.') + 1);
101 if (fileExtension === 'py') {
103 } else if (fileExtension === 'kt') {
109 resetTheUploadedFiles() {
110 this.uploadedFiles = [];
113 textChanges(code: any, key: string) {
114 this.packageCreationStore.addScripts(key, code);
115 this.toastService.success(key + ' is updated successfully');
118 changeDivShow(mapIndex: number) {
119 const divElement = document.getElementById('id-script-' + mapIndex) as HTMLElement;
120 if (divElement.getAttribute('class').includes('show')) {
121 divElement.setAttribute('class', 'collapse');
123 divElement.setAttribute('class', 'collapse show');