c790422c1389ed7dd11e5123af88b2ca9558db75
[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             this.deleteFromList();
65         });
66     }
67
68     private setIsMappingOrTemplate(key: string, templateAndMapping: TemplateAndMapping, isFromTemplate: boolean) {
69         const nameOfFile = key.split('/')[1].split('.')[0].split('-')[0];
70         // const fullName = nameOfFile + ',' + key.split('.');
71         if (this.templateAndMappingMap.has(nameOfFile)) {
72             const templateAndMappingExisted = this.templateAndMappingMap.get(nameOfFile);
73             !isFromTemplate ? templateAndMappingExisted.isMapping = true : templateAndMappingExisted.isTemplate = true;
74             this.templateAndMappingMap.set(nameOfFile, templateAndMappingExisted);
75         } else {
76             this.templateAndMappingMap.set(nameOfFile, templateAndMapping);
77         }
78
79     }
80
81     deleteFromList() {
82         this.sharedService.listAction().subscribe(res => {
83             console.log('response from actionList');
84             console.log(res);
85             if (res) {
86                 this.templateAndMappingMap.delete(res);
87                 if (this.templateAndMappingMap.size <= 0) {
88                     this.openCreationView();
89                 }
90             }
91         });
92     }
93
94     openCreationView() {
95         this.showCreationViewParentNotification.emit('tell parent to open create views');
96         console.log('disable edit mode');
97         this.sharedService.disableEdit();
98
99     }
100     FullView() {
101         this.showFullView.emit('show full view');
102     }
103
104     setSourceCodeEditor(key: string) {
105         this.currentFile = key;
106         const templateKey = 'Templates/' + key + '-template.vtl';
107         this.packageCreationStore.state$.subscribe(cba => {
108             console.log('cba ------');
109             console.log(cba);
110             console.log(key);
111             console.log(this.templateAndMappingMap);
112             const templateInfo = new TemplateInfo();
113             if (cba.templates && cba.templates.files.has(templateKey)) {
114                 const fileContent = cba.templates.getValue(templateKey.trim());
115                 console.log(fileContent);
116                 templateInfo.fileContent = fileContent;
117                 templateInfo.fileName = templateKey;
118                 templateInfo.type = 'template';
119             }
120             const mappingKey = 'Templates/' + key + '-mapping.json';
121             if (cba.mapping && cba.mapping.files.has(mappingKey)) {
122                 const obj = JSON.parse(cba.mapping.getValue(mappingKey));
123                 templateInfo.mapping = obj;
124                 templateInfo.fileName = mappingKey;
125                 templateInfo.type += 'mapping';
126             }
127             this.templateStore.changeTemplateInfo(templateInfo);
128             this.FullView();
129             this.sharedService.enableEdit();
130         });
131     }
132
133     getKeys(templateAndMappingMap: Map<string, TemplateAndMapping>) {
134         return Array.from(this.templateAndMappingMap.keys());
135     }
136
137     getValue(file: string) {
138         return this.templateAndMappingMap.get(file);
139     }
140     initDelete(file) {
141         console.log(file);
142         this.fileToDelete = file;
143     }
144     condifrmDelete() {
145         console.log(this.templateAndMappingMap);
146         this.templateAndMappingMap.delete(this.fileToDelete);
147         if (this.templateAndMappingMap.size <= 0) {
148             this.openCreationView();
149         }
150         // Delete from templates
151         this.packageCreationStore.state.templates.files.delete('Templates/' + this.fileToDelete + '-template.vtl');
152         // Delete from Mapping
153         this.packageCreationStore.state.mapping.files.delete('Templates/' + this.fileToDelete + '-mapping.json');
154
155     }
156
157 }