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';
35 import { isNullOrUndefined } from 'util';
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 const modalRef: NgbModalRef = this.modalService.open(ProjectCreateUpdateComponent, { backdrop: 'static' });
150 modalRef.componentInstance.projectType = 'Add';
151 modalRef.result.then((result: MODALCLOSERESPONSEDATA) => {
158 /** smart table listing manipulation @private */
159 public onChange(perPageValue: number): void {
160 this.dataSource.setPaging(1, perPageValue, true);
163 /** convert UserRowSelect Function @private */
164 public onUserRowSelect(event: MessageEvent): void {
165 Object.assign(event.data, { page: 'projects' });
166 this.dataService.changeMessage(event.data);
169 /** Generate Projects object from loop and return for the datasource @public */
170 public generateProjectData(projectData: ProjectDetails): ProjectData {
172 projectName: projectData.name,
173 modificationDate: this.sharedService.convertEpochTime(!isNullOrUndefined(projectData._admin)
174 ? projectData._admin.modified : null),
175 creationDate: this.sharedService.convertEpochTime(!isNullOrUndefined(projectData._admin) ? projectData._admin.created : null),
177 project: projectData._id,
178 quotas: !isNullOrUndefined(projectData.quotas) ? projectData.quotas : null
183 * Lifecyle hook which get trigger on component destruction
185 public ngOnDestroy(): void {
186 this.generateDataSub.unsubscribe();
189 /** Fetching the data from server to Load in the smarttable @protected */
190 protected generateData(): void {
191 this.isLoadingResults = true;
192 this.restService.getResource(environment.PROJECTS_URL).subscribe((projectsData: ProjectDetails[]) => {
193 this.projectData = [];
194 projectsData.forEach((projectData: ProjectDetails) => {
195 const projectDataObj: ProjectData = this.generateProjectData(projectData);
196 this.projectData.push(projectDataObj);
198 this.dataSource.load(this.projectData).then((data: boolean) => {
199 this.isLoadingResults = false;
201 }, (error: ERRORDATA) => {
202 this.restService.handleError(error, 'get');
203 this.isLoadingResults = false;