2 Copyright 2020 TATA ELXSI
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
8 http://www.apache.org/licenses/LICENSE-2.0
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.
16 Author: KUMARAN M (kumaran.m@tataelxsi.co.in), RAJESH S (rajesh.s@tataelxsi.co.in), BARATH KUMAR R (barath.r@tataelxsi.co.in)
19 * @file K8sRepositoryComponent.ts.
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';
36 * @Component takes K8sRepositoryComponent.html as template url
39 selector: 'app-k8srepository',
40 templateUrl: './K8sRepositoryComponent.html',
41 styleUrls: ['./K8sRepositoryComponent.scss']
43 /** Exporting a class @exports K8sRepositoryComponent */
44 export class K8sRepositoryComponent implements OnInit, OnDestroy {
45 /** To inject services @public */
46 public injector: Injector;
48 /** handle translate @public */
49 public translateService: TranslateService;
51 /** Data of smarttable populate through LocalDataSource @public */
52 public dataSource: LocalDataSource = new LocalDataSource();
54 /** Columns list of the smart table @public */
55 public columnList: object = {};
57 /** Settings for smarttable to populate the table with columns @public */
58 public settings: object = {};
60 /** Check the loading results @public */
61 public isLoadingResults: boolean = true;
63 /** Give the message for the loading @public */
64 public message: string = 'PLEASEWAIT';
66 /** Class for empty and present data @public */
67 public checkDataClass: string;
69 /** Instance of the rest service @private */
70 private restService: RestService;
72 /** dataService to pass the data from one component to another @private */
73 private dataService: DataService;
75 /** Formation of appropriate Data for LocalDatasource @private */
76 private k8sRepoData: {}[] = [];
78 /** Contains all methods related to shared @private */
79 private sharedService: SharedService;
81 /** Instance of the modal service @private */
82 private modalService: NgbModal;
84 /** Instance of subscriptions @private */
85 private generateDataSub: Subscription;
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);
95 /** Lifecyle Hooks the trigger before component is instantiate @public */
96 public ngOnInit(): void {
97 this.generateColumns();
98 this.generateSettings();
100 this.generateDataSub = this.sharedService.dataEvent.subscribe(() => { this.generateData(); });
103 /** smart table Header Colums @public */
104 public generateColumns(): void {
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%' },
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
120 /** smart table Data Settings @public */
121 public generateSettings(): void {
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')
131 /** smart table listing manipulation @public */
132 public onChange(perPageValue: number): void {
133 this.dataSource.setPaging(1, perPageValue, true);
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);
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) => {
148 this.sharedService.callData();
150 }).catch((): void => {
151 // Catch Navigation Error
156 * Lifecyle hook which get trigger on component destruction
158 public ngOnDestroy(): void {
159 this.generateDataSub.unsubscribe();
162 /** Generate nsData object from loop and return for the datasource @public */
163 public generateK8sRepoData(k8sRepodata: K8SREPODATA): K8SREPODATADISPLAY {
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)),
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);
184 if (this.k8sRepoData.length > 0) {
185 this.checkDataClass = 'dataTables_present';
187 this.checkDataClass = 'dataTables_empty';
189 this.dataSource.load(this.k8sRepoData).then((data: boolean) => {
190 this.isLoadingResults = false;
192 this.isLoadingResults = false;
194 }, (error: ERRORDATA) => {
195 this.restService.handleError(error, 'get');
196 this.isLoadingResults = false;