1 import { Component, EventEmitter, OnDestroy, 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, OnDestroy {
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,
39 // this.templateStore.unsubscribe();
40 // this.packageCreationStore.unsubscribe();
44 if (this.route.snapshot.paramMap.has('id')) {
45 this.isCreate = false;
46 this.sharedService.isEdit().subscribe(res => {
51 this.packageCreationStore.state$.subscribe(cba => {
52 if (this.packageCreationStore.state.mapping.files.size > 0 || this.packageCreationStore.state.templates.files.size > 0) {
55 this.openCreationView();
58 this.templates = cba.templates;
59 this.mapping = cba.mapping;
60 console.log(this.mapping);
61 let templateAndMapping;
62 this.templateAndMappingMap.clear();
63 this.templates.files.forEach((value, key) => {
64 templateAndMapping = new TemplateAndMapping();
65 templateAndMapping.isTemplate = true;
66 const isFromTemplate = true;
67 this.setIsMappingOrTemplate(key, templateAndMapping, isFromTemplate);
69 this.mapping.files.forEach((value, key) => {
70 templateAndMapping = new TemplateAndMapping();
71 templateAndMapping.isMapping = true;
72 const isFromTemplate = false;
73 this.setIsMappingOrTemplate(key, templateAndMapping, isFromTemplate);
75 console.log(this.templateAndMappingMap);
77 this.deleteFromList();
81 private setIsMappingOrTemplate(key: string, templateAndMapping: TemplateAndMapping, isFromTemplate: boolean) {
82 const nameOfFile = key.split('/')[1].split('.')[0].split('-')[0];
83 // const fullName = nameOfFile + ',' + key.split('.');
84 if (this.templateAndMappingMap.has(nameOfFile)) {
85 const templateAndMappingExisted = this.templateAndMappingMap.get(nameOfFile);
86 !isFromTemplate ? templateAndMappingExisted.isMapping = true : templateAndMappingExisted.isTemplate = true;
87 this.templateAndMappingMap.set(nameOfFile, templateAndMappingExisted);
89 this.templateAndMappingMap.set(nameOfFile, templateAndMapping);
95 this.sharedService.listAction().subscribe(res => {
96 console.log('response from actionList');
99 console.log('xccccccccccvvvvvv');
100 this.templateAndMappingMap.delete(res);
101 if (this.templateAndMappingMap.size <= 0) {
102 this.openCreationView();
108 createNewTemplate() {
109 this.openCreationView();
110 this.sharedService.disableEdit();
111 if (localStorage.getItem('tour-guide') !== 'end' && localStorage.getItem('tour-guide') !== 'false') {
112 this.tourService.goto('tm-templateName');
116 this.showCreationView.emit('tell parent to open create views');
117 console.log('disable edit mode');
120 console.log('open list view');
121 this.showListView.emit('show full view');
124 setSourceCodeEditor(key: string) {
125 this.currentFile = key;
126 const templateKey = 'Templates/' + key + '-template';
127 this.packageCreationStore.state$.subscribe(cba => {
128 console.log('cba ------');
131 console.log(this.templateAndMappingMap);
132 const templateInfo = new TemplateInfo();
133 // tslint:disable-next-line: forin
134 for (const templateType in TemplateType) {
135 const fileName = templateKey + '.' + TemplateType[templateType];
136 if (cba.templates && cba.templates.files.has(fileName)) {
137 const fileContent = cba.templates.getValue(fileName.trim());
138 console.log(templateType + '......ccccccc.... ' + fileName);
139 templateInfo.fileContent = fileContent;
140 templateInfo.fileName = fileName;
141 templateInfo.ext = TemplateType[templateType];
142 templateInfo.type = 'template';
146 const mappingKey = 'Templates/' + key + '-mapping.json';
147 if (cba.mapping && cba.mapping.files.has(mappingKey)) {
148 const obj = JSON.parse(cba.mapping.getValue(mappingKey));
149 templateInfo.mapping = obj;
150 templateInfo.fileName = mappingKey;
151 templateInfo.type += 'mapping';
153 this.templateStore.changeTemplateInfo(templateInfo);
154 this.openCreationView();
155 this.sharedService.enableEdit();
159 getKeys(templateAndMappingMap: Map<string, TemplateAndMapping>) {
160 return Array.from(this.templateAndMappingMap.keys());
163 getValue(file: string) {
164 return this.templateAndMappingMap.get(file);
168 const templateKey = 'Templates/' + file + '-template';
169 // tslint:disable-next-line: forin
170 for (const templateType in TemplateType) {
171 const fileName = templateKey + '.' + TemplateType[templateType];
172 if (this.packageCreationStore.state.templates.files.has(fileName)) {
173 this.fileToDelete = fileName;
180 const file = this.fileToDelete.split('/')[1].split('-')[0];
181 const ext = this.fileToDelete.split('/')[1].split('.')[1];
182 this.templateAndMappingMap.delete(file);
183 if (this.templateAndMappingMap.size <= 0) {
184 this.openCreationView();
186 // Delete from templates
187 this.packageCreationStore.state.templates.files.delete('Templates/' + file + '-template.' + ext);
188 // Delete from Mapping
189 this.packageCreationStore.state.mapping.files.delete('Templates/' + file + '-mapping.json');
190 console.log(this.templateAndMappingMap);