Initial Commit - NG UI
[osm/NG-UI.git] / src / app / wim-accounts / wim-account-details / WIMAccountDetailsComponent.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 WIM Account Component.
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 { 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';
34 /**
35  * Creating component
36  * @Component takes WIMAccountDetailsComponent.html as template url
37  */
38 @Component({
39     templateUrl: './WIMAccountDetailsComponent.html',
40     styleUrls: ['./WIMAccountDetailsComponent.scss']
41 })
42 /** Exporting a class @exports WIMAccountDetailsComponent */
43 export class WIMAccountDetailsComponent implements OnInit, OnDestroy {
44     /** To inject services @public */
45     public injector: Injector;
46
47     /** handle translate @public */
48     public translateService: TranslateService;
49
50     /** Data of smarttable populate through LocalDataSource @public */
51     public dataSource: LocalDataSource = new LocalDataSource();
52
53     /** Columns list of the smart table @public */
54     public columnLists: object = {};
55
56     /** Settings for smarttable to populate the table with columns @public */
57     public settings: object = {};
58
59     /** Check the loading results @public */
60     public isLoadingResults: boolean = true;
61
62     /** Give the message for the loading @public */
63     public message: string = 'PLEASEWAIT';
64
65     /** Class for empty and present data @public */
66     public checkDataClass: string;
67
68     /** operational State init data @public */
69     public operationalStateFirstStep: string = CONFIGCONSTANT.wimOperationalStateFirstStep;
70
71     /** operational State running data @public */
72     public operationalStateSecondStep: string = CONFIGCONSTANT.wimOperationalStateStateSecondStep;
73
74     /** operational State failed data @public */
75     public operationalStateThirdStep: string = CONFIGCONSTANT.wimOperationalStateThirdStep;
76
77     /** Instance of the rest service @private */
78     private restService: RestService;
79
80     /** dataService to pass the data from one component to another @private */
81     private dataService: DataService;
82
83     /** Formation of appropriate Data for LocalDatasource @private */
84     private wimData: {}[] = [];
85
86     /** Contains all methods related to shared @private */
87     private sharedService: SharedService;
88
89     /** Instance of the modal service @private */
90     private modalService: NgbModal;
91
92     /** Instance of subscriptions @private */
93     private generateDataSub: Subscription;
94
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);
102     }
103     /** Lifecyle Hooks the trigger before component is instantiate @public */
104     public ngOnInit(): void {
105         this.generateColumns();
106         this.generateSettings();
107         this.generateData();
108         this.generateDataSub = this.sharedService.dataEvent.subscribe(() => { this.generateData(); });
109     }
110
111     /** smart table Header Colums @public */
112     public generateColumns(): void {
113         this.columnLists = {
114             name: { title: this.translateService.instant('NAME'), width: '20%', sortDirection: 'asc' },
115             identifier: { title: this.translateService.instant('IDENTIFIER'), width: '20%' },
116             type: {
117                 title: this.translateService.instant('TYPE'), width: '15%',
118                 filter: {
119                     type: 'list',
120                     config: {
121                         selectText: 'Select',
122                         list: WIM_TYPES
123                     }
124                 }
125             },
126             operationalState: {
127                 title: this.translateService.instant('OPERATIONALSTATUS'), width: '15%', type: 'html',
128                 filter: {
129                     type: 'list',
130                     config: {
131                         selectText: 'Select',
132                         list: [
133                             { value: this.operationalStateFirstStep, title: this.operationalStateFirstStep },
134                             { value: this.operationalStateSecondStep, title: this.operationalStateSecondStep },
135                             { value: this.operationalStateThirdStep, title: this.operationalStateThirdStep }
136                         ]
137                     }
138                 },
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>
143                         </span>`;
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>
147                         </span>`;
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>
151                         </span>`;
152                     } else {
153                         return `<span>${row.operationalState}</span>`;
154                     }
155                 }
156             },
157             description: { title: this.translateService.instant('DESCRIPTION'), width: '25%' },
158             Actions: {
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
162             }
163         };
164     }
165
166     /** smart table Data Settings @public */
167     public generateSettings(): void {
168         this.settings = {
169             edit: {
170                 editButtonContent: '<i class="fa fa-edit" title="Edit"></i>', confirmSave: true
171             },
172             delete: {
173                 deleteButtonContent: '<i class="far fa-trash-alt" title="delete"></i>', confirmDelete: true
174             },
175             columns: this.columnLists,
176             actions: {
177                 add: false, edit: false, delete: false, position: 'right'
178             },
179             attr: this.sharedService.tableClassConfig(),
180             pager: this.sharedService.paginationPagerConfig(),
181             noDataMessage: this.translateService.instant('NODATAMSG')
182         };
183     }
184
185     /** smart table listing manipulation @public */
186     public onChange(perPageValue: number): void {
187         this.dataSource.setPaging(1, perPageValue, true);
188     }
189
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);
194     }
195
196     /** Compose new WIM Accounts @public */
197     public composeWIM(): void {
198         const modalRef: NgbModalRef = this.modalService.open(NewWIMAccountComponent, { backdrop: 'static' });
199         modalRef.result.then((result: MODALCLOSERESPONSEDATA) => {
200             if (result) {
201                 this.sharedService.callData();
202             }
203         }).catch();
204     }
205
206     /** Generate generateWIMData object from loop and return for the datasource @public */
207     public generateWIMData(wimAccountData: WIMAccountModel): WIMAccountData {
208         return {
209             name: wimAccountData.name,
210             identifier: wimAccountData._id,
211             type: wimAccountData.wim_type,
212             operationalState: wimAccountData._admin.operationalState,
213             description: wimAccountData.description
214         };
215     }
216
217     /**
218      * Lifecyle hook which get trigger on component destruction
219      */
220     public ngOnDestroy(): void {
221         this.generateDataSub.unsubscribe();
222     }
223
224     /** Fetching the data from server to Load in the smarttable @protected */
225     protected generateData(): void {
226         this.isLoadingResults = true;
227         this.wimData = [];
228         this.restService.getResource(environment.WIMACCOUNTS_URL).subscribe((wimAccountsDetails: {}[]) => {
229             wimAccountsDetails.forEach((wimAccountsData: WIMAccountModel) => {
230                 const wimDataObj: WIMAccountData = this.generateWIMData(wimAccountsData);
231                 this.wimData.push(wimDataObj);
232             });
233             if (this.wimData.length > 0) {
234                 this.checkDataClass = 'dataTables_present';
235             } else {
236                 this.checkDataClass = 'dataTables_empty';
237             }
238             this.dataSource.load(this.wimData).then((data: {}) => {
239                 this.isLoadingResults = false;
240             }).catch();
241         }, (error: ERRORDATA) => {
242             this.restService.handleError(error, 'get');
243             this.isLoadingResults = false;
244         });
245     }
246 }