From 050536844b09cc3a8f1e2751fe02deab3963a680 Mon Sep 17 00:00:00 2001 From: Kristof De Langhe Date: Thu, 5 Jul 2018 12:56:39 +0200 Subject: [PATCH] 54053: Working active filter labels --- .../search-labels.component.html | 8 ++--- .../search-labels/search-labels.component.ts | 29 +++++++++++++------ .../search-service/filter-label.model.ts | 8 +++++ .../search-service/search.service.ts | 26 +++++++++++++++-- 4 files changed, 56 insertions(+), 15 deletions(-) create mode 100644 src/app/+search-page/search-service/filter-label.model.ts diff --git a/src/app/+search-page/search-labels/search-labels.component.html b/src/app/+search-page/search-labels/search-labels.component.html index 2a2f63aa09..e20af58bcc 100644 --- a/src/app/+search-page/search-labels/search-labels.component.html +++ b/src/app/+search-page/search-labels/search-labels.component.html @@ -1,9 +1,9 @@
diff --git a/src/app/+search-page/search-labels/search-labels.component.ts b/src/app/+search-page/search-labels/search-labels.component.ts index 2692322bee..9111ecc2b6 100644 --- a/src/app/+search-page/search-labels/search-labels.component.ts +++ b/src/app/+search-page/search-labels/search-labels.component.ts @@ -1,26 +1,37 @@ import { Component } from '@angular/core'; import { SearchService } from '../search-service/search.service'; -import { SearchFilterService } from '../search-filters/search-filter/search-filter.service'; import { Observable } from 'rxjs/Observable'; import { Params } from '@angular/router'; +import { FilterLabel } from '../search-service/filter-label.model'; +import { map } from 'rxjs/operators'; @Component({ selector: 'ds-search-labels', - // styleUrls: ['./search-labels.component.scss'], templateUrl: './search-labels.component.html', }) export class SearchLabelsComponent { - protected appliedFilters: Observable; + protected appliedFilters: Observable; - constructor(private searchService: SearchService, private filterService: SearchFilterService) { - this.appliedFilters = this.filterService.getCurrentFilters(); - console.log(this.appliedFilters.toArray()); + constructor(private searchService: SearchService) { + this.appliedFilters = this.searchService.getFilterLabels(); } - getQueryParamsWithout(filterName: string, value: string): Observable { - return this.filterService.getCurrentFilters(); - // return this.filterService.getQueryParamsWithoutByName(filterName, value); + getRemoveParams(filterLabel: FilterLabel): Observable { + return this.appliedFilters.pipe( + map((filters) => { + const values = []; + filters.forEach((filter) => { + if (filter.field === filterLabel.field && filter.value !== filterLabel.value) { + values.push(filter.value); + } + }); + return { + [filterLabel.field]: values, + page: 1 + }; + }) + ); } getSearchLink() { diff --git a/src/app/+search-page/search-service/filter-label.model.ts b/src/app/+search-page/search-service/filter-label.model.ts new file mode 100644 index 0000000000..dea351d1c2 --- /dev/null +++ b/src/app/+search-page/search-service/filter-label.model.ts @@ -0,0 +1,8 @@ +export class FilterLabel { + value: string; + field: string; + constructor(value: string, field: string) { + this.value = value; + this.field = field; + } +} diff --git a/src/app/+search-page/search-service/search.service.ts b/src/app/+search-page/search-service/search.service.ts index 774d2287e2..e27b43597b 100644 --- a/src/app/+search-page/search-service/search.service.ts +++ b/src/app/+search-page/search-service/search.service.ts @@ -1,6 +1,6 @@ import { Injectable, OnDestroy } from '@angular/core'; import { - ActivatedRoute, NavigationExtras, PRIMARY_OUTLET, Router, + ActivatedRoute, NavigationExtras, Params, PRIMARY_OUTLET, Router, UrlSegmentGroup } from '@angular/router'; import { Observable } from 'rxjs/Observable'; @@ -40,7 +40,8 @@ import { ListableObject } from '../../shared/object-collection/shared/listable-o import { FacetValueResponseParsingService } from '../../core/data/facet-value-response-parsing.service'; import { FacetConfigResponseParsingService } from '../../core/data/facet-config-response-parsing.service'; import { PaginatedSearchOptions } from '../paginated-search-options.model'; -import { observable } from 'rxjs/symbol/observable'; +import { FilterLabel } from './filter-label.model'; +import { combineLatest } from 'rxjs/observable/combineLatest'; @Injectable() export class SearchService implements OnDestroy { @@ -223,6 +224,27 @@ export class SearchService implements OnDestroy { return this.rdb.toRemoteDataObservable(requestEntryObs, responseCacheObs, payloadObs); } + getFilterLabels(): Observable { + return combineLatest(this.getConfig(), this.route.queryParams).pipe( + map(([rd, params]) => { + const filterLabels: FilterLabel[] = []; + rd.payload.forEach((config: SearchFilterConfig) => { + const param = params[config.paramName]; + if (param !== undefined) { + if (param instanceof Array && param.length > 1) { + param.forEach((p: string) => { + filterLabels.push(new FilterLabel(p, config.paramName)) + }); + } else { + filterLabels.push(new FilterLabel(param, config.paramName)); + } + } + }); + return filterLabels.filter((n) => n !== undefined && n.value.length > 0); + }) + ); + } + getViewMode(): Observable { return this.route.queryParams.map((params) => { if (isNotEmpty(params.view) && hasValue(params.view)) {