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