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 } 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 const modalRef: NgbModalRef = this.modalService.open(ProjectCreateUpdateComponent, { backdrop: 'static' });
149 modalRef.componentInstance.projectType = 'Add';
150 modalRef.result.then((result: MODALCLOSERESPONSEDATA) => {
157 /** smart table listing manipulation @private */
158 public onChange(perPageValue: number): void {
159 this.dataSource.setPaging(1, perPageValue, true);
162 /** convert UserRowSelect Function @private */
163 public onUserRowSelect(event: MessageEvent): void {
164 Object.assign(event.data, { page: 'projects' });
165 this.dataService.changeMessage(event.data);
168 /** Generate Projects object from loop and return for the datasource @public */
169 public generateProjectData(projectData: ProjectDetails): ProjectData {
171 projectName: projectData.name,
172 modificationDate: this.sharedService.convertEpochTime(projectData._admin.modified),
173 creationDate: this.sharedService.convertEpochTime(projectData._admin.created),
175 project: projectData._id
180 * Lifecyle hook which get trigger on component destruction
182 public ngOnDestroy(): void {
183 this.generateDataSub.unsubscribe();
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);
195 this.dataSource.load(this.projectData).then((data: boolean) => {
196 this.isLoadingResults = false;
198 }, (error: ERRORDATA) => {
199 this.restService.handleError(error, 'get');
200 this.isLoadingResults = false;