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