@internetarchive/collection-browser 1.5.2 → 1.5.3-alpha.1

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.
@@ -1,6 +1,6 @@
1
1
  import { LitElement, PropertyValues, TemplateResult } from 'lit';
2
2
  import type { InfiniteScrollerCellProviderInterface } from '@internetarchive/infinite-scroller';
3
- import { CollectionExtraInfo, SearchServiceInterface, SearchType, SortDirection, SortParam } from '@internetarchive/search-service';
3
+ import { SearchServiceInterface, SearchType, SortDirection, SortParam } from '@internetarchive/search-service';
4
4
  import type { SharedResizeObserverInterface, SharedResizeObserverResizeHandlerInterface } from '@internetarchive/shared-resize-observer';
5
5
  import '@internetarchive/infinite-scroller';
6
6
  import type { CollectionNameCacheInterface } from '@internetarchive/collection-name-cache';
@@ -37,7 +37,6 @@ export declare class CollectionBrowser extends LitElement implements InfiniteScr
37
37
  selectedFacets?: SelectedFacets;
38
38
  showHistogramDatePicker: boolean;
39
39
  collectionPagePath: string;
40
- collectionInfo?: CollectionExtraInfo;
41
40
  /** describes where this component is being used */
42
41
  searchContext: string;
43
42
  collectionNameCache?: CollectionNameCacheInterface;
@@ -76,6 +75,8 @@ export declare class CollectionBrowser extends LitElement implements InfiniteScr
76
75
  private mobileView;
77
76
  private mobileFacetsVisible;
78
77
  private contentWidth?;
78
+ private defaultSortField;
79
+ private defaultSortDirection;
79
80
  private placeholderType;
80
81
  private prefixFilterCountMap;
81
82
  private contentContainer;
@@ -357,6 +358,11 @@ export declare class CollectionBrowser extends LitElement implements InfiniteScr
357
358
  facetClickHandler({ detail: { key, state: facetState, negative }, }: CustomEvent<FacetEventDetails>): void;
358
359
  private fetchFacets;
359
360
  private scrollToPage;
361
+ /**
362
+ * Whether sorting by relevance makes sense for the current state.
363
+ * Currently equivalent to having a non-empty query.
364
+ */
365
+ private get isRelevanceSortAvailable();
360
366
  /**
361
367
  * Whether a search may be performed in the current state of the component.
362
368
  * This is only true if the search service is defined, and either
@@ -400,6 +406,12 @@ export declare class CollectionBrowser extends LitElement implements InfiniteScr
400
406
  */
401
407
  fetchPage(pageNumber: number, numInitialPages?: number): Promise<void>;
402
408
  private preloadCollectionNames;
409
+ /**
410
+ * Applies any default sort option for the current collection, by checking
411
+ * for one in the collection's metadata. If none is found, defaults to sorting
412
+ * descending by weekly views.
413
+ */
414
+ private applyDefaultCollectionSort;
403
415
  /**
404
416
  * This is useful for determining whether we need to reload the scroller.
405
417
  *
@@ -11,7 +11,7 @@ import './sort-filter-bar/sort-filter-bar';
11
11
  import './collection-facets';
12
12
  import './circular-activity-indicator';
13
13
  import './sort-filter-bar/sort-filter-bar';
14
- import { SortField, SortFieldToMetadataField, getDefaultSelectedFacets, prefixFilterAggregationKeys, } from './models';
14
+ import { SortField, SortFieldToMetadataField, getDefaultSelectedFacets, prefixFilterAggregationKeys, MetadataFieldToSortField, } from './models';
15
15
  import { RestorationStateHandler, } from './restoration-state-handler';
16
16
  import chevronIcon from './assets/img/icons/chevron';
17
17
  import './empty-placeholder';
@@ -24,7 +24,7 @@ let CollectionBrowser = class CollectionBrowser extends LitElement {
24
24
  this.baseImageUrl = 'https://archive.org';
25
25
  this.searchType = SearchType.METADATA;
26
26
  this.sortParam = null;
27
- this.selectedSort = SortField.relevance;
27
+ this.selectedSort = SortField.default;
28
28
  this.selectedTitleFilter = null;
29
29
  this.selectedCreatorFilter = null;
30
30
  this.sortDirection = null;
@@ -63,6 +63,8 @@ let CollectionBrowser = class CollectionBrowser extends LitElement {
63
63
  this.fullYearAggregationLoading = false;
64
64
  this.mobileView = false;
65
65
  this.mobileFacetsVisible = false;
66
+ this.defaultSortField = SortField.relevance;
67
+ this.defaultSortDirection = null;
66
68
  this.placeholderType = null;
67
69
  this.prefixFilterCountMap = {};
68
70
  /**
@@ -216,7 +218,7 @@ let CollectionBrowser = class CollectionBrowser extends LitElement {
216
218
  if (sort) {
217
219
  this.sortParam = null;
218
220
  this.sortDirection = null;
219
- this.selectedSort = SortField.relevance;
221
+ this.selectedSort = SortField.default;
220
222
  }
221
223
  }
222
224
  /**
@@ -402,8 +404,11 @@ let CollectionBrowser = class CollectionBrowser extends LitElement {
402
404
  get sortFilterBarTemplate() {
403
405
  return html `
404
406
  <sort-filter-bar
407
+ .defaultSortField=${this.defaultSortField}
408
+ .defaultSortDirection=${this.defaultSortDirection}
405
409
  .selectedSort=${this.selectedSort}
406
410
  .sortDirection=${this.sortDirection}
411
+ .showRelevance=${this.isRelevanceSortAvailable}
407
412
  .displayMode=${this.displayMode}
408
413
  .selectedTitleFilter=${this.selectedTitleFilter}
409
414
  .selectedCreatorFilter=${this.selectedCreatorFilter}
@@ -437,7 +442,7 @@ let CollectionBrowser = class CollectionBrowser extends LitElement {
437
442
  });
438
443
  }
439
444
  selectedSortChanged() {
440
- if (this.selectedSort === 'relevance') {
445
+ if ([SortField.default, SortField.relevance].includes(this.selectedSort)) {
441
446
  this.sortParam = null;
442
447
  return;
443
448
  }
@@ -868,6 +873,7 @@ let CollectionBrowser = class CollectionBrowser extends LitElement {
868
873
  return this._initialSearchCompletePromise;
869
874
  }
870
875
  async handleQueryChange() {
876
+ var _a;
871
877
  // only reset if the query has actually changed
872
878
  if (!this.searchService || this.pageFetchQueryKey === this.previousQueryKey)
873
879
  return;
@@ -892,6 +898,10 @@ let CollectionBrowser = class CollectionBrowser extends LitElement {
892
898
  this.infiniteScroller.itemCount = this.estimatedTileCount;
893
899
  this.infiniteScroller.reload();
894
900
  }
901
+ if (this.withinCollection && ((_a = this.baseQuery) === null || _a === void 0 ? void 0 : _a.trim())) {
902
+ this.defaultSortField = SortField.relevance;
903
+ this.defaultSortDirection = null;
904
+ }
895
905
  if (!this.initialQueryChangeHappened && this.initialPageNumber > 1) {
896
906
  this.scrollToPage(this.initialPageNumber);
897
907
  }
@@ -928,7 +938,7 @@ let CollectionBrowser = class CollectionBrowser extends LitElement {
928
938
  this.displayMode = restorationState.displayMode;
929
939
  if (restorationState.searchType != null)
930
940
  this.searchType = restorationState.searchType;
931
- this.selectedSort = (_a = restorationState.selectedSort) !== null && _a !== void 0 ? _a : SortField.relevance;
941
+ this.selectedSort = (_a = restorationState.selectedSort) !== null && _a !== void 0 ? _a : SortField.default;
932
942
  this.sortDirection = (_b = restorationState.sortDirection) !== null && _b !== void 0 ? _b : null;
933
943
  this.selectedTitleFilter = (_c = restorationState.selectedTitleFilter) !== null && _c !== void 0 ? _c : null;
934
944
  this.selectedCreatorFilter = (_d = restorationState.selectedCreatorFilter) !== null && _d !== void 0 ? _d : null;
@@ -1221,6 +1231,14 @@ let CollectionBrowser = class CollectionBrowser extends LitElement {
1221
1231
  }, 0);
1222
1232
  });
1223
1233
  }
1234
+ /**
1235
+ * Whether sorting by relevance makes sense for the current state.
1236
+ * Currently equivalent to having a non-empty query.
1237
+ */
1238
+ get isRelevanceSortAvailable() {
1239
+ var _a;
1240
+ return !!((_a = this.baseQuery) === null || _a === void 0 ? void 0 : _a.trim());
1241
+ }
1224
1242
  /**
1225
1243
  * Whether a search may be performed in the current state of the component.
1226
1244
  * This is only true if the search service is defined, and either
@@ -1341,7 +1359,11 @@ let CollectionBrowser = class CollectionBrowser extends LitElement {
1341
1359
  return;
1342
1360
  }
1343
1361
  this.totalResults = success.response.totalResults;
1344
- this.collectionInfo = success.response.collectionExtraInfo;
1362
+ if (this.withinCollection) {
1363
+ // For collections, we want the UI to respect the default sort option
1364
+ // which can be specified in metadata, or otherwise assumed to be week:desc
1365
+ this.applyDefaultCollectionSort(success.response.collectionExtraInfo);
1366
+ }
1345
1367
  const { results, collectionTitles } = success.response;
1346
1368
  if (results && results.length > 0) {
1347
1369
  // Load any collection titles present on the response into the cache,
@@ -1381,6 +1403,36 @@ let CollectionBrowser = class CollectionBrowser extends LitElement {
1381
1403
  const collectionIdsArray = Array.from(new Set(collectionIds));
1382
1404
  (_a = this.collectionNameCache) === null || _a === void 0 ? void 0 : _a.preloadIdentifiers(collectionIdsArray);
1383
1405
  }
1406
+ /**
1407
+ * Applies any default sort option for the current collection, by checking
1408
+ * for one in the collection's metadata. If none is found, defaults to sorting
1409
+ * descending by weekly views.
1410
+ */
1411
+ applyDefaultCollectionSort(collectionInfo) {
1412
+ var _a, _b;
1413
+ if (this.baseQuery) {
1414
+ // If there's a query set, then we default to relevance sorting regardless of
1415
+ // the collection metadata-specified sort.
1416
+ this.defaultSortField = SortField.relevance;
1417
+ this.defaultSortDirection = null;
1418
+ return;
1419
+ }
1420
+ const defaultSort = (_b = (_a = collectionInfo === null || collectionInfo === void 0 ? void 0 : collectionInfo.public_metadata) === null || _a === void 0 ? void 0 : _a['sort-by']) !== null && _b !== void 0 ? _b : '-week';
1421
+ // Account for both -field and field:dir formats
1422
+ let [field, dir] = defaultSort.split(':');
1423
+ if (field.startsWith('-')) {
1424
+ field = field.slice(1);
1425
+ dir = 'desc';
1426
+ }
1427
+ else if (!['asc', 'desc'].includes(dir)) {
1428
+ dir = 'asc';
1429
+ }
1430
+ const sortField = MetadataFieldToSortField[field];
1431
+ if (sortField && sortField !== SortField.default) {
1432
+ this.defaultSortField = sortField;
1433
+ this.defaultSortDirection = dir;
1434
+ }
1435
+ }
1384
1436
  /**
1385
1437
  * This is useful for determining whether we need to reload the scroller.
1386
1438
  *
@@ -2032,9 +2084,6 @@ __decorate([
2032
2084
  __decorate([
2033
2085
  property({ type: String })
2034
2086
  ], CollectionBrowser.prototype, "collectionPagePath", void 0);
2035
- __decorate([
2036
- property({ type: Object })
2037
- ], CollectionBrowser.prototype, "collectionInfo", void 0);
2038
2087
  __decorate([
2039
2088
  property({ type: String, reflect: true })
2040
2089
  ], CollectionBrowser.prototype, "searchContext", void 0);
@@ -2098,6 +2147,12 @@ __decorate([
2098
2147
  __decorate([
2099
2148
  state()
2100
2149
  ], CollectionBrowser.prototype, "contentWidth", void 0);
2150
+ __decorate([
2151
+ state()
2152
+ ], CollectionBrowser.prototype, "defaultSortField", void 0);
2153
+ __decorate([
2154
+ state()
2155
+ ], CollectionBrowser.prototype, "defaultSortDirection", void 0);
2101
2156
  __decorate([
2102
2157
  state()
2103
2158
  ], CollectionBrowser.prototype, "placeholderType", void 0);