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() showCreationView = new EventEmitter<any>();
17 @Output() showListView = new EventEmitter<any>();
18 templateAndMappingMap = new Map<string, TemplateAndMapping>();
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 => {
43 if (this.packageCreationStore.state.mapping.files.size > 0 || this.packageCreationStore.state.templates.files.size > 0) {
46 this.openCreationView();
49 this.templates = cba.templates;
50 this.mapping = cba.mapping;
51 console.log(this.mapping);
52 let templateAndMapping;
53 this.templateAndMappingMap.clear();
54 this.templates.files.forEach((value, key) => {
55 templateAndMapping = new TemplateAndMapping();
56 templateAndMapping.isTemplate = true;
57 const isFromTemplate = true;
58 this.setIsMappingOrTemplate(key, templateAndMapping, isFromTemplate);
60 this.mapping.files.forEach((value, key) => {
61 templateAndMapping = new TemplateAndMapping();
62 templateAndMapping.isMapping = true;
63 const isFromTemplate = false;
64 this.setIsMappingOrTemplate(key, templateAndMapping, isFromTemplate);
66 console.log(this.templateAndMappingMap);
68 this.deleteFromList();
72 private setIsMappingOrTemplate(key: string, templateAndMapping: TemplateAndMapping, isFromTemplate: boolean) {
73 const nameOfFile = key.split('/')[1].split('.')[0].split('-')[0];
74 // const fullName = nameOfFile + ',' + key.split('.');
75 if (this.templateAndMappingMap.has(nameOfFile)) {
76 const templateAndMappingExisted = this.templateAndMappingMap.get(nameOfFile);
77 !isFromTemplate ? templateAndMappingExisted.isMapping = true : templateAndMappingExisted.isTemplate = true;
78 this.templateAndMappingMap.set(nameOfFile, templateAndMappingExisted);
80 this.templateAndMappingMap.set(nameOfFile, templateAndMapping);
86 this.sharedService.listAction().subscribe(res => {
87 console.log('response from actionList');
90 console.log('xccccccccccvvvvvv');
91 this.templateAndMappingMap.delete(res);
92 if (this.templateAndMappingMap.size <= 0) {
93 this.openCreationView();
100 this.openCreationView();
101 this.sharedService.disableEdit();
104 this.showCreationView.emit('tell parent to open create views');
105 console.log('disable edit mode');
108 console.log('open list view');
109 this.showListView.emit('show full view');
112 setSourceCodeEditor(key: string) {
113 this.currentFile = key;
114 const templateKey = 'Templates/' + key + '-template.vtl';
115 this.packageCreationStore.state$.subscribe(cba => {
116 console.log('cba ------');
119 console.log(this.templateAndMappingMap);
120 const templateInfo = new TemplateInfo();
121 if (cba.templates && cba.templates.files.has(templateKey)) {
122 const fileContent = cba.templates.getValue(templateKey.trim());
123 console.log(fileContent);
124 templateInfo.fileContent = fileContent;
125 templateInfo.fileName = templateKey;
126 templateInfo.type = 'template';
128 const mappingKey = 'Templates/' + key + '-mapping.json';
129 if (cba.mapping && cba.mapping.files.has(mappingKey)) {
130 const obj = JSON.parse(cba.mapping.getValue(mappingKey));
131 templateInfo.mapping = obj;
132 templateInfo.fileName = mappingKey;
133 templateInfo.type += 'mapping';
135 this.templateStore.changeTemplateInfo(templateInfo);
136 this.openCreationView();
137 this.sharedService.enableEdit();
141 getKeys(templateAndMappingMap: Map<string, TemplateAndMapping>) {
142 return Array.from(this.templateAndMappingMap.keys());
145 getValue(file: string) {
146 return this.templateAndMappingMap.get(file);
150 this.fileToDelete = file;
153 console.log(this.templateAndMappingMap);
154 this.templateAndMappingMap.delete(this.fileToDelete);
155 if (this.templateAndMappingMap.size <= 0) {
156 this.openCreationView();
158 // Delete from templates
159 this.packageCreationStore.state.templates.files.delete('Templates/' + this.fileToDelete + '-template.vtl');
160 // Delete from Mapping
161 this.packageCreationStore.state.mapping.files.delete('Templates/' + this.fileToDelete + '-mapping.json');