54053: Working active filter labels

This commit is contained in:
Kristof De Langhe
2018-07-05 12:56:39 +02:00
parent 205fe3ede3
commit 050536844b
4 changed files with 56 additions and 15 deletions

View File

@@ -1,9 +1,9 @@
<div> <div>
<div class="labels"> <div class="labels">
<a *ngFor="let value of (appliedFilters | async)" class="badge badge-primary mr-1" <a *ngFor="let label of (appliedFilters | async)" class="badge badge-primary mr-1"
[routerLink]="[getSearchLink()]" [routerLink]="getSearchLink()"
[queryParams]="getQueryParamsWithout(value.filter, value.value) | async"> [queryParams]="(getRemoveParams(label) | async)" queryParamsHandling="merge">
{{value | dsCapitalize}} {{label.value}}
<span> ×</span> <span> ×</span>
</a> </a>
</div> </div>

View File

@@ -1,26 +1,37 @@
import { Component } from '@angular/core'; import { Component } from '@angular/core';
import { SearchService } from '../search-service/search.service'; import { SearchService } from '../search-service/search.service';
import { SearchFilterService } from '../search-filters/search-filter/search-filter.service';
import { Observable } from 'rxjs/Observable'; import { Observable } from 'rxjs/Observable';
import { Params } from '@angular/router'; import { Params } from '@angular/router';
import { FilterLabel } from '../search-service/filter-label.model';
import { map } from 'rxjs/operators';
@Component({ @Component({
selector: 'ds-search-labels', selector: 'ds-search-labels',
// styleUrls: ['./search-labels.component.scss'],
templateUrl: './search-labels.component.html', templateUrl: './search-labels.component.html',
}) })
export class SearchLabelsComponent { export class SearchLabelsComponent {
protected appliedFilters: Observable<Params>; protected appliedFilters: Observable<FilterLabel[]>;
constructor(private searchService: SearchService, private filterService: SearchFilterService) { constructor(private searchService: SearchService) {
this.appliedFilters = this.filterService.getCurrentFilters(); this.appliedFilters = this.searchService.getFilterLabels();
console.log(this.appliedFilters.toArray());
} }
getQueryParamsWithout(filterName: string, value: string): Observable<Params> { getRemoveParams(filterLabel: FilterLabel): Observable<Params> {
return this.filterService.getCurrentFilters(); return this.appliedFilters.pipe(
// return this.filterService.getQueryParamsWithoutByName(filterName, value); 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() { getSearchLink() {

View File

@@ -0,0 +1,8 @@
export class FilterLabel {
value: string;
field: string;
constructor(value: string, field: string) {
this.value = value;
this.field = field;
}
}

View File

@@ -1,6 +1,6 @@
import { Injectable, OnDestroy } from '@angular/core'; import { Injectable, OnDestroy } from '@angular/core';
import { import {
ActivatedRoute, NavigationExtras, PRIMARY_OUTLET, Router, ActivatedRoute, NavigationExtras, Params, PRIMARY_OUTLET, Router,
UrlSegmentGroup UrlSegmentGroup
} from '@angular/router'; } from '@angular/router';
import { Observable } from 'rxjs/Observable'; 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 { FacetValueResponseParsingService } from '../../core/data/facet-value-response-parsing.service';
import { FacetConfigResponseParsingService } from '../../core/data/facet-config-response-parsing.service'; import { FacetConfigResponseParsingService } from '../../core/data/facet-config-response-parsing.service';
import { PaginatedSearchOptions } from '../paginated-search-options.model'; 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() @Injectable()
export class SearchService implements OnDestroy { export class SearchService implements OnDestroy {
@@ -223,6 +224,27 @@ export class SearchService implements OnDestroy {
return this.rdb.toRemoteDataObservable(requestEntryObs, responseCacheObs, payloadObs); return this.rdb.toRemoteDataObservable(requestEntryObs, responseCacheObs, payloadObs);
} }
getFilterLabels(): Observable<FilterLabel[]> {
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<ViewMode> { getViewMode(): Observable<ViewMode> {
return this.route.queryParams.map((params) => { return this.route.queryParams.map((params) => {
if (isNotEmpty(params.view) && hasValue(params.view)) { if (isNotEmpty(params.view) && hasValue(params.view)) {