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