@internetarchive/collection-browser 2.1.11 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -72,6 +72,12 @@ export class CollectionBrowserDataSource
72
72
  */
73
73
  private facetsLoading = false;
74
74
 
75
+ /**
76
+ * Whether the facets are actually visible -- if not, then we can delay any facet
77
+ * fetches until they become visible.
78
+ */
79
+ private facetsVisible = false;
80
+
75
81
  /**
76
82
  * Whether further query changes should be ignored and not trigger fetches
77
83
  */
@@ -238,6 +244,7 @@ export class CollectionBrowserDataSource
238
244
  this.numTileModels = 0;
239
245
  this.endOfDataReached = false;
240
246
  this.queryInitialized = false;
247
+ this.facetsLoading = false;
241
248
 
242
249
  // Invalidate any fetches in progress
243
250
  this.fetchesInProgress.clear();
@@ -379,21 +386,53 @@ export class CollectionBrowserDataSource
379
386
  this._initialSearchCompleteResolver = res;
380
387
  });
381
388
 
382
- const shouldFetchFacets =
383
- !this.host.suppressFacets &&
384
- !FACETLESS_PAGE_ELEMENTS.includes(this.host.profileElement!);
385
-
386
389
  // Fire the initial page & facet requests
387
390
  this.queryInitialized = true;
388
391
  await Promise.all([
389
392
  this.doInitialPageFetch(),
390
- shouldFetchFacets ? this.fetchFacets() : null,
393
+ this.canFetchFacets ? this.fetchFacets() : null,
391
394
  ]);
392
395
 
393
396
  // Resolve the `initialSearchComplete` promise for this search
394
397
  this._initialSearchCompleteResolver(true);
395
398
  }
396
399
 
400
+ /**
401
+ * @inheritdoc
402
+ */
403
+ async handleFacetVisibilityChange(visible: boolean): Promise<void> {
404
+ const facetsBecameVisible = !this.facetsVisible && visible;
405
+ this.facetsVisible = visible;
406
+
407
+ const needsFetch =
408
+ this.host.lazyLoadFacets && facetsBecameVisible && this.canFetchFacets;
409
+ if (needsFetch) {
410
+ this.fetchFacets();
411
+ }
412
+ }
413
+
414
+ /**
415
+ * Whether the data source & its host are in a state where a facet request should be fired.
416
+ * (i.e., they aren't suppressed or already loading, etc.)
417
+ */
418
+ private get canFetchFacets(): boolean {
419
+ // Don't fetch facets if they are suppressed entirely or not required for the current profile page element
420
+ if (this.host.suppressFacets) return false;
421
+ if (FACETLESS_PAGE_ELEMENTS.includes(this.host.profileElement!))
422
+ return false;
423
+
424
+ // If facets are to be lazy-loaded, don't fetch them if they are not going to be visible anyway
425
+ // (wait until they become visible instead)
426
+ if (this.host.lazyLoadFacets && !this.facetsVisible) return false;
427
+
428
+ // Don't fetch facets again if they are already fetched or pending
429
+ const facetsAlreadyFetched =
430
+ Object.keys(this.aggregations ?? {}).length > 0;
431
+ if (this.facetsLoading || facetsAlreadyFetched) return false;
432
+
433
+ return true;
434
+ }
435
+
397
436
  /**
398
437
  * @inheritdoc
399
438
  */
@@ -877,6 +916,8 @@ export class CollectionBrowserDataSource
877
916
  if (this.fetchesInProgress.has(facetFetchQueryKey)) return;
878
917
  this.fetchesInProgress.add(facetFetchQueryKey);
879
918
 
919
+ this.setFacetsLoading(true);
920
+
880
921
  const sortParams = this.host.sortParam ? [this.host.sortParam] : [];
881
922
  const params: SearchParams = {
882
923
  ...this.pageSpecifierParams,
@@ -893,7 +934,6 @@ export class CollectionBrowserDataSource
893
934
  'aggregations'
894
935
  );
895
936
 
896
- this.setFacetsLoading(true);
897
937
  const searchResponse = await this.host.searchService?.search(
898
938
  params,
899
939
  this.host.searchType
@@ -37,6 +37,7 @@ export interface CollectionBrowserSearchInterface
37
37
  readonly sortParam: SortParam | null;
38
38
  readonly defaultSortParam: SortParam | null;
39
39
  readonly suppressFacets?: boolean;
40
+ readonly lazyLoadFacets?: boolean;
40
41
  readonly initialPageNumber: number;
41
42
  readonly currentVisiblePageNumbers: number[];
42
43
  readonly clearResultsOnEmptyQuery?: boolean;