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 WIM Account Component.
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 { CONFIGCONSTANT, ERRORDATA, MODALCLOSERESPONSEDATA, WIM_TYPES } from 'CommonModel';
25 import { DataService } from 'DataService';
26 import { environment } from 'environment';
27 import { NewWIMAccountComponent } from 'NewWIMAccount';
28 import { LocalDataSource } from 'ng2-smart-table';
29 import { RestService } from 'RestService';
30 import { Subscription } from 'rxjs';
31 import { SharedService } from 'SharedService';
32 import { WIMAccountData, WIMAccountModel } from 'WIMAccountModel';
33 import { WIMAccountsActionComponent } from 'WIMAccountsAction';
36 * @Component takes WIMAccountDetailsComponent.html as template url
39 templateUrl: './WIMAccountDetailsComponent.html',
40 styleUrls: ['./WIMAccountDetailsComponent.scss']
42 /** Exporting a class @exports WIMAccountDetailsComponent */
43 export class WIMAccountDetailsComponent implements OnInit, OnDestroy {
44 /** To inject services @public */
45 public injector: Injector;
47 /** handle translate @public */
48 public translateService: TranslateService;
50 /** Data of smarttable populate through LocalDataSource @public */
51 public dataSource: LocalDataSource = new LocalDataSource();
53 /** Columns list of the smart table @public */
54 public columnLists: object = {};
56 /** Settings for smarttable to populate the table with columns @public */
57 public settings: object = {};
59 /** Check the loading results @public */
60 public isLoadingResults: boolean = true;
62 /** Give the message for the loading @public */
63 public message: string = 'PLEASEWAIT';
65 /** Class for empty and present data @public */
66 public checkDataClass: string;
68 /** operational State init data @public */
69 public operationalStateFirstStep: string = CONFIGCONSTANT.wimOperationalStateFirstStep;
71 /** operational State running data @public */
72 public operationalStateSecondStep: string = CONFIGCONSTANT.wimOperationalStateStateSecondStep;
74 /** operational State failed data @public */
75 public operationalStateThirdStep: string = CONFIGCONSTANT.wimOperationalStateThirdStep;
77 /** Instance of the rest service @private */
78 private restService: RestService;
80 /** dataService to pass the data from one component to another @private */
81 private dataService: DataService;
83 /** Formation of appropriate Data for LocalDatasource @private */
84 private wimData: {}[] = [];
86 /** Contains all methods related to shared @private */
87 private sharedService: SharedService;
89 /** Instance of the modal service @private */
90 private modalService: NgbModal;
92 /** Instance of subscriptions @private */
93 private generateDataSub: Subscription;
95 constructor(injector: Injector) {
96 this.injector = injector;
97 this.restService = this.injector.get(RestService);
98 this.dataService = this.injector.get(DataService);
99 this.sharedService = this.injector.get(SharedService);
100 this.translateService = this.injector.get(TranslateService);
101 this.modalService = this.injector.get(NgbModal);
103 /** Lifecyle Hooks the trigger before component is instantiate @public */
104 public ngOnInit(): void {
105 this.generateColumns();
106 this.generateSettings();
108 this.generateDataSub = this.sharedService.dataEvent.subscribe(() => { this.generateData(); });
111 /** smart table Header Colums @public */
112 public generateColumns(): void {
114 name: { title: this.translateService.instant('NAME'), width: '20%', sortDirection: 'asc' },
115 identifier: { title: this.translateService.instant('IDENTIFIER'), width: '20%' },
117 title: this.translateService.instant('TYPE'), width: '15%',
121 selectText: 'Select',
127 title: this.translateService.instant('OPERATIONALSTATUS'), width: '15%', type: 'html',
131 selectText: 'Select',
133 { value: this.operationalStateFirstStep, title: this.operationalStateFirstStep },
134 { value: this.operationalStateSecondStep, title: this.operationalStateSecondStep },
135 { value: this.operationalStateThirdStep, title: this.operationalStateThirdStep }
139 valuePrepareFunction: (cell: WIMAccountData, row: WIMAccountData): string => {
140 if (row.operationalState === this.operationalStateFirstStep) {
141 return `<span class="icon-label" title="${row.operationalState}">
142 <i class="fas fa-clock text-warning"></i>
144 } else if (row.operationalState === this.operationalStateSecondStep) {
145 return `<span class="icon-label" title="${row.operationalState}">
146 <i class="fas fa-check-circle text-success"></i>
148 } else if (row.operationalState === this.operationalStateThirdStep) {
149 return `<span class="icon-label" title="${row.operationalState}">
150 <i class="fas fa-times-circle text-danger"></i>
153 return `<span>${row.operationalState}</span>`;
157 description: { title: this.translateService.instant('DESCRIPTION'), width: '25%' },
159 name: 'Action', width: '5%', filter: false, sort: false, title: this.translateService.instant('ACTIONS'), type: 'custom',
160 valuePrepareFunction: (cell: WIMAccountData, row: WIMAccountData): WIMAccountData => row,
161 renderComponent: WIMAccountsActionComponent
166 /** smart table Data Settings @public */
167 public generateSettings(): void {
170 editButtonContent: '<i class="fa fa-edit" title="Edit"></i>', confirmSave: true
173 deleteButtonContent: '<i class="far fa-trash-alt" title="delete"></i>', confirmDelete: true
175 columns: this.columnLists,
177 add: false, edit: false, delete: false, position: 'right'
179 attr: this.sharedService.tableClassConfig(),
180 pager: this.sharedService.paginationPagerConfig(),
181 noDataMessage: this.translateService.instant('NODATAMSG')
185 /** smart table listing manipulation @public */
186 public onChange(perPageValue: number): void {
187 this.dataSource.setPaging(1, perPageValue, true);
190 /** smart table listing manipulation @public */
191 public onUserRowSelect(event: MessageEvent): void {
192 Object.assign(event.data, { page: 'wim-account' });
193 this.dataService.changeMessage(event.data);
196 /** Compose new WIM Accounts @public */
197 public composeWIM(): void {
198 // eslint-disable-next-line security/detect-non-literal-fs-filename
199 const modalRef: NgbModalRef = this.modalService.open(NewWIMAccountComponent, { backdrop: 'static' });
200 modalRef.result.then((result: MODALCLOSERESPONSEDATA) => {
202 this.sharedService.callData();
204 }).catch((): void => {
205 // Catch Navigation Error
209 /** Generate generateWIMData object from loop and return for the datasource @public */
210 public generateWIMData(wimAccountData: WIMAccountModel): WIMAccountData {
212 name: wimAccountData.name,
213 identifier: wimAccountData._id,
214 type: wimAccountData.wim_type,
215 operationalState: wimAccountData._admin.operationalState,
216 description: wimAccountData.description
221 * Lifecyle hook which get trigger on component destruction
223 public ngOnDestroy(): void {
224 this.generateDataSub.unsubscribe();
227 /** Fetching the data from server to Load in the smarttable @protected */
228 protected generateData(): void {
229 this.isLoadingResults = true;
231 this.restService.getResource(environment.WIMACCOUNTS_URL).subscribe((wimAccountsDetails: {}[]) => {
232 wimAccountsDetails.forEach((wimAccountsData: WIMAccountModel) => {
233 const wimDataObj: WIMAccountData = this.generateWIMData(wimAccountsData);
234 this.wimData.push(wimDataObj);
236 if (this.wimData.length > 0) {
237 this.checkDataClass = 'dataTables_present';
239 this.checkDataClass = 'dataTables_empty';
241 this.dataSource.load(this.wimData).then((data: {}) => {
242 this.isLoadingResults = false;
243 }).catch((): void => {
244 // Catch Navigation Error
246 }, (error: ERRORDATA) => {
247 this.restService.handleError(error, 'get');
248 this.isLoadingResults = false;