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 { SetResourcesState } 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;
48 sources: ISourcesData;
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 this.sourcesOptions = [];
70 for (let key in this.sources) {
73 data: this.sources[key]
75 this.sourcesOptions.push(source);
80 onChange(item, $event) {
81 var editedData = JSON.parse($event.srcElement.value);
82 var originalSources = this.resources.definition.sources;
83 for (let key in originalSources) {
84 if (key == item.name) {
85 originalSources[key] = editedData;
88 this.resources.definition.sources = Object.assign({}, originalSources);
90 let resourcesState = {
91 resources: this.resources,
93 isUpdateSuccess: true,
96 this.store.dispatch(new SetResourcesState(resourcesState));
99 // to remove this method
100 selected(sourceValue) {
101 this.sourcesData = [];//this.sources[value];
102 this.apiService.getModelType(sourceValue.value)
105 data.forEach(item => {
106 if (typeof (item) == "object") {
107 for (let key1 in item) {
108 if (key1 == 'properties') {
110 for (let key2 in item[key1]) {
111 console.log(item[key1][key2]);
112 let varType = item[key1][key2].type
113 // let property : varType =
114 newPropOnj[key2] = item[key1][key2];
120 this.sourcesData = data;
121 this.sourcesOptions.forEach(item => {
122 if (item.name == sourceValue.name) {
126 return this.sourcesData;
131 if (confirm("Are sure you want to delete this source ?")) {
132 var originalSources = this.resources.definition.sources;
133 for (let key in originalSources) {
134 if (key == item.name) {
135 delete originalSources[key];
138 this.resources.definition.sources = Object.assign({}, originalSources);
139 this.sourcesOptions.splice(i, 1);
140 if (!this.checkIfSourceExists(this.option, item.name)) {
141 this.tempOption.forEach(tempOptionitem => {
142 if (tempOptionitem.name == item.name) {
143 this.option.push(tempOptionitem);
148 this.sourcesOptions.forEach(sourceItem => {
149 newsources[sourceItem.name] = {};
150 newsources[sourceItem.name] = sourceItem.data;
152 this.resources.definition.sources = newsources;
153 let resourcesState = {
154 resources: this.resources,
156 isUpdateSuccess: true,
159 this.store.dispatch(new SetResourcesState(resourcesState));
163 UploadSourcesData() {
164 this.resourcesData.emit(this.resources);
167 drop(event: CdkDragDrop<string[]>) {
168 if (!this.checkIfSourceExists(this.sourcesOptions, event.item.element.nativeElement.innerText)) {
169 if (event.previousContainer === event.container) {
170 moveItemInArray(event.container.data, event.previousIndex, event.currentIndex);
172 transferArrayItem(event.previousContainer.data,
173 event.container.data,
177 this.tempOption.forEach((item) => {
178 if (item.name == event.item.element.nativeElement.innerText) {
179 this.apiService.getModelType(item.value)
182 data.forEach(dataitem => {
183 if (typeof (dataitem) == "object") {
184 for (let key1 in dataitem) {
185 if (key1 == 'properties') {
187 newPropObj["name"] = event.item.element.nativeElement.innerText;
188 newPropObj['data'] = {};
189 let newSoruceObj = {};
190 for (let key2 in dataitem[key1]) {
191 newSoruceObj[key2] = '';;
193 newPropObj['data']['properties'] = newSoruceObj;
194 this.sourcesOptions.forEach(sourcesOptionsitem => {
195 if (sourcesOptionsitem.name == item.name) {
196 sourcesOptionsitem.data = newPropObj['data'];
204 this.sourcesOptions.forEach(sourceItem => {
206 newsources[sourceItem.name] = {};
207 newsources[sourceItem.name] = sourceItem.data;
209 this.resources.definition.sources = newsources;
210 let resourcesState = {
211 resources: this.resources,
213 isUpdateSuccess: true,
216 this.store.dispatch(new SetResourcesState(resourcesState));
224 this.apiService.getSources()
227 for (let key in data[0]) {
228 let sourceObj = { name: key, value: data[0][key] }
229 this.option.push(sourceObj);
230 this.tempOption.push(sourceObj);
237 checkIfSourceExists(sourceList, sourceName) {
238 let sourceExists: boolean = false;
239 sourceList.forEach(item => {
240 if (item.name == sourceName) {