Skip to content

Commit 827b3fa

Browse files
[DSC-2302] fix filters visibility computation
1 parent d3a97d9 commit 827b3fa

3 files changed

Lines changed: 31 additions & 31 deletions

File tree

src/app/shared/search/search-filters/search-filter/search-filter.component.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { Component, EventEmitter, Inject, Input, OnInit, Output } from '@angular/core';
1+
import { Component, Inject, Input, OnInit } from '@angular/core';
22

3-
import { BehaviorSubject, Observable, of as observableOf } from 'rxjs';
4-
import { filter, map, startWith, switchMap, take } from 'rxjs/operators';
3+
import { BehaviorSubject, distinctUntilChanged, Observable, of as observableOf } from 'rxjs';
4+
import { filter, map, switchMap, take } from 'rxjs/operators';
55

66
import { SearchFilterConfig } from '../../models/search-filter-config.model';
77
import { SearchFilterService } from '../../../../core/shared/search/search-filter.service';
@@ -43,8 +43,6 @@ export class SearchFilterComponent implements OnInit {
4343
*/
4444
@Input() scope: string;
4545

46-
@Output() isVisibilityComputed = new EventEmitter<boolean>();
47-
4846
/**
4947
* True when the filter is 100% collapsed in the UI
5048
*/
@@ -93,19 +91,14 @@ export class SearchFilterComponent implements OnInit {
9391
*/
9492
ngOnInit() {
9593
this.selectedValues$ = this.getSelectedValues();
96-
this.active$ = this.isActive().pipe(
97-
startWith(true)
98-
);
94+
this.active$ = this.isActive();
9995
this.collapsed$ = this.isCollapsed();
10096
this.initializeFilter();
10197
this.selectedValues$.pipe(take(1)).subscribe((selectedValues) => {
10298
if (isNotEmpty(selectedValues)) {
10399
this.filterService.expand(this.filter.name);
104100
}
105101
});
106-
this.isActive().pipe(take(1)).subscribe(() => {
107-
this.isVisibilityComputed.emit(true);
108-
});
109102
}
110103

111104
/**
@@ -177,6 +170,7 @@ export class SearchFilterComponent implements OnInit {
177170
*/
178171
private isActive(): Observable<boolean> {
179172
return this.selectedValues$.pipe(
173+
distinctUntilChanged(),
180174
switchMap((isActive) => {
181175
if (isNotEmpty(isActive)) {
182176
return observableOf(true);

src/app/shared/search/search-filters/search-filters.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ <h3>{{"search.filters.head" | translate}}</h3>
22
<div *ngIf="(filters | async)?.hasSucceeded">
33
<div [class.visually-hidden]="filtersWithComputedVisibility !== (filters | async)?.payload?.length"
44
*ngFor="let filter of (filters | async)?.payload; trackBy: trackUpdate">
5-
<ds-search-filter (isVisibilityComputed)="countFiltersWithComputedVisibility($event)" [scope]="currentScope" [filter]="filter" [inPlaceSearch]="inPlaceSearch" [refreshFilters]="refreshFilters"></ds-search-filter>
5+
<ds-search-filter [scope]="currentScope" [filter]="filter" [inPlaceSearch]="inPlaceSearch" [refreshFilters]="refreshFilters"></ds-search-filter>
66
</div>
77
</div>
88

src/app/shared/search/search-filters/search-filters.component.ts

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { Component, Inject, Input, OnDestroy, OnInit } from '@angular/core';
1+
import { AfterViewChecked, Component, Inject, Input, OnDestroy, OnInit, QueryList, ViewChildren } from '@angular/core';
22
import { Router } from '@angular/router';
33

4-
import { BehaviorSubject, Observable } from 'rxjs';
4+
import { BehaviorSubject, combineLatest, distinctUntilChanged, Observable } from 'rxjs';
55
import { map, tap } from 'rxjs/operators';
66

77
import { SearchService } from '../../../core/shared/search/search.service';
@@ -13,6 +13,7 @@ import { SEARCH_CONFIG_SERVICE } from '../../../my-dspace-page/my-dspace-page.co
1313
import { currentPath } from '../../utils/route.utils';
1414
import { hasValue } from '../../empty.util';
1515
import { 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

Comments
 (0)