enable 2-way binding between metadata and editor tabs
[ccsdk/cds.git] / cds-ui / designer-client / src / app / modules / feature-modules / resource-dictionary / resource-dictionary-creation / sources-template / sources-template.component.ts
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
25 @Component({
26   selector: 'app-sources-template',
27   templateUrl: './sources-template.component.html',
28   styleUrls: ['./sources-template.component.css']
29 })
30 export class SourcesTemplateComponent implements OnInit {
31   private searchQuery = '';
32   lang = 'json';
33   sources = {};
34   option = [];
35   sourcesOptions = [];
36   textValue: any;
37   selectItem: boolean;
38   ddSource = [];
39   checked: boolean;
40   searchText = '';
41   text = '';
42   selectedArray = [];
43   constructor(
44     private sourcesStore: SourcesStore,
45     private dictionaryCreationService: DictionaryCreationService
46   ) {
47
48   }
49
50   ngOnInit() {
51     //  this.sourcesStore.getAllSources();
52     this.dictionaryCreationService.getSources().subscribe(sources => {
53       // console.log(sources);
54       this.sources = sources[0];
55       // this.sources = {
56       //   "input": "source-input", "rest": "source-rest", "default":"source-default", "capability": "source-capability",
57       // "sdnc": "source-rest", "vault-data": "source-rest", "processor-db": "source-db", "aai-data": "source-rest",
58       // "script": "source-capability"
59       // };
60       for (const key in this.sources) {
61         if (key) {
62           console.log(key + ' - ' + this.sources[key]);
63           const sourceObj = { name: key, value: this.sources[key] };
64           this.option.push(sourceObj);
65         }
66       }
67
68     });
69   }
70
71   saveSorcesDataToStore() {
72     this.sourcesStore.saveSources(this.ddSource);
73   }
74
75   drop(event: CdkDragDrop<string[]>) {
76     console.log('-------------');
77     console.log(event);
78     this.ddSource = [];
79     if (event.previousContainer === event.container) {
80       moveItemInArray(event.container.data, event.previousIndex, event.currentIndex);
81     } else {
82       transferArrayItem(event.previousContainer.data,
83         event.container.data,
84         event.previousIndex,
85         event.currentIndex);
86     }
87
88     for (const key2 in this.sources) {
89       if (key2) {
90         const originalSources = this.sourcesOptions;
91         for (const key of originalSources) {
92           /* tslint:disable:no-string-literal */
93           if (key.name === key2['name']) {
94             const obj = `{${key.name}: ${key.value}}`;
95             this.ddSource.push(obj);
96           }
97         }
98       }
99     }
100   }
101
102   searchDictionary(event: any) {
103     this.searchQuery = event.target.value;
104     this.searchQuery = this.searchQuery.trim();
105     console.log(this.searchQuery);
106     // this.dictionaryStore.search(this.searchQuery);
107   }
108
109   onChange(item: string, isChecked: boolean) {
110     if (isChecked) {
111       this.selectedArray.push(item);
112     }
113   }
114
115   textChanged(event, item) {
116     const editedData = JSON.parse(event);
117     const originalSources = this.sources;
118     for (const key in originalSources) {
119       if (key === item.name) {
120         this.sources[key] = editedData;
121       }
122     }
123     this.option = [];
124     this.sourcesStore.changeSources(this.sources);
125   }
126
127 }