NG-UI Added support for the OSM Repository
[osm/NG-UI.git] / src / app / osm-repositories / osm-repositories-details / OsmRepositoriesDetailsComponent.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 OsmRepositoriesDetailsComponent.ts
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, OSMREPO_TYPES } from 'CommonModel';
25 import { DataService } from 'DataService';
26 import { environment } from 'environment';
27 import { LocalDataSource } from 'ng2-smart-table';
28 import { OsmRepoCreateUpdateComponent } from 'OsmRepoCreateUpdate';
29 import { OSMRepoData, OSMRepoDetails } from 'OsmRepoModel';
30 import { OsmRepositoriesActionComponent } from 'OsmRepositoriesAction';
31 import { RestService } from 'RestService';
32 import { Subscription } from 'rxjs';
33 import { SharedService } from 'SharedService';
34 /**
35  * Creating Component
36  * @Component takes OsmRepositoriesComponent.html as template url
37  */
38 @Component({
39   selector: 'app-osm-repositories-details',
40   templateUrl: './OsmRepositoriesDetailsComponent.html',
41   styleUrls: ['./OsmRepositoriesDetailsComponent.scss']
42 })
43 export class OsmRepositoriesDetailsComponent implements OnInit {
44   /** To inject services @public */
45   public injector: Injector;
46
47   /** handle translate @public */
48   public translateService: TranslateService;
49
50   /** Data of smarttable populate through LocalDataSource @public */
51   public dataSource: LocalDataSource = new LocalDataSource();
52
53   /** Columns list of the smart table @public */
54   public columnLists: object = {};
55
56   /** Settings for smarttable to populate the table with columns @public */
57   public settings: object = {};
58
59   /** Check the loading results @public */
60   public isLoadingResults: boolean = true;
61
62   /** Give the message for the loading @public */
63   public message: string = 'PLEASEWAIT';
64
65   /** Contains osm repo details data @public */
66   public osmRepoData: OSMRepoData[] = [];
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   /** Contains all methods related to shared @private */
78   private sharedService: SharedService;
79
80   /** Instance of subscriptions @private */
81   private generateDataSub: Subscription;
82
83   // creates osm repository component
84   constructor(injector: Injector) {
85     this.injector = injector;
86     this.restService = this.injector.get(RestService);
87     this.dataService = this.injector.get(DataService);
88     this.sharedService = this.injector.get(SharedService);
89     this.modalService = this.injector.get(NgbModal);
90     this.translateService = this.injector.get(TranslateService);
91   }
92
93   /** Lifecyle Hooks the trigger before component is instantiate @public */
94   public ngOnInit(): void {
95     this.generateColumns();
96     this.generateSettings();
97     this.generateData();
98     this.generateDataSub = this.sharedService.dataEvent.subscribe(() => { this.generateData(); });
99   }
100
101   /** smart table listing manipulation @private */
102   public onChange(perPageValue: number): void {
103     this.dataSource.setPaging(1, perPageValue, true);
104   }
105
106   /** convert UserRowSelect Function @private */
107   public onUserRowSelect(event: MessageEvent): void {
108     Object.assign(event.data, { page: 'osmrepo' });
109     this.dataService.changeMessage(event.data);
110   }
111
112   /** smart table Header Colums @public */
113   public generateColumns(): void {
114     this.columnLists = {
115       name: { title: this.translateService.instant('NAME'), width: '15%', sortDirection: 'asc' },
116       identifier: { title: this.translateService.instant('IDENTIFIER'), width: '15%' },
117       url: { title: this.translateService.instant('URL'), width: '15%' },
118       type: {
119         title: this.translateService.instant('TYPE'), width: '15%',
120         filter: {
121           type: 'list',
122           config: {
123             selectText: 'Select',
124             list: OSMREPO_TYPES
125           }
126         }
127       },
128       modified: { title: this.translateService.instant('MODIFIED'), width: '15%' },
129       created: { title: this.translateService.instant('CREATED'), width: '15%' },
130       Actions: {
131         name: 'Action', width: '10%', filter: false, sort: false, type: 'custom',
132         title: this.translateService.instant('ACTIONS'),
133         valuePrepareFunction: (cell: OSMRepoData, row: OSMRepoData): OSMRepoData => row,
134         renderComponent: OsmRepositoriesActionComponent
135       }
136     };
137   }
138
139   /** smart table Data Settings @public */
140   public generateSettings(): void {
141     this.settings = {
142       columns: this.columnLists,
143       actions: {
144         add: false,
145         edit: false,
146         delete: false,
147         position: 'right'
148       },
149       attr: this.sharedService.tableClassConfig(),
150       pager: this.sharedService.paginationPagerConfig(),
151       noDataMessage: this.translateService.instant('NODATAMSG')
152     };
153   }
154
155   /** Generate osmRepoData object from loop and return for the datasource @public */
156   public generateOsmRepoData(osmRepo: OSMRepoDetails): OSMRepoData {
157     return {
158       name: osmRepo.name,
159       identifier: osmRepo._id,
160       url: osmRepo.url,
161       type: osmRepo.type,
162       description: osmRepo.description,
163       modified: this.sharedService.convertEpochTime(Number(osmRepo._admin.modified)),
164       created: this.sharedService.convertEpochTime(Number(osmRepo._admin.created))
165     };
166   }
167
168   /** Create a osm repo @public */
169   public addOsmrepo(): void {
170     const modalRef: NgbModalRef = this.modalService.open(OsmRepoCreateUpdateComponent, { backdrop: 'static' });
171     modalRef.componentInstance.createupdateType = 'Add';
172     modalRef.result.then((result: MODALCLOSERESPONSEDATA) => {
173       if (result) {
174         this.generateData();
175       }
176     }).catch();
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.OSMREPOS_URL).subscribe((osmRepoData: OSMRepoDetails[]) => {
190       this.osmRepoData = [];
191       osmRepoData.forEach((osmRepo: OSMRepoDetails) => {
192         const osmRepoDataObj: OSMRepoData = this.generateOsmRepoData(osmRepo);
193         this.osmRepoData.push(osmRepoDataObj);
194       });
195       this.dataSource.load(this.osmRepoData).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 }