2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 TechMahindra
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
23 import { Component, OnInit, ViewChild, EventEmitter, Output, Input } 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 import { ResourceEditService } from '../resource-edit.service';
37 selector: 'app-sources-template',
38 templateUrl: './sources-template.component.html',
39 styleUrls: ['./sources-template.component.scss']
41 export class SourcesTemplateComponent implements OnInit {
43 @ViewChild(JsonEditorComponent) editor: JsonEditorComponent;
44 options = new JsonEditorOptions();
45 rdState: Observable<IResourcesState>;
46 resources: IResources;
51 @Output() resourcesData = new EventEmitter();
54 constructor(private store: Store<IAppState>, private apiService: ResourceEditService) {
55 this.rdState = this.store.select('resources');
56 this.options.mode = 'text';
57 this.options.modes = [ 'text', 'tree', 'view'];
58 this.options.statusBar = false;
62 this.rdState.subscribe(
64 var resourcesState: IResourcesState = { resources: resourcesdata.resources, isLoadSuccess: resourcesdata.isLoadSuccess, isSaveSuccess: resourcesdata.isSaveSuccess, isUpdateSuccess: resourcesdata.isUpdateSuccess };
65 this.resources=resourcesState.resources;
66 if(resourcesState.resources.definition && resourcesState.resources.definition.sources) {
67 this.sources = resourcesState.resources.definition.sources;
69 for (let key in this.sources) {
72 data: this.sources[key]
74 this.sourcesOptions.push(source);
79 onChange(item,$event) {
80 var editedData =JSON.parse($event.srcElement.value);
81 var originalSources = this.resources.sources;
82 for (let key in originalSources){
84 originalSources[key] = editedData;
87 this.resources.sources = Object.assign({},originalSources);
90 selected(sourceValue){
91 this.sourcesData= [];//this.sources[value];
92 this.apiService.getModelType(sourceValue.value)
96 if(typeof(item)== "object") {
97 for (let key1 in item) {
98 if(key1 == 'properties') {
100 for (let key2 in item[key1]) {
101 console.log(item[key1][key2]);
102 let varType = item[key1][key2].type
103 // let property : varType =
104 newPropOnj[key2] = item[key1][key2];
110 this.sourcesData = data;
111 this.sourcesOptions.forEach(item=>{
112 if(item.name == sourceValue.name) {
116 return this.sourcesData;
121 if(confirm("Are sure you want to delete this source ?")) {
122 var originalSources = this.resources.sources;
123 for (let key in originalSources){
125 delete originalSources[key];
128 this.resources.sources = Object.assign({},originalSources);
129 this.sourcesOptions.splice(i,1);
133 UploadSourcesData() {
134 this.resourcesData.emit(this.resources);
137 drop(event: CdkDragDrop<string[]>) {
138 if (!this.checkIfSourceExists(event.item.element.nativeElement.innerText)) {
139 if (event.previousContainer === event.container) {
140 moveItemInArray(event.container.data, event.previousIndex, event.currentIndex);
142 transferArrayItem(event.previousContainer.data,
143 event.container.data,
147 this.tempOption.forEach((item) => {
148 if (item.name == event.item.element.nativeElement.innerText) {
149 this.apiService.getModelType(item.value)
152 data.forEach(dataitem => {
153 if (typeof (dataitem) == "object") {
154 for (let key1 in dataitem) {
155 if (key1 == 'properties') {
157 for (let key2 in dataitem[key1]) {
158 console.log(dataitem[key1][key2]);
159 let varType = dataitem[key1][key2].type
160 // let property : varType =
161 newPropOnj[key2] = dataitem[key1][key2];
167 this.sourcesData = data;
168 this.sourcesOptions.forEach(sourcesOptionsitem => {
169 if (sourcesOptionsitem.name == item.name) {
170 sourcesOptionsitem.data = data;
180 this.apiService.getSources()
183 for (let key in data[0]) {
184 let sourceObj = { name: key, value: data[0][key] }
185 this.option.push(sourceObj);
186 this.tempOption.push(sourceObj);
193 checkIfSourceExists(sourceName) {
194 let sourceExists: boolean = false;
195 this.sourcesOptions.forEach(item => {
196 if (item.name == sourceName) {