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