import { Component, OnInit, Input, EventEmitter, OnChanges, Output } from '@angular/core'; import { FileManagementService } from '../file-management.service'; @Component({ selector: 'lib-file-summary', templateUrl: './file-summary.component.html', styleUrls: ['./file-summary.component.css'] }) export class FileSummaryComponent implements OnInit, OnChanges { _opened = true; sidebar3Opened = true; reportsToDisplay: any; selectedOptionDocs: any; sortColumn = ''; sliceLength = 3; @Input() viewMode; @Input() dataSource; @Input() breadCrumbs; @Output() editFile = new EventEmitter(); @Output() deleteFile = new EventEmitter(); @Output() openFile = new EventEmitter(); @Output() changeView = new EventEmitter(); constructor(private service: FileManagementService) { } ngOnInit() { } ngOnChanges(changes) { if (changes && changes.dataSource && changes.dataSource.currentValue !== changes.dataSource.previousValue) { this.dataSource = changes.dataSource.currentValue; if (this.dataSource.filter && this.dataSource.filters.length > 0) { this.dataSource.filters.forEach(ele => { this.service.getAPIs(ele.apiLink).subscribe((res: any) => { ele.values = res; this.reportsToDisplay = Object.assign({}, this.dataSource); this.selectedOptionDocs = Object.assign({}, this.dataSource); }, error => { console.log(error); }); }); } if (!(this.dataSource.filter && this.dataSource.filters.length > 0)) { this.reportsToDisplay = Object.assign({}, this.dataSource); this.selectedOptionDocs = Object.assign({}, this.dataSource); } } if (changes && changes.breadCrumbs && changes.breadCrumbs.currentValue !== changes.breadCrumbs.previousValue) { this.breadCrumbs = changes.breadCrumbs.currentValue; } if (changes && changes.viewMode) { this.viewMode = changes.viewMode.currentValue; this.sliceLength = this.viewMode === 'home' ? 3 : this.reportsToDisplay.data.length; } } typeOf(obj) { return typeof (obj); } // To prevent alphabetical sorting of object using keyvalue pipe sortDefault() { return 0; } sortDocuments(filterKey) { if (filterKey === this.sortColumn) { this.reportsToDisplay.data.reverse(); } else { this.sortColumn = filterKey; // this.sortDesc = false; if (this.sortColumn.includes('date')) { this.reportsToDisplay.data.sort((x, y) => new Date(x['columns'][filterKey]) < new Date(y['columns'][filterKey]) ? -1 : 1); } else { this.reportsToDisplay.data.sort((x, y) => x['columns'][filterKey].toLowerCase() < y['columns'][filterKey].toLowerCase() ? -1 : 1); } } } editTags(fileObj) { this.editFile.emit(fileObj); } deleteFileRequest(id) { this.deleteFile.emit(id); } showDetails(type, data) { this.reportsToDisplay.data = this.dataSource.data.filter(x => (x.columns.tags[type] && x.columns.tags[type].findIndex(y => y.id === data) !== -1)); this.selectedOptionDocs = Object.assign({}, this.reportsToDisplay); } openFileRequest(file) { this.openFile.emit(file); } isChecked(id, selectedFilters) { if (selectedFilters.length > 0 && selectedFilters.findIndex(x => x === id) >= 0) { return true; } else { return false; } } applyFilters(id, event, filterKey, selectedFilters, isMultiSelect) { if (event) { if (isMultiSelect) { selectedFilters.push(id); } else if (!isMultiSelect) { selectedFilters = [id]; } } else { selectedFilters.splice(selectedFilters.findIndex(x => x === id), 1); } if (selectedFilters.length > 0) { this.reportsToDisplay.data = this.selectedOptionDocs.data.filter(x => [...new Set(x[filterKey])].filter(val => new Set(selectedFilters).has(val)).length > 0); } else { this.reportsToDisplay = Object.assign({}, this.selectedOptionDocs); } } changeViewMode(view) { this.viewMode = view; this.changeView.emit(this.viewMode); this.sliceLength = this.viewMode === 'home' ? 3 : this.reportsToDisplay.data.length; } }