Skip to content

Commit

Permalink
WEBDEV-7022 Ensure initial page of results always triggers scroller r…
Browse files Browse the repository at this point in the history
…efresh (#402)

* Ensure initial page of results always triggers scroller refresh

* Add additional logging

* Adds monitor to ensure tile count reflects data size

* Remove log statements
  • Loading branch information
latonv authored Nov 15, 2024
1 parent 5ad05dd commit b45b065
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
20 changes: 20 additions & 0 deletions src/collection-browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1424,6 +1424,8 @@ export class CollectionBrowser
if (oldObserver) this.disconnectResizeObserver(oldObserver);
this.setupResizeObserver();
}

this.ensureAvailableTilesDisplayed();
}

connectedCallback(): void {
Expand Down Expand Up @@ -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
Expand Down
19 changes: 11 additions & 8 deletions src/data-source/collection-browser-data-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
}
Expand All @@ -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();
}
Expand Down

0 comments on commit b45b065

Please sign in to comment.