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 removeInitFile(index) {
49 this.uploadedFiles.splice(index, 1);
53 this.fileToDelete = file;
55 removeFile(filePath: string, FileIndex: number) {
56 const filename = filePath.split('/')[2] || '';
57 // const filename = 'Scripts/' + this.getFileType(this.uploadedFiles[fileIndex].name) + '/' + this.uploadedFiles[fileIndex].name;
58 this.packageCreationStore.removeFileFromState(filePath);
59 // remove from upload files array
60 // tslint:disable-next-line: prefer-for-of
61 for (let i = 0; i < this.uploadedFiles.length; i++) {
62 if (this.uploadedFiles[i].name === filename) {
63 this.uploadedFiles.splice(i, 1);
69 public fileOver(event) {
73 public fileLeave(event) {
79 for (const droppedFile of this.uploadedFiles) {
80 droppedFile.file((file: File) => {
81 const fileReader = new FileReader();
82 fileReader.onload = (e) => {
83 this.packageCreationStore.addScripts('Scripts/' + this.getFileType(droppedFile.name) + '/' + droppedFile.name,
84 fileReader.result.toString());
86 fileReader.readAsText(file);
92 getFileType(filename: string): string {
94 const fileExtension = filename.substring(filename.lastIndexOf('.') + 1);
95 if (fileExtension === 'py') {
97 } else if (fileExtension === 'kt') {
103 resetTheUploadedFiles() {
104 this.uploadedFiles = [];
107 textChanges(code: any, key: string) {
108 this.packageCreationStore.addScripts(key, code);
111 changeDivShow(mapIndex: number) {
112 const divElement = document.getElementById('id-script-' + mapIndex) as HTMLElement;
113 if (divElement.getAttribute('class').includes('show')) {
114 divElement.setAttribute('class', 'collapse');
116 divElement.setAttribute('class', 'collapse show');