Skip to content

Commit b55686e

Browse files
117287: Removed method calls returning observables from the pagination component
1 parent 680ed3b commit b55686e

2 files changed

Lines changed: 52 additions & 56 deletions

File tree

src/app/shared/pagination/pagination.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<div class="row">
44
<div *ngIf="!hidePaginationDetail && collectionSize > 0" class="col-auto pagination-info">
55
<span class="align-middle hidden-xs-down">{{ 'pagination.showing.label' | translate }}</span>
6-
<span class="align-middle">{{ 'pagination.showing.detail' | translate:(getShowingDetails(collectionSize)|async)}}</span>
6+
<span class="align-middle">{{ 'pagination.showing.detail' | translate:(showingDetails$ | async)}}</span>
77
</div>
88
<div class="col">
99
<div *ngIf="!hideGear" ngbDropdown #paginationControls="ngbDropdown" placement="bottom-right" class="d-inline-block float-right">

src/app/shared/pagination/pagination.component.ts

Lines changed: 51 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,33 @@ import {
44
Component,
55
EventEmitter,
66
Input,
7+
OnChanges,
78
OnDestroy,
89
OnInit,
910
Output,
10-
ViewEncapsulation
11+
SimpleChanges,
12+
ViewEncapsulation,
1113
} from '@angular/core';
1214

13-
import { Observable, of as observableOf, Subscription } from 'rxjs';
15+
import { Observable, of as observableOf, Subscription, switchMap } from 'rxjs';
1416

1517
import { HostWindowService } from '../host-window.service';
16-
import { HostWindowState } from '../search/host-window.reducer';
1718
import { PaginationComponentOptions } from './pagination-component-options.model';
1819
import { SortDirection, SortOptions } from '../../core/cache/models/sort-options.model';
19-
import { hasValue } from '../empty.util';
20+
import { hasValue, hasValueOperator } from '../empty.util';
2021
import { PageInfo } from '../../core/shared/page-info.model';
2122
import { PaginationService } from '../../core/pagination/pagination.service';
22-
import { map, take } from 'rxjs/operators';
23+
import { map, take, startWith } from 'rxjs/operators';
2324
import { RemoteData } from '../../core/data/remote-data';
2425
import { PaginatedList } from '../../core/data/paginated-list.model';
2526
import { ListableObject } from '../object-collection/shared/listable-object.model';
2627
import { ViewMode } from '../../core/shared/view-mode.model';
2728

29+
interface PaginationDetails {
30+
range: string;
31+
total: number;
32+
}
33+
2834
/**
2935
* The default pagination controls component.
3036
*/
@@ -36,7 +42,7 @@ import { ViewMode } from '../../core/shared/view-mode.model';
3642
changeDetection: ChangeDetectionStrategy.Default,
3743
encapsulation: ViewEncapsulation.Emulated
3844
})
39-
export class PaginationComponent implements OnDestroy, OnInit {
45+
export class PaginationComponent implements OnChanges, OnDestroy, OnInit {
4046
/**
4147
* ViewMode that should be passed to {@link ListableObjectComponentLoaderComponent}.
4248
*/
@@ -143,11 +149,6 @@ export class PaginationComponent implements OnDestroy, OnInit {
143149
*/
144150
public currentPageState: number = undefined;
145151

146-
/**
147-
* An observable of HostWindowState type
148-
*/
149-
public hostWindow: Observable<HostWindowState>;
150-
151152
/**
152153
* ID for the pagination instance. This ID is used in the routing to retrieve the pagination options.
153154
* This ID needs to be unique between different pagination components when more than one will be displayed on the same page.
@@ -186,6 +187,9 @@ export class PaginationComponent implements OnDestroy, OnInit {
186187
public sortField$;
187188
public defaultSortField = 'name';
188189

190+
191+
public showingDetails$: Observable<PaginationDetails>;
192+
189193
/**
190194
* Array to track all subscriptions and unsubscribe them onDestroy
191195
* @type {Array}
@@ -214,6 +218,12 @@ export class PaginationComponent implements OnDestroy, OnInit {
214218
this.initializeConfig();
215219
}
216220

221+
ngOnChanges(changes: SimpleChanges): void {
222+
if (changes.collectionSize.currentValue !== changes.collectionSize.previousValue) {
223+
this.showingDetails$ = this.getShowingDetails(this.collectionSize);
224+
}
225+
}
226+
217227
/**
218228
* Method provided by Angular. Invoked when the instance is destroyed.
219229
*/
@@ -251,19 +261,11 @@ export class PaginationComponent implements OnDestroy, OnInit {
251261
);
252262
}
253263

254-
/**
255-
* @param cdRef
256-
* ChangeDetectorRef is a singleton service provided by Angular.
257-
* @param route
258-
* Route is a singleton service provided by Angular.
259-
* @param router
260-
* Router is a singleton service provided by Angular.
261-
* @param hostWindowService
262-
* the HostWindowService singleton.
263-
*/
264-
constructor(private cdRef: ChangeDetectorRef,
265-
private paginationService: PaginationService,
266-
public hostWindowService: HostWindowService) {
264+
constructor(
265+
protected cdRef: ChangeDetectorRef,
266+
protected paginationService: PaginationService,
267+
public hostWindowService: HostWindowService,
268+
) {
267269
}
268270

269271
/**
@@ -299,17 +301,6 @@ export class PaginationComponent implements OnDestroy, OnInit {
299301
this.emitPaginationChange();
300302
}
301303

302-
/**
303-
* Method to change the route to the given sort field
304-
*
305-
* @param sortField
306-
* The sort field being navigated to.
307-
*/
308-
public doSortFieldChange(field: string) {
309-
this.updateParams({ pageId: this.id, page: 1, sortField: field });
310-
this.emitPaginationChange();
311-
}
312-
313304
/**
314305
* Method to emit a general pagination change event
315306
*/
@@ -328,26 +319,31 @@ export class PaginationComponent implements OnDestroy, OnInit {
328319
/**
329320
* Method to get pagination details of the current viewed page.
330321
*/
331-
public getShowingDetails(collectionSize: number): Observable<any> {
332-
let showingDetails = observableOf({ range: null + ' - ' + null, total: null });
333-
if (collectionSize) {
334-
showingDetails = this.paginationService.getCurrentPagination(this.id, this.paginationOptions).pipe(
335-
map((currentPaginationOptions) => {
336-
let firstItem;
337-
let lastItem;
338-
const pageMax = currentPaginationOptions.pageSize * currentPaginationOptions.currentPage;
339-
340-
firstItem = currentPaginationOptions.pageSize * (currentPaginationOptions.currentPage - 1) + 1;
341-
if (collectionSize > pageMax) {
342-
lastItem = pageMax;
343-
} else {
344-
lastItem = collectionSize;
345-
}
346-
return {range: firstItem + ' - ' + lastItem, total: collectionSize};
347-
})
348-
);
349-
}
350-
return showingDetails;
322+
public getShowingDetails(collectionSize: number): Observable<PaginationDetails> {
323+
return observableOf(collectionSize).pipe(
324+
hasValueOperator(),
325+
switchMap(() => this.paginationService.getCurrentPagination(this.id, this.paginationOptions)),
326+
map((currentPaginationOptions) => {
327+
let firstItem: number;
328+
let lastItem: number;
329+
const pageMax = currentPaginationOptions.pageSize * currentPaginationOptions.currentPage;
330+
331+
firstItem = currentPaginationOptions.pageSize * (currentPaginationOptions.currentPage - 1) + 1;
332+
if (collectionSize > pageMax) {
333+
lastItem = pageMax;
334+
} else {
335+
lastItem = collectionSize;
336+
}
337+
return {
338+
range: `${firstItem} - ${lastItem}`,
339+
total: collectionSize,
340+
};
341+
}),
342+
startWith({
343+
range: `${null} - ${null}`,
344+
total: null,
345+
}),
346+
);
351347
}
352348

353349
/**

0 commit comments

Comments
 (0)