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 import { TourService } from 'ngx-tour-md-menu';
12 selector: 'app-templ-mapp-listing',
13 templateUrl: './templ-mapp-listing.component.html',
14 styleUrls: ['./templ-mapp-listing.component.css']
16 export class TemplMappListingComponent implements OnInit {
17 @Output() showCreationView = new EventEmitter<any>();
18 @Output() showListView = new EventEmitter<any>();
19 templateAndMappingMap = new Map<string, TemplateAndMapping>();
25 fileToDelete: any = {};
28 private packageCreationStore: PackageCreationStore,
29 private templateStore: TemplateStore,
30 private route: ActivatedRoute,
31 private sharedService: SharedService,
32 private tourService: TourService,
38 if (this.route.snapshot.paramMap.has('id')) {
39 this.isCreate = false;
40 this.sharedService.isEdit().subscribe(res => {
45 this.packageCreationStore.state$.subscribe(cba => {
46 if (this.packageCreationStore.state.mapping.files.size > 0 || this.packageCreationStore.state.templates.files.size > 0) {
49 this.openCreationView();
52 this.templates = cba.templates;
53 this.mapping = cba.mapping;
54 console.log(this.mapping);
55 let templateAndMapping;
56 this.templateAndMappingMap.clear();
57 this.templates.files.forEach((value, key) => {
58 templateAndMapping = new TemplateAndMapping();
59 templateAndMapping.isTemplate = true;
60 const isFromTemplate = true;
61 this.setIsMappingOrTemplate(key, templateAndMapping, isFromTemplate);
63 this.mapping.files.forEach((value, key) => {
64 templateAndMapping = new TemplateAndMapping();
65 templateAndMapping.isMapping = true;
66 const isFromTemplate = false;
67 this.setIsMappingOrTemplate(key, templateAndMapping, isFromTemplate);
69 console.log(this.templateAndMappingMap);
71 this.deleteFromList();
75 private setIsMappingOrTemplate(key: string, templateAndMapping: TemplateAndMapping, isFromTemplate: boolean) {
76 const nameOfFile = key.split('/')[1].split('.')[0].split('-')[0];
77 // const fullName = nameOfFile + ',' + key.split('.');
78 if (this.templateAndMappingMap.has(nameOfFile)) {
79 const templateAndMappingExisted = this.templateAndMappingMap.get(nameOfFile);
80 !isFromTemplate ? templateAndMappingExisted.isMapping = true : templateAndMappingExisted.isTemplate = true;
81 this.templateAndMappingMap.set(nameOfFile, templateAndMappingExisted);
83 this.templateAndMappingMap.set(nameOfFile, templateAndMapping);
89 this.sharedService.listAction().subscribe(res => {
90 console.log('response from actionList');
93 console.log('xccccccccccvvvvvv');
94 this.templateAndMappingMap.delete(res);
95 if (this.templateAndMappingMap.size <= 0) {
96 this.openCreationView();
102 createNewTemplate() {
103 this.openCreationView();
104 this.sharedService.disableEdit();
105 this.tourService.goto('tm-templateName');
108 this.showCreationView.emit('tell parent to open create views');
109 console.log('disable edit mode');
112 console.log('open list view');
113 this.showListView.emit('show full view');
116 setSourceCodeEditor(key: string) {
117 this.currentFile = key;
118 const templateKey = 'Templates/' + key + '-template.vtl';
119 this.packageCreationStore.state$.subscribe(cba => {
120 console.log('cba ------');
123 console.log(this.templateAndMappingMap);
124 const templateInfo = new TemplateInfo();
125 if (cba.templates && cba.templates.files.has(templateKey)) {
126 const fileContent = cba.templates.getValue(templateKey.trim());
127 console.log(fileContent);
128 templateInfo.fileContent = fileContent;
129 templateInfo.fileName = templateKey;
130 templateInfo.type = 'template';
132 const mappingKey = 'Templates/' + key + '-mapping.json';
133 if (cba.mapping && cba.mapping.files.has(mappingKey)) {
134 const obj = JSON.parse(cba.mapping.getValue(mappingKey));
135 templateInfo.mapping = obj;
136 templateInfo.fileName = mappingKey;
137 templateInfo.type += 'mapping';
139 this.templateStore.changeTemplateInfo(templateInfo);
140 this.openCreationView();
141 this.sharedService.enableEdit();
145 getKeys(templateAndMappingMap: Map<string, TemplateAndMapping>) {
146 return Array.from(this.templateAndMappingMap.keys());
149 getValue(file: string) {
150 return this.templateAndMappingMap.get(file);
154 this.fileToDelete = file;
157 console.log(this.templateAndMappingMap);
158 this.templateAndMappingMap.delete(this.fileToDelete);
159 if (this.templateAndMappingMap.size <= 0) {
160 this.openCreationView();
162 // Delete from templates
163 this.packageCreationStore.state.templates.files.delete('Templates/' + this.fileToDelete + '-template.vtl');
164 // Delete from Mapping
165 this.packageCreationStore.state.mapping.files.delete('Templates/' + this.fileToDelete + '-mapping.json');