f25dcb1bef16488f1672c43613eac01323954fc6
[ccsdk/cds.git] /
1 import { Component, EventEmitter, OnInit, Output } from '@angular/core';
2 import { PackageCreationStore } from '../../package-creation.store';
3 import { Mapping, Template } from '../../mapping-models/CBAPacakge.model';
4 import { TemplateInfo, TemplateStore } from '../../template.store';
5 import { TemplateAndMapping } from '../TemplateAndMapping';
6 import { ActivatedRoute } from '@angular/router';
7 import { SharedService } from '../shared-service';
8
9
10 @Component({
11     selector: 'app-templ-mapp-listing',
12     templateUrl: './templ-mapp-listing.component.html',
13     styleUrls: ['./templ-mapp-listing.component.css']
14 })
15 export class TemplMappListingComponent implements OnInit {
16     @Output() showCreationViewParentNotification = new EventEmitter<any>();
17     @Output() showFullView = new EventEmitter<any>();
18     private templateAndMappingMap = new Map<string, TemplateAndMapping>();
19     private templates: Template;
20     private mapping: Mapping;
21     isCreate = true;
22     currentFile: string;
23     edit = false;
24     fileToDelete: any = {};
25
26     constructor(
27         private packageCreationStore: PackageCreationStore,
28         private templateStore: TemplateStore,
29         private route: ActivatedRoute,
30         private sharedService: SharedService
31     ) {
32     }
33
34     ngOnInit() {
35         if (this.route.snapshot.paramMap.has('id')) {
36             this.isCreate = false;
37             this.sharedService.isEdit().subscribe(res => {
38                 this.edit = res;
39             });
40
41         }
42         this.packageCreationStore.state$.subscribe(cba => {
43             if (cba.templates) {
44                 this.templates = cba.templates;
45                 this.mapping = cba.mapping;
46                 console.log(this.mapping);
47                 let templateAndMapping;
48                 this.templateAndMappingMap.clear();
49                 this.templates.files.forEach((value, key) => {
50                     templateAndMapping = new TemplateAndMapping();
51                     templateAndMapping.isTemplate = true;
52                     const isFromTemplate = true;
53                     this.setIsMappingOrTemplate(key, templateAndMapping, isFromTemplate);
54                 });
55                 this.mapping.files.forEach((value, key) => {
56                     templateAndMapping = new TemplateAndMapping();
57                     templateAndMapping.isMapping = true;
58                     const isFromTemplate = false;
59                     this.setIsMappingOrTemplate(key, templateAndMapping, isFromTemplate);
60                 });
61                 console.log('hello there ');
62                 console.log(this.templateAndMappingMap);
63             }
64         });
65     }
66
67     private setIsMappingOrTemplate(key: string, templateAndMapping: TemplateAndMapping, isFromTemplate: boolean) {
68         const nameOfFile = key.split('/')[1].split('.')[0].split('-')[0];
69         // const fullName = nameOfFile + ',' + key.split('.');
70         if (this.templateAndMappingMap.has(nameOfFile)) {
71             const templateAndMappingExisted = this.templateAndMappingMap.get(nameOfFile);
72             !isFromTemplate ? templateAndMappingExisted.isMapping = true : templateAndMappingExisted.isTemplate = true;
73             this.templateAndMappingMap.set(nameOfFile, templateAndMappingExisted);
74         } else {
75             this.templateAndMappingMap.set(nameOfFile, templateAndMapping);
76         }
77
78     }
79
80     openCreationView() {
81         this.showCreationViewParentNotification.emit('tell parent to open create views');
82         console.log('disable edit mode');
83         this.sharedService.disableEdit();
84
85     }
86     FullView() {
87         this.showFullView.emit('show full view');
88     }
89
90     setSourceCodeEditor(key: string) {
91         this.currentFile = key;
92         const templateKey = 'Templates/' + key + '-template.vtl';
93         this.packageCreationStore.state$.subscribe(cba => {
94             console.log('cba ------');
95             console.log(cba);
96             console.log(key);
97             console.log(this.templateAndMappingMap);
98             const templateInfo = new TemplateInfo();
99             if (cba.templates && cba.templates.files.has(templateKey)) {
100                 const fileContent = cba.templates.getValue(templateKey.trim());
101                 console.log(fileContent);
102                 templateInfo.fileContent = fileContent;
103                 templateInfo.fileName = templateKey;
104                 templateInfo.type = 'template';
105             }
106             const mappingKey = 'Templates/' + key + '-mapping.json';
107             if (cba.mapping && cba.mapping.files.has(mappingKey)) {
108                 const obj = JSON.parse(cba.mapping.getValue(mappingKey));
109                 templateInfo.mapping = obj;
110                 templateInfo.fileName = mappingKey;
111                 templateInfo.type += 'mapping';
112             }
113             this.templateStore.changeTemplateInfo(templateInfo);
114             this.FullView();
115             this.sharedService.enableEdit();
116         });
117     }
118
119     getKeys(templateAndMappingMap: Map<string, TemplateAndMapping>) {
120         return Array.from(this.templateAndMappingMap.keys());
121     }
122
123     getValue(file: string) {
124         return this.templateAndMappingMap.get(file);
125     }
126     initDelete(file) {
127         console.log(file);
128         this.fileToDelete = file;
129     }
130     condifrmDelete() {
131         console.log(this.templateAndMappingMap);
132         this.templateAndMappingMap.delete(this.fileToDelete);
133         if (this.templateAndMappingMap.size <= 0) {
134             this.openCreationView();
135         }
136         // Delete from templates
137         this.packageCreationStore.state.templates.files.delete('Templates/' + this.fileToDelete + '-template.vtl');
138         // Delete from Mapping
139         this.packageCreationStore.state.mapping.files.delete('Templates/' + this.fileToDelete + '-mapping.json');
140
141     }
142
143 }