ed9f3eeb397dacdb78cb7a87051035525d6012de
[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
8
9 @Component({
10     selector: 'app-templ-mapp-listing',
11     templateUrl: './templ-mapp-listing.component.html',
12     styleUrls: ['./templ-mapp-listing.component.css']
13 })
14 export class TemplMappListingComponent implements OnInit {
15     @Output() showCreationViewParentNotification = new EventEmitter<any>();
16     @Output() showFullView = new EventEmitter<any>();
17     private templateAndMappingMap = new Map<string, TemplateAndMapping>();
18     private templates: Template;
19     private mapping: Mapping;
20     isCreate = true;
21     currentFile: string;
22
23     constructor(
24         private packageCreationStore: PackageCreationStore,
25         private templateStore: TemplateStore,
26         private route: ActivatedRoute
27     ) {
28     }
29
30     ngOnInit() {
31         if (this.route.snapshot.paramMap.has('id')) {
32             this.isCreate = false;
33         }
34         this.packageCreationStore.state$.subscribe(cba => {
35             if (cba.templates) {
36                 this.templates = cba.templates;
37                 this.mapping = cba.mapping;
38                 console.log(this.mapping);
39                 let templateAndMapping;
40                 this.templateAndMappingMap.clear();
41                 this.templates.files.forEach((value, key) => {
42                     templateAndMapping = new TemplateAndMapping();
43                     templateAndMapping.isTemplate = true;
44                     const isFromTemplate = true;
45                     this.setIsMappingOrTemplate(key, templateAndMapping, isFromTemplate);
46                 });
47                 this.mapping.files.forEach((value, key) => {
48                     templateAndMapping = new TemplateAndMapping();
49                     templateAndMapping.isMapping = true;
50                     const isFromTemplate = false;
51                     this.setIsMappingOrTemplate(key, templateAndMapping, isFromTemplate);
52                 });
53                 console.log('hello there ');
54                 console.log(this.templateAndMappingMap);
55             }
56         });
57     }
58
59     private setIsMappingOrTemplate(key: string, templateAndMapping: TemplateAndMapping, isFromTemplate: boolean) {
60         const nameOfFile = key.split('/')[1].split('.')[0].split('-')[0];
61         // const fullName = nameOfFile + ',' + key.split('.');
62         if (this.templateAndMappingMap.has(nameOfFile)) {
63             const templateAndMappingExisted = this.templateAndMappingMap.get(nameOfFile);
64             !isFromTemplate ? templateAndMappingExisted.isMapping = true : templateAndMappingExisted.isTemplate = true;
65             this.templateAndMappingMap.set(nameOfFile, templateAndMappingExisted);
66         } else {
67             this.templateAndMappingMap.set(nameOfFile, templateAndMapping);
68         }
69
70     }
71
72     openCreationView() {
73         this.showCreationViewParentNotification.emit('tell parent to open create views');
74     }
75     FullView() {
76         this.showFullView.emit('show full view');
77     }
78
79     setSourceCodeEditor(key: string) {
80         this.currentFile = key;
81         const templateKey = 'Templates/' + key + '-template.vtl';
82         this.packageCreationStore.state$.subscribe(cba => {
83             console.log('cba ------');
84             console.log(cba);
85             console.log(key);
86             console.log(this.templateAndMappingMap);
87             const templateInfo = new TemplateInfo();
88             if (cba.templates && cba.templates.files.has(templateKey)) {
89                 const fileContent = cba.templates.getValue(templateKey.trim());
90                 console.log(fileContent);
91                 templateInfo.fileContent = fileContent;
92                 templateInfo.fileName = templateKey;
93                 templateInfo.type = 'template';
94             }
95             const mappingKey = 'Templates/' + key + '-mapping.json';
96             if (cba.mapping && cba.mapping.files.has(mappingKey)) {
97                 const obj = JSON.parse(cba.mapping.getValue(mappingKey));
98                 templateInfo.mapping = obj;
99                 templateInfo.fileName = mappingKey;
100                 templateInfo.type += 'mapping';
101             }
102             this.templateStore.changeTemplateInfo(templateInfo);
103             this.FullView();
104         });
105     }
106
107     getKeys(templateAndMappingMap: Map<string, TemplateAndMapping>) {
108         return Array.from(this.templateAndMappingMap.keys());
109     }
110
111     getValue(file: string) {
112         return this.templateAndMappingMap.get(file);
113     }
114
115 }