bffaef42c56b740b755cdc6c230ce9c25e65168b
[sdc/sdc-workflow-designer.git] /
1 /**
2  * Copyright (c) 2017 ZTE Corporation.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * and the Apache License 2.0 which both accompany this distribution,
6  * and are available at http://www.eclipse.org/legal/epl-v10.html
7  * and http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Contributors:
10  *     ZTE - initial API and implementation and/or initial documentation
11  */
12
13 import { Component, Input, OnChanges, ViewChild } from '@angular/core';
14 import { ModalDirective } from 'ngx-bootstrap/modal';
15
16 import { Microservice } from '../../../../model/workflow/microservice';
17 import { WorkflowConfigService } from '../../../../services/workflow-config.service';
18 import { Swagger } from "../../../../model/swagger";
19 import { RestConfig } from '../../../../model/rest-config';
20
21 /**
22  * toolbar component contains some basic operations(save) and all of the supported workflow nodes.
23  * The supported nodes can be dragged to container component. which will add a new node to the workflow.
24  */
25 @Component({
26     selector: 'b4t-microservice-detail',
27     templateUrl: 'microservice-detail.component.html',
28 })
29 export class MicroserviceDetailComponent implements OnChanges {
30     @Input() microservice: RestConfig;
31
32     public detail: string;
33     public dynamic = false;
34
35     constructor(private configService: WorkflowConfigService) {
36     }
37
38     public ngOnChanges() {
39         if(this.microservice == null) {
40             this.microservice = new RestConfig('', '', null, '');
41         }
42         this.checkDynamic();
43         this.parseSwagger2String();
44     }
45
46     private checkDynamic() {
47         if(this.microservice.url) {
48             this.dynamic = true;
49         } else {
50             this.dynamic = false;
51         }
52     }
53
54     private parseSwagger2String() {
55         if (this.microservice.swagger) {
56             this.detail = JSON.stringify(this.microservice.swagger);
57         } else {
58             this.detail = '';
59         }
60     }
61
62     public onDetailChanged(detail: string) {
63         try {
64             if(detail) {
65                 const swagger = new Swagger(JSON.parse(detail));
66                 this.detail = detail;
67                 console.log(swagger);
68                 this.microservice.swagger = swagger;
69             } else {
70                 this.detail = '';
71                 this.microservice.swagger = null;
72             }
73         } catch (e) {
74             // if detail is not a json object, then not change the swagger
75         }
76     }
77
78     public toggleDynamic(dynamic: boolean) {
79         this.dynamic = dynamic;
80         this.onDetailChanged(null);
81
82         if(!dynamic) {
83             this.microservice.url = null;
84         }
85     }
86
87     private loadDynamicInfo() {
88         this.configService.loadDynamicInfo(this.microservice.url)
89         .subscribe(response => {
90             try {
91
92                 this.microservice.swagger = response;
93                 this.parseSwagger2String();
94             } catch (e) {
95                 console.log('detail transfer error');
96                 console.error(e);
97             }
98         });
99     }
100 }