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