Fix Bug 2121: NG-UI uses unmaintained Chokidar version
[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     // eslint-disable-next-line security/detect-non-literal-fs-filename
145     const modalRef: NgbModalRef = this.modalService.open(K8sAddRepoComponent, { backdrop: 'static' });
146     modalRef.result.then((result: MODALCLOSERESPONSEDATA) => {
147       if (result) {
148         this.sharedService.callData();
149       }
150     }).catch((): void => {
151       // Catch Navigation Error
152   });
153   }
154
155   /**
156    * Lifecyle hook which get trigger on component destruction
157    */
158   public ngOnDestroy(): void {
159     this.generateDataSub.unsubscribe();
160   }
161
162   /** Generate nsData object from loop and return for the datasource @public */
163   public generateK8sRepoData(k8sRepodata: K8SREPODATA): K8SREPODATADISPLAY {
164     return {
165       name: k8sRepodata.name,
166       identifier: k8sRepodata._id,
167       url: k8sRepodata.url,
168       type: k8sRepodata.type,
169       created: this.sharedService.convertEpochTime(Number(k8sRepodata._admin.created)),
170       modified: this.sharedService.convertEpochTime(Number(k8sRepodata._admin.modified)),
171       pageType: 'repo'
172     };
173   }
174
175   /** Fetching the data from server to Load in the smarttable @protected */
176   protected generateData(): void {
177     this.isLoadingResults = true;
178     this.restService.getResource(environment.K8REPOS_URL).subscribe((k8sRepoDatas: K8SREPODATA[]) => {
179       this.k8sRepoData = [];
180       k8sRepoDatas.forEach((k8sRepodata: K8SREPODATA) => {
181         const k8sRepoDataObj: K8SREPODATADISPLAY = this.generateK8sRepoData(k8sRepodata);
182         this.k8sRepoData.push(k8sRepoDataObj);
183       });
184       if (this.k8sRepoData.length > 0) {
185         this.checkDataClass = 'dataTables_present';
186       } else {
187         this.checkDataClass = 'dataTables_empty';
188       }
189       this.dataSource.load(this.k8sRepoData).then((data: boolean) => {
190         this.isLoadingResults = false;
191       }).catch(() => {
192         this.isLoadingResults = false;
193       });
194     }, (error: ERRORDATA) => {
195       this.restService.handleError(error, 'get');
196       this.isLoadingResults = false;
197     });
198   }
199 }