26740f3e1c4483e52fd6d9ef211fa3ea30184ec1
[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() showCreationView = new EventEmitter<any>();
17     @Output() showListView = new EventEmitter<any>();
18     templateAndMappingMap = new Map<string, TemplateAndMapping>();
19     templates: Template;
20     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 (this.packageCreationStore.state.mapping.files.size > 0 || this.packageCreationStore.state.templates.files.size > 0) {
44                 this.openListView();
45             } else {
46                 this.openCreationView();
47             }
48             if (cba.templates) {
49                 this.templates = cba.templates;
50                 this.mapping = cba.mapping;
51                 console.log(this.mapping);
52                 let templateAndMapping;
53                 this.templateAndMappingMap.clear();
54                 this.templates.files.forEach((value, key) => {
55                     templateAndMapping = new TemplateAndMapping();
56                     templateAndMapping.isTemplate = true;
57                     const isFromTemplate = true;
58                     this.setIsMappingOrTemplate(key, templateAndMapping, isFromTemplate);
59                 });
60                 this.mapping.files.forEach((value, key) => {
61                     templateAndMapping = new TemplateAndMapping();
62                     templateAndMapping.isMapping = true;
63                     const isFromTemplate = false;
64                     this.setIsMappingOrTemplate(key, templateAndMapping, isFromTemplate);
65                 });
66                 console.log(this.templateAndMappingMap);
67             }
68             this.deleteFromList();
69         });
70     }
71
72     private setIsMappingOrTemplate(key: string, templateAndMapping: TemplateAndMapping, isFromTemplate: boolean) {
73         const nameOfFile = key.split('/')[1].split('.')[0].split('-')[0];
74         // const fullName = nameOfFile + ',' + key.split('.');
75         if (this.templateAndMappingMap.has(nameOfFile)) {
76             const templateAndMappingExisted = this.templateAndMappingMap.get(nameOfFile);
77             !isFromTemplate ? templateAndMappingExisted.isMapping = true : templateAndMappingExisted.isTemplate = true;
78             this.templateAndMappingMap.set(nameOfFile, templateAndMappingExisted);
79         } else {
80             this.templateAndMappingMap.set(nameOfFile, templateAndMapping);
81         }
82
83     }
84
85     deleteFromList() {
86         this.sharedService.listAction().subscribe(res => {
87             console.log('response from actionList');
88             console.log(res);
89             if (res) {
90                 console.log('xccccccccccvvvvvv');
91                 this.templateAndMappingMap.delete(res);
92                 if (this.templateAndMappingMap.size <= 0) {
93                     this.openCreationView();
94                 }
95             }
96         });
97     }
98
99     createNewTemplate() {
100         this.openCreationView();
101         this.sharedService.disableEdit();
102     }
103     openCreationView() {
104         this.showCreationView.emit('tell parent to open create views');
105         console.log('disable edit mode');
106     }
107     openListView() {
108         console.log('open list view');
109         this.showListView.emit('show full view');
110     }
111
112     setSourceCodeEditor(key: string) {
113         this.currentFile = key;
114         const templateKey = 'Templates/' + key + '-template.vtl';
115         this.packageCreationStore.state$.subscribe(cba => {
116             console.log('cba ------');
117             console.log(cba);
118             console.log(key);
119             console.log(this.templateAndMappingMap);
120             const templateInfo = new TemplateInfo();
121             if (cba.templates && cba.templates.files.has(templateKey)) {
122                 const fileContent = cba.templates.getValue(templateKey.trim());
123                 console.log(fileContent);
124                 templateInfo.fileContent = fileContent;
125                 templateInfo.fileName = templateKey;
126                 templateInfo.type = 'template';
127             }
128             const mappingKey = 'Templates/' + key + '-mapping.json';
129             if (cba.mapping && cba.mapping.files.has(mappingKey)) {
130                 const obj = JSON.parse(cba.mapping.getValue(mappingKey));
131                 templateInfo.mapping = obj;
132                 templateInfo.fileName = mappingKey;
133                 templateInfo.type += 'mapping';
134             }
135             this.templateStore.changeTemplateInfo(templateInfo);
136             this.openCreationView();
137             this.sharedService.enableEdit();
138         });
139     }
140
141     getKeys(templateAndMappingMap: Map<string, TemplateAndMapping>) {
142         return Array.from(this.templateAndMappingMap.keys());
143     }
144
145     getValue(file: string) {
146         return this.templateAndMappingMap.get(file);
147     }
148     initDelete(file) {
149         console.log(file);
150         this.fileToDelete = file;
151     }
152     condifrmDelete() {
153         console.log(this.templateAndMappingMap);
154         this.templateAndMappingMap.delete(this.fileToDelete);
155         if (this.templateAndMappingMap.size <= 0) {
156             this.openCreationView();
157         }
158         // Delete from templates
159         this.packageCreationStore.state.templates.files.delete('Templates/' + this.fileToDelete + '-template.vtl');
160         // Delete from Mapping
161         this.packageCreationStore.state.mapping.files.delete('Templates/' + this.fileToDelete + '-mapping.json');
162
163     }
164
165 }