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 Roles Deatils component.
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';
37 * @Component takes RolesComponent.html as template url
40 selector: 'app-roles-details',
41 templateUrl: './RolesDetailsComponent.html',
42 styleUrls: ['./RolesDetailsComponent.scss']
44 export class RolesDetailsComponent implements OnInit {
45 /** To inject services @public */
46 public injector: Injector;
48 /** Formation of appropriate Data for LocalDatasource @public */
49 public dataSource: LocalDataSource = new LocalDataSource();
51 /** handle translate @public */
52 public translateService: TranslateService;
54 /** Columns list of the smart table @public */
55 public columnLists: object = {};
57 /** Settings for smarttable to populate the table with columns @public */
58 public settings: object = {};
60 /** Check the loading results @public */
61 public isLoadingResults: boolean = true;
63 /** Give the message for the loading @public */
64 public message: string = 'PLEASEWAIT';
66 /** Instance of the rest service @private */
67 private restService: RestService;
69 /** dataService to pass the data from one component to another @private */
70 private dataService: DataService;
72 /** Contains role details data @private */
73 private roleData: RoleData[] = [];
75 /** Contains all methods related to shared @private */
76 private sharedService: SharedService;
78 /** Instance of subscriptions @private */
79 private generateDataSub: Subscription;
81 /** Holds the instance of roter service @private */
82 private router: Router;
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);
92 /** Lifecyle Hooks the trigger before component is instantiate @public */
93 public ngOnInit(): void {
94 this.generateColumns();
95 this.generateSettings();
97 this.generateDataSub = this.sharedService.dataEvent.subscribe(() => { this.generateData(); });
99 /** smart table Header Colums @public */
100 public generateColumns(): void {
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%' },
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
115 /** smart table Data Settings @public */
116 public generateSettings(): void {
119 editButtonContent: '<i class="fa fa-edit" title="Edit"></i>',
123 deleteButtonContent: '<i class="far fa-trash-alt" title="delete"></i>',
126 columns: this.columnLists,
134 attr: this.sharedService.tableClassConfig(),
135 pager: this.sharedService.paginationPagerConfig(),
136 noDataMessage: this.translateService.instant('NODATAMSG')
140 /** smart table listing manipulation @public */
141 public onChange(perPageValue: number): void {
142 this.dataSource.setPaging(1, perPageValue, true);
145 /** convert UserRowSelect Function @private */
146 public onUserRowSelect(event: MessageEvent): void {
147 Object.assign(event.data, { page: 'roles' });
148 this.dataService.changeMessage(event.data);
151 /** Fetching the role data from API and Load it in the smarttable @public */
152 public generateData(): void {
153 this.isLoadingResults = true;
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);
160 this.dataSource.load(this.roleData).then((data: boolean) => {
161 this.isLoadingResults = false;
162 }).catch((): void => {
163 // Catch Navigation Error
165 }, (error: ERRORDATA) => {
166 this.restService.handleError(error, 'get');
167 this.isLoadingResults = false;
171 /** Generate role data object and return for the datasource @public */
172 public generateRoleData(roleData: RoleDetails): RoleData {
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
182 /** Create role click handler @public */
183 public createRole(): void {
184 this.router.navigate(['/roles/create']).catch(() => {
185 // Catch Navigation Error
189 /** Lifecyle hook which get trigger on component destruction @private */
190 public ngOnDestroy(): void {
191 this.generateDataSub.unsubscribe();