1- import { Component , Inject , Input , OnDestroy , OnInit } from '@angular/core' ;
1+ import { AfterViewChecked , Component , Inject , Input , OnDestroy , OnInit , QueryList , ViewChildren } from '@angular/core' ;
22import { Router } from '@angular/router' ;
33
4- import { BehaviorSubject , Observable } from 'rxjs' ;
4+ import { BehaviorSubject , combineLatest , distinctUntilChanged , Observable } from 'rxjs' ;
55import { map , tap } from 'rxjs/operators' ;
66
77import { SearchService } from '../../../core/shared/search/search.service' ;
@@ -13,6 +13,7 @@ import { SEARCH_CONFIG_SERVICE } from '../../../my-dspace-page/my-dspace-page.co
1313import { currentPath } from '../../utils/route.utils' ;
1414import { hasValue } from '../../empty.util' ;
1515import { APP_CONFIG , AppConfig } from '../../../../config/app-config.interface' ;
16+ import { SearchFilterComponent } from './search-filter/search-filter.component' ;
1617
1718@Component ( {
1819 selector : 'ds-search-filters' ,
@@ -24,7 +25,7 @@ import { APP_CONFIG, AppConfig } from '../../../../config/app-config.interface';
2425/**
2526 * This component represents the part of the search sidebar that contains filters.
2627 */
27- export class SearchFiltersComponent implements OnInit , OnDestroy {
28+ export class SearchFiltersComponent implements OnInit , AfterViewChecked , OnDestroy {
2829 /**
2930 * An observable containing configuration about which filters are shown and how they are shown
3031 */
@@ -56,6 +57,11 @@ export class SearchFiltersComponent implements OnInit, OnDestroy {
5657 */
5758 @Input ( ) refreshFilters : BehaviorSubject < boolean > ;
5859
60+ /**
61+ * List of children filters
62+ */
63+ @ViewChildren ( SearchFilterComponent ) childrenFilters ! : QueryList < SearchFilterComponent > ;
64+
5965 /**
6066 * Link to the search page
6167 */
@@ -88,16 +94,14 @@ export class SearchFiltersComponent implements OnInit, OnDestroy {
8894 }
8995
9096 ngOnInit ( ) : void {
91- this . router . events . subscribe ( ( ) => {
92- this . clearParams = this . searchConfigService . getCurrentFrontendFilters ( ) . pipe (
93- tap ( ( ) => this . filtersWithComputedVisibility = 0 ) ,
94- map ( ( filters ) => {
95- Object . keys ( filters ) . forEach ( ( f ) => filters [ f ] = null ) ;
96- return filters ;
97- } )
98- ) ;
99- this . searchLink = this . getSearchLink ( ) ;
100- } ) ;
97+ this . clearParams = this . searchConfigService . getCurrentFrontendFilters ( ) . pipe (
98+ tap ( ( ) => this . filtersWithComputedVisibility = 0 ) ,
99+ map ( ( filters ) => {
100+ Object . keys ( filters ) . forEach ( ( f ) => filters [ f ] = null ) ;
101+ return filters ;
102+ } )
103+ ) ;
104+ this . searchLink = this . getSearchLink ( ) ;
101105 }
102106
103107 /**
@@ -117,17 +121,19 @@ export class SearchFiltersComponent implements OnInit, OnDestroy {
117121 return config ? config . name : undefined ;
118122 }
119123
124+ ngAfterViewChecked ( ) {
125+ this . subs . push (
126+ combineLatest ( this . childrenFilters . map ( child => child . active$ ) ) . pipe ( distinctUntilChanged ( ) ) . subscribe ( ( visibilityValues ) => {
127+ this . filtersWithComputedVisibility = visibilityValues . filter ( visible => ! ! visible ) . length ;
128+ } )
129+ ) ;
130+ }
131+
120132 ngOnDestroy ( ) {
121133 this . subs . forEach ( ( sub ) => {
122134 if ( hasValue ( sub ) ) {
123135 sub . unsubscribe ( ) ;
124136 }
125137 } ) ;
126138 }
127-
128- countFiltersWithComputedVisibility ( computed : boolean ) {
129- if ( computed ) {
130- this . filtersWithComputedVisibility += 1 ;
131- }
132- }
133139}
0 commit comments