c4c9649b04225ef3f5bceeed1a05492fa4f536f4
[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 { Swagger } from "../../../../model/swagger";
17 import { RestConfig } from '../../../../model/rest-config';
18 import { RestService } from '../../../../services/rest.service';
19
20 /**
21  * toolbar component contains some basic operations(save) and all of the supported workflow nodes.
22  * The supported nodes can be dragged to container component. which will add a new node to the workflow.
23  */
24 @Component({
25     selector: 'b4t-microservice-detail',
26     templateUrl: 'microservice-detail.component.html',
27 })
28 export class MicroserviceDetailComponent implements OnChanges {
29     @Input() microservice: RestConfig;
30
31     public detail: string;
32     public dynamic = false;
33
34     constructor(private configService: RestService) {
35     }
36
37     public ngOnChanges() {
38         if(this.microservice == null) {
39             this.microservice = new RestConfig('', '', null, '');
40         }
41         this.checkDynamic();
42         this.parseSwagger2String();
43     }
44
45     private checkDynamic() {
46         if(this.microservice.url) {
47             this.dynamic = true;
48         } else {
49             this.dynamic = false;
50         }
51     }
52
53     private parseSwagger2String() {
54         if (this.microservice.swagger) {
55             this.detail = JSON.stringify(this.microservice.swagger);
56         } else {
57             this.detail = '';
58         }
59     }
60
61     public onDetailChanged(detail: string) {
62         try {
63             if(detail) {
64                 const swagger = new Swagger(JSON.parse(detail));
65                 this.detail = detail;
66                 console.log(swagger);
67                 this.microservice.swagger = swagger;
68             } else {
69                 this.detail = '';
70                 this.microservice.swagger = null;
71             }
72         } catch (e) {
73             // if detail is not a json object, then not change the swagger
74         }
75     }
76
77     public toggleDynamic(dynamic: boolean) {
78         this.dynamic = dynamic;
79         this.onDetailChanged(null);
80
81         if(!dynamic) {
82             this.microservice.url = null;
83         }
84     }
85
86     private loadDynamicInfo() {
87         this.configService.getDynamicSwaggerInfo(this.microservice.url)
88         .subscribe(response => {
89             try {
90                 this.microservice.swagger = response;
91                 this.parseSwagger2String();
92             } catch (e) {
93                 console.log('detail transfer error');
94                 console.error(e);
95             }
96         });
97     }
98 }