46e67fc07ed8ba04e42372d7f09aefd744bc3fe8
[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                 let templateAndMapping;
27                 this.templateAndMappingMap.clear();
28                 this.templates.files.forEach((value, key) => {
29                     templateAndMapping = new TemplateAndMapping();
30                     templateAndMapping.isTemplate = true;
31                     const isFromTemplate = true;
32                     this.setIsMappingOrTemplate(key, templateAndMapping, isFromTemplate);
33                 });
34                 this.mapping.files.forEach((value, key) => {
35                     templateAndMapping = new TemplateAndMapping();
36                     templateAndMapping.isMapping = true;
37                     const isFromTemplate = false;
38                     this.setIsMappingOrTemplate(key, templateAndMapping, isFromTemplate);
39                 });
40                 console.log('hello there ');
41                 console.log(this.templateAndMappingMap);
42             }
43         });
44     }
45
46     private setIsMappingOrTemplate(key: string, templateAndMapping: TemplateAndMapping, isFromTemplate: boolean) {
47         const nameOfFile = key.split('/')[1].split('.')[0].split('-')[0];
48         if (this.templateAndMappingMap.has(nameOfFile)) {
49             const templateAndMappingExisted = this.templateAndMappingMap.get(nameOfFile);
50             !isFromTemplate ? templateAndMappingExisted.isMapping = true : templateAndMappingExisted.isTemplate = true;
51             this.templateAndMappingMap.set(nameOfFile, templateAndMappingExisted);
52         } else {
53
54             this.templateAndMappingMap.set(nameOfFile, templateAndMapping);
55         }
56
57     }
58
59     openCreationView() {
60         this.showCreationViewParentNotification.emit('tell parent to open create views');
61     }
62
63     setSourceCodeEditor(key: string) {
64         this.packageCreationStore.state$.subscribe(cba => {
65             if (cba.templates) {
66                 const fileContent = cba.templates.getValue(key);
67                 const templateInfo = new TemplateInfo();
68                 templateInfo.fileContent = fileContent;
69                 templateInfo.fileName = key;
70                 this.templateStore.changeTemplateInfo(templateInfo);
71             }
72         });
73     }
74
75     getKeys(templateAndMappingMap: Map<string, TemplateAndMapping>) {
76         return Array.from(this.templateAndMappingMap.keys());
77     }
78
79     getValue(file: string) {
80         return this.templateAndMappingMap.get(file);
81     }
82
83 }