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