@internetarchive/collection-browser 1.14.14-alpha.1 → 1.14.16-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.
Files changed (69) hide show
  1. package/dist/index.d.ts +1 -0
  2. package/dist/index.js +1 -0
  3. package/dist/index.js.map +1 -1
  4. package/dist/src/app-root.d.ts +0 -2
  5. package/dist/src/app-root.js +0 -8
  6. package/dist/src/app-root.js.map +1 -1
  7. package/dist/src/collection-browser.d.ts +24 -169
  8. package/dist/src/collection-browser.js +68 -684
  9. package/dist/src/collection-browser.js.map +1 -1
  10. package/dist/src/collection-facets/facet-row.d.ts +2 -2
  11. package/dist/src/collection-facets/facet-row.js +5 -10
  12. package/dist/src/collection-facets/facet-row.js.map +1 -1
  13. package/dist/src/collection-facets/facets-template.d.ts +2 -2
  14. package/dist/src/collection-facets/facets-template.js +2 -2
  15. package/dist/src/collection-facets/facets-template.js.map +1 -1
  16. package/dist/src/collection-facets/more-facets-content.d.ts +2 -9
  17. package/dist/src/collection-facets/more-facets-content.js +13 -18
  18. package/dist/src/collection-facets/more-facets-content.js.map +1 -1
  19. package/dist/src/collection-facets.d.ts +2 -3
  20. package/dist/src/collection-facets.js +5 -9
  21. package/dist/src/collection-facets.js.map +1 -1
  22. package/dist/src/data-source/collection-browser-data-source.d.ts +402 -0
  23. package/dist/src/data-source/collection-browser-data-source.js +742 -0
  24. package/dist/src/data-source/collection-browser-data-source.js.map +1 -0
  25. package/dist/src/data-source/models.d.ts +35 -0
  26. package/dist/src/data-source/models.js +2 -0
  27. package/dist/src/data-source/models.js.map +1 -0
  28. package/dist/src/tiles/hover/hover-pane-controller.d.ts +2 -2
  29. package/dist/src/tiles/hover/hover-pane-controller.js +1 -1
  30. package/dist/src/tiles/hover/hover-pane-controller.js.map +1 -1
  31. package/dist/src/tiles/hover/tile-hover-pane.d.ts +2 -2
  32. package/dist/src/tiles/hover/tile-hover-pane.js +2 -2
  33. package/dist/src/tiles/hover/tile-hover-pane.js.map +1 -1
  34. package/dist/src/tiles/list/tile-list.d.ts +3 -3
  35. package/dist/src/tiles/list/tile-list.js +7 -13
  36. package/dist/src/tiles/list/tile-list.js.map +1 -1
  37. package/dist/src/tiles/tile-dispatcher.d.ts +2 -2
  38. package/dist/src/tiles/tile-dispatcher.js +2 -3
  39. package/dist/src/tiles/tile-dispatcher.js.map +1 -1
  40. package/dist/test/collection-browser.test.js +43 -67
  41. package/dist/test/collection-browser.test.js.map +1 -1
  42. package/dist/test/collection-facets/facet-row.test.js +6 -17
  43. package/dist/test/collection-facets/facet-row.test.js.map +1 -1
  44. package/dist/test/collection-facets.test.js +0 -5
  45. package/dist/test/collection-facets.test.js.map +1 -1
  46. package/dist/test/tiles/list/tile-list.test.js +0 -5
  47. package/dist/test/tiles/list/tile-list.test.js.map +1 -1
  48. package/index.ts +4 -0
  49. package/package.json +3 -3
  50. package/src/app-root.ts +0 -10
  51. package/src/collection-browser.ts +79 -841
  52. package/src/collection-facets/facet-row.ts +4 -9
  53. package/src/collection-facets/facets-template.ts +3 -3
  54. package/src/collection-facets/more-facets-content.ts +15 -20
  55. package/src/collection-facets.ts +5 -10
  56. package/src/data-source/collection-browser-data-source.ts +1133 -0
  57. package/src/data-source/models.ts +45 -0
  58. package/src/tiles/hover/hover-pane-controller.ts +3 -3
  59. package/src/tiles/hover/tile-hover-pane.ts +3 -3
  60. package/src/tiles/list/tile-list.ts +13 -18
  61. package/src/tiles/tile-dispatcher.ts +3 -4
  62. package/test/collection-browser.test.ts +57 -79
  63. package/test/collection-facets/facet-row.test.ts +5 -18
  64. package/test/collection-facets.test.ts +0 -5
  65. package/test/tiles/list/tile-list.test.ts +0 -5
  66. package/dist/test/mocks/mock-collection-name-cache.d.ts +0 -9
  67. package/dist/test/mocks/mock-collection-name-cache.js +0 -18
  68. package/dist/test/mocks/mock-collection-name-cache.js.map +0 -1
  69. package/test/mocks/mock-collection-name-cache.ts +0 -24
@@ -0,0 +1,742 @@
1
+ import { FilterConstraint, FilterMapBuilder, SearchType, } from '@internetarchive/search-service';
2
+ import { prefixFilterAggregationKeys, } from '../models';
3
+ import { sha1 } from '../utils/sha1';
4
+ export class CollectionBrowserDataSource {
5
+ constructor(
6
+ /** The host element to which this controller should attach listeners */
7
+ host,
8
+ /** Default size of result pages */
9
+ pageSize) {
10
+ this.host = host;
11
+ this.pageSize = pageSize;
12
+ this.pages = {};
13
+ this.offset = 0;
14
+ this.numTileModels = 0;
15
+ /**
16
+ * Maps the full query key to the pages being fetched for that query
17
+ */
18
+ this.pageFetchesInProgress = {};
19
+ this.totalResults = 0;
20
+ this.endOfDataReached = false;
21
+ /**
22
+ * @inheritdoc
23
+ */
24
+ this.collectionTitles = new Map();
25
+ /**
26
+ * @inheritdoc
27
+ */
28
+ this.parentCollections = [];
29
+ /**
30
+ * @inheritdoc
31
+ */
32
+ this.prefixFilterCountMap = {};
33
+ this.host.addController(this);
34
+ }
35
+ /**
36
+ * @inheritdoc
37
+ */
38
+ get size() {
39
+ return this.numTileModels;
40
+ }
41
+ /**
42
+ * @inheritdoc
43
+ */
44
+ addPage(pageNum, pageTiles) {
45
+ this.pages[pageNum] = pageTiles;
46
+ this.numTileModels += pageTiles.length;
47
+ this.host.requestUpdate();
48
+ }
49
+ /**
50
+ * @inheritdoc
51
+ */
52
+ getPage(pageNum) {
53
+ return this.pages[pageNum];
54
+ }
55
+ /**
56
+ * @inheritdoc
57
+ */
58
+ hasPage(pageNum) {
59
+ return !!this.pages[pageNum];
60
+ }
61
+ /**
62
+ * @inheritdoc
63
+ */
64
+ getTileModelAt(index) {
65
+ var _a;
66
+ const pageNum = Math.floor(index / this.pageSize) + 1;
67
+ const indexOnPage = index % this.pageSize;
68
+ return (_a = this.pages[pageNum]) === null || _a === void 0 ? void 0 : _a[indexOnPage];
69
+ }
70
+ /**
71
+ * @inheritdoc
72
+ */
73
+ setPageSize(pageSize) {
74
+ this.reset();
75
+ this.pageSize = pageSize;
76
+ }
77
+ /**
78
+ * @inheritdoc
79
+ */
80
+ reset() {
81
+ this.pages = {};
82
+ this.aggregations = {};
83
+ this.yearHistogramAggregation = undefined;
84
+ this.pageFetchesInProgress = {};
85
+ this.offset = 0;
86
+ this.numTileModels = 0;
87
+ this.totalResults = 0;
88
+ this.host.requestUpdate();
89
+ }
90
+ /**
91
+ * @inheritdoc
92
+ */
93
+ async handleQueryChange() {
94
+ this.reset();
95
+ await Promise.all([
96
+ this.doInitialPageFetch(),
97
+ this.host.suppressFacets ? null : this.fetchFacets(),
98
+ ]);
99
+ }
100
+ /**
101
+ * @inheritdoc
102
+ */
103
+ map(callback) {
104
+ this.pages = Object.fromEntries(Object.entries(this.pages).map(([page, tileModels]) => [
105
+ page,
106
+ tileModels.map((model, index, array) => model ? callback(model, index, array) : model),
107
+ ]));
108
+ this.host.requestUpdate();
109
+ }
110
+ /**
111
+ * @inheritdoc
112
+ */
113
+ checkAllTiles() {
114
+ this.map(model => ({ ...model, checked: true }));
115
+ }
116
+ /**
117
+ * @inheritdoc
118
+ */
119
+ uncheckAllTiles() {
120
+ this.map(model => ({ ...model, checked: false }));
121
+ }
122
+ /**
123
+ * @inheritdoc
124
+ */
125
+ removeCheckedTiles() {
126
+ // To make sure our data source remains page-aligned, we will offset our data source by
127
+ // the number of removed tiles, so that we can just add the offset when the infinite
128
+ // scroller queries for cell contents.
129
+ // This only matters while we're still viewing the same set of results. If the user changes
130
+ // their query/filters/sort, then the data source is overwritten and the offset cleared.
131
+ const { checkedTileModels, uncheckedTileModels } = this;
132
+ const numChecked = checkedTileModels.length;
133
+ if (numChecked === 0)
134
+ return;
135
+ this.offset += numChecked;
136
+ const newPages = {};
137
+ // Which page the remaining tile models start on, post-offset
138
+ let offsetPageNumber = Math.floor(this.offset / this.pageSize) + 1;
139
+ let indexOnPage = this.offset % this.pageSize;
140
+ // Fill the pages up to that point with empty models
141
+ for (let page = 1; page <= offsetPageNumber; page += 1) {
142
+ const remainingHidden = this.offset - this.pageSize * (page - 1);
143
+ const offsetCellsOnPage = Math.min(this.pageSize, remainingHidden);
144
+ newPages[page] = Array(offsetCellsOnPage).fill(undefined);
145
+ }
146
+ // Shift all the remaining tiles into their new positions in the data source
147
+ for (const model of uncheckedTileModels) {
148
+ if (!newPages[offsetPageNumber])
149
+ newPages[offsetPageNumber] = [];
150
+ newPages[offsetPageNumber].push(model);
151
+ indexOnPage += 1;
152
+ if (indexOnPage >= this.pageSize) {
153
+ offsetPageNumber += 1;
154
+ indexOnPage = 0;
155
+ }
156
+ }
157
+ // Swap in the new pages
158
+ this.pages = newPages;
159
+ this.numTileModels -= numChecked;
160
+ this.host.requestUpdate();
161
+ }
162
+ /**
163
+ * @inheritdoc
164
+ */
165
+ get checkedTileModels() {
166
+ return this.getFilteredTileModels(model => model.checked);
167
+ }
168
+ /**
169
+ * @inheritdoc
170
+ */
171
+ get uncheckedTileModels() {
172
+ return this.getFilteredTileModels(model => !model.checked);
173
+ }
174
+ /**
175
+ * Returns a flattened, filtered array of all the tile models in the data source
176
+ * for which the given predicate returns a truthy value.
177
+ *
178
+ * @param predicate A callback function to apply on each tile model, as with Array.filter
179
+ * @returns A filtered array of tile models satisfying the predicate
180
+ */
181
+ getFilteredTileModels(predicate) {
182
+ return Object.values(this.pages)
183
+ .flat()
184
+ .filter((model, index, array) => model ? predicate(model, index, array) : false);
185
+ }
186
+ // DATA FETCHES
187
+ /**
188
+ * @inheritdoc
189
+ */
190
+ get canPerformSearch() {
191
+ var _a;
192
+ if (!this.host.searchService)
193
+ return false;
194
+ const trimmedQuery = (_a = this.host.baseQuery) === null || _a === void 0 ? void 0 : _a.trim();
195
+ const hasNonEmptyQuery = !!trimmedQuery;
196
+ const isCollectionSearch = !!this.host.withinCollection;
197
+ const isMetadataSearch = this.host.searchType === SearchType.METADATA;
198
+ // Metadata searches within a collection are allowed to have no query.
199
+ // Otherwise, a non-empty query must be set.
200
+ return hasNonEmptyQuery || (isCollectionSearch && isMetadataSearch);
201
+ }
202
+ /**
203
+ * The query key is a string that uniquely identifies the current search.
204
+ * It consists of:
205
+ * - The current base query
206
+ * - The current collection
207
+ * - The current search type
208
+ * - Any currently-applied facets
209
+ * - Any currently-applied date range
210
+ * - Any currently-applied prefix filters
211
+ * - The current sort options
212
+ *
213
+ * This lets us keep track of queries so we don't persist data that's
214
+ * no longer relevant.
215
+ */
216
+ get pageFetchQueryKey() {
217
+ var _a, _b, _c, _d;
218
+ const sortField = (_b = (_a = this.host.sortParam) === null || _a === void 0 ? void 0 : _a.field) !== null && _b !== void 0 ? _b : 'none';
219
+ const sortDirection = (_d = (_c = this.host.sortParam) === null || _c === void 0 ? void 0 : _c.direction) !== null && _d !== void 0 ? _d : 'none';
220
+ return `${this.fullQuery}-${this.host.withinCollection}-${this.host.searchType}-${sortField}-${sortDirection}`;
221
+ }
222
+ /**
223
+ * Similar to `pageFetchQueryKey` above, but excludes sort fields since they
224
+ * are not relevant in determining aggregation queries.
225
+ */
226
+ get facetFetchQueryKey() {
227
+ return `${this.fullQuery}-${this.host.withinCollection}-${this.host.searchType}`;
228
+ }
229
+ /**
230
+ * Constructs a search service FilterMap object from the combination of
231
+ * all the currently-applied filters. This includes any facets, letter
232
+ * filters, and date range.
233
+ */
234
+ get filterMap() {
235
+ const builder = new FilterMapBuilder();
236
+ // Add the date range, if applicable
237
+ if (this.host.minSelectedDate) {
238
+ builder.addFilter('year', this.host.minSelectedDate, FilterConstraint.GREATER_OR_EQUAL);
239
+ }
240
+ if (this.host.maxSelectedDate) {
241
+ builder.addFilter('year', this.host.maxSelectedDate, FilterConstraint.LESS_OR_EQUAL);
242
+ }
243
+ // Add any selected facets
244
+ if (this.host.selectedFacets) {
245
+ for (const [facetName, facetValues] of Object.entries(this.host.selectedFacets)) {
246
+ const { name, values } = this.prepareFacetForFetch(facetName, facetValues);
247
+ for (const [value, bucket] of Object.entries(values)) {
248
+ let constraint;
249
+ if (bucket.state === 'selected') {
250
+ constraint = FilterConstraint.INCLUDE;
251
+ }
252
+ else if (bucket.state === 'hidden') {
253
+ constraint = FilterConstraint.EXCLUDE;
254
+ }
255
+ if (constraint) {
256
+ builder.addFilter(name, value, constraint);
257
+ }
258
+ }
259
+ }
260
+ }
261
+ // Add any letter filters
262
+ if (this.host.selectedTitleFilter) {
263
+ builder.addFilter('firstTitle', this.host.selectedTitleFilter, FilterConstraint.INCLUDE);
264
+ }
265
+ if (this.host.selectedCreatorFilter) {
266
+ builder.addFilter('firstCreator', this.host.selectedCreatorFilter, FilterConstraint.INCLUDE);
267
+ }
268
+ const filterMap = builder.build();
269
+ return filterMap;
270
+ }
271
+ /**
272
+ * Produces a compact unique ID for a search request that can help with debugging
273
+ * on the backend by making related requests easier to trace through different services.
274
+ * (e.g., tying the hits/aggregations requests for the same page back to a single hash).
275
+ *
276
+ * @param params The search service parameters for the request
277
+ * @param kind The kind of request (hits-only, aggregations-only, or both)
278
+ * @returns A Promise resolving to the uid to apply to the request
279
+ */
280
+ async requestUID(params, kind) {
281
+ var _a;
282
+ const paramsToHash = JSON.stringify({
283
+ pageType: params.pageType,
284
+ pageTarget: params.pageTarget,
285
+ query: params.query,
286
+ fields: params.fields,
287
+ filters: params.filters,
288
+ sort: params.sort,
289
+ searchType: this.host.searchType,
290
+ });
291
+ const fullQueryHash = (await sha1(paramsToHash)).slice(0, 20); // First 80 bits of SHA-1 are plenty for this
292
+ const sessionId = (await this.host.getSessionId()).slice(0, 20); // Likewise
293
+ const page = (_a = params.page) !== null && _a !== void 0 ? _a : 0;
294
+ const kindPrefix = kind.charAt(0); // f = full, h = hits, a = aggregations
295
+ const currentTime = Date.now();
296
+ return `R:${fullQueryHash}-S:${sessionId}-P:${page}-K:${kindPrefix}-T:${currentTime}`;
297
+ }
298
+ /**
299
+ * @inheritdoc
300
+ */
301
+ get collectionParams() {
302
+ return this.host.withinCollection
303
+ ? {
304
+ pageType: 'collection_details',
305
+ pageTarget: this.host.withinCollection,
306
+ }
307
+ : null;
308
+ }
309
+ /**
310
+ * The full query, including year facets and date range clauses
311
+ */
312
+ get fullQuery() {
313
+ var _a, _b;
314
+ let fullQuery = (_b = (_a = this.host.baseQuery) === null || _a === void 0 ? void 0 : _a.trim()) !== null && _b !== void 0 ? _b : '';
315
+ const { facetQuery, dateRangeQueryClause, sortFilterQueries } = this;
316
+ if (facetQuery) {
317
+ fullQuery += ` AND ${facetQuery}`;
318
+ }
319
+ if (dateRangeQueryClause) {
320
+ fullQuery += ` AND ${dateRangeQueryClause}`;
321
+ }
322
+ if (sortFilterQueries) {
323
+ fullQuery += ` AND ${sortFilterQueries}`;
324
+ }
325
+ return fullQuery.trim();
326
+ }
327
+ /**
328
+ * Generates a query string representing the current set of applied facets
329
+ *
330
+ * Example: `mediatype:("collection" OR "audio" OR -"etree") AND year:("2000" OR "2001")`
331
+ */
332
+ get facetQuery() {
333
+ var _a;
334
+ if (!this.host.selectedFacets)
335
+ return undefined;
336
+ const facetClauses = [];
337
+ for (const [facetName, facetValues] of Object.entries(this.host.selectedFacets)) {
338
+ facetClauses.push(this.buildFacetClause(facetName, facetValues));
339
+ }
340
+ return (_a = this.joinFacetClauses(facetClauses)) === null || _a === void 0 ? void 0 : _a.trim();
341
+ }
342
+ get dateRangeQueryClause() {
343
+ if (!this.host.minSelectedDate || !this.host.maxSelectedDate) {
344
+ return undefined;
345
+ }
346
+ return `year:[${this.host.minSelectedDate} TO ${this.host.maxSelectedDate}]`;
347
+ }
348
+ get sortFilterQueries() {
349
+ const queries = [this.titleQuery, this.creatorQuery];
350
+ return queries.filter(q => q).join(' AND ');
351
+ }
352
+ /**
353
+ * Returns a query clause identifying the currently selected title filter,
354
+ * e.g., `firstTitle:X`.
355
+ */
356
+ get titleQuery() {
357
+ return this.host.selectedTitleFilter
358
+ ? `firstTitle:${this.host.selectedTitleFilter}`
359
+ : undefined;
360
+ }
361
+ /**
362
+ * Returns a query clause identifying the currently selected creator filter,
363
+ * e.g., `firstCreator:X`.
364
+ */
365
+ get creatorQuery() {
366
+ return this.host.selectedCreatorFilter
367
+ ? `firstCreator:${this.host.selectedCreatorFilter}`
368
+ : undefined;
369
+ }
370
+ /**
371
+ * Builds an OR-joined facet clause for the given facet name and values.
372
+ *
373
+ * E.g., for name `subject` and values
374
+ * `{ foo: { state: 'selected' }, bar: { state: 'hidden' } }`
375
+ * this will produce the clause
376
+ * `subject:("foo" OR -"bar")`.
377
+ *
378
+ * @param facetName The facet type (e.g., 'collection')
379
+ * @param facetValues The facet buckets, mapped by their keys
380
+ */
381
+ buildFacetClause(facetName, facetValues) {
382
+ const { name: facetQueryName, values } = this.prepareFacetForFetch(facetName, facetValues);
383
+ const facetEntries = Object.entries(values);
384
+ if (facetEntries.length === 0)
385
+ return '';
386
+ const facetValuesArray = [];
387
+ for (const [key, facetData] of facetEntries) {
388
+ const plusMinusPrefix = facetData.state === 'hidden' ? '-' : '';
389
+ facetValuesArray.push(`${plusMinusPrefix}"${key}"`);
390
+ }
391
+ const valueQuery = facetValuesArray.join(` OR `);
392
+ return `${facetQueryName}:(${valueQuery})`;
393
+ }
394
+ /**
395
+ * Handles some special pre-request normalization steps for certain facet types
396
+ * that require them.
397
+ *
398
+ * @param facetName The name of the facet type (e.g., 'language')
399
+ * @param facetValues An array of values for that facet type
400
+ */
401
+ prepareFacetForFetch(facetName, facetValues) {
402
+ // eslint-disable-next-line prefer-const
403
+ let [normalizedName, normalizedValues] = [facetName, facetValues];
404
+ // The full "search engine" name of the lending field is "lending___status"
405
+ if (facetName === 'lending') {
406
+ normalizedName = 'lending___status';
407
+ }
408
+ return {
409
+ name: normalizedName,
410
+ values: normalizedValues,
411
+ };
412
+ }
413
+ /**
414
+ * Takes an array of facet clauses, and combines them into a
415
+ * full AND-joined facet query string. Empty clauses are ignored.
416
+ */
417
+ joinFacetClauses(facetClauses) {
418
+ const nonEmptyFacetClauses = facetClauses.filter(clause => clause.length > 0);
419
+ return nonEmptyFacetClauses.length > 0
420
+ ? `(${nonEmptyFacetClauses.join(' AND ')})`
421
+ : undefined;
422
+ }
423
+ /**
424
+ * Fires a backend request to fetch a set of aggregations (representing UI facets) for
425
+ * the current search state.
426
+ */
427
+ async fetchFacets() {
428
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j;
429
+ const trimmedQuery = (_a = this.host.baseQuery) === null || _a === void 0 ? void 0 : _a.trim();
430
+ if (!this.canPerformSearch)
431
+ return;
432
+ const { facetFetchQueryKey } = this;
433
+ const sortParams = this.host.sortParam ? [this.host.sortParam] : [];
434
+ const params = {
435
+ ...this.collectionParams,
436
+ query: trimmedQuery || '',
437
+ rows: 0,
438
+ filters: this.filterMap,
439
+ // Fetch a few extra buckets beyond the 6 we show, in case some get suppressed
440
+ aggregationsSize: 10,
441
+ // Note: we don't need an aggregations param to fetch the default aggregations from the PPS.
442
+ // The default aggregations for the search_results page type should be what we need here.
443
+ };
444
+ params.uid = await this.requestUID({ ...params, sort: sortParams }, 'aggregations');
445
+ this.host.setFacetsLoading(true);
446
+ const searchResponse = await ((_b = this.host.searchService) === null || _b === void 0 ? void 0 : _b.search(params, this.host.searchType));
447
+ const success = searchResponse === null || searchResponse === void 0 ? void 0 : searchResponse.success;
448
+ // This is checking to see if the query has changed since the data was fetched.
449
+ // If so, we just want to discard this set of aggregations because they are
450
+ // likely no longer valid for the newer query.
451
+ const queryChangedSinceFetch = facetFetchQueryKey !== this.facetFetchQueryKey;
452
+ if (queryChangedSinceFetch)
453
+ return;
454
+ if (!success) {
455
+ const errorMsg = (_c = searchResponse === null || searchResponse === void 0 ? void 0 : searchResponse.error) === null || _c === void 0 ? void 0 : _c.message;
456
+ const detailMsg = (_e = (_d = searchResponse === null || searchResponse === void 0 ? void 0 : searchResponse.error) === null || _d === void 0 ? void 0 : _d.details) === null || _e === void 0 ? void 0 : _e.message;
457
+ if (!errorMsg && !detailMsg) {
458
+ // @ts-ignore: Property 'Sentry' does not exist on type 'Window & typeof globalThis'
459
+ (_g = (_f = window === null || window === void 0 ? void 0 : window.Sentry) === null || _f === void 0 ? void 0 : _f.captureMessage) === null || _g === void 0 ? void 0 : _g.call(_f, 'Missing or malformed facet response from backend', 'error');
460
+ }
461
+ return;
462
+ }
463
+ const { aggregations, collectionTitles } = success.response;
464
+ this.aggregations = aggregations;
465
+ if (collectionTitles) {
466
+ for (const [id, title] of Object.entries(collectionTitles)) {
467
+ this.collectionTitles.set(id, title);
468
+ }
469
+ }
470
+ this.yearHistogramAggregation =
471
+ (_j = (_h = success === null || success === void 0 ? void 0 : success.response) === null || _h === void 0 ? void 0 : _h.aggregations) === null || _j === void 0 ? void 0 : _j.year_histogram;
472
+ this.host.setFacetsLoading(false);
473
+ }
474
+ /**
475
+ * Performs the initial page fetch(es) for the current search state.
476
+ */
477
+ async doInitialPageFetch() {
478
+ this.host.setSearchResultsLoading(true);
479
+ // Try to batch 2 initial page requests when possible
480
+ await this.fetchPage(this.host.initialPageNumber, 2);
481
+ this.host.setSearchResultsLoading(false);
482
+ }
483
+ /**
484
+ * Fetches one or more pages of results and updates the data source.
485
+ *
486
+ * @param pageNumber The page number to fetch
487
+ * @param numInitialPages If this is an initial page fetch (`pageNumber = 1`),
488
+ * specifies how many pages to batch together in one request. Ignored
489
+ * if `pageNumber != 1`, defaulting to a single page.
490
+ */
491
+ async fetchPage(pageNumber, numInitialPages = 1) {
492
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
493
+ const trimmedQuery = (_a = this.host.baseQuery) === null || _a === void 0 ? void 0 : _a.trim();
494
+ if (!this.canPerformSearch)
495
+ return;
496
+ // if we already have data, don't fetch again
497
+ if (this.hasPage(pageNumber))
498
+ return;
499
+ if (this.endOfDataReached)
500
+ return;
501
+ // Batch multiple initial page requests together if needed (e.g., can request
502
+ // pages 1 and 2 together in a single request).
503
+ const numPages = pageNumber === 1 ? numInitialPages : 1;
504
+ const numRows = this.pageSize * numPages;
505
+ // if a fetch is already in progress for this query and page, don't fetch again
506
+ const { pageFetchQueryKey } = this;
507
+ const pageFetches = (_b = this.pageFetchesInProgress[pageFetchQueryKey]) !== null && _b !== void 0 ? _b : new Set();
508
+ if (pageFetches.has(pageNumber))
509
+ return;
510
+ for (let i = 0; i < numPages; i += 1) {
511
+ pageFetches.add(pageNumber + i);
512
+ }
513
+ this.pageFetchesInProgress[pageFetchQueryKey] = pageFetches;
514
+ const sortParams = this.host.sortParam ? [this.host.sortParam] : [];
515
+ const params = {
516
+ ...this.collectionParams,
517
+ query: trimmedQuery || '',
518
+ page: pageNumber,
519
+ rows: numRows,
520
+ sort: sortParams,
521
+ filters: this.filterMap,
522
+ aggregations: { omit: true },
523
+ };
524
+ params.uid = await this.requestUID(params, 'hits');
525
+ const searchResponse = await ((_c = this.host.searchService) === null || _c === void 0 ? void 0 : _c.search(params, this.host.searchType));
526
+ const success = searchResponse === null || searchResponse === void 0 ? void 0 : searchResponse.success;
527
+ // This is checking to see if the query has changed since the data was fetched.
528
+ // If so, we just want to discard the data since there should be a new query
529
+ // right behind it.
530
+ const queryChangedSinceFetch = pageFetchQueryKey !== this.pageFetchQueryKey;
531
+ if (queryChangedSinceFetch)
532
+ return;
533
+ if (!success) {
534
+ const errorMsg = (_d = searchResponse === null || searchResponse === void 0 ? void 0 : searchResponse.error) === null || _d === void 0 ? void 0 : _d.message;
535
+ const detailMsg = (_f = (_e = searchResponse === null || searchResponse === void 0 ? void 0 : searchResponse.error) === null || _e === void 0 ? void 0 : _e.details) === null || _f === void 0 ? void 0 : _f.message;
536
+ this.host.queryErrorMessage = `${errorMsg !== null && errorMsg !== void 0 ? errorMsg : ''}${detailMsg ? `; ${detailMsg}` : ''}`;
537
+ if (!this.host.queryErrorMessage) {
538
+ this.host.queryErrorMessage =
539
+ 'Missing or malformed response from backend';
540
+ // @ts-ignore: Property 'Sentry' does not exist on type 'Window & typeof globalThis'
541
+ (_h = (_g = window === null || window === void 0 ? void 0 : window.Sentry) === null || _g === void 0 ? void 0 : _g.captureMessage) === null || _h === void 0 ? void 0 : _h.call(_g, this.queryErrorMessage, 'error');
542
+ }
543
+ for (let i = 0; i < numPages; i += 1) {
544
+ (_j = this.pageFetchesInProgress[pageFetchQueryKey]) === null || _j === void 0 ? void 0 : _j.delete(pageNumber + i);
545
+ }
546
+ this.host.setSearchResultsLoading(false);
547
+ return;
548
+ }
549
+ this.totalResults = success.response.totalResults - this.offset;
550
+ // display event to offshoot when result count is zero.
551
+ if (this.totalResults === 0) {
552
+ this.host.emitEmptyResults();
553
+ }
554
+ if (this.host.withinCollection) {
555
+ this.collectionExtraInfo = success.response.collectionExtraInfo;
556
+ // For collections, we want the UI to respect the default sort option
557
+ // which can be specified in metadata, or otherwise assumed to be week:desc
558
+ this.host.applyDefaultCollectionSort(this.collectionExtraInfo);
559
+ if (this.collectionExtraInfo) {
560
+ this.parentCollections = [].concat((_l = (_k = this.collectionExtraInfo.public_metadata) === null || _k === void 0 ? void 0 : _k.collection) !== null && _l !== void 0 ? _l : []);
561
+ }
562
+ }
563
+ const { results, collectionTitles } = success.response;
564
+ if (results && results.length > 0) {
565
+ // Load any collection titles present on the response into the cache,
566
+ // or queue up preload fetches for them if none were present.
567
+ if (collectionTitles) {
568
+ for (const [id, title] of Object.entries(collectionTitles)) {
569
+ this.collectionTitles.set(id, title);
570
+ }
571
+ }
572
+ // Update the data source for each returned page
573
+ for (let i = 0; i < numPages; i += 1) {
574
+ const pageStartIndex = this.pageSize * i;
575
+ this.addTilesToDataSource(pageNumber + i, results.slice(pageStartIndex, pageStartIndex + this.pageSize));
576
+ }
577
+ }
578
+ // When we reach the end of the data, we can set the infinite scroller's
579
+ // item count to the real total number of results (rather than the
580
+ // temporary estimates based on pages rendered so far).
581
+ const resultCountDiscrepancy = numRows - results.length;
582
+ if (resultCountDiscrepancy > 0) {
583
+ this.endOfDataReached = true;
584
+ this.host.setTotalResultCount(this.totalResults);
585
+ }
586
+ for (let i = 0; i < numPages; i += 1) {
587
+ (_m = this.pageFetchesInProgress[pageFetchQueryKey]) === null || _m === void 0 ? void 0 : _m.delete(pageNumber + i);
588
+ }
589
+ }
590
+ /**
591
+ * Update the datasource from the fetch response
592
+ *
593
+ * @param pageNumber
594
+ * @param results
595
+ */
596
+ addTilesToDataSource(pageNumber, results) {
597
+ // copy our existing datasource so when we set it below, it gets set
598
+ // instead of modifying the existing dataSource since object changes
599
+ // don't trigger a re-render
600
+ // const datasource = { ...this.dataSource };
601
+ const tiles = [];
602
+ results === null || results === void 0 ? void 0 : results.forEach(result => {
603
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13;
604
+ if (!result.identifier)
605
+ return;
606
+ let loginRequired = false;
607
+ let contentWarning = false;
608
+ // Check if item and item in "modifying" collection, setting above flags
609
+ if (((_a = result.collection) === null || _a === void 0 ? void 0 : _a.values.length) &&
610
+ ((_b = result.mediatype) === null || _b === void 0 ? void 0 : _b.value) !== 'collection') {
611
+ for (const collection of (_d = (_c = result.collection) === null || _c === void 0 ? void 0 : _c.values) !== null && _d !== void 0 ? _d : []) {
612
+ if (collection === 'loggedin') {
613
+ loginRequired = true;
614
+ if (contentWarning)
615
+ break;
616
+ }
617
+ if (collection === 'no-preview') {
618
+ contentWarning = true;
619
+ if (loginRequired)
620
+ break;
621
+ }
622
+ }
623
+ }
624
+ tiles.push({
625
+ averageRating: (_e = result.avg_rating) === null || _e === void 0 ? void 0 : _e.value,
626
+ checked: false,
627
+ collections: (_g = (_f = result.collection) === null || _f === void 0 ? void 0 : _f.values) !== null && _g !== void 0 ? _g : [],
628
+ collectionFilesCount: (_j = (_h = result.collection_files_count) === null || _h === void 0 ? void 0 : _h.value) !== null && _j !== void 0 ? _j : 0,
629
+ collectionSize: (_l = (_k = result.collection_size) === null || _k === void 0 ? void 0 : _k.value) !== null && _l !== void 0 ? _l : 0,
630
+ commentCount: (_o = (_m = result.num_reviews) === null || _m === void 0 ? void 0 : _m.value) !== null && _o !== void 0 ? _o : 0,
631
+ creator: (_p = result.creator) === null || _p === void 0 ? void 0 : _p.value,
632
+ creators: (_r = (_q = result.creator) === null || _q === void 0 ? void 0 : _q.values) !== null && _r !== void 0 ? _r : [],
633
+ dateAdded: (_s = result.addeddate) === null || _s === void 0 ? void 0 : _s.value,
634
+ dateArchived: (_t = result.publicdate) === null || _t === void 0 ? void 0 : _t.value,
635
+ datePublished: (_u = result.date) === null || _u === void 0 ? void 0 : _u.value,
636
+ dateReviewed: (_v = result.reviewdate) === null || _v === void 0 ? void 0 : _v.value,
637
+ description: (_w = result.description) === null || _w === void 0 ? void 0 : _w.values.join('\n'),
638
+ favCount: (_y = (_x = result.num_favorites) === null || _x === void 0 ? void 0 : _x.value) !== null && _y !== void 0 ? _y : 0,
639
+ href: this.collapseRepeatedQuotes((_z = result.__href__) === null || _z === void 0 ? void 0 : _z.value),
640
+ identifier: result.identifier,
641
+ issue: (_0 = result.issue) === null || _0 === void 0 ? void 0 : _0.value,
642
+ itemCount: (_2 = (_1 = result.item_count) === null || _1 === void 0 ? void 0 : _1.value) !== null && _2 !== void 0 ? _2 : 0,
643
+ mediatype: this.getMediatype(result),
644
+ snippets: (_4 = (_3 = result.highlight) === null || _3 === void 0 ? void 0 : _3.values) !== null && _4 !== void 0 ? _4 : [],
645
+ source: (_5 = result.source) === null || _5 === void 0 ? void 0 : _5.value,
646
+ subjects: (_7 = (_6 = result.subject) === null || _6 === void 0 ? void 0 : _6.values) !== null && _7 !== void 0 ? _7 : [],
647
+ title: (_9 = (_8 = result.title) === null || _8 === void 0 ? void 0 : _8.value) !== null && _9 !== void 0 ? _9 : '',
648
+ volume: (_10 = result.volume) === null || _10 === void 0 ? void 0 : _10.value,
649
+ viewCount: (_12 = (_11 = result.downloads) === null || _11 === void 0 ? void 0 : _11.value) !== null && _12 !== void 0 ? _12 : 0,
650
+ weeklyViewCount: (_13 = result.week) === null || _13 === void 0 ? void 0 : _13.value,
651
+ loginRequired,
652
+ contentWarning,
653
+ });
654
+ });
655
+ this.addPage(pageNumber, tiles);
656
+ const visiblePages = this.host.currentVisiblePageNumbers;
657
+ const needsReload = visiblePages.includes(pageNumber);
658
+ if (needsReload) {
659
+ this.host.refreshVisibleResults();
660
+ }
661
+ }
662
+ /**
663
+ * Returns the mediatype string for the given search result, taking into account
664
+ * the special `favorited_search` hit type.
665
+ * @param result The search result to extract a mediatype from
666
+ */
667
+ getMediatype(result) {
668
+ var _a, _b, _c;
669
+ /**
670
+ * hit_type == 'favorited_search' is basically a new hit_type
671
+ * - we are getting from PPS.
672
+ * - which gives response for fav- collection
673
+ * - having favorited items like account/collection/item etc..
674
+ * - as user can also favorite a search result (a search page)
675
+ * - so we need to have response (having fav- items and fav- search results)
676
+ *
677
+ * if backend hit_type == 'favorited_search'
678
+ * - let's assume a "search" as new mediatype
679
+ */
680
+ if (((_a = result === null || result === void 0 ? void 0 : result.rawMetadata) === null || _a === void 0 ? void 0 : _a.hit_type) === 'favorited_search') {
681
+ return 'search';
682
+ }
683
+ return (_c = (_b = result.mediatype) === null || _b === void 0 ? void 0 : _b.value) !== null && _c !== void 0 ? _c : 'data';
684
+ }
685
+ /**
686
+ * Returns the input string, but removing one set of quotes from all instances of
687
+ * ""clauses wrapped in two sets of quotes"". This assumes the quotes are already
688
+ * URL-encoded.
689
+ *
690
+ * This should be a temporary measure to address the fact that the __href__ field
691
+ * sometimes acquires extra quotation marks during query rewriting. Once there is a
692
+ * full Lucene parser in place that handles quoted queries correctly, this can likely
693
+ * be removed.
694
+ */
695
+ collapseRepeatedQuotes(str) {
696
+ return str === null || str === void 0 ? void 0 : str.replace(/%22%22(?!%22%22)(.+?)%22%22/g, '%22$1%22');
697
+ }
698
+ /**
699
+ * Fetches the aggregation buckets for the given prefix filter type.
700
+ */
701
+ async fetchPrefixFilterBuckets(filterType) {
702
+ var _a, _b, _c, _d, _e, _f, _g;
703
+ const trimmedQuery = (_a = this.host.baseQuery) === null || _a === void 0 ? void 0 : _a.trim();
704
+ if (!this.canPerformSearch)
705
+ return [];
706
+ const filterAggregationKey = prefixFilterAggregationKeys[filterType];
707
+ const sortParams = this.host.sortParam ? [this.host.sortParam] : [];
708
+ const params = {
709
+ ...this.collectionParams,
710
+ query: trimmedQuery || '',
711
+ rows: 0,
712
+ filters: this.filterMap,
713
+ // Only fetch the firstTitle or firstCreator aggregation
714
+ aggregations: { simpleParams: [filterAggregationKey] },
715
+ // Fetch all 26 letter buckets
716
+ aggregationsSize: 26,
717
+ };
718
+ params.uid = await this.requestUID({ ...params, sort: sortParams }, 'aggregations');
719
+ const searchResponse = await ((_b = this.host.searchService) === null || _b === void 0 ? void 0 : _b.search(params, this.host.searchType));
720
+ return ((_g = (_f = (_e = (_d = (_c = searchResponse === null || searchResponse === void 0 ? void 0 : searchResponse.success) === null || _c === void 0 ? void 0 : _c.response) === null || _d === void 0 ? void 0 : _d.aggregations) === null || _e === void 0 ? void 0 : _e[filterAggregationKey]) === null || _f === void 0 ? void 0 : _f.buckets) !== null && _g !== void 0 ? _g : []);
721
+ }
722
+ /**
723
+ * Fetches and caches the prefix filter counts for the given filter type.
724
+ */
725
+ async updatePrefixFilterCounts(filterType) {
726
+ const { facetFetchQueryKey } = this;
727
+ const buckets = await this.fetchPrefixFilterBuckets(filterType);
728
+ // Don't update the filter counts for an outdated query (if it has been changed
729
+ // since we sent the request)
730
+ const queryChangedSinceFetch = facetFetchQueryKey !== this.facetFetchQueryKey;
731
+ if (queryChangedSinceFetch)
732
+ return;
733
+ // Unpack the aggregation buckets into a simple map like { 'A': 50, 'B': 25, ... }
734
+ this.prefixFilterCountMap = { ...this.prefixFilterCountMap }; // Clone the object to trigger an update
735
+ this.prefixFilterCountMap[filterType] = buckets.reduce((acc, bucket) => {
736
+ acc[bucket.key.toUpperCase()] = bucket.doc_count;
737
+ return acc;
738
+ }, {});
739
+ this.host.requestUpdate();
740
+ }
741
+ }
742
+ //# sourceMappingURL=collection-browser-data-source.js.map