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';
13 selector: 'app-templ-mapp-listing',
14 templateUrl: './templ-mapp-listing.component.html',
15 styleUrls: ['./templ-mapp-listing.component.css']
17 export class TemplMappListingComponent implements OnInit, OnDestroy {
18 @Output() showCreationView = new EventEmitter<any>();
19 @Output() showListView = new EventEmitter<any>();
20 templateAndMappingMap = new Map<string, TemplateAndMapping>();
29 private packageCreationStore: PackageCreationStore,
30 private templateStore: TemplateStore,
31 private route: ActivatedRoute,
32 private sharedService: SharedService,
33 private tourService: TourService,
38 // this.templateStore.unsubscribe();
39 // this.packageCreationStore.unsubscribe();
43 this.templateAndMappingMap = new Map<string, TemplateAndMapping>();
45 if (this.route.snapshot.paramMap.has('id')) {
46 this.isCreate = false;
47 this.sharedService.isEdit().subscribe(res => {
52 this.packageCreationStore.state$.subscribe(cba => {
53 if (this.packageCreationStore.state.mapping.files.size > 0 || this.packageCreationStore.state.templates.files.size > 0) {
56 this.openCreationView();
59 this.templates = cba.templates;
60 this.mapping = cba.mapping;
61 console.log(this.mapping);
62 let templateAndMapping;
63 this.templateAndMappingMap.clear();
64 this.templates.files.forEach((value, key) => {
65 templateAndMapping = new TemplateAndMapping();
66 templateAndMapping.isTemplate = true;
67 const isFromTemplate = true;
68 this.setIsMappingOrTemplate(key, templateAndMapping, isFromTemplate);
70 this.mapping.files.forEach((value, key) => {
71 templateAndMapping = new TemplateAndMapping();
72 templateAndMapping.isMapping = true;
73 const isFromTemplate = false;
74 this.setIsMappingOrTemplate(key, templateAndMapping, isFromTemplate);
76 console.log(this.templateAndMappingMap);
78 this.deleteFromList();
82 private setIsMappingOrTemplate(key: string, templateAndMapping: TemplateAndMapping, isFromTemplate: boolean) {
83 const nameOfFile = isFromTemplate ?
84 key.split('/')[1].split('.')[0].split('-template')[0]
85 : key.split('/')[1].split('.')[0].split('-mapping')[0];
86 // const fullName = nameOfFile + ',' + key.split('.');
87 if (this.templateAndMappingMap.has(nameOfFile)) {
88 const templateAndMappingExisted = this.templateAndMappingMap.get(nameOfFile);
89 !isFromTemplate ? templateAndMappingExisted.isMapping = true : templateAndMappingExisted.isTemplate = true;
90 this.templateAndMappingMap.set(nameOfFile, templateAndMappingExisted);
92 this.templateAndMappingMap.set(nameOfFile, templateAndMapping);
98 this.sharedService.listAction().subscribe(res => {
99 console.log('response from actionList');
102 console.log('xccccccccccvvvvvv');
103 this.templateAndMappingMap.delete(res);
104 if (this.templateAndMappingMap.size <= 0) {
105 this.openCreationView();
111 createNewTemplate() {
112 this.openCreationView();
113 this.sharedService.disableEdit();
114 if (localStorage.getItem('tour-guide') !== 'end' && localStorage.getItem('tour-guide') !== 'false') {
115 this.tourService.goto('tm-templateName');
120 this.showCreationView.emit('tell parent to open create views');
121 console.log('disable edit mode');
125 console.log('open list view');
126 this.showListView.emit('show full view');
129 setSourceCodeEditor(key: string) {
130 this.currentFile = key;
131 const templateKey = 'Templates/' + key + '-template';
132 this.packageCreationStore.state$.subscribe(cba => {
133 console.log('cba ------');
136 console.log(this.templateAndMappingMap);
137 const templateInfo = new TemplateInfo();
138 // tslint:disable-next-line: forin
139 for (const templateType in TemplateType) {
140 const fileName = templateKey + '.' + TemplateType[templateType];
141 if (cba.templates && cba.templates.files.has(fileName)) {
142 const fileContent = cba.templates.getValue(fileName.trim());
143 console.log(templateType + '......ccccccc.... ' + fileName);
144 templateInfo.fileContent = fileContent;
145 templateInfo.fileName = fileName;
146 templateInfo.ext = TemplateType[templateType];
147 templateInfo.type = 'template';
151 const mappingKey = 'Templates/' + key + '-mapping.json';
152 if (cba.mapping && cba.mapping.files.has(mappingKey)) {
153 const obj = JSON.parse(cba.mapping.getValue(mappingKey));
154 templateInfo.mapping = obj;
155 templateInfo.fileName = mappingKey;
156 templateInfo.type += 'mapping';
158 this.templateStore.changeTemplateInfo(templateInfo);
159 this.openCreationView();
160 if (templateInfo.fileName && templateInfo.fileName.length > 0) {
161 this.sharedService.enableEdit();
166 getKeys(templateAndMappingMap: Map<string, TemplateAndMapping>) {
167 return Array.from(this.templateAndMappingMap.keys());
170 getValue(file: string) {
171 return this.templateAndMappingMap.get(file);
176 const templateKey = 'Templates/' + file + '-template';
177 const mappingKey = 'Templates/' + file + '-mapping.json';
178 // tslint:disable-next-line: forin
179 for (const templateType in TemplateType) {
180 const fileName = templateKey + '.' + TemplateType[templateType];
181 if (this.packageCreationStore.state.templates.files.has(fileName)) {
182 this.fileToDelete = fileName;
186 if (this.fileToDelete.length <= 0) {
187 // tslint:disable-next-line: forin
188 if (this.packageCreationStore.state.mapping.files.has(mappingKey)) {
189 this.fileToDelete = mappingKey;
197 const fullName = this.fileToDelete.split('/')[1];
198 console.log(this.fileToDelete + ' -- ' + fullName);
199 const file = fullName.substr(0, fullName.lastIndexOf('-'));
200 const ext = fullName.substr(fullName.lastIndexOf('.') + 1);
201 this.templateAndMappingMap.delete(file);
202 if (this.templateAndMappingMap.size <= 0) {
203 this.openCreationView();
205 // Delete from templates
206 this.packageCreationStore.state.templates.files.delete('Templates/' + file + '-template.' + ext);
207 // Delete from Mapping
208 this.packageCreationStore.state.mapping.files.delete('Templates/' + file + '-mapping.json');
209 console.log(this.templateAndMappingMap);