722be321c405b717d43ba00977b09fc962af9a61
[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 import { TourService } from 'ngx-tour-md-menu';
9 import { TemplateType } from '../utils/TemplateType';
10 import { of } from 'rxjs';
11
12
13 @Component({
14     selector: 'app-templ-mapp-listing',
15     templateUrl: './templ-mapp-listing.component.html',
16     styleUrls: ['./templ-mapp-listing.component.css']
17 })
18 export class TemplMappListingComponent implements OnInit {
19     @Output() showCreationView = new EventEmitter<any>();
20     @Output() showListView = new EventEmitter<any>();
21     templateAndMappingMap = new Map<string, TemplateAndMapping>();
22     templates: Template;
23     mapping: Mapping;
24     isCreate = true;
25     currentFile: string;
26     edit = false;
27     fileToDelete = '';
28
29     constructor(
30         private packageCreationStore: PackageCreationStore,
31         private templateStore: TemplateStore,
32         private route: ActivatedRoute,
33         private sharedService: SharedService,
34         private tourService: TourService,
35
36     ) {
37     }
38
39     ngOnInit() {
40         if (this.route.snapshot.paramMap.has('id')) {
41             this.isCreate = false;
42             this.sharedService.isEdit().subscribe(res => {
43                 this.edit = res;
44             });
45
46         }
47         this.packageCreationStore.state$.subscribe(cba => {
48             if (this.packageCreationStore.state.mapping.files.size > 0 || this.packageCreationStore.state.templates.files.size > 0) {
49                 this.openListView();
50             } else {
51                 this.openCreationView();
52             }
53             if (cba.templates) {
54                 this.templates = cba.templates;
55                 this.mapping = cba.mapping;
56                 console.log(this.mapping);
57                 let templateAndMapping;
58                 this.templateAndMappingMap.clear();
59                 this.templates.files.forEach((value, key) => {
60                     templateAndMapping = new TemplateAndMapping();
61                     templateAndMapping.isTemplate = true;
62                     const isFromTemplate = true;
63                     this.setIsMappingOrTemplate(key, templateAndMapping, isFromTemplate);
64                 });
65                 this.mapping.files.forEach((value, key) => {
66                     templateAndMapping = new TemplateAndMapping();
67                     templateAndMapping.isMapping = true;
68                     const isFromTemplate = false;
69                     this.setIsMappingOrTemplate(key, templateAndMapping, isFromTemplate);
70                 });
71                 console.log(this.templateAndMappingMap);
72             }
73             this.deleteFromList();
74         });
75     }
76
77     private setIsMappingOrTemplate(key: string, templateAndMapping: TemplateAndMapping, isFromTemplate: boolean) {
78         const nameOfFile = key.split('/')[1].split('.')[0].split('-')[0];
79         // const fullName = nameOfFile + ',' + key.split('.');
80         if (this.templateAndMappingMap.has(nameOfFile)) {
81             const templateAndMappingExisted = this.templateAndMappingMap.get(nameOfFile);
82             !isFromTemplate ? templateAndMappingExisted.isMapping = true : templateAndMappingExisted.isTemplate = true;
83             this.templateAndMappingMap.set(nameOfFile, templateAndMappingExisted);
84         } else {
85             this.templateAndMappingMap.set(nameOfFile, templateAndMapping);
86         }
87
88     }
89
90     deleteFromList() {
91         this.sharedService.listAction().subscribe(res => {
92             console.log('response from actionList');
93             console.log(res);
94             if (res) {
95                 console.log('xccccccccccvvvvvv');
96                 this.templateAndMappingMap.delete(res);
97                 if (this.templateAndMappingMap.size <= 0) {
98                     this.openCreationView();
99                 }
100             }
101         });
102     }
103
104     createNewTemplate() {
105         this.openCreationView();
106         this.sharedService.disableEdit();
107         if (localStorage.getItem('tour-guide') !== 'end' && localStorage.getItem('tour-guide') !== 'false') {
108             this.tourService.goto('tm-templateName');
109         }
110     }
111     openCreationView() {
112         this.showCreationView.emit('tell parent to open create views');
113         console.log('disable edit mode');
114     }
115     openListView() {
116         console.log('open list view');
117         this.showListView.emit('show full view');
118     }
119
120     setSourceCodeEditor(key: string) {
121         this.currentFile = key;
122         const templateKey = 'Templates/' + key + '-template';
123         this.packageCreationStore.state$.subscribe(cba => {
124             console.log('cba ------');
125             console.log(cba);
126             console.log(key);
127             console.log(this.templateAndMappingMap);
128             const templateInfo = new TemplateInfo();
129             // tslint:disable-next-line: forin
130             for (const templateType in TemplateType) {
131                 const fileName = templateKey + '.' + TemplateType[templateType];
132                 if (cba.templates && cba.templates.files.has(fileName)) {
133                     const fileContent = cba.templates.getValue(fileName.trim());
134                     console.log(templateType + '......ccccccc.... ' + fileName);
135                     templateInfo.fileContent = fileContent;
136                     templateInfo.fileName = fileName;
137                     templateInfo.ext = TemplateType[templateType];
138                     templateInfo.type = 'template';
139                     break;
140                 }
141             }
142             const mappingKey = 'Templates/' + key + '-mapping.json';
143             if (cba.mapping && cba.mapping.files.has(mappingKey)) {
144                 const obj = JSON.parse(cba.mapping.getValue(mappingKey));
145                 templateInfo.mapping = obj;
146                 templateInfo.fileName = mappingKey;
147                 templateInfo.type += 'mapping';
148             }
149             this.templateStore.changeTemplateInfo(templateInfo);
150             this.openCreationView();
151             this.sharedService.enableEdit();
152         });
153     }
154
155     getKeys(templateAndMappingMap: Map<string, TemplateAndMapping>) {
156         return Array.from(this.templateAndMappingMap.keys());
157     }
158
159     getValue(file: string) {
160         return this.templateAndMappingMap.get(file);
161     }
162     initDelete(file) {
163         console.log(file);
164         const templateKey = 'Templates/' + file + '-template';
165         // tslint:disable-next-line: forin
166         for (const templateType in TemplateType) {
167             const fileName = templateKey + '.' + TemplateType[templateType];
168             if (this.packageCreationStore.state.templates.files.has(fileName)) {
169                 this.fileToDelete = fileName;
170                 break;
171             }
172         }
173
174     }
175     condifrmDelete() {
176         const file = this.fileToDelete.split('/')[1].split('-')[0];
177         const ext = this.fileToDelete.split('/')[1].split('.')[1];
178         this.templateAndMappingMap.delete(file);
179         if (this.templateAndMappingMap.size <= 0) {
180             this.openCreationView();
181         }
182         // Delete from templates
183         this.packageCreationStore.state.templates.files.delete('Templates/' + file + '-template.' + ext);
184         // Delete from Mapping
185         this.packageCreationStore.state.mapping.files.delete('Templates/' + file + '-mapping.json');
186         console.log(this.templateAndMappingMap);
187
188     }
189
190 }