Fix Bug 2121: NG-UI uses unmaintained Chokidar version
[osm/NG-UI.git] / src / app / roles / roles-details / RolesDetailsComponent.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 Roles Deatils component.
20  */
21 import { isNullOrUndefined } from 'util';
22 import { Component, Injector, OnInit } from '@angular/core';
23 import { Router } from '@angular/router';
24 import { TranslateService } from '@ngx-translate/core';
25 import { ERRORDATA } from 'CommonModel';
26 import { DataService } from 'DataService';
27 import { environment } from 'environment';
28 import { LocalDataSource } from 'ng2-smart-table';
29 import { RestService } from 'RestService';
30 import { RolesActionComponent } from 'RolesAction';
31 import { RoleData, RoleDetails } from 'RolesModel';
32 import { Subscription } from 'rxjs';
33 import { SharedService } from 'SharedService';
34
35 /**
36  * Creating component
37  * @Component takes RolesComponent.html as template url
38  */
39 @Component({
40   selector: 'app-roles-details',
41   templateUrl: './RolesDetailsComponent.html',
42   styleUrls: ['./RolesDetailsComponent.scss']
43 })
44 export class RolesDetailsComponent implements OnInit {
45   /** To inject services @public */
46   public injector: Injector;
47
48   /** Formation of appropriate Data for LocalDatasource @public */
49   public dataSource: LocalDataSource = new LocalDataSource();
50
51   /** handle translate @public */
52   public translateService: TranslateService;
53
54   /** Columns list of the smart table @public */
55   public columnLists: 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   /** Instance of the rest service @private */
67   private restService: RestService;
68
69   /** dataService to pass the data from one component to another @private */
70   private dataService: DataService;
71
72   /** Contains role details data @private */
73   private roleData: RoleData[] = [];
74
75   /** Contains all methods related to shared @private */
76   private sharedService: SharedService;
77
78    /** Instance of subscriptions @private */
79   private generateDataSub: Subscription;
80
81   /** Holds the instance of roter service @private */
82   private router: Router;
83
84   constructor(injector: Injector) {
85     this.injector = injector;
86     this.restService = this.injector.get(RestService);
87     this.dataService = this.injector.get(DataService);
88     this.sharedService = this.injector.get(SharedService);
89     this.translateService = this.injector.get(TranslateService);
90     this.router = this.injector.get(Router);
91   }
92   /** Lifecyle Hooks the trigger before component is instantiate @public */
93   public ngOnInit(): void {
94     this.generateColumns();
95     this.generateSettings();
96     this.generateData();
97     this.generateDataSub = this.sharedService.dataEvent.subscribe(() => { this.generateData(); });
98   }
99   /** smart table Header Colums @public */
100   public generateColumns(): void {
101     this.columnLists = {
102       name: { title: this.translateService.instant('NAME'), width: '30%', sortDirection: 'asc' },
103       identifier: { title: this.translateService.instant('IDENTIFIER'), width: '35%' },
104       modified: { title: this.translateService.instant('MODIFIED'), width: '15%' },
105       created: { title: this.translateService.instant('CREATED'), width: '15%' },
106       Actions: {
107         name: 'Actions', width: '5%', filter: false, sort: false, type: 'custom',
108         title: this.translateService.instant('ACTIONS'),
109         valuePrepareFunction: (cell: RoleData, row: RoleData): RoleData => row,
110         renderComponent: RolesActionComponent
111       }
112     };
113   }
114
115   /** smart table Data Settings @public */
116   public generateSettings(): void {
117     this.settings = {
118       edit: {
119         editButtonContent: '<i class="fa fa-edit" title="Edit"></i>',
120         confirmSave: true
121       },
122       delete: {
123         deleteButtonContent: '<i class="far fa-trash-alt" title="delete"></i>',
124         confirmDelete: true
125       },
126       columns: this.columnLists,
127       actions: {
128         add: false,
129         edit: false,
130         delete: false,
131         topology: false,
132         position: 'right'
133       },
134       attr: this.sharedService.tableClassConfig(),
135       pager: this.sharedService.paginationPagerConfig(),
136       noDataMessage: this.translateService.instant('NODATAMSG')
137     };
138   }
139
140   /** smart table listing manipulation @public */
141   public onChange(perPageValue: number): void {
142     this.dataSource.setPaging(1, perPageValue, true);
143   }
144
145   /** convert UserRowSelect Function @private */
146   public onUserRowSelect(event: MessageEvent): void {
147     Object.assign(event.data, { page: 'roles' });
148     this.dataService.changeMessage(event.data);
149   }
150
151   /** Fetching the role data from API and Load it in the smarttable @public */
152   public generateData(): void {
153     this.isLoadingResults = true;
154     this.roleData = [];
155     this.restService.getResource(environment.ROLES_URL).subscribe((roleList: RoleDetails[]) => {
156       roleList.forEach((role: RoleDetails) => {
157         const roleDataObj: RoleData = this.generateRoleData(role);
158         this.roleData.push(roleDataObj);
159       });
160       this.dataSource.load(this.roleData).then((data: boolean) => {
161         this.isLoadingResults = false;
162       }).catch((): void => {
163         // Catch Navigation Error
164     });
165     }, (error: ERRORDATA) => {
166       this.restService.handleError(error, 'get');
167       this.isLoadingResults = false;
168     });
169   }
170
171   /** Generate role data object and return for the datasource @public */
172   public generateRoleData(roleData: RoleDetails): RoleData {
173     return {
174       name: roleData.name,
175       identifier: roleData._id,
176       modified: this.sharedService.convertEpochTime(!isNullOrUndefined(roleData._admin) ? Number(roleData._admin.modified) : null),
177       created: this.sharedService.convertEpochTime(!isNullOrUndefined(roleData._admin) ? Number(roleData._admin.created) : null),
178       permissions: roleData.permissions
179     };
180   }
181
182   /** Create role click handler @public */
183   public createRole(): void {
184     this.router.navigate(['/roles/create']).catch(() => {
185       // Catch Navigation Error
186     });
187   }
188
189   /** Lifecyle hook which get trigger on component destruction @private */
190   public ngOnDestroy(): void {
191     this.generateDataSub.unsubscribe();
192   }
193 }