25b81994a11e4f3b41ec483ccfd43cbea88044ea
[ccsdk/cds.git] /
1 /*
2 * ============LICENSE_START=======================================================
3 * ONAP : CDS
4 * ================================================================================
5 * Copyright (C) 2020 TechMahindra
6 *=================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 *     http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
19 */
20 import { Component, OnInit } from '@angular/core';
21 import { CdkDragDrop, moveItemInArray, transferArrayItem } from '@angular/cdk/drag-drop';
22 import { SourcesStore } from './sources.store';
23 import { DictionaryCreationService } from '../dictionary-creation.service';
24 import { DictionaryCreationStore } from '../dictionary-creation.store';
25 import { MetaData } from '../../model/metaData.model';
26
27 @Component({
28   selector: 'app-sources-template',
29   templateUrl: './sources-template.component.html',
30   styleUrls: ['./sources-template.component.css']
31 })
32 export class SourcesTemplateComponent implements OnInit {
33   private searchQuery = '';
34   lang = 'json';
35   sources = {};
36   option = [];
37   textValue: any;
38   selectItem: boolean;
39   // ddSource = [];
40   checked: boolean;
41   searchText = '';
42   text = '';
43   selectedArray = [];
44   metaDataTab: MetaData = new MetaData();
45
46   tempSources = new Map();
47   toDeleteSource = new Map<string, any>();
48   sourcesOptions = new Map<string, any>();
49
50   constructor(
51     private sourcesStore: SourcesStore,
52     private dictionaryCreationService: DictionaryCreationService,
53     private dictionaryCreationStore: DictionaryCreationStore
54   ) {
55
56   }
57
58   ngOnInit() {
59     //  this.sourcesStore.getAllSources();
60     this.dictionaryCreationService.getSources().subscribe(sources => {
61       // console.log(sources);
62       this.sources = sources[0];
63       // this.sources = {
64       //   "input": "source-input", "rest": "source-rest", "default": "source-default", "capability": "source-capability",
65       //   "sdnc": "source-rest", "vault-data": "source-rest", "processor-db": "source-db", "aai-data": "source-rest",
66       //   "script": "source-capability"
67       // };
68       for (const key in this.sources) {
69         if (key) {
70           console.log(key + ' - ' + this.sources[key]);
71           const sourceObj = { name: key, value: this.sources[key] };
72           this.option.push(sourceObj);
73         }
74       }
75
76     });
77
78     this.dictionaryCreationStore.state$.subscribe(element => {
79       console.log('################');
80       console.log(this.metaDataTab);
81       if (element && element.metaData) {
82         this.metaDataTab = element.metaData;
83         this.addSourcesInUI();
84       }
85     });
86   }
87
88   addSourcesInUI() {
89     // add sources from json to UI
90     const originalSources = this.sourcesOptions;
91     // for (const key of this.sourcesOptions) {
92     // this.sourcesOptions;
93     for (const source in this.metaDataTab.sources) {
94       if (source) {
95         console.log(source);
96       }
97     }
98   }
99
100   saveSorcesDataToStore() {
101     const ddSource = [];
102     for (const key of this.sourcesOptions.keys()) {
103       ddSource.push(this.sourcesOptions.get(key));
104     }
105     console.log(ddSource);
106     this.metaDataTab.sources = {};
107     for (const obj of ddSource) {
108       this.metaDataTab.sources = { ...this.metaDataTab.sources, ...obj };
109     }
110     //  this.metaDataTab.sources = { ...this.ddSource }
111     console.log(this.metaDataTab.sources);
112     this.dictionaryCreationStore.changeMetaData(this.metaDataTab);
113   }
114
115   setTempSources(event, item) {
116     console.log(event.target.checked);
117     if (event.target.checked) {
118       this.tempSources.set(item.name, item);
119     } else {
120       this.tempSources.delete(item.name);
121     }
122     console.log(this.tempSources);
123   }
124
125   selectAll() {
126     if (this.tempSources.size === 0) {
127       for (const option of this.option) {
128         if (!this.sourcesOptions.has(option.name)) {
129           this.tempSources.set(option.name, option);
130         }
131       }
132     } else {
133       this.tempSources = new Map<string, any>();
134     }
135   }
136
137   setToDeleteSources(event, item) {
138     console.log(event.target.checked);
139     if (event.target.checked) {
140       this.toDeleteSource.set(item.key, item);
141     } else {
142       this.toDeleteSource.delete(item.key);
143     }
144
145   }
146
147   addSources() {
148     //  this.tempSources
149     const originalSources = this.tempSources;
150     for (const key of originalSources.keys()) {
151       /* tslint:disable:no-string-literal */
152       this.sourcesOptions.set(key, {
153         [key]: {
154           type: originalSources.get(key).value,
155           properties: {}
156         }
157       });
158
159     }
160
161     console.log(this.sourcesOptions);
162     this.tempSources.clear();
163     this.saveSorcesDataToStore();
164   }
165
166   deleteSource() {
167     console.log(this.toDeleteSource);
168     for (const key of this.toDeleteSource.keys()) {
169       console.log(key);
170       this.sourcesOptions.delete(key);
171     }
172     this.toDeleteSource = new Map<string, any>();
173     this.saveSorcesDataToStore();
174   }
175
176   // drop(event: CdkDragDrop<string[]>) {
177   //   console.log('-------------');
178   //   // console.log(event);
179
180   //   if (event.previousContainer === event.container) {
181   //     moveItemInArray(event.container.data, event.previousIndex, event.currentIndex);
182   //   } else {
183   //     transferArrayItem(event.previousContainer.data,
184   //       event.container.data,
185   //       event.previousIndex,
186   //       event.currentIndex);
187   //   }
188
189   //   console.log(this.sourcesOptions);
190   //   console.log(this.sources);
191
192   //   this.ddSource = [];
193
194   //   const originalSources = this.sourcesOptions;
195   //   for (const key of originalSources) {
196   //     /* tslint:disable:no-string-literal */
197   //     this.ddSource.push({
198   //       [key.name]: {
199   //         type: key.value,
200   //         properties: {}
201   //       }
202   //     });
203
204   //   }
205
206   //   this.saveSorcesDataToStore();
207   // }
208
209   getMapKey(map) {
210     return Object.assign({}, map.keys());
211   }
212
213   searchDictionary(event: any) {
214     this.searchQuery = event.target.value;
215     this.searchQuery = this.searchQuery.trim();
216     console.log(this.searchQuery);
217     // this.dictionaryStore.search(this.searchQuery);
218   }
219
220   onChange(item: string, isChecked: boolean) {
221     if (isChecked) {
222       this.selectedArray.push(item);
223     }
224   }
225
226   textChanged(event, item) {
227     // const editedData = JSON.parse(event);
228     // const originalSources = this.sources;
229     // for (const key in originalSources) {
230     //   if (key === item.name) {
231     //     this.sources[key] = editedData;
232     //   }
233     // }
234     // this.option = [];
235     // this.sourcesStore.changeSources(this.sources);
236
237     console.log(item);
238     console.log(event);
239
240     // this.metaDataTab.sources[item.name].properties = editedData;
241   }
242
243 }