Initial Commit - NG UI
[osm/NG-UI.git] / src / app / k8s / k8srepository / K8sRepositoryComponent.ts
1 /*
2  Copyright 2020 TATA ELXSI
3
4  Licensed under the Apache License, Version 2.0 (the 'License');
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7
8   http://www.apache.org/licenses/LICENSE-2.0
9
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15
16  Author: KUMARAN M (kumaran.m@tataelxsi.co.in), RAJESH S (rajesh.s@tataelxsi.co.in), BARATH KUMAR R (barath.r@tataelxsi.co.in)
17 */
18 /**
19  * @file K8sRepositoryComponent.ts.
20  */
21 import { Component, Injector, OnDestroy, OnInit } from '@angular/core';
22 import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap';
23 import { TranslateService } from '@ngx-translate/core';
24 import { ERRORDATA, MODALCLOSERESPONSEDATA } from 'CommonModel';
25 import { DataService } from 'DataService';
26 import { environment } from 'environment';
27 import { K8sActionComponent } from 'K8sActionComponent';
28 import { K8sAddRepoComponent } from 'K8sAddRepoComponent';
29 import { K8SREPODATA, K8SREPODATADISPLAY } from 'K8sModel';
30 import { LocalDataSource } from 'ng2-smart-table';
31 import { RestService } from 'RestService';
32 import { Subscription } from 'rxjs';
33 import { SharedService } from 'SharedService';
34 /**
35  * Creating Component
36  * @Component takes K8sRepositoryComponent.html as template url
37  */
38 @Component({
39   selector: 'app-k8srepository',
40   templateUrl: './K8sRepositoryComponent.html',
41   styleUrls: ['./K8sRepositoryComponent.scss']
42 })
43 /** Exporting a class @exports K8sRepositoryComponent */
44 export class K8sRepositoryComponent implements OnInit, OnDestroy {
45   /** To inject services @public */
46   public injector: Injector;
47
48   /** handle translate @public */
49   public translateService: TranslateService;
50
51   /** Data of smarttable populate through LocalDataSource @public */
52   public dataSource: LocalDataSource = new LocalDataSource();
53
54   /** Columns list of the smart table @public */
55   public columnList: object = {};
56
57   /** Settings for smarttable to populate the table with columns @public */
58   public settings: object = {};
59
60   /** Check the loading results @public */
61   public isLoadingResults: boolean = true;
62
63   /** Give the message for the loading @public */
64   public message: string = 'PLEASEWAIT';
65
66   /** Class for empty and present data @public */
67   public checkDataClass: string;
68
69   /** Instance of the rest service @private */
70   private restService: RestService;
71
72   /** dataService to pass the data from one component to another @private */
73   private dataService: DataService;
74
75   /** Formation of appropriate Data for LocalDatasource @private */
76   private k8sRepoData: {}[] = [];
77
78   /** Contains all methods related to shared @private */
79   private sharedService: SharedService;
80
81   /** Instance of the modal service @private */
82   private modalService: NgbModal;
83
84   /** Instance of subscriptions @private */
85   private generateDataSub: Subscription;
86
87   constructor(injector: Injector) {
88     this.injector = injector;
89     this.restService = this.injector.get(RestService);
90     this.dataService = this.injector.get(DataService);
91     this.sharedService = this.injector.get(SharedService);
92     this.translateService = this.injector.get(TranslateService);
93     this.modalService = this.injector.get(NgbModal);
94   }
95   /** Lifecyle Hooks the trigger before component is instantiate @public */
96   public ngOnInit(): void {
97     this.generateColumns();
98     this.generateSettings();
99     this.generateData();
100     this.generateDataSub = this.sharedService.dataEvent.subscribe(() => { this.generateData(); });
101   }
102
103   /** smart table Header Colums @public */
104   public generateColumns(): void {
105     this.columnList = {
106       name: { title: this.translateService.instant('NAME'), width: '20%', sortDirection: 'asc' },
107       identifier: { title: this.translateService.instant('IDENTIFIER'), width: '20%' },
108       url: { title: this.translateService.instant('URL'), width: '15%' },
109       type: { title: this.translateService.instant('TYPE'), width: '10%' },
110       created: { title: this.translateService.instant('CREATED'), width: '15%' },
111       modified: { title: this.translateService.instant('MODIFIED'), width: '15%' },
112       Actions: {
113         name: 'Action', width: '5%', filter: false, sort: false, title: this.translateService.instant('ACTIONS'), type: 'custom',
114         valuePrepareFunction: (cell: K8SREPODATADISPLAY, row: K8SREPODATADISPLAY): K8SREPODATADISPLAY => row,
115         renderComponent: K8sActionComponent
116       }
117     };
118   }
119
120   /** smart table Data Settings @public */
121   public generateSettings(): void {
122     this.settings = {
123       columns: this.columnList,
124       actions: { add: false, edit: false, delete: false, position: 'right' },
125       attr: this.sharedService.tableClassConfig(),
126       pager: this.sharedService.paginationPagerConfig(),
127       noDataMessage: this.translateService.instant('NODATAMSG')
128     };
129   }
130
131   /** smart table listing manipulation @public */
132   public onChange(perPageValue: number): void {
133     this.dataSource.setPaging(1, perPageValue, true);
134   }
135
136   /** smart table listing manipulation @public */
137   public onUserRowSelect(event: MessageEvent): void {
138     Object.assign(event.data, { page: 'k8-repo' });
139     this.dataService.changeMessage(event.data);
140   }
141
142   /** Compose new K8s Repo Accounts @public */
143   public addK8sRepo(): void {
144     const modalRef: NgbModalRef = this.modalService.open(K8sAddRepoComponent, { backdrop: 'static' });
145     modalRef.result.then((result: MODALCLOSERESPONSEDATA) => {
146       if (result) {
147         this.sharedService.callData();
148       }
149     }).catch();
150   }
151
152   /**
153    * Lifecyle hook which get trigger on component destruction
154    */
155   public ngOnDestroy(): void {
156     this.generateDataSub.unsubscribe();
157   }
158
159   /** Generate nsData object from loop and return for the datasource @public */
160   // tslint:disable-next-line: typedef
161   public generateK8sRepoData(k8sRepodata: K8SREPODATA): K8SREPODATADISPLAY {
162     return {
163       name: k8sRepodata.name,
164       identifier: k8sRepodata._id,
165       url: k8sRepodata.url,
166       type: k8sRepodata.type,
167       created: this.sharedService.convertEpochTime(Number(k8sRepodata._admin.created)),
168       modified: this.sharedService.convertEpochTime(Number(k8sRepodata._admin.modified)),
169       pageType: 'repo'
170     };
171   }
172
173   /** Fetching the data from server to Load in the smarttable @protected */
174   protected generateData(): void {
175     this.isLoadingResults = true;
176     this.restService.getResource(environment.K8REPOS_URL).subscribe((k8sRepoDatas: K8SREPODATA[]) => {
177       this.k8sRepoData = [];
178       k8sRepoDatas.forEach((k8sRepodata: K8SREPODATA) => {
179         const k8sRepoDataObj: K8SREPODATADISPLAY = this.generateK8sRepoData(k8sRepodata);
180         this.k8sRepoData.push(k8sRepoDataObj);
181       });
182       if (this.k8sRepoData.length > 0) {
183         this.checkDataClass = 'dataTables_present';
184       } else {
185         this.checkDataClass = 'dataTables_empty';
186       }
187       this.dataSource.load(this.k8sRepoData).then((data: boolean) => {
188         this.isLoadingResults = false;
189       }).catch(() => {
190         this.isLoadingResults = false;
191       });
192     }, (error: ERRORDATA) => {
193       this.restService.handleError(error, 'get');
194       this.isLoadingResults = false;
195     });
196   }
197
198 }