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