@@ -2,14 +2,7 @@ import { animate, state, style, transition, trigger } from '@angular/animations'
22import { Component , Inject , OnDestroy , OnInit } from '@angular/core' ;
33import { Router } from '@angular/router' ;
44
5- import {
6- BehaviorSubject ,
7- combineLatest as observableCombineLatest ,
8- Observable ,
9- of as observableOf ,
10- Subject ,
11- Subscription
12- } from 'rxjs' ;
5+ import { BehaviorSubject , combineLatest as observableCombineLatest , Observable , of as observableOf , Subject , Subscription } from 'rxjs' ;
136import { debounceTime , distinctUntilChanged , filter , map , mergeMap , switchMap , take , tap } from 'rxjs/operators' ;
147
158import { RemoteDataBuildService } from '../../../../../core/cache/builders/remote-data-build.service' ;
@@ -20,12 +13,7 @@ import { EmphasizePipe } from '../../../../utils/emphasize.pipe';
2013import { FacetValue } from '../../../models/facet-value.model' ;
2114import { SearchFilterConfig } from '../../../models/search-filter-config.model' ;
2215import { SearchService } from '../../../../../core/shared/search/search.service' ;
23- import {
24- FILTER_CONFIG ,
25- IN_PLACE_SEARCH ,
26- REFRESH_FILTER ,
27- SearchFilterService
28- } from '../../../../../core/shared/search/search-filter.service' ;
16+ import { FILTER_CONFIG , IN_PLACE_SEARCH , REFRESH_FILTER , SearchFilterService } from '../../../../../core/shared/search/search-filter.service' ;
2917import { SearchConfigurationService } from '../../../../../core/shared/search/search-configuration.service' ;
3018import { getFirstSucceededRemoteData } from '../../../../../core/shared/operators' ;
3119import { InputSuggestion } from '../../../../input-suggestions/input-suggestions.model' ;
@@ -35,6 +23,7 @@ import { currentPath } from '../../../../utils/route.utils';
3523import { getFacetValueForType , stripOperatorFromFilterValue } from '../../../search.utils' ;
3624import { createPendingRemoteDataObject } from '../../../../remote-data.utils' ;
3725import { FacetValues } from '../../../models/facet-values.model' ;
26+ import { AppliedFilter } from '../../../models/applied-filter.model' ;
3827
3928@Component ( {
4029 selector : 'ds-search-facet-filter' ,
@@ -48,7 +37,7 @@ export class SearchFacetFilterComponent implements OnInit, OnDestroy {
4837 /**
4938 * Emits an array of pages with values found for this facet
5039 */
51- filterValues$ : Subject < RemoteData < PaginatedList < FacetValue > [ ] > > ;
40+ filterValues$ : Subject < RemoteData < FacetValues [ ] > > ;
5241
5342 /**
5443 * Emits the current last shown page of this facet's values
@@ -78,7 +67,7 @@ export class SearchFacetFilterComponent implements OnInit, OnDestroy {
7867 /**
7968 * Emits the active values for this filter
8069 */
81- selectedValues$ : Observable < FacetValue [ ] > ;
70+ selectedValues$ : Observable < AppliedFilter [ ] > ;
8271
8372 protected collapseNextUpdate = true ;
8473
@@ -260,7 +249,7 @@ export class SearchFacetFilterComponent implements OnInit, OnDestroy {
260249 queryParams :
261250 {
262251 [ this . filterConfig . paramName ] : [
263- ...selectedValues . map ( ( facet ) => this . getFacetValue ( facet ) ) ,
252+ ...selectedValues . map ( ( appliedFilter : AppliedFilter ) => appliedFilter . value ) ,
264253 data
265254 ]
266255 } ,
@@ -280,12 +269,9 @@ export class SearchFacetFilterComponent implements OnInit, OnDestroy {
280269 return getFacetValueForType ( facet , this . filterConfig ) ;
281270 }
282271
283- protected retrieveFilterValues ( useCachedVersionIfAvailable = true ) : Observable < RemoteData < PaginatedList < FacetValue > [ ] > > {
284- const facetValues$ = observableCombineLatest ( [ this . searchOptions$ , this . currentPage ] ) . pipe (
285- map ( ( [ options , page ] ) => {
286- return { options, page } ;
287- } ) ,
288- switchMap ( ( { options, page } ) => {
272+ protected retrieveFilterValues ( useCachedVersionIfAvailable = true ) : Observable < RemoteData < FacetValues [ ] > > {
273+ const facetValues$ : Observable < { values : Observable < RemoteData < FacetValues > > , page : number } > = observableCombineLatest ( [ this . searchOptions$ , this . currentPage ] ) . pipe (
274+ switchMap ( ( [ options , page ] : [ SearchOptions , number ] ) => {
289275 return this . searchService . getFacetValuesFor ( this . filterConfig , page , options , null , useCachedVersionIfAvailable )
290276 . pipe (
291277 getFirstSucceededRemoteData ( ) ,
@@ -301,9 +287,9 @@ export class SearchFacetFilterComponent implements OnInit, OnDestroy {
301287 } )
302288 ) ;
303289
304- let filterValues = [ ] ;
290+ let filterValues : Observable < RemoteData < FacetValues > > [ ] = [ ] ;
305291 return facetValues$ . pipe (
306- mergeMap ( ( facetOutcome ) => {
292+ mergeMap ( ( facetOutcome : { values : Observable < RemoteData < FacetValues > > , page : number } ) => {
307293 const newValues$ = facetOutcome . values ;
308294
309295 if ( this . collapseNextUpdate ) {
@@ -319,22 +305,16 @@ export class SearchFacetFilterComponent implements OnInit, OnDestroy {
319305
320306 return this . rdbs . aggregate ( filterValues ) ;
321307 } ) ,
322- tap ( ( rd : RemoteData < PaginatedList < FacetValue > [ ] > ) => {
308+ tap ( ( rd : RemoteData < FacetValues [ ] > ) => {
309+ const appliedFilters : AppliedFilter [ ] = [ ] . concat ( ...rd . payload . map ( ( facetValues : FacetValues ) => facetValues . appliedFilters ) )
310+ . filter ( ( appliedFilter : AppliedFilter ) => hasValue ( appliedFilter ) ) ;
323311 this . selectedValues$ = this . filterService . getSelectedValuesForFilter ( this . filterConfig ) . pipe (
324- map ( ( selectedValues ) => {
312+ map ( ( selectedValues : string [ ] ) => {
325313 return selectedValues . map ( ( value : string ) => {
326- const fValue = [ ] . concat ( ...rd . payload . map ( ( page ) => page . page ) )
327- . find ( ( facetValue : FacetValue ) => this . getFacetValue ( facetValue ) === value ) ;
328- if ( hasValue ( fValue ) ) {
329- return fValue ;
330- }
331- const filterValue = stripOperatorFromFilterValue ( value ) ;
332- return Object . assign ( new FacetValue ( ) , { label : filterValue , value : filterValue } ) ;
333- } ) ;
334- } )
314+ return appliedFilters . find ( ( appliedFilter : AppliedFilter ) => appliedFilter . value === stripOperatorFromFilterValue ( value ) ) ;
315+ } ) . filter ( ( appliedFilter : AppliedFilter ) => hasValue ( appliedFilter ) ) ;
316+ } ) ,
335317 ) ;
336- } ) ,
337- tap ( ( rd : RemoteData < PaginatedList < FacetValue > [ ] > ) => {
338318 this . animationState = 'ready' ;
339319 this . filterValues$ . next ( rd ) ;
340320 } )
0 commit comments