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 currentFileContent = '';
25 currentExtension = '';
28 private packageCreationStore: PackageCreationStore,
29 private toastService: ToastrService
36 this.packageCreationStore.state$.subscribe(cbaPackage => {
37 if (cbaPackage.scripts && cbaPackage.scripts.files && cbaPackage.scripts.files.size > 0) {
38 this.scriptsFiles = cbaPackage.scripts.files;
43 public dropped(files: NgxFileDropEntry[]) {
45 for (const droppedFile of files) {
46 // Is it a file & Not added before ?
47 if (droppedFile.fileEntry.isFile) {
48 const fileEntry = droppedFile.fileEntry as FileSystemFileEntry;
49 this.uploadedFiles.push(fileEntry);
50 console.log(fileEntry.name);
51 this.fileNames.add(fileEntry.name);
57 removeInitFile(index) {
58 this.uploadedFiles.splice(index, 1);
62 this.fileToDelete = file;
65 removeFile(filePath: string, FileIndex: number) {
66 const filename = filePath.split('/')[2] || '';
67 // const filename = 'Scripts/' + this.getFileType(this.uploadedFiles[fileIndex].name) + '/' + this.uploadedFiles[fileIndex].name;
68 this.packageCreationStore.removeFileFromState(filePath);
69 // remove from upload files array
70 // tslint:disable-next-line: prefer-for-of
71 for (let i = 0; i < this.uploadedFiles.length; i++) {
72 if (this.uploadedFiles[i].name === filename) {
73 this.uploadedFiles.splice(i, 1);
79 public fileOver(event) {
83 public fileLeave(event) {
89 for (const droppedFile of this.uploadedFiles) {
90 droppedFile.file((file: File) => {
91 const fileReader = new FileReader();
92 fileReader.onload = (e) => {
93 this.packageCreationStore.addScripts('Scripts/' + this.getFileType(droppedFile.name) + '/' + droppedFile.name,
94 fileReader.result.toString());
96 fileReader.readAsText(file);
102 getFileType(filename: string): string {
104 const fileExtension = filename.substring(filename.lastIndexOf('.') + 1);
105 if (fileExtension === 'py') {
107 } else if (fileExtension === 'kt') {
113 resetTheUploadedFiles() {
114 this.uploadedFiles = [];
117 textChanges(code: any, key: string) {
118 this.packageCreationStore.addScripts(key, code);
119 this.toastService.success(key + ' is updated successfully');
122 changeDivShow(mapIndex: number) {
123 const divElement = document.getElementById('id-script-' + mapIndex) as HTMLElement;
124 if (divElement.getAttribute('class').includes('show')) {
125 divElement.setAttribute('class', 'collapse');
127 divElement.setAttribute('class', 'collapse show');
131 textCurrentChanges() {
132 console.log(this.currentFileName);
133 console.log(this.currentFileContent);
134 if (this.currentFileName) {
135 let fileExtension = 'kt';
136 if (this.currentExtension.includes('python')) {
137 fileExtension = 'py';
138 } else if (this.currentExtension.includes('ansible')) {
139 fileExtension = 'yaml';
141 this.packageCreationStore.addScripts('Scripts/' + this.currentExtension + '/' + this.currentFileName + '.' + fileExtension
142 , this.currentFileContent);
143 this.toastService.success('the ' + this.currentFileName + ' has been added');
144 this.currentFileName = '';
145 this.currentExtension = '';
146 this.currentFileContent = '';
151 this.currentExtension = this.scriptExtension;
152 console.log(this.scriptExtension);