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 Project details Component.
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 { ERRORDATA, MODALCLOSERESPONSEDATA } from 'CommonModel';
25 import { DataService } from 'DataService';
26 import { environment } from 'environment';
27 import { LocalDataSource } from 'ng2-smart-table';
28 import { ProjectCreateUpdateComponent } from 'ProjectCreateUpdate';
29 import { ProjectLinkComponent } from 'ProjectLinkComponent';
30 import { ProjectData, ProjectDetails } from 'ProjectModel';
31 import { ProjectsActionComponent } from 'ProjectsAction';
32 import { RestService } from 'RestService';
33 import { Subscription } from 'rxjs';
34 import { SharedService, isNullOrUndefined } from 'SharedService';
38 * @Component takes ProjectsComponent.html as template url
41 selector: 'app-projects',
42 templateUrl: './ProjectsComponent.html',
43 styleUrls: ['./ProjectsComponent.scss']
45 /** Exporting a class @exports ProjectsComponent */
46 export class ProjectsComponent implements OnInit, OnDestroy {
47 /** To inject services @public */
48 public injector: Injector;
50 /** handle translate @public */
51 public translateService: TranslateService;
53 /** Data of smarttable populate through LocalDataSource @public */
54 public dataSource: LocalDataSource = new LocalDataSource();
56 /** Columns list of the smart table @public */
57 public columnLists: object = {};
59 /** Settings for smarttable to populate the table with columns @public */
60 public settings: object = {};
62 /** Check the loading results @public */
63 public isLoadingResults: boolean = true;
65 /** Give the message for the loading @public */
66 public message: string = 'PLEASEWAIT';
68 /** Instance of the rest service @private */
69 private restService: RestService;
71 /** dataService to pass the data from one component to another @private */
72 private dataService: DataService;
74 /** Instance of the modal service @private */
75 private modalService: NgbModal;
77 /** Formation of appropriate Data for LocalDatasource @private */
78 private projectData: ProjectData[] = [];
80 /** Contains all methods related to shared @private */
81 private sharedService: SharedService;
83 /** Instance of subscriptions @private */
84 private generateDataSub: Subscription;
86 constructor(injector: Injector) {
87 this.injector = injector;
88 this.restService = this.injector.get(RestService);
89 this.dataService = this.injector.get(DataService);
90 this.sharedService = this.injector.get(SharedService);
91 this.modalService = this.injector.get(NgbModal);
92 this.translateService = this.injector.get(TranslateService);
95 /** Lifecyle Hooks the trigger before component is instantiate @public */
96 public ngOnInit(): void {
97 this.generateColumns();
98 this.generateSettings();
100 this.generateDataSub = this.sharedService.dataEvent.subscribe(() => { this.generateData(); });
103 /** smart table Header Colums @public */
104 public generateColumns(): void {
107 title: this.translateService.instant('NAME'), width: '55%', sortDirection: 'asc', type: 'custom',
108 valuePrepareFunction: (cell: ProjectData, row: ProjectData): ProjectData => row,
109 renderComponent: ProjectLinkComponent
111 modificationDate: { title: this.translateService.instant('MODIFIED'), width: '20%' },
112 creationDate: { title: this.translateService.instant('CREATED'), width: '20%' },
114 name: 'Action', width: '5%', filter: false, sort: false, type: 'custom',
115 title: this.translateService.instant('ACTIONS'),
116 valuePrepareFunction: (cell: ProjectData, row: ProjectData): ProjectData => row,
117 renderComponent: ProjectsActionComponent
122 /** smart table Data Settings @public */
123 public generateSettings(): void {
126 editButtonContent: '<i class="fa fa-edit" title="Edit"></i>',
130 deleteButtonContent: '<i class="far fa-trash-alt" title="Delete"></i>',
133 columns: this.columnLists,
140 attr: this.sharedService.tableClassConfig(),
141 pager: this.sharedService.paginationPagerConfig(),
142 noDataMessage: this.translateService.instant('NODATAMSG')
146 /** Modal service to initiate the project add @private */
147 public projectAdd(): void {
148 // eslint-disable-next-line security/detect-non-literal-fs-filename
149 const modalRef: NgbModalRef = this.modalService.open(ProjectCreateUpdateComponent, { size: 'lg', backdrop: 'static' });
150 modalRef.componentInstance.projectType = 'Add';
151 modalRef.result.then((result: MODALCLOSERESPONSEDATA) => {
155 }).catch((): void => {
156 // Catch Navigation Error
160 /** smart table listing manipulation @private */
161 public onChange(perPageValue: number): void {
162 this.dataSource.setPaging(1, perPageValue, true);
165 /** convert UserRowSelect Function @private */
166 public onUserRowSelect(event: MessageEvent): void {
167 Object.assign(event.data, { page: 'projects' });
168 this.dataService.changeMessage(event.data);
171 /** Generate Projects object from loop and return for the datasource @public */
172 public generateProjectData(projectData: ProjectDetails): ProjectData {
174 projectName: projectData.name,
175 modificationDate: this.sharedService.convertEpochTime(!isNullOrUndefined(projectData._admin)
176 ? projectData._admin.modified : null),
177 creationDate: this.sharedService.convertEpochTime(!isNullOrUndefined(projectData._admin) ? projectData._admin.created : null),
179 project: projectData._id,
180 quotas: !isNullOrUndefined(projectData.quotas) && Object.keys(projectData.quotas).length !== 0 ? projectData.quotas : null
185 * Lifecyle hook which get trigger on component destruction
187 public ngOnDestroy(): void {
188 this.generateDataSub.unsubscribe();
191 /** Fetching the data from server to Load in the smarttable @protected */
192 protected generateData(): void {
193 this.isLoadingResults = true;
194 this.restService.getResource(environment.PROJECTS_URL).subscribe((projectsData: ProjectDetails[]) => {
195 this.projectData = [];
196 projectsData.forEach((projectData: ProjectDetails) => {
197 const projectDataObj: ProjectData = this.generateProjectData(projectData);
198 this.projectData.push(projectDataObj);
200 this.dataSource.load(this.projectData).then((data: boolean) => {
201 this.isLoadingResults = false;
202 }).catch((): void => {
203 // Catch Navigation Error
205 }, (error: ERRORDATA) => {
206 this.restService.handleError(error, 'get');
207 this.isLoadingResults = false;