76654: PaginationService

This commit is contained in:
Yana De Pauw
2021-02-05 15:47:07 +01:00
parent 11b6b3c929
commit fab226912f
49 changed files with 826 additions and 826 deletions

View File

@@ -14,17 +14,21 @@ import { RouteService } from '../../services/route.service';
import { getFirstSucceededRemoteData } from '../operators';
import { hasNoValue, hasValue, isNotEmpty, isNotEmptyOperator } from '../../../shared/empty.util';
import { createSuccessfulRemoteDataObject$ } from '../../../shared/remote-data.utils';
import { PaginationService } from '../../pagination/pagination.service';
import { tap } from 'rxjs/internal/operators/tap';
/**
* Service that performs all actions that have to do with the current search configuration
*/
@Injectable()
export class SearchConfigurationService implements OnDestroy {
public paginationID = 'spc';
/**
* Default pagination settings
*/
protected defaultPagination = Object.assign(new PaginationComponentOptions(), {
id: 'search-page-configuration',
id: this.paginationID,
pageSize: 10,
currentPage: 1
});
@@ -75,6 +79,7 @@ export class SearchConfigurationService implements OnDestroy {
* @param {ActivatedRoute} route
*/
constructor(protected routeService: RouteService,
protected paginationService: PaginationService,
protected route: ActivatedRoute) {
this.initDefaults();
@@ -91,7 +96,7 @@ export class SearchConfigurationService implements OnDestroy {
this.paginatedSearchOptions = new BehaviorSubject<PaginatedSearchOptions>(defs);
this.searchOptions = new BehaviorSubject<SearchOptions>(defs);
this.subs.push(this.subscribeToSearchOptions(defs));
this.subs.push(this.subscribeToPaginatedSearchOptions(defs));
this.subs.push(this.subscribeToPaginatedSearchOptions(defs.pagination.id, defs));
}
);
}
@@ -140,34 +145,15 @@ export class SearchConfigurationService implements OnDestroy {
/**
* @returns {Observable<string>} Emits the current pagination settings
*/
getCurrentPagination(defaultPagination: PaginationComponentOptions): Observable<PaginationComponentOptions> {
const page$ = this.routeService.getQueryParameterValue('page');
const size$ = this.routeService.getQueryParameterValue('pageSize');
return observableCombineLatest(page$, size$).pipe(map(([page, size]) => {
return Object.assign(new PaginationComponentOptions(), defaultPagination, {
currentPage: page || defaultPagination.currentPage,
pageSize: size || defaultPagination.pageSize
});
})
);
getCurrentPagination(paginationId: string, defaultPagination: PaginationComponentOptions): Observable<PaginationComponentOptions> {
return this.paginationService.getCurrentPagination(paginationId, defaultPagination);
}
/**
* @returns {Observable<string>} Emits the current sorting settings
*/
getCurrentSort(defaultSort: SortOptions): Observable<SortOptions> {
const sortDirection$ = this.routeService.getQueryParameterValue('sortDirection');
const sortField$ = this.routeService.getQueryParameterValue('sortField');
return observableCombineLatest(sortDirection$, sortField$).pipe(map(([sortDirection, sortField]) => {
// Dirty fix because sometimes the observable value is null somehow
sortField = this.route.snapshot.queryParamMap.get('sortField');
const field = sortField || defaultSort.field;
const direction = SortDirection[sortDirection] || defaultSort.direction;
return new SortOptions(field, direction);
}
)
);
getCurrentSort(paginationId: string, defaultSort: SortOptions): Observable<SortOptions> {
return this.paginationService.getCurrentSort(paginationId, defaultSort);
}
/**
@@ -234,10 +220,10 @@ export class SearchConfigurationService implements OnDestroy {
* @param {PaginatedSearchOptions} defaults Default values for when no parameters are available
* @returns {Subscription} The subscription to unsubscribe from
*/
private subscribeToPaginatedSearchOptions(defaults: PaginatedSearchOptions): Subscription {
private subscribeToPaginatedSearchOptions(paginationId: string, defaults: PaginatedSearchOptions): Subscription {
return observableMerge(
this.getPaginationPart(defaults.pagination),
this.getSortPart(defaults.sort),
this.getPaginationPart(paginationId, defaults.pagination),
this.getSortPart(paginationId, defaults.sort),
this.getConfigurationPart(defaults.configuration),
this.getScopePart(defaults.scope),
this.getQueryPart(defaults.query),
@@ -317,8 +303,8 @@ export class SearchConfigurationService implements OnDestroy {
/**
* @returns {Observable<string>} Emits the current pagination settings as a partial SearchOptions object
*/
private getPaginationPart(defaultPagination: PaginationComponentOptions): Observable<any> {
return this.getCurrentPagination(defaultPagination).pipe(map((pagination) => {
private getPaginationPart(paginationId: string, defaultPagination: PaginationComponentOptions): Observable<any> {
return this.getCurrentPagination(paginationId, defaultPagination).pipe(map((pagination) => {
return { pagination };
}));
}
@@ -326,8 +312,8 @@ export class SearchConfigurationService implements OnDestroy {
/**
* @returns {Observable<string>} Emits the current sorting settings as a partial SearchOptions object
*/
private getSortPart(defaultSort: SortOptions): Observable<any> {
return this.getCurrentSort(defaultSort).pipe(map((sort) => {
private getSortPart(paginationId: string, defaultSort: SortOptions): Observable<any> {
return this.getCurrentSort(paginationId, defaultSort).pipe(map((sort) => {
return { sort };
}));
}