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 OsmRepositoriesDetailsComponent.ts
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';
36 * @Component takes OsmRepositoriesComponent.html as template url
39 selector: 'app-osm-repositories-details',
40 templateUrl: './OsmRepositoriesDetailsComponent.html',
41 styleUrls: ['./OsmRepositoriesDetailsComponent.scss']
43 export class OsmRepositoriesDetailsComponent implements OnInit {
44 /** To inject services @public */
45 public injector: Injector;
47 /** handle translate @public */
48 public translateService: TranslateService;
50 /** Data of smarttable populate through LocalDataSource @public */
51 public dataSource: LocalDataSource = new LocalDataSource();
53 /** Columns list of the smart table @public */
54 public columnLists: object = {};
56 /** Settings for smarttable to populate the table with columns @public */
57 public settings: object = {};
59 /** Check the loading results @public */
60 public isLoadingResults: boolean = true;
62 /** Give the message for the loading @public */
63 public message: string = 'PLEASEWAIT';
65 /** Contains osm repo details data @public */
66 public osmRepoData: OSMRepoData[] = [];
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 /** Contains all methods related to shared @private */
78 private sharedService: SharedService;
80 /** Instance of subscriptions @private */
81 private generateDataSub: Subscription;
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);
93 /** Lifecyle Hooks the trigger before component is instantiate @public */
94 public ngOnInit(): void {
95 this.generateColumns();
96 this.generateSettings();
98 this.generateDataSub = this.sharedService.dataEvent.subscribe(() => { this.generateData(); });
101 /** smart table listing manipulation @private */
102 public onChange(perPageValue: number): void {
103 this.dataSource.setPaging(1, perPageValue, true);
106 /** convert UserRowSelect Function @private */
107 public onUserRowSelect(event: MessageEvent): void {
108 Object.assign(event.data, { page: 'osmrepo' });
109 this.dataService.changeMessage(event.data);
112 /** smart table Header Colums @public */
113 public generateColumns(): void {
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%' },
119 title: this.translateService.instant('TYPE'), width: '15%',
123 selectText: 'Select',
128 modified: { title: this.translateService.instant('MODIFIED'), width: '15%' },
129 created: { title: this.translateService.instant('CREATED'), width: '15%' },
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
139 /** smart table Data Settings @public */
140 public generateSettings(): void {
142 columns: this.columnLists,
149 attr: this.sharedService.tableClassConfig(),
150 pager: this.sharedService.paginationPagerConfig(),
151 noDataMessage: this.translateService.instant('NODATAMSG')
155 /** Generate osmRepoData object from loop and return for the datasource @public */
156 public generateOsmRepoData(osmRepo: OSMRepoDetails): OSMRepoData {
159 identifier: osmRepo._id,
162 description: osmRepo.description,
163 modified: this.sharedService.convertEpochTime(Number(osmRepo._admin.modified)),
164 created: this.sharedService.convertEpochTime(Number(osmRepo._admin.created))
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) => {
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.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);
195 this.dataSource.load(this.osmRepoData).then((data: boolean) => {
196 this.isLoadingResults = false;
198 }, (error: ERRORDATA) => {
199 this.restService.handleError(error, 'get');
200 this.isLoadingResults = false;