55b80628d1adb5f6755c3973867490a2faedcedb
[ccsdk/cds.git] / cds-ui / client / src / app / feature-modules / resource-definition / resource-edit / resource-edit.component.ts
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 } from '@angular/core';
22 import { IResources } from 'src/app/common/core/store/models/resources.model';
23 import { IResourcesState } from 'src/app/common/core/store/models/resourcesState.model';
24 import { LoadResourcesSuccess,UpdateResources,SetResourcesState } from 'src/app/common/core/store/actions/resources.action';
25 import { Store } from '@ngrx/store';
26 import { IAppState } from '../../../common/core/store/state/app.state';
27 import { JsonEditorComponent, JsonEditorOptions } from 'ang-jsoneditor';
28 import { Observable } from 'rxjs';
29 import { A11yModule } from '@angular/cdk/a11y';
30
31 @Component({
32   selector: 'app-resource-edit',
33   templateUrl: './resource-edit.component.html',
34   styleUrls: ['./resource-edit.component.scss']
35 })
36 export class ResourceEditComponent implements OnInit {
37
38     resources:IResources;
39     data:IResources;
40     rdState: Observable<IResourcesState>;
41     designerMode: boolean = true;
42     editorMode: boolean = false;
43     viewText: string = "Open in Editor Mode";
44     @ViewChild(JsonEditorComponent) editor: JsonEditorComponent;
45     options = new JsonEditorOptions();
46   
47   constructor(private store: Store<IAppState>) {
48         this.rdState = this.store.select('resources');
49     this.options.mode = 'text';
50     this.options.modes = [ 'text', 'tree', 'view'];
51     this.options.statusBar = false;    
52   }
53
54   ngOnInit() {
55     this.rdState.subscribe(
56       resourcesdata => {
57         var resourcesState: IResourcesState = { resources: resourcesdata.resources, isLoadSuccess: resourcesdata.isLoadSuccess, isSaveSuccess: resourcesdata.isSaveSuccess, isUpdateSuccess: resourcesdata.isUpdateSuccess };
58           this.resources=resourcesState.resources;
59     })     
60   }
61
62  metaDataDetail(data: IResources) {
63     this.data=data;       
64   }
65     
66  sourcesDetails(data: IResources) {
67     this.data=data; 
68  }
69     
70  onChange($event) {
71       this.data=JSON.parse($event.srcElement.value);
72   };
73   
74  updateResourcesState(){
75       console.log(this.data);
76       let resourcesState = {
77       resources: this.data,
78       isLoadSuccess: true,
79       isUpdateSuccess:true,
80       isSaveSuccess:true
81     }  
82    this.store.dispatch(new SetResourcesState(resourcesState));   
83   }
84     
85  changeView() {
86     if(this.viewText == 'Open in Editor Mode') {
87       this.editorMode =  true;
88       this.designerMode = false;
89       this.viewText = 'Open in Form Mode'
90     } else {
91       this.editorMode =  false;
92       this.designerMode = true;
93       this.viewText = 'Open in Editor Mode'
94     }
95   }  
96 }