import {Component, OnInit, Input, EventEmitter, Output} from '@angular/core'; import {Filter, FilterInfo, FilterInfoGroup} from '../types/filter-info'; import {FiltersDataService} from '../providers/filters-data.service'; import {SelectedFiltersDataService} from '../providers/selected-filters-data.service'; import {FilterChangeNotifyService} from '../providers/filter-change-notify.service'; import {isNullOrUndefined} from 'util'; @Component({ selector: 'ics-radio-dropdowns', templateUrl: './radio-dropdowns.component.html', styleUrls: ['./radio-dropdowns.component.scss'] }) export class RadioDropdownsComponent implements OnInit { @Input() filterInfo: FilterInfo; @Input() filterGroupData: any[]; @Output() instantiated: EventEmitter; @Output() filterSet: EventEmitter; filterData: any[]; selectedData: any; childFlags: boolean[]; // used to check if child instantiated or not currentConditionName: string; // the selected radio value groupFilterInfo: FilterInfoGroup[]; // the groups which are dependent on radio. allFilterInstantiated = true; groupSelectedData: any[]; constructor(private filtersDataService: FiltersDataService, private fs: SelectedFiltersDataService, private broadcastService: FilterChangeNotifyService) { this.groupSelectedData = []; this.childFlags = []; this.instantiated = new EventEmitter(); this.filterSet = new EventEmitter(); } ngOnInit() { this.instantiated.emit(this.filterInfo); this.setDefaultValue(); this.updateCurrentGroup(); // first load set the default value and update the group components this.onOptionChange(); this.resolve(); } setDefaultValue() { this.filterData = this.filterGroupData[this.filterInfo.name]; this.selectedData = this.filterData[0].value; } // flags to check if all the childs are instantiated and ready acknowledgeReciever(filter: Filter) { this.childFlags[filter.name] = true; } // triggered when user changes the option onOptionChange(value?: any) { if (value) { this.selectedData = value; } this.updateCurrentGroup(); this.fs.storeSelectedData(this.filterInfo.name, this.selectedData); } // // when user changes the options update the filters that need to render updateCurrentGroup() { this.groupFilterInfo = this.filterInfo.filterGroups.filter(filter => { return filter.conditionValue === this.selectedData; }); this.groupSelectedData = new Array(); if (this.doesNotHaveGroups()) { this.notifyFilterChange(); } } // used to send the data to dependent filters on their request with respect to name of // filter getFilterData(filter: Filter) { return this.filterGroupData[filter.name]; } updateGroupSelectedData(group: any) { // get the slugname since name is identical to get the data slugname is distinct const filterName = group.filter.slugName; const selectedData = group.data; this.groupSelectedData[filterName] = selectedData; // get the values from the properties of array, group names are stored as properties but not indexing array const valuesArray = this.getValuesOfGroup(this.groupSelectedData); this.fs.storeSelectedData(group.filter.name, valuesArray); /** if it does not have groups or * all the current groups have given their default values then send * notification that filter has changed. * */ if (this.groupFilterInfo.length === Object.keys(this.groupSelectedData).length || this.doesNotHaveGroups() ) { // this.filterInfo.resolve(true); this.notifyFilterChange(); } } notifyFilterChange() { this.broadcastService.notifyFilterChange(this.filterInfo); } getValuesOfGroup(obj: any) { const arr = new Array(); Object.getOwnPropertyNames(obj).forEach( function (val, idx, array) { if (val !== 'length') { arr.push(obj[val]); } } ); return arr; } private doesNotHaveGroups(): boolean { return isNullOrUndefined(this.groupFilterInfo) || this.groupFilterInfo.length === 0; } resolve() { this.filterSet.emit(this.filterInfo); } }