Fix Bug 2121: NG-UI uses unmaintained Chokidar version
[osm/NG-UI.git] / src / app / projects / ProjectsComponent.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 Project details Component.
20  */
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';
36
37 /**
38  * Creating component
39  * @Component takes ProjectsComponent.html as template url
40  */
41 @Component({
42     selector: 'app-projects',
43     templateUrl: './ProjectsComponent.html',
44     styleUrls: ['./ProjectsComponent.scss']
45 })
46 /** Exporting a class @exports ProjectsComponent */
47 export class ProjectsComponent implements OnInit, OnDestroy {
48     /** To inject services @public */
49     public injector: Injector;
50
51     /** handle translate @public */
52     public translateService: TranslateService;
53
54     /** Data of smarttable populate through LocalDataSource @public */
55     public dataSource: LocalDataSource = new LocalDataSource();
56
57     /** Columns list of the smart table @public */
58     public columnLists: object = {};
59
60     /** Settings for smarttable to populate the table with columns @public */
61     public settings: object = {};
62
63     /** Check the loading results @public */
64     public isLoadingResults: boolean = true;
65
66     /** Give the message for the loading @public */
67     public message: string = 'PLEASEWAIT';
68
69     /** Instance of the rest service @private */
70     private restService: RestService;
71
72     /** dataService to pass the data from one component to another @private */
73     private dataService: DataService;
74
75     /** Instance of the modal service @private */
76     private modalService: NgbModal;
77
78     /** Formation of appropriate Data for LocalDatasource @private */
79     private projectData: ProjectData[] = [];
80
81     /** Contains all methods related to shared @private */
82     private sharedService: SharedService;
83
84     /** Instance of subscriptions @private */
85     private generateDataSub: Subscription;
86
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);
94     }
95
96     /** Lifecyle Hooks the trigger before component is instantiate @public */
97     public ngOnInit(): void {
98         this.generateColumns();
99         this.generateSettings();
100         this.generateData();
101         this.generateDataSub = this.sharedService.dataEvent.subscribe(() => { this.generateData(); });
102     }
103
104     /** smart table Header Colums @public */
105     public generateColumns(): void {
106         this.columnLists = {
107             projectName: {
108                 title: this.translateService.instant('NAME'), width: '55%', sortDirection: 'asc', type: 'custom',
109                 valuePrepareFunction: (cell: ProjectData, row: ProjectData): ProjectData => row,
110                 renderComponent: ProjectLinkComponent
111             },
112             modificationDate: { title: this.translateService.instant('MODIFIED'), width: '20%' },
113             creationDate: { title: this.translateService.instant('CREATED'), width: '20%' },
114             Actions: {
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
119             }
120         };
121     }
122
123     /** smart table Data Settings @public */
124     public generateSettings(): void {
125         this.settings = {
126             edit: {
127                 editButtonContent: '<i class="fa fa-edit" title="Edit"></i>',
128                 confirmSave: true
129             },
130             delete: {
131                 deleteButtonContent: '<i class="far fa-trash-alt" title="Delete"></i>',
132                 confirmDelete: true
133             },
134             columns: this.columnLists,
135             actions: {
136                 add: false,
137                 edit: false,
138                 delete: false,
139                 position: 'right'
140             },
141             attr: this.sharedService.tableClassConfig(),
142             pager: this.sharedService.paginationPagerConfig(),
143             noDataMessage: this.translateService.instant('NODATAMSG')
144         };
145     }
146
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) => {
153             if (result) {
154                 this.generateData();
155             }
156         }).catch((): void => {
157             // Catch Navigation Error
158         });
159     }
160
161     /** smart table listing manipulation @private */
162     public onChange(perPageValue: number): void {
163         this.dataSource.setPaging(1, perPageValue, true);
164     }
165
166     /** convert UserRowSelect Function @private */
167     public onUserRowSelect(event: MessageEvent): void {
168         Object.assign(event.data, { page: 'projects' });
169         this.dataService.changeMessage(event.data);
170     }
171
172     /** Generate Projects object from loop and return for the datasource @public */
173     public generateProjectData(projectData: ProjectDetails): ProjectData {
174         return {
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),
179             id: projectData._id,
180             project: projectData._id,
181             quotas: !isNullOrUndefined(projectData.quotas) && Object.keys(projectData.quotas).length !== 0 ? projectData.quotas : null
182         };
183     }
184
185     /**
186      * Lifecyle hook which get trigger on component destruction
187      */
188     public ngOnDestroy(): void {
189         this.generateDataSub.unsubscribe();
190     }
191
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);
200             });
201             this.dataSource.load(this.projectData).then((data: boolean) => {
202                 this.isLoadingResults = false;
203             }).catch((): void => {
204                 // Catch Navigation Error
205             });
206         }, (error: ERRORDATA) => {
207             this.restService.handleError(error, 'get');
208             this.isLoadingResults = false;
209         });
210     }
211 }