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