blob: d32526bb4b4e31157c291b5bbcf7dc000115e0ba [file] [log] [blame]
/********************************************************************************
* 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, Settings } 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 { AppConfigService } from '@grid-failure-information-table-app/app/app-config.service';
import { take } from 'rxjs/operators';
import { VisibilityEnum } from '@grid-failure-information-app/shared/constants/enums';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss'],
})
export class TableComponent implements OnInit, OnDestroy {
public Globals = Globals;
public VisibilityEnum = VisibilityEnum;
public columnDefs: any;
public defaultColDef: any;
public gridOptions: GridOptions;
public noRowsTemplate: string;
public gridFailures: GridFailure[];
private _gridApi;
private _gridFailuresAll: GridFailure[];
private _subscription: Subscription = new Subscription();
constructor(private _appTableService: AppTableService, private _configService: AppConfigService) {
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.add(
this._configService
.getConfig()
.pipe(take(1))
.subscribe((config: Settings) => {
if (config && config.visibilityConfiguration) {
APP_TABLE_COLDEF.forEach((column: any) => {
column['hide'] = config.visibilityConfiguration.tableExternColumnVisibility[column['field']] === VisibilityEnum.HIDE;
});
this.columnDefs = APP_TABLE_COLDEF;
}
})
);
this._subscription.add(
this._appTableService
.loadGridFailureData()
.pipe(take(1))
.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);
}
ngOnDestroy(): void {
this._subscription.unsubscribe();
}
}