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 { 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 import { isNullOrUndefined } from 'util';
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;
163 }, (error: ERRORDATA) => {
164 this.restService.handleError(error, 'get');
165 this.isLoadingResults = false;
169 /** Generate role data object and return for the datasource @public */
170 public generateRoleData(roleData: RoleDetails): RoleData {
173 identifier: roleData._id,
174 modified: this.sharedService.convertEpochTime(!isNullOrUndefined(roleData._admin) ? Number(roleData._admin.modified) : null),
175 created: this.sharedService.convertEpochTime(!isNullOrUndefined(roleData._admin) ? Number(roleData._admin.created) : null),
176 permissions: roleData.permissions
180 /** Create role click handler @public */
181 public createRole(): void {
182 this.router.navigate(['/roles/create']).catch(() => {
183 // Catch Navigation Error
187 /** Lifecyle hook which get trigger on component destruction @private */
188 public ngOnDestroy(): void {
189 this.generateDataSub.unsubscribe();