0c47b3bbc7dffbc9f8483a88ef654e3e37a9f680
[ccsdk/cds.git] /
1 import { Component, OnDestroy, OnInit } from '@angular/core';
2 import { DesignerStore } from '../designer.store';
3 import { PackageCreationStore } from '../../package-creation/package-creation.store';
4 import { Subject } from 'rxjs';
5 import { distinctUntilChanged, takeUntil } from 'rxjs/operators';
6 import { CBAPackage } from '../../package-creation/mapping-models/CBAPacakge.model';
7 import { TemplateAndMapping } from '../../package-creation/template-mapping/TemplateAndMapping';
8
9 @Component({
10     selector: 'app-functions-attribute',
11     templateUrl: './functions-attribute.component.html',
12     styleUrls: ['./functions-attribute.component.css']
13 })
14 export class FunctionsAttributeComponent implements OnInit, OnDestroy {
15
16     ngUnsubscribe = new Subject();
17     designerDashboardState: DecodeSuccessCallback;
18     cbaPackage: CBAPackage;
19     templateAndMappingMap = new Map<string, TemplateAndMapping>();
20     selectedTemplates = new Map<string, TemplateAndMapping>();
21     fileToDelete: string;
22
23     constructor(
24         private designerStore: DesignerStore,
25         private packageCreationStore: PackageCreationStore
26     ) {
27     }
28
29     ngOnInit() {
30         this.designerStore.state$
31             .pipe(
32                 distinctUntilChanged((a: any, b: any) => JSON.stringify(a) === JSON.stringify(b)),
33                 takeUntil(this.ngUnsubscribe))
34             .subscribe(designerDashboardState => {
35                 this.designerDashboardState = designerDashboardState;
36             });
37
38         this.packageCreationStore.state$
39             .pipe(
40                 distinctUntilChanged((a: any, b: any) => JSON.stringify(a) === JSON.stringify(b)),
41                 takeUntil(this.ngUnsubscribe))
42             .subscribe(cbaPackage => {
43                 this.cbaPackage = cbaPackage;
44                 console.log('File name =>================== ');
45                 console.log(this.cbaPackage.templates.files);
46                 this.cbaPackage.templates.files.forEach((value, key) => {
47                     console.log('File name => ' + key);
48                     const templateAndMapping = new TemplateAndMapping();
49                     templateAndMapping.isTemplate = true;
50                     const isFromTemplate = true;
51                     this.setIsMappingOrTemplate(key, templateAndMapping, isFromTemplate);
52                 });
53
54                 this.cbaPackage.mapping.files.forEach((value, key) => {
55                     const templateAndMapping = new TemplateAndMapping();
56                     templateAndMapping.isMapping = true;
57                     const isFromTemplate = false;
58                     this.setIsMappingOrTemplate(key, templateAndMapping, isFromTemplate);
59                 });
60             });
61
62     }
63
64     ngOnDestroy() {
65         this.ngUnsubscribe.next();
66         this.ngUnsubscribe.complete();
67     }
68     // Template logic
69
70     private setIsMappingOrTemplate(key: string, templateAndMapping: TemplateAndMapping, isFromTemplate: boolean) {
71         const nameOfFile = isFromTemplate ?
72             key.split('/')[1].split('.')[0].split('-template')[0]
73             : key.split('/')[1].split('.')[0].split('-mapping')[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);
79         } else {
80             this.templateAndMappingMap.set(nameOfFile, templateAndMapping);
81         }
82
83     }
84
85     addTemplates() { }
86
87     setTemplate(file: string) {
88         if (this.selectedTemplates.has(file)) {
89             this.selectedTemplates.delete(file);
90         } else {
91             this.selectedTemplates.set(file, this.templateAndMappingMap.get(file));
92         }
93         console.log(this.selectedTemplates);
94     }
95
96     getKeys(templateAndMappingMap: Map<string, TemplateAndMapping>) {
97         return Array.from(templateAndMappingMap.keys());
98     }
99     getValue(file: string) {
100         return this.templateAndMappingMap.get(file);
101     }
102 }