import { FilterInfo, FilterInfoGroup } from '../types/filter-info'; import { Component, Input, OnInit, EventEmitter, Output } from '@angular/core'; import { FilterParentPipe } from '../pipes/filter.pipe'; import { SelectedFiltersDataService } from '../providers/selected-filters-data.service'; import { FilterChangeNotifyService } from '../providers/filter-change-notify.service'; import { NameValue } from '../types/name-value'; import { SU } from './../utils/string-util'; @Component({ selector: 'ics-dropdown-group', templateUrl: './dropdown-group.component.html', styleUrls: ['./dropdown-group.component.scss'], providers: [FilterParentPipe] }) export class DropdownGroupComponent implements OnInit { @Input() filterInfo: FilterInfoGroup; @Input() filterData: any[]; @Output() dataEmitter: EventEmitter<{ filter: FilterInfoGroup, data: any }>; @Output() filterSet: EventEmitter; selectedData: any; constructor( private fs: SelectedFiltersDataService, private filterParent: FilterParentPipe, private broadcastService: FilterChangeNotifyService ) { this.dataEmitter = new EventEmitter(); this.filterSet = new EventEmitter(); } ngOnInit() { // is the data is in form of dependency data then transform the data accordingly if (this.filterData[0][this.filterInfo.name] !== undefined) { this.filterData = this.filterParent.transform(this.filterData, null, 'ALL', this.filterInfo.name); } this.updateFilterData(); this.setDefaultValue(); this.onOptionChange(); this.resolve(); } resolve() { // this.filterInfo.resolve(true); this.filterSet.emit(this.filterInfo); } setDefaultValue() { let index = 0; // if user hasnt specified default index then 0 if (this.filterInfo.defaultIndex && this.filterInfo.defaultIndex >= 0) { index = this.filterInfo.defaultIndex; } this.selectedData = this.filterData[index].value; } onOptionChange() { const tempData = { filter: this.filterInfo, data: this.selectedData }; this.dataEmitter.emit(tempData); // store the slugName also into service to apart from this.storeData().then( res => { this.notifyFilterChange(); } ); } storeData() { return this.fs.storeSelectedData(this.filterInfo.slugName, this.selectedData); } notifyFilterChange() { this.broadcastService.notifyFilterChange(this.filterInfo); } // check if data needs to be altered updateFilterData() { if (this.filterInfo.containsAll && SU.doesNotContainAll(this.filterData[0])) { // do not use unshift directly on data since it refers the main data. this.filterData = JSON.parse(JSON.stringify(this.filterData)); this.filterData.unshift(new NameValue('All', 'All')); } } }