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,
28 this.packageCreationStore.state$.subscribe(cbaPackage => {
29 if (cbaPackage.scripts && cbaPackage.scripts.files && cbaPackage.scripts.files.size > 0) {
30 this.scriptsFiles = cbaPackage.scripts.files;
35 public dropped(files: NgxFileDropEntry[]) {
37 for (const droppedFile of files) {
38 // Is it a file & Not added before ?
39 if (droppedFile.fileEntry.isFile) {
40 const fileEntry = droppedFile.fileEntry as FileSystemFileEntry;
41 this.uploadedFiles.push(fileEntry);
42 console.log(fileEntry.name);
43 this.fileNames.add(fileEntry.name);
49 removeInitFile(index) {
50 this.uploadedFiles.splice(index, 1);
54 this.fileToDelete = file;
56 removeFile(filePath: string, FileIndex: number) {
57 const filename = filePath.split('/')[2] || '';
58 // const filename = 'Scripts/' + this.getFileType(this.uploadedFiles[fileIndex].name) + '/' + this.uploadedFiles[fileIndex].name;
59 this.packageCreationStore.removeFileFromState(filePath);
60 // remove from upload files array
61 // tslint:disable-next-line: prefer-for-of
62 for (let i = 0; i < this.uploadedFiles.length; i++) {
63 if (this.uploadedFiles[i].name === filename) {
64 this.uploadedFiles.splice(i, 1);
70 public fileOver(event) {
74 public fileLeave(event) {
80 for (const droppedFile of this.uploadedFiles) {
81 droppedFile.file((file: File) => {
82 const fileReader = new FileReader();
83 fileReader.onload = (e) => {
84 this.packageCreationStore.addScripts('Scripts/' + this.getFileType(droppedFile.name) + '/' + droppedFile.name,
85 fileReader.result.toString());
87 fileReader.readAsText(file);
93 getFileType(filename: string): string {
95 const fileExtension = filename.substring(filename.lastIndexOf('.') + 1);
96 if (fileExtension === 'py') {
98 } else if (fileExtension === 'kt') {
104 resetTheUploadedFiles() {
105 this.uploadedFiles = [];
108 textChanges(code: any, key: string) {
109 this.packageCreationStore.addScripts(key, code);
112 changeDivShow(mapIndex: number) {
113 const divElement = document.getElementById('id-script-' + mapIndex) as HTMLElement;
114 if (divElement.getAttribute('class').includes('show')) {
115 divElement.setAttribute('class', 'collapse');
117 divElement.setAttribute('class', 'collapse show');