517add87b076d0f1870b9929f9747d069c429e8f
[ccsdk/cds.git] /
1 /*
2 * ============LICENSE_START=======================================================
3 * ONAP : CDS
4 * ================================================================================
5 * Copyright (C) 2019 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
21 import { Component, OnInit, ViewChild, EventEmitter, Output } from '@angular/core';
22 import { CdkDragDrop, moveItemInArray, transferArrayItem } from '@angular/cdk/drag-drop';
23 import { IResources } from 'src/app/common/core/store/models/resources.model';
24 import { IResourcesState } from 'src/app/common/core/store/models/resourcesState.model';
25 import { Observable } from 'rxjs';
26 import { Store } from '@ngrx/store';
27 import { IAppState } from '../../../../common/core/store/state/app.state';
28 import { A11yModule } from '@angular/cdk/a11y';
29 import { LoadResourcesSuccess } from 'src/app/common/core/store/actions/resources.action';
30 import { ISourcesData } from 'src/app/common/core/store/models/sourcesData.model';
31 import { JsonEditorComponent, JsonEditorOptions } from 'ang-jsoneditor';
32
33 @Component({
34   selector: 'app-sources-template',
35   templateUrl: './sources-template.component.html',
36   styleUrls: ['./sources-template.component.scss']
37 })
38 export class SourcesTemplateComponent implements OnInit {
39
40     @ViewChild(JsonEditorComponent) editor: JsonEditorComponent;
41     options = new JsonEditorOptions(); 
42     rdState: Observable<IResourcesState>;
43     resources: IResources;
44     option = ['mdsal','default'];
45     sources:ISourcesData; 
46     sourcesOptions = [];
47     sourcesData = [];
48     @Output() resourcesData = new EventEmitter();  
49  
50  constructor(private store: Store<IAppState>) {
51     this.rdState = this.store.select('resources');
52     this.options.mode = 'text';
53     this.options.modes = [ 'text', 'tree', 'view'];
54     this.options.statusBar = false;     
55  }
56
57  ngOnInit() {
58     this.rdState.subscribe(
59       resourcesdata => {
60         var resourcesState: IResourcesState = { resources: resourcesdata.resources, isLoadSuccess: resourcesdata.isLoadSuccess, isSaveSuccess: resourcesdata.isSaveSuccess, isUpdateSuccess: resourcesdata.isUpdateSuccess };
61         this.resources=resourcesState.resources;
62         this.sources = resourcesState.resources.sources;
63         for (let key in this.sources) {
64             this.sourcesOptions.push(key);  
65         }
66     })
67  }
68
69  onChange(item,$event) {
70     var editedData =JSON.parse($event.srcElement.value);
71     var originalSources = this.resources.sources;
72      for (let key in originalSources){
73         if(key == item){
74             originalSources[key] = editedData;
75         }
76      }
77      this.resources.sources = Object.assign({},originalSources);
78  };
79     
80  selected(value){
81         this.sourcesData=this.sources[value];
82     return this.sourcesData;    
83  }    
84
85  delete(item,i){
86         if(confirm("Are sure you want to delete this source ?")) {
87         var originalSources = this.resources.sources;
88         for (let key in originalSources){
89                 if(key == item){    
90                         delete originalSources[key];
91                 }
92         }
93         this.resources.sources = Object.assign({},originalSources);
94                 this.sourcesOptions.splice(i,1);
95         }     
96  } 
97   
98  UploadSourcesData() {
99         this.resourcesData.emit(this.resources);        
100   }
101     
102  drop(event: CdkDragDrop<string[]>) {
103     if (event.previousContainer === event.container) {
104       moveItemInArray(event.container.data, event.previousIndex, event.currentIndex);
105     } else {
106       transferArrayItem(event.previousContainer.data,
107                         event.container.data,
108                         event.previousIndex,
109                         event.currentIndex);
110     }
111   }    
112 }