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';
9 import { TemplateType } from '../utils/TemplateType';
10 import { of } from 'rxjs';
14 selector: 'app-templ-mapp-listing',
15 templateUrl: './templ-mapp-listing.component.html',
16 styleUrls: ['./templ-mapp-listing.component.css']
18 export class TemplMappListingComponent implements OnInit {
19 @Output() showCreationView = new EventEmitter<any>();
20 @Output() showListView = new EventEmitter<any>();
21 templateAndMappingMap = new Map<string, TemplateAndMapping>();
30 private packageCreationStore: PackageCreationStore,
31 private templateStore: TemplateStore,
32 private route: ActivatedRoute,
33 private sharedService: SharedService,
34 private tourService: TourService,
40 if (this.route.snapshot.paramMap.has('id')) {
41 this.isCreate = false;
42 this.sharedService.isEdit().subscribe(res => {
47 this.packageCreationStore.state$.subscribe(cba => {
48 if (this.packageCreationStore.state.mapping.files.size > 0 || this.packageCreationStore.state.templates.files.size > 0) {
51 this.openCreationView();
54 this.templates = cba.templates;
55 this.mapping = cba.mapping;
56 console.log(this.mapping);
57 let templateAndMapping;
58 this.templateAndMappingMap.clear();
59 this.templates.files.forEach((value, key) => {
60 templateAndMapping = new TemplateAndMapping();
61 templateAndMapping.isTemplate = true;
62 const isFromTemplate = true;
63 this.setIsMappingOrTemplate(key, templateAndMapping, isFromTemplate);
65 this.mapping.files.forEach((value, key) => {
66 templateAndMapping = new TemplateAndMapping();
67 templateAndMapping.isMapping = true;
68 const isFromTemplate = false;
69 this.setIsMappingOrTemplate(key, templateAndMapping, isFromTemplate);
71 console.log(this.templateAndMappingMap);
73 this.deleteFromList();
77 private setIsMappingOrTemplate(key: string, templateAndMapping: TemplateAndMapping, isFromTemplate: boolean) {
78 const nameOfFile = key.split('/')[1].split('.')[0].split('-')[0];
79 // const fullName = nameOfFile + ',' + key.split('.');
80 if (this.templateAndMappingMap.has(nameOfFile)) {
81 const templateAndMappingExisted = this.templateAndMappingMap.get(nameOfFile);
82 !isFromTemplate ? templateAndMappingExisted.isMapping = true : templateAndMappingExisted.isTemplate = true;
83 this.templateAndMappingMap.set(nameOfFile, templateAndMappingExisted);
85 this.templateAndMappingMap.set(nameOfFile, templateAndMapping);
91 this.sharedService.listAction().subscribe(res => {
92 console.log('response from actionList');
95 console.log('xccccccccccvvvvvv');
96 this.templateAndMappingMap.delete(res);
97 if (this.templateAndMappingMap.size <= 0) {
98 this.openCreationView();
104 createNewTemplate() {
105 this.openCreationView();
106 this.sharedService.disableEdit();
107 if (localStorage.getItem('tour-guide') !== 'end' && localStorage.getItem('tour-guide') !== 'false') {
108 this.tourService.goto('tm-templateName');
112 this.showCreationView.emit('tell parent to open create views');
113 console.log('disable edit mode');
116 console.log('open list view');
117 this.showListView.emit('show full view');
120 setSourceCodeEditor(key: string) {
121 this.currentFile = key;
122 const templateKey = 'Templates/' + key + '-template';
123 this.packageCreationStore.state$.subscribe(cba => {
124 console.log('cba ------');
127 console.log(this.templateAndMappingMap);
128 const templateInfo = new TemplateInfo();
129 // tslint:disable-next-line: forin
130 for (const templateType in TemplateType) {
131 const fileName = templateKey + '.' + TemplateType[templateType];
132 if (cba.templates && cba.templates.files.has(fileName)) {
133 const fileContent = cba.templates.getValue(fileName.trim());
134 console.log(templateType + '......ccccccc.... ' + fileName);
135 templateInfo.fileContent = fileContent;
136 templateInfo.fileName = fileName;
137 templateInfo.ext = TemplateType[templateType];
138 templateInfo.type = 'template';
142 const mappingKey = 'Templates/' + key + '-mapping.json';
143 if (cba.mapping && cba.mapping.files.has(mappingKey)) {
144 const obj = JSON.parse(cba.mapping.getValue(mappingKey));
145 templateInfo.mapping = obj;
146 templateInfo.fileName = mappingKey;
147 templateInfo.type += 'mapping';
149 this.templateStore.changeTemplateInfo(templateInfo);
150 this.openCreationView();
151 this.sharedService.enableEdit();
155 getKeys(templateAndMappingMap: Map<string, TemplateAndMapping>) {
156 return Array.from(this.templateAndMappingMap.keys());
159 getValue(file: string) {
160 return this.templateAndMappingMap.get(file);
164 const templateKey = 'Templates/' + file + '-template';
165 // tslint:disable-next-line: forin
166 for (const templateType in TemplateType) {
167 const fileName = templateKey + '.' + TemplateType[templateType];
168 if (this.packageCreationStore.state.templates.files.has(fileName)) {
169 this.fileToDelete = fileName;
176 const file = this.fileToDelete.split('/')[1].split('-')[0];
177 const ext = this.fileToDelete.split('/')[1].split('.')[1];
178 this.templateAndMappingMap.delete(file);
179 if (this.templateAndMappingMap.size <= 0) {
180 this.openCreationView();
182 // Delete from templates
183 this.packageCreationStore.state.templates.files.delete('Templates/' + file + '-template.' + ext);
184 // Delete from Mapping
185 this.packageCreationStore.state.mapping.files.delete('Templates/' + file + '-mapping.json');
186 console.log(this.templateAndMappingMap);