| /******************************************************************************** |
| * Copyright (c) 2020 Contributors to the Eclipse Foundation |
| * |
| * See the NOTICE file(s) distributed with this work for additional |
| * information regarding copyright ownership. |
| * |
| * This program and the accompanying materials are made available under the |
| * terms of the Eclipse Public License v. 2.0 which is available at |
| * http://www.eclipse.org/legal/epl-2.0. |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| ********************************************************************************/ |
| import { Component, Input, OnInit, OnDestroy } from '@angular/core'; |
| import { AppTableService } from '@grid-failure-information-table-app/app/app-table.service'; |
| import { GridFailure } from '@grid-failure-information-app/shared/models'; |
| import { APP_TABLE_COLDEF } from '@grid-failure-information-table-app/app/app-table-column-definition'; |
| import { GridOptions } from 'ag-grid-community'; |
| import { Globals } from '@grid-failure-information-app/shared/constants/globals'; |
| import { Subscription } from 'rxjs'; |
| import { DatePipe } from '@angular/common'; |
| |
| @Component({ |
| selector: 'app-root', |
| templateUrl: './app.component.html', |
| styleUrls: ['./app.component.scss'], |
| }) |
| export class TableComponent implements OnInit, OnDestroy { |
| public Globals = Globals; |
| public columnDefs: any = APP_TABLE_COLDEF; |
| public defaultColDef: any; |
| public gridOptions: GridOptions; |
| public noRowsTemplate: string; |
| public gridFailures: GridFailure[]; |
| public lastModDate: string; |
| |
| private _gridApi; |
| private _gridFailuresAll: GridFailure[]; |
| private _subscription: Subscription; |
| |
| constructor(private _appTableService: AppTableService, private _datePipe: DatePipe) { |
| this.defaultColDef = { |
| sortable: true, |
| suppressMovable: true, |
| }; |
| this.noRowsTemplate = `<span>Keine Einträge</span>`; |
| } |
| @Input() set postcode(value: string) { |
| value = value.trim(); |
| let filterFunc = (x: GridFailure) => x.postcode === value || x.freetextPostcode === value; |
| this.gridFailures = value.length > 0 ? this._gridFailuresAll.filter(filterFunc) : this._gridFailuresAll; |
| } |
| |
| ngOnInit() { |
| this._subscription = this._appTableService.loadGridFailureData().subscribe((data: GridFailure[]) => { |
| this.gridFailures = data; |
| this._gridFailuresAll = data; |
| }); |
| this.gridOptions = { |
| localeText: Globals.LOCALE_TEXT, |
| }; |
| } |
| |
| onGridReady(params): void { |
| this._gridApi = params.api; |
| const sortModel = [{ colId: 'failureBegin', sort: 'desc' }]; |
| this._gridApi.setSortModel(sortModel); |
| this.lastModDate = this._datePipe.transform(this._getLastModeDate(), Globals.DATE_TIME_FORMAT); |
| } |
| |
| ngOnDestroy(): void { |
| this._subscription.unsubscribe(); |
| } |
| |
| private _getLastModeDate(): number{ |
| let modeDates:number[] = this.gridFailures.map(gf => Date.parse(gf.modDate)); |
| modeDates = modeDates.sort((a,b) => b-a); // sort timestamps descending |
| return modeDates[0]; |
| } |
| } |