aboutsummaryrefslogtreecommitdiffstats
path: root/cds-ui/designer-client/src/app/modules/feature-modules/packages/package-creation/scripts-tab/scripts-tab.component.ts
blob: 60be0d7bab501fe7d3cef2ee5929c76411414590 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import {Component, OnInit} from '@angular/core';
import {FileSystemFileEntry, NgxFileDropEntry} from 'ngx-file-drop';
import {PackageCreationStore} from '../package-creation.store';
import 'ace-builds/src-noconflict/ace';
import 'ace-builds/webpack-resolver';
import {ToastrService} from 'ngx-toastr';

declare var $: any;

@Component({
    selector: 'app-scripts-tab',
    templateUrl: './scripts-tab.component.html',
    styleUrls: ['./scripts-tab.component.css']
})
export class ScriptsTabComponent implements OnInit {

    public scriptsFiles: Map<string, string> = new Map<string, string>();
    public uploadedFiles: FileSystemFileEntry[] = [];
    public files: NgxFileDropEntry[] = [];
    private fileNames: Set<string> = new Set();
    fileToDelete: any = {};
    currentFileName: any;
    scriptExtension: any;
    currentFileContent = '';
    currentExtension = '';

    constructor(
        private packageCreationStore: PackageCreationStore,
        private toastService: ToastrService
    ) {
    }


    ngOnInit() {

        this.packageCreationStore.state$.subscribe(cbaPackage => {
            if (cbaPackage.scripts && cbaPackage.scripts.files && cbaPackage.scripts.files.size > 0) {
                this.scriptsFiles = cbaPackage.scripts.files;
            }
        });
    }

    public dropped(files: NgxFileDropEntry[]) {
        this.files = files;
        for (const droppedFile of files) {
            // Is it a file & Not added before ?
            if (droppedFile.fileEntry.isFile) {
                const fileEntry = droppedFile.fileEntry as FileSystemFileEntry;
                this.uploadedFiles.push(fileEntry);
                console.log(fileEntry.name);
                this.fileNames.add(fileEntry.name);

            }
        }
    }

    removeInitFile(index) {
        this.uploadedFiles.splice(index, 1);
    }

    initDelete(file) {
        this.fileToDelete = file;
    }

    removeFile(filePath: string, FileIndex: number) {
        const filename = filePath.split('/')[2] || '';
        //  const filename = 'Scripts/' + this.getFileType(this.uploadedFiles[fileIndex].name) + '/' + this.uploadedFiles[fileIndex].name;
        this.packageCreationStore.removeFileFromState(filePath);
        // remove from upload files array
        // tslint:disable-next-line: prefer-for-of
        for (let i = 0; i < this.uploadedFiles.length; i++) {
            if (this.uploadedFiles[i].name === filename) {
                this.uploadedFiles.splice(i, 1);
                break;
            }
        }
    }

    public fileOver(event) {
        console.log(event);
    }

    public fileLeave(event) {
        console.log(event);
    }


    setFilesToStore() {
        for (const droppedFile of this.uploadedFiles) {
            droppedFile.file((file: File) => {
                const fileReader = new FileReader();
                fileReader.onload = (e) => {
                    this.packageCreationStore.addScripts('Scripts/' + this.getFileType(droppedFile.name) + '/' + droppedFile.name,
                        fileReader.result.toString());
                };
                fileReader.readAsText(file);
            });

        }
    }

    getFileType(filename: string): string {
        let fileType = '';
        const fileExtension = filename.substring(filename.lastIndexOf('.') + 1);
        if (fileExtension === 'py') {
            fileType = 'python';
        } else if (fileExtension === 'kt') {
            fileType = 'kotlin';
        }
        return fileType;
    }

    resetTheUploadedFiles() {
        this.uploadedFiles = [];
    }

    textChanges(code: any, key: string) {
        this.packageCreationStore.addScripts(key, code);
        this.toastService.success(key + ' is updated successfully');
    }

    changeDivShow(mapIndex: number) {
        const divElement = document.getElementById('id-script-' + mapIndex) as HTMLElement;
        if (divElement.getAttribute('class').includes('show')) {
            divElement.setAttribute('class', 'collapse');
        } else {
            divElement.setAttribute('class', 'collapse show');
        }
    }

    textCurrentChanges() {
        console.log(this.currentFileName);
        console.log(this.currentFileContent);
        if (this.currentFileName) {
            let fileExtension = 'kt';
            if (this.currentExtension.includes('python')) {
                fileExtension = 'py';
            } else if (this.currentExtension.includes('ansible')) {
                fileExtension = 'yaml';
            }
            this.packageCreationStore.addScripts('Scripts/' + this.currentExtension + '/' + this.currentFileName + '.' + fileExtension
                , this.currentFileContent);
            this.toastService.success('the ' + this.currentFileName + ' has been added');
            this.currentFileName = '';
            this.currentExtension = '';
            this.currentFileContent = '';
        }
    }

    changeExtension() {
        this.currentExtension = this.scriptExtension;
        console.log(this.scriptExtension);
    }
}