import { Component, OnInit } from '@angular/core'; import { FilterInfo,Filter, FiltersDataService } from '@varmasagi/filters'; import { config } from './filters-config'; import { region, city, regiona } from './filter-data'; @Component({ selector: 'app-filters-test', templateUrl: './filters-test.component.html', styleUrls: ['./filters-test.component.css'] }) export class FiltersTestComponent implements OnInit { readonly pageName = 'csi-other-diagnostics'; public filterConfig: FilterInfo[]; public currentPageFilterInfo: FilterInfo[] ; allFilterInstantiated = false; filterInitFlags: boolean[] = []; filterSetFlags: boolean[] = []; constructor( private filterDataService: FiltersDataService ) { this.filterDataService.setData('regiona', regiona); this.filterDataService.setData('city', city); this.filterDataService.setData('region', region); this.currentPageFilterInfo = config.filter(con => { return con.pageName === this.pageName; })[0].filterInfo; this.filterSetFlags['regiona'] = false; this.filterInitFlags['region'] = false; this.filterInitFlags['city'] = false; } ngOnInit() { } // all filters need to be instantiated for dependent filters to communicate // else parent cant find the child if not instantiated. // This method checks if all are instantiated or not. checkFiltersInstantiated(filter: FilterInfo) { this.filterInitFlags[filter.name] = true; // -1 removes the count of length property in an object. let filterCount = -1; // total count of the fitlers in the current page. let filterInitCount = 0; // filters count who sent acknowledgement. Object.getOwnPropertyNames(this.filterInitFlags).forEach((value, index, arr) => { filterCount++; if (this.filterInitFlags[value] === true) { filterInitCount++; } }); if (filterInitCount === filterCount) { // all filters are instantiated and can now set their default values setTimeout(() => { this.allFilterInstantiated = true; }, 500); } } // when filter is set with the default value set its flag // when all filters are set then send notification to rightsidenav checkFiltersSet(filter: Filter) { this.filterSetFlags[filter.name] = true; // -1 removes the count of length property in an object. let filterCount = -1; // total count of the fitlers in the current page. let filterSetCount = 0; // filters count who sent acknowledgement. Object.getOwnPropertyNames(this.filterSetFlags).forEach((value, index, arr) => { filterCount++; if (this.filterSetFlags[value] === true) { filterSetCount++; } }); if (filterSetCount === filterCount) { // all filters are set and can now send notifications to leftsidenav this.onFilterSet(); } } /** * *Emits false since filters are instantiating. * */ onFilterSet() { } // will return the data of request filter by its name private getFilterData(filter: FilterInfo) { return this.filterDataService.getData(filter.name); } }