4a4f215cdc31f96fb93021784c726240b33e93c4
[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
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   selectedArray = [];
40   constructor(private sourcesStore: SourcesStore) {
41     this.sourcesStore.state$.subscribe(sources => {
42       this.sources = sources.sources;
43       for (const key in this.sources) {
44         if (key) {
45           const sourceObj = { name: key, value: JSON.stringify(this.sources[key] )};
46           this.option.push(sourceObj);
47         }
48       }
49     });
50   }
51
52   ngOnInit() {
53     this.sourcesStore.getAllSources();
54   }
55
56   saveSorcesDataToStore() {
57     this.sourcesStore.saveSources(this.ddSource);
58   }
59
60   drop(event: CdkDragDrop<string[]>) {
61       this.ddSource = [];
62       if (event.previousContainer === event.container) {
63          moveItemInArray(event.container.data, event.previousIndex, event.currentIndex);
64       } else {
65          transferArrayItem(event.previousContainer.data,
66             event.container.data,
67             event.previousIndex,
68             event.currentIndex);
69       }
70
71       for (const key2 in this.sources) {
72         if (key2) {
73           const originalSources = this.sourcesOptions;
74           for (const key of originalSources) {
75             if (key.name === key2) {
76               const obj = `{${key.name}: ${key.value}}`;
77               this.ddSource.push(obj);
78             }
79           }
80         }
81       }
82   }
83
84   searchDictionary(event: any) {
85     this.searchQuery = event.target.value;
86     this.searchQuery = this.searchQuery.trim();
87     console.log(this.searchQuery);
88     // this.dictionaryStore.search(this.searchQuery);
89   }
90
91   onChange(item: string, isChecked: boolean) {
92     if (isChecked) {
93       this.selectedArray.push(item);
94     }
95   }
96
97   textChanged(event, item) {
98     const editedData = JSON.parse(event);
99     const originalSources = this.sources;
100     for (const key in originalSources) {
101         if (key === item.name) {
102           this.sources[key] = editedData;
103         }
104     }
105     this.option = [];
106     this.sourcesStore.changeSources(this.sources);
107   }
108
109 }