blob: 6a2ca34db6d4a3b56c2172385a6fbea3e46f1fb2 [file] [log] [blame]
kumaran.m3b4814a2020-05-01 19:48:54 +05301/*
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 PDU Instance Component
20 */
21import { Component, Injector, OnDestroy, OnInit } from '@angular/core';
22import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap';
23import { TranslateService } from '@ngx-translate/core';
24import { AddPDUInstancesComponent } from 'AddPDUInstancesComponent';
25import { ERRORDATA, MODALCLOSERESPONSEDATA } from 'CommonModel';
26import { DataService } from 'DataService';
27import { environment } from 'environment';
28import { LocalDataSource } from 'ng2-smart-table';
29import { PDUInstanceDetails } from 'PDUInstanceModel';
30import { PDUInstancesActionComponent } from 'PDUInstancesActionComponent';
31import { RestService } from 'RestService';
32import { Subscription } from 'rxjs';
33import { SharedService } from 'SharedService';
34
35/**
36 * Creating component
37 * @Component takes PDUInstancesComponent.html as template url
38 */
39@Component({
40 templateUrl: './PDUInstancesComponent.html',
41 styleUrls: ['./PDUInstancesComponent.scss']
42})
43/** Exporting a class @exports PDUInstancesComponent */
44export class PDUInstancesComponent implements OnInit, OnDestroy {
45 /** Injector to invoke other services @public */
46 public injector: Injector;
47
48 /** NS Instance array @public */
49 public pduInstanceData: object[] = [];
50
51 /** Datasource instance @public */
52 public dataSource: LocalDataSource = new LocalDataSource();
53
54 /** SelectedRows array @public */
55 public selectedRows: object[] = [];
56
57 /** Selected list array @public */
58 public selectList: object[] = [];
59
60 /** Instance component are stored in settings @public */
61 public settings: {} = {};
62
63 /** Contains objects for menu settings @public */
64 public columnList: {} = {};
65
66 /** Check the loading results @public */
67 public isLoadingResults: boolean = true;
68
69 /** Give the message for the loading @public */
70 public message: string = 'PLEASEWAIT';
71
72 /** Class for empty and present data @public */
73 public checkDataClass: string;
74
75 /** Instance of the modal service @private */
76 private modalService: NgbModal;
77
78 /** dataService to pass the data from one component to another @private */
79 private dataService: DataService;
80
81 /** Utilizes rest service for any CRUD operations @private */
82 private restService: RestService;
83
84 /** Contains all methods related to shared @private */
85 private sharedService: SharedService;
86
87 /** Contains tranlsate instance @private */
88 private translateService: TranslateService;
89
90 /** Instance of subscriptions @private */
91 private generateDataSub: Subscription;
92
93 constructor(injector: Injector) {
94 this.injector = injector;
95 this.restService = this.injector.get(RestService);
96 this.dataService = this.injector.get(DataService);
97 this.sharedService = this.injector.get(SharedService);
98 this.translateService = this.injector.get(TranslateService);
99 this.modalService = this.injector.get(NgbModal);
100 }
101
102 /**
103 * Lifecyle Hooks the trigger before component is instantiate
104 */
105 public ngOnInit(): void {
106 this.generateTableColumn();
107 this.generateTableSettings();
108 this.generateData();
109 this.generateDataSub = this.sharedService.dataEvent.subscribe(() => { this.generateData(); });
110 }
111
112 /** Generate smart table row title and filters @public */
113 public generateTableSettings(): void {
114 this.settings = {
115 columns: this.columnList,
116 actions: { add: false, edit: false, delete: false, position: 'right' },
117 attr: this.sharedService.tableClassConfig(),
118 pager: this.sharedService.paginationPagerConfig(),
119 noDataMessage: this.translateService.instant('NODATAMSG')
120 };
121 }
122
123 /** Generate smart table row title and filters @public */
124 public generateTableColumn(): void {
125 this.columnList = {
126 identifier: { title: this.translateService.instant('IDENTIFIER'), width: '25%' },
SANDHYA.JS5f8c8022023-10-13 11:45:25 +0530127 name: { title: this.translateService.instant('NAME'), width: '20%' },
kumaran.m3b4814a2020-05-01 19:48:54 +0530128 type: { title: this.translateService.instant('TYPE'), width: '15%' },
129 usageState: { title: this.translateService.instant('USAGESTATE'), width: '15%' },
SANDHYA.JS5f8c8022023-10-13 11:45:25 +0530130 CreatedAt: {
131 title: this.translateService.instant('CREATEDAT'), width: '15%', sortDirection: 'desc',
132 compareFunction: this.sharedService.compareFunction
133 },
kumaran.m3b4814a2020-05-01 19:48:54 +0530134 Actions: {
135 name: 'Action', width: '10%', filter: false, sort: false, type: 'custom',
136 title: this.translateService.instant('ACTIONS'),
137 valuePrepareFunction: (cell: PDUInstanceDetails, row: PDUInstanceDetails): PDUInstanceDetails => row,
138 renderComponent: PDUInstancesActionComponent
139 }
140 };
141 }
142
143 /** generateData initiate the ns-instance list @public */
144 public generateData(): void {
145 this.pduInstanceData = [];
146 this.isLoadingResults = true;
147 this.restService.getResource(environment.PDUINSTANCE_URL).subscribe((pduInstancesData: PDUInstanceDetails[]) => {
148 pduInstancesData.forEach((pduInstanceData: PDUInstanceDetails) => {
149 const pduDataObj: {} = {
150 name: pduInstanceData.name,
151 identifier: pduInstanceData._id,
152 type: pduInstanceData.type,
153 usageState: pduInstanceData._admin.usageState,
154 CreatedAt: this.sharedService.convertEpochTime(Number(pduInstanceData._admin.created))
155 };
156 this.pduInstanceData.push(pduDataObj);
157 });
158 if (this.pduInstanceData.length > 0) {
159 this.checkDataClass = 'dataTables_present';
160 } else {
161 this.checkDataClass = 'dataTables_empty';
162 }
163 this.dataSource.load(this.pduInstanceData).then((data: {}) => {
164 this.isLoadingResults = false;
SANDHYA.JS0a34dfa2023-04-25 23:59:41 +0530165 }).catch((): void => {
166 // Catch Navigation Error
167 });
kumaran.m3b4814a2020-05-01 19:48:54 +0530168 }, (error: ERRORDATA) => {
169 this.restService.handleError(error, 'get');
170 this.isLoadingResults = false;
171 });
172 }
173
174 /** smart table listing manipulation @public */
175 public onChange(perPageValue: number): void {
176 this.dataSource.setPaging(1, perPageValue, true);
177 }
178
179 /** smart table listing manipulation @public */
180 public onUserRowSelect(event: MessageEvent): void {
181 Object.assign(event.data, { page: 'pdu-instances' });
182 this.dataService.changeMessage(event.data);
183 }
184
185 /** Add PDU Instance modal using modalservice @public */
186 public addPDUInstanceModal(): void {
SANDHYA.JS0a34dfa2023-04-25 23:59:41 +0530187 // eslint-disable-next-line security/detect-non-literal-fs-filename
kumaran.m3b4814a2020-05-01 19:48:54 +0530188 const modalRef: NgbModalRef = this.modalService.open(AddPDUInstancesComponent, { backdrop: 'static' });
189 modalRef.componentInstance.title = this.translateService.instant('PAGE.PDUINSTANCE.NEWPDUINSTANCE');
190 modalRef.result.then((result: MODALCLOSERESPONSEDATA) => {
191 if (result) {
192 this.generateData();
193 }
SANDHYA.JS0a34dfa2023-04-25 23:59:41 +0530194 }).catch((): void => {
195 // Catch Navigation Error
196 });
kumaran.m3b4814a2020-05-01 19:48:54 +0530197 }
198
199 /**
200 * Lifecyle hook which get trigger on component destruction
201 */
202 public ngOnDestroy(): void {
203 this.generateDataSub.unsubscribe();
204 }
205}