1b70a17efbb8f76ead327706cae7ecb3f5a59b30
[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
24 @Component({
25   selector: 'app-sources-template',
26   templateUrl: './sources-template.component.html',
27   styleUrls: ['./sources-template.component.css']
28 })
29 export class SourcesTemplateComponent implements OnInit {
30   private searchQuery = '';
31   lang = 'json';
32   sources = [];
33   option = [];
34   sourcesOptions = [];
35   textValue: any;
36   selectItem: boolean;
37   ddSource = [];
38   checked: boolean;
39   searchText = '';
40   text = '';
41   selectedArray = [];
42   constructor(private sourcesStore: SourcesStore) {
43     this.sourcesStore.state$.subscribe(sources => {
44       this.sources = sources.sources;
45       for (const key in this.sources) {
46         if (key) {
47           const sourceObj = { name: key, value: JSON.stringify(this.sources[key]) };
48           this.option.push(sourceObj);
49         }
50       }
51     });
52   }
53
54   ngOnInit() {
55     this.sourcesStore.getAllSources();
56   }
57
58   saveSorcesDataToStore() {
59     this.sourcesStore.saveSources(this.ddSource);
60   }
61
62   drop(event: CdkDragDrop<string[]>) {
63     this.ddSource = [];
64     if (event.previousContainer === event.container) {
65       moveItemInArray(event.container.data, event.previousIndex, event.currentIndex);
66     } else {
67       transferArrayItem(event.previousContainer.data,
68         event.container.data,
69         event.previousIndex,
70         event.currentIndex);
71     }
72
73     for (const key2 in this.sources) {
74       if (key2) {
75         const originalSources = this.sourcesOptions;
76         for (const key of originalSources) {
77           if (key.name === key2) {
78             const obj = `{${key.name}: ${key.value}}`;
79             this.ddSource.push(obj);
80           }
81         }
82       }
83     }
84   }
85
86   searchDictionary(event: any) {
87     this.searchQuery = event.target.value;
88     this.searchQuery = this.searchQuery.trim();
89     console.log(this.searchQuery);
90     // this.dictionaryStore.search(this.searchQuery);
91   }
92
93   onChange(item: string, isChecked: boolean) {
94     if (isChecked) {
95       this.selectedArray.push(item);
96     }
97   }
98
99   textChanged(event, item) {
100     const editedData = JSON.parse(event);
101     const originalSources = this.sources;
102     for (const key in originalSources) {
103       if (key === item.name) {
104         this.sources[key] = editedData;
105       }
106     }
107     this.option = [];
108     this.sourcesStore.changeSources(this.sources);
109   }
110
111 }