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';
9 selector: 'app-scripts-tab',
10 templateUrl: './scripts-tab.component.html',
11 styleUrls: ['./scripts-tab.component.css']
13 export class ScriptsTabComponent implements OnInit {
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 fileToDelete: any = {};
22 private packageCreationStore: PackageCreationStore,
27 this.packageCreationStore.state$.subscribe(cbaPackage => {
28 if (cbaPackage.scripts && cbaPackage.scripts.files && cbaPackage.scripts.files.size > 0) {
29 this.scriptsFiles = cbaPackage.scripts.files;
34 public dropped(files: NgxFileDropEntry[]) {
36 for (const droppedFile of files) {
37 // Is it a file & Not added before ?
38 if (droppedFile.fileEntry.isFile) {
39 const fileEntry = droppedFile.fileEntry as FileSystemFileEntry;
40 this.uploadedFiles.push(fileEntry);
41 console.log(fileEntry.name);
42 this.fileNames.add(fileEntry.name);
48 this.fileToDelete = file;
50 removeFile(filePath: string, FileIndex: number) {
51 const filename = filePath.split('/')[2] || '';
52 // const filename = 'Scripts/' + this.getFileType(this.uploadedFiles[fileIndex].name) + '/' + this.uploadedFiles[fileIndex].name;
53 this.packageCreationStore.removeFileFromState(filePath);
54 // remove from upload files array
55 // tslint:disable-next-line: prefer-for-of
56 for (let i = 0; i < this.uploadedFiles.length; i++) {
57 if (this.uploadedFiles[i].name === filename) {
58 this.uploadedFiles.splice(i, 1);
64 public fileOver(event) {
68 public fileLeave(event) {
74 for (const droppedFile of this.uploadedFiles) {
75 droppedFile.file((file: File) => {
76 const fileReader = new FileReader();
77 fileReader.onload = (e) => {
78 this.packageCreationStore.addScripts('Scripts/' + this.getFileType(droppedFile.name) + '/' + droppedFile.name,
79 fileReader.result.toString());
81 fileReader.readAsText(file);
87 getFileType(filename: string): string {
89 const fileExtension = filename.substring(filename.lastIndexOf('.') + 1);
90 if (fileExtension === 'py') {
92 } else if (fileExtension === 'kt') {
98 resetTheUploadedFiles() {
99 this.uploadedFiles = [];
102 textChanges(code: any, key: string) {
103 this.packageCreationStore.addScripts(key, code);
106 changeDivShow(mapIndex: number) {
107 const divElement = document.getElementById('id-script-' + mapIndex) as HTMLElement;
108 if (divElement.getAttribute('class').includes('show')) {
109 divElement.setAttribute('class', 'collapse');
111 divElement.setAttribute('class', 'collapse show');