Catalog alignment
[sdc.git] / catalog-ui / src / app / ng2 / pages / workspace / disribution / distribution.component.ts
1 import { Component, Input, OnInit, ViewChild } from '@angular/core';
2 import { SdcUiCommon, SdcUiServices } from 'onap-ui-angular';
3 import { EventListenerService } from '../../../../services/event-listener-service';
4 import { AuthenticationService } from '../../../services/authentication.service';
5 import { WorkspaceService } from '../workspace.service';
6 import { DistributionService } from './distribution.service';
7 import { EVENTS } from '../../../../utils/constants';
8
9 @Component({
10   selector: 'distribution',
11   templateUrl: './distribution.component.html',
12   styleUrls: ['../../../../../assets/styles/table-style.less', './distribution.component.less']
13 })
14 export class DistributionComponent implements OnInit {
15
16   @ViewChild('distributionTable', { }) table: any;
17
18   @Input() isModal: boolean = false;
19   @Input() statusFilter: string;
20   @Input() rowDistributionID: string;
21   public componentUuid: string;
22   public distributions = [];
23   private expanded: any = {};
24   private serviceHasDistibutions: boolean = false;
25   private readonly uniqueId: string;
26   private  userRole: string;
27
28   constructor(private workspaceService: WorkspaceService,
29               private distributionService: DistributionService,
30               private loaderService: SdcUiServices.LoaderService,
31               private authService: AuthenticationService,
32               private eventListenerService: EventListenerService) {
33     this.componentUuid = this.workspaceService.metadata.uuid;
34     this.uniqueId = this.workspaceService.metadata.uniqueId;
35   }
36
37
38
39   async ngOnInit() {
40     this.userRole = this.authService.getLoggedinUser().role;
41     this.eventListenerService.registerObserverCallback(EVENTS.ON_DISTRIBUTION_SUCCESS, async () => {
42       await this.refreshDistributions();
43     });
44     await this.initDistributions(this.componentUuid, this.rowDistributionID);
45   }
46
47   ngOnDestroy(): void {
48     this.eventListenerService.unRegisterObserver(EVENTS.ON_DISTRIBUTION_SUCCESS);
49   }
50
51   async initDistributions(componentUuid: string, specificDistributionID?: string)  {
52     this.loaderService.activate();
53     await this.distributionService.initDistributionsList(componentUuid);
54     this.distributions = this.distributionService.getDistributionList();
55     this.distributions.length > 0 ? this.serviceHasDistibutions = true : this.serviceHasDistibutions = false;
56     if (specificDistributionID) {
57       this.distributions = this.distributionService.getDistributionList(specificDistributionID);
58     }
59     this.loaderService.deactivate();
60   }
61
62   getIconName(rowStatus: string ) {
63     if (rowStatus === 'Distributed') {
64       return 'distributed';
65     }
66     if (rowStatus === 'Deployed') {
67       return 'v-circle';
68     }
69   }
70
71   getIconMode(rowStatus: string) {
72     if (rowStatus === 'Distributed') {
73       return 'primary';
74     }
75     if (rowStatus === 'Deployed') {
76       return 'secondary';
77     }
78   }
79
80   private async markDeploy(distributionID: string, status: string) {
81     if (status === 'Distributed') {
82       console.log('Should send MarkDeploy POST Request ServiceID:' + this.uniqueId + ' DISTID:' + distributionID);
83       await this.distributionService.markDeploy(this.uniqueId, distributionID);
84       this.refreshDistributions();
85     }
86   }
87
88   private async refreshDistributions() {
89     await this.initDistributions(this.componentUuid);
90   }
91
92   private updateFilter(event) {
93     const val = event.target.value.toLowerCase();
94
95     // filter our data
96     this.distributions = this.distributionService.getDistributionList().filter((distribution: any[]) => {
97       return !val ||
98           // tslint:disable:no-string-literal
99           distribution['distributionID'].toLowerCase().indexOf(val) !== -1;
100     });
101   }
102
103   private generateDataTestID(preFix: string, distributionID: string, isModal?: boolean ): string {
104     if (isModal) {
105       return preFix + distributionID.substring(0, 5) + '_Modal';
106     } else {
107       return preFix + distributionID.substring(0, 5);
108     }
109   }
110
111   private async expandRow(row: any, expanded: boolean) {
112     if (!expanded) {
113       await this.distributionService.initDistributionsStatusForDistributionID(row.distributionID);
114     }
115     this.table.rowDetail.toggleExpandRow(row);
116   }
117 }