diff --git a/projects/ng-core-tester/src/app/app-routing.module.ts b/projects/ng-core-tester/src/app/app-routing.module.ts index ae08462a7..b3f817e17 100644 --- a/projects/ng-core-tester/src/app/app-routing.module.ts +++ b/projects/ng-core-tester/src/app/app-routing.module.ts @@ -309,9 +309,34 @@ const routes: Routes = [ } ], searchFilters: [ + { + label: 'Advanced search', + filter: 'simple', + value: '0', + /* If you set this value, the url parameter will still be present, + but with different values */ + disabledValue: '1', + persistent: true, + url: { + routerLink: ['/record', 'search', 'documents'], + title: 'Link to search document' + } + }, { label: 'Open access', - filter: 'open_access' + filter: 'open_access', + value: '1', + url: { + external: true, + link: 'https://sonar.rero.ch', + target: '_blank', + title: 'Link to Sonar interface' + } + }, + { + label: 'Another filter', + filter: 'other', + value: '1' } ] }, diff --git a/projects/rero/ng-core/src/lib/record/record.ts b/projects/rero/ng-core/src/lib/record/record.ts index 2ccdd5bf3..4750250e2 100644 --- a/projects/rero/ng-core/src/lib/record/record.ts +++ b/projects/rero/ng-core/src/lib/record/record.ts @@ -66,5 +66,16 @@ export interface SearchField { export interface SearchFilter { filter: string; label: string; - value?: string; + value: string; + /* If you set this value, the url parameter will still be present, + but with different values */ + disabledValue?: string; + persistent?: boolean; + url?: { + external?: boolean; + link?: string; + routerLink?: Array; + target?: string; + title?: string; + }; } diff --git a/projects/rero/ng-core/src/lib/record/search/record-search-page.component.ts b/projects/rero/ng-core/src/lib/record/search/record-search-page.component.ts index deb7cf99a..b23397ee2 100644 --- a/projects/rero/ng-core/src/lib/record/search/record-search-page.component.ts +++ b/projects/rero/ng-core/src/lib/record/search/record-search-page.component.ts @@ -21,6 +21,7 @@ import { JSONSchema7 } from '../editor/editor.component'; import { combineLatest, Subscription } from 'rxjs'; import { ActionStatus } from '../action-status'; import { RecordSearchService } from './record-search.service'; +import { RecordUiService } from '../record-ui.service'; @Component({ selector: 'ng-core-record-search-page', @@ -116,7 +117,8 @@ export class RecordSearchPageComponent implements OnInit, OnDestroy { constructor( protected _route: ActivatedRoute, protected _router: Router, - protected _recordSearchService: RecordSearchService + protected _recordSearchService: RecordSearchService, + protected _recordUiService: RecordUiService ) { } /** @@ -131,6 +133,9 @@ export class RecordSearchPageComponent implements OnInit, OnDestroy { ([paramMap, queryParams]) => { // store current type of resource this.currentType = paramMap.get('type'); + this._recordUiService.types = this._route.snapshot.data.types; + + const config = this._recordUiService.getResourceConfig(this.currentType); // Stores query parameters this.q = queryParams.get('q') || ''; @@ -147,6 +152,15 @@ export class RecordSearchPageComponent implements OnInit, OnDestroy { } }); + // Add filter parameter value + if (config.searchFilters) { + config.searchFilters.forEach((filter: any) => { + if (queryParams.get(filter.filter) === null && filter.disabledValue) { + aggregationsFilters.push({ key: filter.filter, values: [filter.disabledValue] }); + } + }); + } + // No default parameters found, we update the url to put them if (queryParams.has('q') === false || queryParams.has('size') === false || queryParams.has('page') === false) { this.updateUrl({ diff --git a/projects/rero/ng-core/src/lib/record/search/record-search.component.html b/projects/rero/ng-core/src/lib/record/search/record-search.component.html index 7966a48d3..78b3bf347 100644 --- a/projects/rero/ng-core/src/lib/record/search/record-search.component.html +++ b/projects/rero/ng-core/src/lib/record/search/record-search.component.html @@ -129,10 +129,35 @@
- +
+
+ + + + + + + + + + + + + + +
+
. */ import { Component, EventEmitter, Input, OnChanges, OnDestroy, OnInit, Output, SimpleChanges } from '@angular/core'; +import { ActivatedRoute } from '@angular/router'; import { FormlyFieldConfig } from '@ngx-formly/core'; import { TranslateService } from '@ngx-translate/core'; import { cloneDeep } from 'lodash-es'; @@ -205,6 +206,7 @@ export class RecordSearchComponent implements OnInit, OnChanges, OnDestroy { * @param _translateService Translate service. * @param _spinner Spinner service. * @param _apiService Api service. + * @param _activatedRoute Activated Route */ constructor( private _recordService: RecordService, @@ -212,7 +214,8 @@ export class RecordSearchComponent implements OnInit, OnChanges, OnDestroy { private _recordSearchService: RecordSearchService, private _translateService: TranslateService, private _spinner: NgxSpinnerService, - private _apiService: ApiService + private _apiService: ApiService, + private _activatedRoute: ActivatedRoute ) { } /** @@ -480,7 +483,9 @@ export class RecordSearchComponent implements OnInit, OnChanges, OnDestroy { this.q = event; this.aggregationsFilters = []; this._searchParamsHasChanged(); - this._recordSearchService.setAggregationsFilters([]); + this._recordSearchService.setAggregationsFilters( + this._extractPersistentAggregationsFilters() + ); } /** @@ -723,8 +728,23 @@ export class RecordSearchComponent implements OnInit, OnChanges, OnDestroy { * @returns void */ searchFilter(filter: SearchFilter): void { - const values = - this.isFilterActive(filter) === true ? [] : [filter.value || '1']; + let values = []; + const agg = this.aggregationsFilters.filter((item: any) => { + return item.key === filter.filter; + }); + if (agg.length > 0) { + const aggFilter = agg[0]; + if (!aggFilter.values.includes(filter.value)) { + values = [filter.value]; + } else { + if (filter.disabledValue) { + values = [filter.disabledValue]; + } + } + } else { + values = [filter.value]; + } + this._recordSearchService.updateAggregationFilter(filter.filter, values); } @@ -735,8 +755,11 @@ export class RecordSearchComponent implements OnInit, OnChanges, OnDestroy { * @returns true if the given filter is selected. */ isFilterActive(filter: SearchFilter): boolean { + if (!this.aggregationsFilters) { + return false; + } return this.aggregationsFilters.some((item: any) => { - return item.key === filter.filter; + return item.key === filter.filter && item.values.includes(String(filter.value)); }); } @@ -891,4 +914,20 @@ export class RecordSearchComponent implements OnInit, OnChanges, OnDestroy { ? this._config.aggregationsName[key] : null; } + + /** Extract persistent search filters on current url */ + private _extractPersistentAggregationsFilters(): Array { + const persistent = []; + const filters = this.searchFilters.filter(filter => filter.persistent === true); + filters.forEach((filter: SearchFilter) => { + if (this._activatedRoute.snapshot.queryParams.hasOwnProperty(filter.filter)) { + let data = this._activatedRoute.snapshot.queryParams[filter.filter]; + if (!Array.isArray(data)) { + data = [data]; + } + persistent.push({ key: filter.filter, values: data }); + } + }); + return persistent; + } } diff --git a/projects/rero/ng-core/src/lib/record/search/record-search.service.ts b/projects/rero/ng-core/src/lib/record/search/record-search.service.ts index 2fb52851d..66ecdb840 100644 --- a/projects/rero/ng-core/src/lib/record/search/record-search.service.ts +++ b/projects/rero/ng-core/src/lib/record/search/record-search.service.ts @@ -78,6 +78,9 @@ export class RecordSearchService { * @param values Selected values */ updateAggregationFilter(term: string, values: string[]) { + if (this._aggregationsFilters === null) { + this._aggregationsFilters = []; + } const index = this._aggregationsFilters.findIndex(item => item.key === term); if (values.length === 0) {