b44d423f277fb355ed47f8ca719ea523cb8fe8bc
[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, EventEmitter, Input, Output, ViewChild } from '@angular/core';
14 import { ModalDirective } from 'ngx-bootstrap/modal';
15
16 import { Microservice } from '../../../../model/workflow/microservice';
17 import { RestConfig } from '../../../../model/rest-config';
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-list',
25     templateUrl: 'microservice-list.component.html',
26 })
27 export class MicroserviceListComponent {
28     @Input() microservices: RestConfig[];
29     @Output() microserviceSelected = new EventEmitter<RestConfig>();
30
31     public onMicroserviceSelected(microservice: RestConfig) {
32         this.microserviceSelected.emit(microservice);
33     }
34
35     public addMicroservice() {
36         const microservice = new RestConfig(this.getConfigId(), 'new microservice', '', null);
37         this.microservices.push(microservice);
38
39         this.onMicroserviceSelected(microservice);
40     }
41
42     public deleteMicroservice(index: number, microservice: Microservice) {
43         this.deleteMicroService(microservice.name, microservice.version);
44
45         // set the next microservice selected
46         let selectedMicroservice;
47         if (this.microservices.length > 0) {
48             if (this.microservices[index]) {
49                 selectedMicroservice = this.microservices[index];
50             } else {
51                 selectedMicroservice = this.microservices[index - 1];
52             }
53         }
54         this.onMicroserviceSelected(selectedMicroservice);
55     }
56
57     private deleteMicroService(name: string, version: string) {
58         const index = this.microservices.findIndex(service => (service.name === name && service.version === version));
59         if(index !== -1) {
60             return this.microservices.splice(index, 1)[0];
61         }
62
63         return undefined;
64     }
65
66     private getConfigId(): string {
67         const idSet = new Set<string>();
68         this.microservices.forEach(config => {
69             idSet.add(config.id);
70         });
71
72         for(let index = 0; index < idSet.size; index++) {
73             const id = `config${index}`;
74             if(!idSet.has(id)) {
75                 return id;
76             }
77         }
78
79         return `config0`;
80     }
81 }