import { Component, ViewChild, OnInit, Input, Output, EventEmitter, Inject, OnChanges, OnDestroy } from '@angular/core'; import { DatatableComponent } from '@swimlane/ngx-datatable'; import { Subscription } from 'rxjs'; import { HBORConfiguration } from '../../configuration/configuration'; import { HBORCommunicationService } from '../../services/hbor-communication.service'; import { HBORSettingsService } from '../../services/hbor-settings.service'; @Component({ selector: 'hbor-search-table', templateUrl: './search-table.component.html', styleUrls: ['./search-table.component.css'] }) export class SearchTableComponent implements OnInit, OnChanges, OnDestroy { @Output() itemSelected: EventEmitter = new EventEmitter(); @Input() searchPlaceholder = 'Unesite naziv klijenta za pretragu...'; @Input() rows: any[]; @Input() selected: any[] = []; @Input() timeout: any; @Input() columns: any[]; @Input() temp = []; @Input() scrollbarH: boolean; @Input() scrollbarV = false; @Input() height; @Input() hideSearch = false; @Input() showColumnPicker: boolean; @Input() footerMessages = { emptyMessage: 'Nema podataka', totalMessage: 'Ukupno' }; allColumns: any[]; @Input() rowHeight = 'auto'; @Input() headerHeight = 'auto'; @Input() footerHeight = 'auto'; @Input() limit: number; tableFontSize: number @Input() columnMode: string; @Input() showTimestampFields = false; @Input() searchProp = null; @Input() shouldFormatDate = true; subscriptionRowLimit: Subscription; dataLoaded = false; @ViewChild(DatatableComponent) table: DatatableComponent; constructor(private hborCommunicationService: HBORCommunicationService, private hborSettings: HBORSettingsService) { this.columnMode = 'standard'; } ngOnInit() { if (this.showTimestampFields === false) { this.setMenuColumns(); } this.subscribeOnSettings(); } ngOnChanges() { this.subscribeOnSettings(); if (this.rows !== undefined) { if (this.shouldFormatDate) { this.rows.forEach(element => { element.tsIns = new Date(element.tsIns).toLocaleDateString(HBORConfiguration.dateLocale) + ' ' + new Date(element.tsIns).toTimeString().slice(0, 8); if (this.showTimestampFields === false) { element.tsPromjena = element.tsPromjena == null ? null : new Date(element.tsPromjena).toLocaleDateString(HBORConfiguration.dateLocale) + ' ' + new Date(element.tsPromjena).toTimeString().slice(0, 8); element.tsPotvrda = element.tsPotvrda == null ? null : new Date(element.tsPotvrda).toLocaleDateString(HBORConfiguration.dateLocale) + ' ' + new Date(element.tsPotvrda).toTimeString().slice(0, 8); } }); } this.dataLoaded = true; } } ngOnDestroy() { if (this.subscriptionRowLimit != null) { this.subscriptionRowLimit.unsubscribe(); } } getHeight(): number { return this.height; } subscribeOnSettings() { if (this.limit == null) { this.limit = this.hborSettings.rowLimitRaw; this.subscriptionRowLimit = this.hborSettings.rowLimit$.subscribe( data => { if (data) { this.limit = data; this.table.recalculatePages(); this.dataLoaded = true; } }); } } updateFilter(event, shouldRemoveFilter?: boolean) { let val; let temp = []; if (shouldRemoveFilter) { val = ''; } else { val = event.target.value.toLowerCase(); } // filter our data // If searchProp is passed it will use search prom as filter parameter! if (this.searchProp != null) { if (this.searchProp.toString().split('.').length > 1) { temp = this.temp.filter(row => row[this.searchProp.toString().split('.')[0]][this.searchProp.toString().split('.')[1]].toString().toLowerCase().indexOf(val) !== -1 || !val); } else { temp = this.temp.filter(row => row[this.searchProp].toString().toLowerCase().indexOf(val) !== -1 || !val); } } else { temp = this.temp.filter(function (d) { if (d.partnerName != null) { return d.partnerName.toLowerCase().indexOf(val) !== -1 || !val } else if (d.nazivProtustranke != null) { return d.nazivProtustranke.toLowerCase().indexOf(val) !== -1 || !val; } else if (d.partner != null) { return d.partner.toLowerCase().indexOf(val) !== -1 || !val; } else if (d.osnovaOpis != null) { return d.osnovaOpis.toLowerCase().indexOf(val) !== -1 || !val; } else if (d.nazivOsnoveUlaganja != null) { return d.nazivOsnoveUlaganja.toLowerCase().indexOf(val) !== -1 || !val; } else if (d.opis != null) { return d.opis.toLowerCase().indexOf(val) !== -1 || !val; } else if (d.partnerNaziv != null) { return d.partnerNaziv.toLowerCase().indexOf(val) !== -1 || !val; } else if (d.naziv !== undefined && d.naziv != null) { return d.naziv.toLowerCase().indexOf(val) !== -1 || !val; } else if (d.holidayType !== undefined && d.holidayType != null) { return d.holidayType.toLowerCase().indexOf(val) !== -1 || !val; } else if (d.oib !== undefined && d.oib != null) { return d.oib.toLowerCase().indexOf(val) !== -1 || !val; } else if (d.datumOdTemp !== undefined && d.datumOdTemp != null) { return d.datumOdTemp.toLowerCase().indexOf(val) !== -1 || !val; } else { return (d.ime + d.prezime).toLowerCase().indexOf(val) !== -1 || !val; } }); } // update the rows this.rows = temp; // Whenever the filter changes, always go back to the first page this.table.offset = 0; } onPage(event) { clearTimeout(this.timeout); this.timeout = setTimeout(() => { }, 0); } getRowHeight(row) { return row.height; } onSelect({ selected }) { this.itemSelected.emit(this.selected); } onActivate(event) { // this.log().info('Activate Event', event); } recalculate() { this.table.recalculate(); } toggle(col) { const isChecked = this.isChecked(col); if (isChecked) { this.removeColumn(col); } else { this.addColumn(col); } } removeColumn(col) { this.columns = this.columns.filter(c => { return c.name.replace(/\
/g, ' ') !== col.name; }); } addColumn(col) { const columnDefault = Object.assign({}, col) columnDefault.name = columnDefault.name.replace(' ', '
'); this.columns = [...this.columns, columnDefault]; } isChecked(col) { return this.columns.find(c => { return c.prop === col.prop; }); } resetColumns($event) { this.columns = [] this.allColumns.forEach(element => { if (!(element.prop === 'tsIns' || element.prop === 'tsPromjena' || element.prop === 'tsPotvrda' || element.prop === 'kamatnaStopaKratkorocnoBv' || element.prop === 'kamatnaStopaDugorocnoBv' || element.prop === 'kamatnaStopaKratkorocnoVv' || element.prop === 'kamatnaStopaDugorocnoVv')) { this.addColumn(element) } }); } setMenuColumns() { this.allColumns = new Array(); this.columns.forEach(element => { this.allColumns.push(Object.assign({}, element)); if (element.prop === 'tsIns' || element.prop === 'tsPromjena' || element.prop === 'tsPotvrda' || element.prop === 'kamatnaStopaKratkorocnoBv' || element.prop === 'kamatnaStopaDugorocnoBv' || element.prop === 'kamatnaStopaKratkorocnoVv' || element.prop === 'kamatnaStopaDugorocnoVv') { element.name = element.name.replace(/\
/g, ' '); this.removeColumn(element); } }); this.allColumns.forEach(element => { element.name = element.name.replace(/\
/g, ' '); }); } }