1a950163e87923b8af18105124e57118e3206dd4
[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   sourcesOptions = [];
38   textValue: any;
39   selectItem: boolean;
40   ddSource = [];
41   checked: boolean;
42   searchText = '';
43   text = '';
44   selectedArray = [];
45   metaDataTab: MetaData = new MetaData();
46
47   constructor(
48     private sourcesStore: SourcesStore,
49     private dictionaryCreationService: DictionaryCreationService,
50     private dictionaryCreationStore: DictionaryCreationStore
51   ) {
52
53   }
54
55   ngOnInit() {
56     //  this.sourcesStore.getAllSources();
57     this.dictionaryCreationService.getSources().subscribe(sources => {
58       // console.log(sources);
59       this.sources = sources[0];
60       // this.sources = {
61       //   "input": "source-input", "rest": "source-rest", "default":"source-default", "capability": "source-capability",
62       // "sdnc": "source-rest", "vault-data": "source-rest", "processor-db": "source-db", "aai-data": "source-rest",
63       // "script": "source-capability"
64       // };
65       for (const key in this.sources) {
66         if (key) {
67           console.log(key + ' - ' + this.sources[key]);
68           const sourceObj = { name: key, value: this.sources[key] };
69           this.option.push(sourceObj);
70         }
71       }
72
73     });
74
75     this.dictionaryCreationStore.state$.subscribe(element => {
76       console.log('################');
77       console.log(this.metaDataTab);
78       if (element && element.metaData) {
79         this.metaDataTab = element.metaData;
80         this.addSourcesInUI();
81       }
82     });
83   }
84
85   addSourcesInUI() {
86     // add sources from json to UI
87     const originalSources = this.sourcesOptions;
88     // for (const key of this.sourcesOptions) {
89     // this.sourcesOptions;
90     for (const source in this.metaDataTab.sources) {
91       if (source) {
92         console.log(source);
93       }
94     }
95   }
96
97   saveSorcesDataToStore() {
98     console.log(this.ddSource);
99     this.metaDataTab.sources = {};
100     for (const obj of this.ddSource) {
101       this.metaDataTab.sources = { ...this.metaDataTab.sources, ...obj };
102     }
103     //  this.metaDataTab.sources = { ...this.ddSource }
104     console.log(this.metaDataTab.sources);
105     this.dictionaryCreationStore.changeMetaData(this.metaDataTab);
106   }
107
108   drop(event: CdkDragDrop<string[]>) {
109     console.log('-------------');
110     // console.log(event);
111
112     if (event.previousContainer === event.container) {
113       moveItemInArray(event.container.data, event.previousIndex, event.currentIndex);
114     } else {
115       transferArrayItem(event.previousContainer.data,
116         event.container.data,
117         event.previousIndex,
118         event.currentIndex);
119     }
120
121     console.log(this.sourcesOptions);
122     console.log(this.sources);
123
124     this.ddSource = [];
125
126     const originalSources = this.sourcesOptions;
127     for (const key of originalSources) {
128       /* tslint:disable:no-string-literal */
129       this.ddSource.push({
130         [key.name]: {
131           type: key.value,
132           properties: {}
133         }
134       });
135
136     }
137
138     this.saveSorcesDataToStore();
139   }
140
141   searchDictionary(event: any) {
142     this.searchQuery = event.target.value;
143     this.searchQuery = this.searchQuery.trim();
144     console.log(this.searchQuery);
145     // this.dictionaryStore.search(this.searchQuery);
146   }
147
148   onChange(item: string, isChecked: boolean) {
149     if (isChecked) {
150       this.selectedArray.push(item);
151     }
152   }
153
154   textChanged(event, item) {
155     // const editedData = JSON.parse(event);
156     // const originalSources = this.sources;
157     // for (const key in originalSources) {
158     //   if (key === item.name) {
159     //     this.sources[key] = editedData;
160     //   }
161     // }
162     // this.option = [];
163     // this.sourcesStore.changeSources(this.sources);
164
165     console.log(item);
166     console.log(event);
167
168     // this.metaDataTab.sources[item.name].properties = editedData;
169   }
170
171 }