52b19f728421e8959b1ffa21f8a812eb3a80253b
[ccsdk/cds.git] /
1 /*
2 * ============LICENSE_START=======================================================
3 * ONAP : CDS
4 * ================================================================================
5 * Copyright (C) 2019 TechMahindra
6 *
7 * Modifications Copyright (C) 2019 IBM
8 *=================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 *     http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
21 */
22
23 import { Component, OnInit, ViewChild, EventEmitter, Output } from '@angular/core';
24 import { CdkDragDrop, moveItemInArray, transferArrayItem } from '@angular/cdk/drag-drop';
25 import { IResources } from 'src/app/common/core/store/models/resources.model';
26 import { IResourcesState } from 'src/app/common/core/store/models/resourcesState.model';
27 import { Observable } from 'rxjs';
28 import { Store } from '@ngrx/store';
29 import { IAppState } from '../../../../common/core/store/state/app.state';
30 import { A11yModule } from '@angular/cdk/a11y';
31 import { LoadResourcesSuccess } from 'src/app/common/core/store/actions/resources.action';
32 import { ISourcesData } from 'src/app/common/core/store/models/sourcesData.model';
33 import { JsonEditorComponent, JsonEditorOptions } from 'ang-jsoneditor';
34
35 @Component({
36   selector: 'app-sources-template',
37   templateUrl: './sources-template.component.html',
38   styleUrls: ['./sources-template.component.scss']
39 })
40 export class SourcesTemplateComponent implements OnInit {
41
42     @ViewChild(JsonEditorComponent) editor: JsonEditorComponent;
43     options = new JsonEditorOptions(); 
44     rdState: Observable<IResourcesState>;
45     resources: IResources;
46     option = ['mdsal','default'];
47     sources:ISourcesData; 
48     sourcesOptions = [];
49     sourcesData = [];
50     @Output() resourcesData = new EventEmitter();  
51  
52  constructor(private store: Store<IAppState>) {
53     this.rdState = this.store.select('resources');
54     this.options.mode = 'text';
55     this.options.modes = [ 'text', 'tree', 'view'];
56     this.options.statusBar = false;     
57  }
58
59  ngOnInit() {
60     this.rdState.subscribe(
61       resourcesdata => {
62         var resourcesState: IResourcesState = { resources: resourcesdata.resources, isLoadSuccess: resourcesdata.isLoadSuccess, isSaveSuccess: resourcesdata.isSaveSuccess, isUpdateSuccess: resourcesdata.isUpdateSuccess };
63         this.resources=resourcesState.resources;
64         this.sources = resourcesState.resources.sources;
65         for (let key in this.sources) {
66             this.sourcesOptions.push(key);  
67         }
68     })
69  }
70
71  onChange(item,$event) {
72     var editedData =JSON.parse($event.srcElement.value);
73     var originalSources = this.resources.sources;
74      for (let key in originalSources){
75         if(key == item){
76             originalSources[key] = editedData;
77         }
78      }
79      this.resources.sources = Object.assign({},originalSources);
80  };
81     
82  selected(value){
83         this.sourcesData=this.sources[value];
84     return this.sourcesData;    
85  }    
86
87  delete(item,i){
88         if(confirm("Are sure you want to delete this source ?")) {
89         var originalSources = this.resources.sources;
90         for (let key in originalSources){
91                 if(key == item){    
92                         delete originalSources[key];
93                 }
94         }
95         this.resources.sources = Object.assign({},originalSources);
96                 this.sourcesOptions.splice(i,1);
97         }     
98  } 
99   
100  UploadSourcesData() {
101         this.resourcesData.emit(this.resources);        
102   }
103     
104  drop(event: CdkDragDrop<string[]>) {
105     if (event.previousContainer === event.container) {
106       moveItemInArray(event.container.data, event.previousIndex, event.currentIndex);
107     } else {
108       transferArrayItem(event.previousContainer.data,
109                         event.container.data,
110                         event.previousIndex,
111                         event.currentIndex);
112     }
113   }    
114 }