diff --git a/src/collection-browser.ts b/src/collection-browser.ts index 49fdef6b..f5c2eb87 100644 --- a/src/collection-browser.ts +++ b/src/collection-browser.ts @@ -1424,6 +1424,8 @@ export class CollectionBrowser if (oldObserver) this.disconnectResizeObserver(oldObserver); this.setupResizeObserver(); } + + this.ensureAvailableTilesDisplayed(); } connectedCallback(): void { @@ -1461,6 +1463,24 @@ export class CollectionBrowser this.updateLeftColumnHeight(); } + /** + * Ensures that if we have new results from the data source that are not yet + * displayed in the infinite scroller, that they are immediately reflected + * in the tile count. + */ + private ensureAvailableTilesDisplayed(): void { + if ( + this.infiniteScroller && + this.infiniteScroller.itemCount < this.dataSource.size + ) { + this.setTileCount( + this.dataSource.endOfDataReached + ? this.dataSource.size + : this.estimatedTileCount + ); + } + } + /** * Updates the data source with the current state of facet readiness for loading, * so that they will begin to load in at the appropriate time according to the diff --git a/src/data-source/collection-browser-data-source.ts b/src/data-source/collection-browser-data-source.ts index 082c1688..de599dc1 100644 --- a/src/data-source/collection-browser-data-source.ts +++ b/src/data-source/collection-browser-data-source.ts @@ -1120,19 +1120,21 @@ export class CollectionBrowserDataSource // Update the data source for each returned page. // For loans and web archives, we must account for receiving more pages than we asked for. - if ( - this.host.profileElement === 'lending' || - this.host.profileElement === 'web_archives' - ) { + const isUnpagedElement = ['lending', 'web_archives'].includes( + this.host.profileElement! + ); + if (isUnpagedElement) { numPages = Math.ceil(results.length / this.pageSize); this.endOfDataReached = true; if (this.activeOnHost) this.host.setTileCount(this.totalResults); } + for (let i = 0; i < numPages; i += 1) { const pageStartIndex = this.pageSize * i; this.addFetchedResultsToDataSource( pageNumber + i, - results.slice(pageStartIndex, pageStartIndex + this.pageSize) + results.slice(pageStartIndex, pageStartIndex + this.pageSize), + !isUnpagedElement || i === numPages - 1 ); } } @@ -1157,16 +1159,17 @@ export class CollectionBrowserDataSource */ private addFetchedResultsToDataSource( pageNumber: number, - results: SearchResult[] + results: SearchResult[], + needsReload = true ): void { const tiles: TileModel[] = []; results?.forEach(result => { if (!result.identifier) return; tiles.push(new TileModel(result)); }); + this.addPage(pageNumber, tiles); - const visiblePages = this.host.currentVisiblePageNumbers; - const needsReload = visiblePages.includes(pageNumber); + if (needsReload) { this.refreshVisibleResults(); }