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