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';
11 selector: 'app-templ-mapp-listing',
12 templateUrl: './templ-mapp-listing.component.html',
13 styleUrls: ['./templ-mapp-listing.component.css']
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;
24 fileToDelete: any = {};
27 private packageCreationStore: PackageCreationStore,
28 private templateStore: TemplateStore,
29 private route: ActivatedRoute,
30 private sharedService: SharedService
35 if (this.route.snapshot.paramMap.has('id')) {
36 this.isCreate = false;
37 this.sharedService.isEdit().subscribe(res => {
42 this.packageCreationStore.state$.subscribe(cba => {
44 this.templates = cba.templates;
45 this.mapping = cba.mapping;
46 console.log(this.mapping);
47 let templateAndMapping;
48 this.templateAndMappingMap.clear();
49 this.templates.files.forEach((value, key) => {
50 templateAndMapping = new TemplateAndMapping();
51 templateAndMapping.isTemplate = true;
52 const isFromTemplate = true;
53 this.setIsMappingOrTemplate(key, templateAndMapping, isFromTemplate);
55 this.mapping.files.forEach((value, key) => {
56 templateAndMapping = new TemplateAndMapping();
57 templateAndMapping.isMapping = true;
58 const isFromTemplate = false;
59 this.setIsMappingOrTemplate(key, templateAndMapping, isFromTemplate);
61 console.log('hello there ');
62 console.log(this.templateAndMappingMap);
67 private setIsMappingOrTemplate(key: string, templateAndMapping: TemplateAndMapping, isFromTemplate: boolean) {
68 const nameOfFile = key.split('/')[1].split('.')[0].split('-')[0];
69 // const fullName = nameOfFile + ',' + key.split('.');
70 if (this.templateAndMappingMap.has(nameOfFile)) {
71 const templateAndMappingExisted = this.templateAndMappingMap.get(nameOfFile);
72 !isFromTemplate ? templateAndMappingExisted.isMapping = true : templateAndMappingExisted.isTemplate = true;
73 this.templateAndMappingMap.set(nameOfFile, templateAndMappingExisted);
75 this.templateAndMappingMap.set(nameOfFile, templateAndMapping);
81 this.showCreationViewParentNotification.emit('tell parent to open create views');
82 console.log('disable edit mode');
83 this.sharedService.disableEdit();
87 this.showFullView.emit('show full view');
90 setSourceCodeEditor(key: string) {
91 this.currentFile = key;
92 const templateKey = 'Templates/' + key + '-template.vtl';
93 this.packageCreationStore.state$.subscribe(cba => {
94 console.log('cba ------');
97 console.log(this.templateAndMappingMap);
98 const templateInfo = new TemplateInfo();
99 if (cba.templates && cba.templates.files.has(templateKey)) {
100 const fileContent = cba.templates.getValue(templateKey.trim());
101 console.log(fileContent);
102 templateInfo.fileContent = fileContent;
103 templateInfo.fileName = templateKey;
104 templateInfo.type = 'template';
106 const mappingKey = 'Templates/' + key + '-mapping.json';
107 if (cba.mapping && cba.mapping.files.has(mappingKey)) {
108 const obj = JSON.parse(cba.mapping.getValue(mappingKey));
109 templateInfo.mapping = obj;
110 templateInfo.fileName = mappingKey;
111 templateInfo.type += 'mapping';
113 this.templateStore.changeTemplateInfo(templateInfo);
115 this.sharedService.enableEdit();
119 getKeys(templateAndMappingMap: Map<string, TemplateAndMapping>) {
120 return Array.from(this.templateAndMappingMap.keys());
123 getValue(file: string) {
124 return this.templateAndMappingMap.get(file);
128 this.fileToDelete = file;
131 console.log(this.templateAndMappingMap);
132 this.templateAndMappingMap.delete(this.fileToDelete);
133 if (this.templateAndMappingMap.size <= 0) {
134 this.openCreationView();