@internetarchive/collection-browser 2.18.2-webdev-7743.1 → 2.18.3-alpha-webdev5427.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.
Files changed (48) hide show
  1. package/.editorconfig +29 -29
  2. package/.github/workflows/ci.yml +27 -27
  3. package/.github/workflows/gh-pages-main.yml +39 -39
  4. package/.github/workflows/npm-publish.yml +39 -39
  5. package/.github/workflows/pr-preview.yml +38 -38
  6. package/.husky/pre-commit +4 -4
  7. package/.prettierignore +1 -1
  8. package/LICENSE +661 -661
  9. package/README.md +83 -83
  10. package/dist/src/collection-browser.js +682 -682
  11. package/dist/src/collection-browser.js.map +1 -1
  12. package/dist/src/collection-facets.js +291 -274
  13. package/dist/src/collection-facets.js.map +1 -1
  14. package/dist/src/expanded-date-picker.d.ts +6 -4
  15. package/dist/src/expanded-date-picker.js +64 -55
  16. package/dist/src/expanded-date-picker.js.map +1 -1
  17. package/dist/src/models.js.map +1 -1
  18. package/dist/src/sort-filter-bar/sort-filter-bar.js +382 -382
  19. package/dist/src/sort-filter-bar/sort-filter-bar.js.map +1 -1
  20. package/dist/src/tiles/grid/item-tile.js +139 -139
  21. package/dist/src/tiles/grid/item-tile.js.map +1 -1
  22. package/dist/src/tiles/grid/styles/tile-grid-shared-styles.js +118 -118
  23. package/dist/src/tiles/grid/styles/tile-grid-shared-styles.js.map +1 -1
  24. package/dist/src/tiles/list/tile-list.js +289 -289
  25. package/dist/src/tiles/list/tile-list.js.map +1 -1
  26. package/dist/src/tiles/tile-dispatcher.js +200 -200
  27. package/dist/src/tiles/tile-dispatcher.js.map +1 -1
  28. package/dist/test/sort-filter-bar/sort-filter-bar.test.js +37 -37
  29. package/dist/test/sort-filter-bar/sort-filter-bar.test.js.map +1 -1
  30. package/eslint.config.mjs +53 -53
  31. package/index.html +24 -24
  32. package/local.archive.org.cert +86 -86
  33. package/local.archive.org.key +27 -27
  34. package/package.json +117 -117
  35. package/renovate.json +6 -6
  36. package/src/collection-browser.ts +2712 -2712
  37. package/src/collection-facets.ts +990 -966
  38. package/src/expanded-date-picker.ts +191 -175
  39. package/src/models.ts +822 -822
  40. package/src/sort-filter-bar/sort-filter-bar.ts +1189 -1189
  41. package/src/tiles/grid/item-tile.ts +339 -339
  42. package/src/tiles/grid/styles/tile-grid-shared-styles.ts +129 -129
  43. package/src/tiles/list/tile-list.ts +688 -688
  44. package/src/tiles/tile-dispatcher.ts +486 -486
  45. package/test/sort-filter-bar/sort-filter-bar.test.ts +692 -692
  46. package/tsconfig.json +20 -20
  47. package/web-dev-server.config.mjs +30 -30
  48. package/web-test-runner.config.mjs +41 -41
@@ -1,966 +1,990 @@
1
- import {
2
- css,
3
- html,
4
- LitElement,
5
- PropertyValues,
6
- nothing,
7
- TemplateResult,
8
- } from 'lit';
9
- import { customElement, property, state } from 'lit/decorators.js';
10
- import { map } from 'lit/directives/map.js';
11
- import { ref } from 'lit/directives/ref.js';
12
- import { msg } from '@lit/localize';
13
- import { classMap } from 'lit/directives/class-map.js';
14
- import {
15
- Aggregation,
16
- AggregationSortType,
17
- Bucket,
18
- FilterMap,
19
- SearchServiceInterface,
20
- SearchType,
21
- } from '@internetarchive/search-service';
22
- import '@internetarchive/histogram-date-range';
23
- import '@internetarchive/feature-feedback';
24
- import {
25
- ModalConfig,
26
- ModalManagerInterface,
27
- } from '@internetarchive/modal-manager';
28
- import type { FeatureFeedbackServiceInterface } from '@internetarchive/feature-feedback';
29
- import type { RecaptchaManagerInterface } from '@internetarchive/recaptcha-manager';
30
- import type { AnalyticsManagerInterface } from '@internetarchive/analytics-manager';
31
- import type { SharedResizeObserverInterface } from '@internetarchive/shared-resize-observer';
32
- import type { BinSnappingInterval } from '@internetarchive/histogram-date-range';
33
- import chevronIcon from './assets/img/icons/chevron';
34
- import expandIcon from './assets/img/icons/expand';
35
- import {
36
- FacetOption,
37
- SelectedFacets,
38
- FacetGroup,
39
- FacetBucket,
40
- defaultFacetDisplayOrder,
41
- facetTitles,
42
- lendingFacetDisplayNames,
43
- lendingFacetKeysVisibility,
44
- LendingFacetKey,
45
- suppressedCollections,
46
- defaultFacetSort,
47
- FacetEventDetails,
48
- } from './models';
49
- import type {
50
- CollectionTitles,
51
- PageSpecifierParams,
52
- TVChannelAliases,
53
- } from './data-source/models';
54
- import {
55
- analyticsActions,
56
- analyticsCategories,
57
- } from './utils/analytics-events';
58
- import { srOnlyStyle } from './styles/sr-only';
59
- import { ExpandedDatePicker } from './expanded-date-picker';
60
- import {
61
- sortBucketsBySelectionState,
62
- updateSelectedFacetBucket,
63
- } from './utils/facet-utils';
64
-
65
- import '@internetarchive/histogram-date-range';
66
- import './collection-facets/more-facets-content';
67
- import './collection-facets/facets-template';
68
- import './collection-facets/facet-tombstone-row';
69
- import './expanded-date-picker';
70
-
71
- @customElement('collection-facets')
72
- export class CollectionFacets extends LitElement {
73
- @property({ type: Object }) searchService?: SearchServiceInterface;
74
-
75
- @property({ type: Number }) searchType?: SearchType;
76
-
77
- @property({ type: Object }) aggregations?: Record<string, Aggregation>;
78
-
79
- @property({ type: Object }) histogramAggregation?: Aggregation;
80
-
81
- @property({ type: String }) minSelectedDate?: string;
82
-
83
- @property({ type: String }) maxSelectedDate?: string;
84
-
85
- @property({ type: Boolean }) moreLinksVisible = true;
86
-
87
- @property({ type: Boolean }) facetsLoading = false;
88
-
89
- @property({ type: Boolean }) histogramAggregationLoading = false;
90
-
91
- @property({ type: Object }) selectedFacets?: SelectedFacets;
92
-
93
- @property({ type: Boolean }) collapsableFacets = false;
94
-
95
- @property({ type: Number }) contentWidth?: number;
96
-
97
- @property({ type: Boolean }) showHistogramDatePicker = false;
98
-
99
- @property({ type: Boolean }) allowExpandingDatePicker = false;
100
-
101
- @property({ type: Boolean }) suppressMediatypeFacets = false;
102
-
103
- @property({ type: String }) query?: string;
104
-
105
- @property({ type: Object }) pageSpecifierParams?: PageSpecifierParams;
106
-
107
- @property({ type: Array }) parentCollections: string[] = [];
108
-
109
- @property({ type: Object }) filterMap?: FilterMap;
110
-
111
- @property({ type: String }) baseNavigationUrl?: string;
112
-
113
- @property({ type: String }) collectionPagePath: string = '/details/';
114
-
115
- @property({ type: Boolean }) isManageView = false;
116
-
117
- @property({ type: Boolean }) isTvSearch = false;
118
-
119
- @property({ type: Array }) facetDisplayOrder: FacetOption[] =
120
- defaultFacetDisplayOrder;
121
-
122
- @property({ type: Object, attribute: false })
123
- modalManager?: ModalManagerInterface;
124
-
125
- @property({ type: Object, attribute: false })
126
- resizeObserver?: SharedResizeObserverInterface;
127
-
128
- @property({ type: Object, attribute: false })
129
- featureFeedbackService?: FeatureFeedbackServiceInterface;
130
-
131
- @property({ type: Object, attribute: false })
132
- recaptchaManager?: RecaptchaManagerInterface;
133
-
134
- @property({ type: Object, attribute: false })
135
- analyticsHandler?: AnalyticsManagerInterface;
136
-
137
- @property({ type: Object, attribute: false })
138
- collectionTitles?: CollectionTitles;
139
-
140
- @property({ type: Object, attribute: false })
141
- tvChannelAliases?: TVChannelAliases;
142
-
143
- @state() openFacets: Record<FacetOption, boolean> = {
144
- subject: false,
145
- lending: false,
146
- mediatype: false,
147
- language: false,
148
- creator: false,
149
- collection: false,
150
- year: false,
151
- program: false,
152
- person: false,
153
- sponsor: false,
154
- };
155
-
156
- /**
157
- * Maximum # of facet buckets to render per facet group
158
- */
159
- private allowedFacetCount = 6;
160
-
161
- render() {
162
- const containerClasses = classMap({
163
- loading: this.facetsLoading,
164
- managing: this.isManageView,
165
- });
166
-
167
- // Added data-testid for Playwright testing
168
- // Using facet-group class and aria-labels is not ideal for Playwright locator
169
- const datePickerLabelId = 'date-picker-label';
170
- return html`
171
- <div id="container" class=${containerClasses}>
172
- ${this.showHistogramDatePicker &&
173
- (this.histogramAggregation || this.histogramAggregationLoading)
174
- ? html`
175
- <section
176
- class="facet-group"
177
- aria-labelledby=${datePickerLabelId}
178
- data-testid="facet-group-header-label-date-picker"
179
- >
180
- <h3 id=${datePickerLabelId}>
181
- Year Published <span class="sr-only">range filter</span>
182
- ${this.expandDatePickerBtnTemplate}
183
- </h3>
184
- ${this.histogramTemplate}
185
- </section>
186
- `
187
- : nothing}
188
- ${this.collectionPartOfTemplate}
189
- ${this.mergedFacets.map(facetGroup =>
190
- this.getFacetGroupTemplate(facetGroup),
191
- )}
192
- </div>
193
- `;
194
- }
195
-
196
- private get collectionPartOfTemplate(): TemplateResult | typeof nothing {
197
- // We only display the "Part Of" section on collection pages
198
- if (!this.parentCollections?.length) return nothing;
199
-
200
- // Added data-testid for Playwright testing
201
- // Using className and aria-labels is not ideal for Playwright locator
202
- const headingId = 'partof-heading';
203
- return html`
204
- <section
205
- class="facet-group partof-collections"
206
- aria-labelledby=${headingId}
207
- data-testid="facet-group-partof-collections"
208
- >
209
- <div class="facet-group-header">
210
- <h3 id=${headingId}>${msg('Part Of')}</h3>
211
- </div>
212
- <ul>
213
- ${map(this.parentCollections, collxn => {
214
- const collectionURL = `${this.baseNavigationUrl}${this.collectionPagePath}${collxn}`;
215
-
216
- return html` <li>
217
- <a
218
- href=${collectionURL}
219
- data-id=${collxn}
220
- @click=${this.partOfCollectionClicked}
221
- >
222
- ${this.collectionTitles?.get(collxn) ?? collxn}
223
- </a>
224
- </li>`;
225
- })}
226
- </ul>
227
- </section>
228
- `;
229
- }
230
-
231
- private partOfCollectionClicked(e: Event): void {
232
- this.analyticsHandler?.sendEvent({
233
- category: analyticsCategories.default,
234
- action: analyticsActions.partOfCollectionClicked,
235
- label: (e.target as HTMLElement).dataset.id,
236
- });
237
- }
238
-
239
- /**
240
- * Properties to pass into the date-picker histogram component
241
- */
242
- private get histogramProps() {
243
- const { histogramAggregation: aggregation } = this;
244
- if (!aggregation) return undefined;
245
-
246
- // Normalize some properties from the raw aggregation
247
- const firstYear =
248
- aggregation.first_bucket_year ?? aggregation.first_bucket_key;
249
- const lastYear =
250
- aggregation.last_bucket_year ?? aggregation.last_bucket_key;
251
- if (firstYear == null || lastYear == null) return undefined; // We at least need a start/end year defined
252
-
253
- const firstMonth = aggregation.first_bucket_month ?? 1;
254
- const lastMonth = aggregation.last_bucket_month ?? 12;
255
-
256
- const yearInterval = aggregation.interval ?? 1;
257
- const monthInterval = aggregation.interval_in_months ?? 12;
258
-
259
- const zeroPadMonth = (month: number) => month.toString().padStart(2, '0');
260
-
261
- // Format the min/max dates appropriately
262
- const minDate = this.isTvSearch
263
- ? `${firstYear}-${zeroPadMonth(firstMonth)}`
264
- : `${firstYear}`;
265
-
266
- const maxDate = this.isTvSearch
267
- ? `${lastYear}-${zeroPadMonth(lastMonth + monthInterval - 1)}`
268
- : `${lastYear + yearInterval - 1}`;
269
-
270
- const hasMonths = this.isTvSearch && monthInterval < 12;
271
- return {
272
- buckets: aggregation.buckets as number[],
273
- dateFormat: this.isTvSearch ? 'YYYY-MM' : 'YYYY',
274
- tooltipDateFormat: hasMonths ? 'MMM YYYY' : 'YYYY',
275
- binSnapping: (hasMonths ? 'month' : 'year') as BinSnappingInterval,
276
- minDate,
277
- maxDate,
278
- };
279
- }
280
-
281
- /**
282
- * Opens a modal dialog containing an enlarged version of the date picker.
283
- */
284
- private showDatePickerModal(): void {
285
- const { histogramProps } = this;
286
- if (!histogramProps) return;
287
-
288
- const {
289
- buckets,
290
- dateFormat,
291
- tooltipDateFormat,
292
- binSnapping,
293
- minDate,
294
- maxDate,
295
- } = histogramProps;
296
-
297
- // Because the modal manager does not clear its DOM content after being closed,
298
- // it may try to render the exact same date picker template when it is reopened.
299
- // And because it isn't actually a descendent of this collection-facets component,
300
- // changes to the template defined here may not trigger a reactive update to the date
301
- // picker, resulting in it displaying a stale date range.
302
- // This ref callback ensures that every time the date picker modal is opened, it will
303
- // always propagate the most recent date range into the date picker regardless of
304
- // whether Lit thinks the update is necessary.
305
- const expandedDatePickerChanged = (elmt?: Element) => {
306
- if (elmt && elmt instanceof ExpandedDatePicker) {
307
- const expandedDatePicker = elmt as ExpandedDatePicker;
308
- expandedDatePicker.minSelectedDate = this.minSelectedDate;
309
- expandedDatePicker.maxSelectedDate = this.maxSelectedDate;
310
- }
311
- };
312
-
313
- const customModalContent = html`
314
- <expanded-date-picker
315
- ${ref(expandedDatePickerChanged)}
316
- .minDate=${minDate}
317
- .maxDate=${maxDate}
318
- .minSelectedDate=${this.minSelectedDate}
319
- .maxSelectedDate=${this.maxSelectedDate}
320
- .dateFormat=${dateFormat}
321
- .tooltipDateFormat=${tooltipDateFormat}
322
- .binSnapping=${binSnapping}
323
- .buckets=${buckets}
324
- .modalManager=${this.modalManager}
325
- .analyticsHandler=${this.analyticsHandler}
326
- @histogramDateRangeApplied=${this.histogramDateRangeUpdated}
327
- @modalClosed=${this.handleExpandedDatePickerClosed}
328
- ></expanded-date-picker>
329
- `;
330
-
331
- const config = new ModalConfig({
332
- bodyColor: '#fff',
333
- headerColor: '#194880',
334
- showHeaderLogo: false,
335
- closeOnBackdropClick: true, // TODO: want to fire analytics
336
- title: html`${msg('Select a date range')}`,
337
- });
338
-
339
- this.modalManager?.classList.add('expanded-date-picker');
340
- this.modalManager?.showModal({
341
- config,
342
- customModalContent,
343
- userClosedModalCallback: this.handleExpandedDatePickerClosed,
344
- });
345
-
346
- this.analyticsHandler?.sendEvent({
347
- category: analyticsCategories.default,
348
- action: analyticsActions.histogramExpanded,
349
- label: window.location.href,
350
- });
351
- }
352
-
353
- private handleExpandedDatePickerClosed = (): void => {
354
- this.modalManager?.classList.remove('expanded-date-picker');
355
- };
356
-
357
- updated(changed: PropertyValues) {
358
- if (changed.has('selectedFacets')) {
359
- this.dispatchFacetsChangedEvent();
360
- }
361
- }
362
-
363
- // TODO: want to fire analytics?
364
- private dispatchFacetsChangedEvent() {
365
- const event = new CustomEvent<SelectedFacets>('facetsChanged', {
366
- detail: this.selectedFacets,
367
- });
368
- this.dispatchEvent(event);
369
- }
370
-
371
- /**
372
- * Template for the "Expand" button to show the date picker modal, or
373
- * `nothing` if that button should currently not be shown.
374
- */
375
- private get expandDatePickerBtnTemplate(): TemplateResult | typeof nothing {
376
- return this.allowExpandingDatePicker && !this.facetsLoading
377
- ? html`<button
378
- class="expand-date-picker-btn"
379
- aria-hidden="true"
380
- @click=${this.showDatePickerModal}
381
- >
382
- ${expandIcon}
383
- </button>`
384
- : nothing;
385
- }
386
-
387
- private get histogramTemplate(): TemplateResult | typeof nothing {
388
- if (this.histogramAggregationLoading) {
389
- return html` <div class="histogram-loading-indicator">&hellip;</div> `;
390
- }
391
-
392
- const { histogramProps } = this;
393
- if (!histogramProps) return nothing;
394
-
395
- const {
396
- buckets,
397
- dateFormat,
398
- tooltipDateFormat,
399
- binSnapping,
400
- minDate,
401
- maxDate,
402
- } = histogramProps;
403
-
404
- return html`
405
- <histogram-date-range
406
- class=${this.isTvSearch ? 'wide-inputs' : nothing}
407
- .minDate=${minDate}
408
- .maxDate=${maxDate}
409
- .minSelectedDate=${this.minSelectedDate ?? minDate}
410
- .maxSelectedDate=${this.maxSelectedDate ?? maxDate}
411
- .updateDelay=${100}
412
- .dateFormat=${dateFormat}
413
- .tooltipDateFormat=${tooltipDateFormat}
414
- .binSnapping=${binSnapping}
415
- .bins=${buckets}
416
- missingDataMessage="..."
417
- .width=${this.collapsableFacets && this.contentWidth
418
- ? this.contentWidth
419
- : 180}
420
- @histogramDateRangeUpdated=${this.histogramDateRangeUpdated}
421
- ></histogram-date-range>
422
- `;
423
- }
424
-
425
- /**
426
- * Dispatches a `histogramDateRangeUpdated` event with the date range copied from the
427
- * input event.
428
- *
429
- * Arrow function to ensure `this` is always bound to the current component.
430
- */
431
- private histogramDateRangeUpdated = (
432
- e: CustomEvent<{
433
- minDate: string;
434
- maxDate: string;
435
- }>,
436
- ): void => {
437
- const { minDate, maxDate } = e.detail;
438
- const event = new CustomEvent('histogramDateRangeUpdated', {
439
- detail: { minDate, maxDate },
440
- });
441
- this.dispatchEvent(event);
442
- };
443
-
444
- /**
445
- * Combines the selected facets with the aggregations to create a single list of facets
446
- */
447
- private get mergedFacets(): FacetGroup[] {
448
- const facetGroups: FacetGroup[] = [];
449
-
450
- this.facetDisplayOrder.forEach(facetKey => {
451
- if (facetKey === 'mediatype' && this.suppressMediatypeFacets) return;
452
-
453
- const selectedFacetGroup = this.selectedFacetGroups.find(
454
- group => group.key === facetKey,
455
- );
456
- const aggregateFacetGroup = this.aggregationFacetGroups.find(
457
- group => group.key === facetKey,
458
- );
459
-
460
- // if the user selected a facet, but it's not in the aggregation, we add it as-is
461
- if (selectedFacetGroup && !aggregateFacetGroup) {
462
- facetGroups.push(selectedFacetGroup);
463
- return;
464
- }
465
-
466
- // if we don't have an aggregate facet group, don't add this to the list
467
- if (!aggregateFacetGroup) return;
468
-
469
- // start with either the selected group if we have one, or the aggregate group
470
- const facetGroup = selectedFacetGroup ?? aggregateFacetGroup;
471
-
472
- // attach the counts to the selected buckets
473
- let bucketsWithCount =
474
- selectedFacetGroup?.buckets.map(bucket => {
475
- const selectedBucket = aggregateFacetGroup.buckets.find(
476
- b => b.key === bucket.key,
477
- );
478
- return selectedBucket
479
- ? {
480
- ...bucket,
481
- count: selectedBucket.count,
482
- }
483
- : bucket;
484
- }) ?? [];
485
-
486
- // append any additional buckets that were not selected
487
- aggregateFacetGroup.buckets.forEach(bucket => {
488
- const existingBucket = bucketsWithCount.find(b => b.key === bucket.key);
489
- if (existingBucket) return;
490
- bucketsWithCount.push(bucket);
491
- });
492
-
493
- /**
494
- * render limited facet items on page facet area
495
- *
496
- * - by-default we are showing 6 items
497
- * - additionally want to show all items (selected/suppressed) in page facet area
498
- */
499
- let allowedFacetCount = Object.keys(
500
- (selectedFacetGroup?.buckets as []) || [],
501
- )?.length;
502
- if (allowedFacetCount < this.allowedFacetCount) {
503
- allowedFacetCount = this.allowedFacetCount; // splice start index from 0th
504
- }
505
-
506
- // For lending facets, only include a specific subset of buckets
507
- if (facetKey === 'lending') {
508
- bucketsWithCount = bucketsWithCount.filter(
509
- bucket => lendingFacetKeysVisibility[bucket.key as LendingFacetKey],
510
- );
511
- }
512
-
513
- // Sort the FacetBuckets so that selected and hidden buckets come before the rest
514
- sortBucketsBySelectionState(bucketsWithCount, defaultFacetSort[facetKey]);
515
-
516
- // For mediatype facets, ensure the collection bucket is always shown if present
517
- if (facetKey === 'mediatype') {
518
- const collectionIndex = bucketsWithCount.findIndex(
519
- bucket => bucket.key === 'collection',
520
- );
521
-
522
- if (collectionIndex >= allowedFacetCount) {
523
- const [collectionBucket] = bucketsWithCount.splice(
524
- collectionIndex,
525
- 1,
526
- );
527
-
528
- // If we're showing lots of selected facets, ensure we're not cutting off the last one
529
- if (allowedFacetCount > this.allowedFacetCount) {
530
- allowedFacetCount += 1;
531
- }
532
-
533
- bucketsWithCount.splice(allowedFacetCount - 1, 0, collectionBucket);
534
- }
535
- }
536
-
537
- // For TV creator facets, uppercase the display text
538
- if (facetKey === 'creator' && this.isTvSearch) {
539
- bucketsWithCount.forEach(b => {
540
- b.displayText = (b.displayText ?? b.key)?.toLocaleUpperCase();
541
-
542
- const channelLabel = this.tvChannelAliases?.get(b.displayText);
543
- if (channelLabel && channelLabel !== b.displayText) {
544
- b.extraNote = `(${channelLabel})`;
545
- }
546
- });
547
- }
548
-
549
- // slice off how many items we want to show in page facet area
550
- facetGroup.buckets = bucketsWithCount.slice(0, allowedFacetCount);
551
-
552
- facetGroups.push(facetGroup);
553
- });
554
-
555
- return facetGroups;
556
- }
557
-
558
- /**
559
- * Converts the selected facets to a `FacetGroup` array,
560
- * which is easier to work with
561
- */
562
- private get selectedFacetGroups(): FacetGroup[] {
563
- if (!this.selectedFacets) return [];
564
-
565
- const facetGroups: FacetGroup[] = Object.entries(this.selectedFacets).map(
566
- ([key, selectedFacets]) => {
567
- const option = key as FacetOption;
568
- const title = facetTitles[option];
569
-
570
- const buckets: FacetBucket[] = Object.entries(selectedFacets).map(
571
- ([value, facetData]) => {
572
- let displayText: string = value;
573
- // for lending facets, convert the key to a readable format
574
- if (option === 'lending') {
575
- displayText =
576
- lendingFacetDisplayNames[value as LendingFacetKey] ?? value;
577
- }
578
- return {
579
- displayText,
580
- key: value,
581
- count: facetData.count,
582
- state: facetData.state,
583
- };
584
- },
585
- );
586
-
587
- return {
588
- title,
589
- key: option,
590
- buckets,
591
- };
592
- },
593
- );
594
-
595
- return facetGroups;
596
- }
597
-
598
- /**
599
- * Converts the raw `aggregations` to `FacetGroups`, which are easier to use
600
- */
601
- private get aggregationFacetGroups(): FacetGroup[] {
602
- const facetGroups: FacetGroup[] = [];
603
- Object.entries(this.aggregations ?? []).forEach(([key, aggregation]) => {
604
- // the year_histogram and date_histogram data is in a different format so can't be handled here
605
- if (['year_histogram', 'date_histogram'].includes(key)) return;
606
-
607
- const option = key as FacetOption;
608
- const title = facetTitles[option];
609
- if (!title) return;
610
-
611
- let castedBuckets = aggregation.getSortedBuckets(
612
- defaultFacetSort[option],
613
- ) as Bucket[];
614
-
615
- if (option === 'collection') {
616
- // we are not showing fav- collections or certain deemphasized collections in facets
617
- castedBuckets = castedBuckets?.filter(bucket => {
618
- const bucketKey = bucket?.key?.toString();
619
- return (
620
- !suppressedCollections[bucketKey] && !bucketKey?.startsWith('fav-')
621
- );
622
- });
623
- }
624
-
625
- const facetBuckets: FacetBucket[] = castedBuckets.map(bucket => {
626
- const bucketKey = bucket.key;
627
- let displayText = `${bucket.key}`;
628
- // for lending facets, convert the bucket key to a readable format
629
- if (option === 'lending') {
630
- displayText =
631
- lendingFacetDisplayNames[bucket.key as LendingFacetKey] ??
632
- `${bucket.key}`;
633
- }
634
- return {
635
- displayText,
636
- key: `${bucketKey}`,
637
- count: bucket.doc_count,
638
- state: 'none',
639
- };
640
- });
641
- const group: FacetGroup = {
642
- title,
643
- key: option,
644
- buckets: facetBuckets,
645
- };
646
- facetGroups.push(group);
647
- });
648
- return facetGroups;
649
- }
650
-
651
- /**
652
- * Generate the template for a facet group with a header and the collapsible
653
- * chevron for the mobile view
654
- */
655
- private getFacetGroupTemplate(
656
- facetGroup: FacetGroup,
657
- ): TemplateResult | typeof nothing {
658
- if (!this.facetsLoading && facetGroup.buckets.length === 0) return nothing;
659
-
660
- const { key } = facetGroup;
661
- const isOpen = this.openFacets[key];
662
- const collapser = html`
663
- <span class="collapser ${isOpen ? 'open' : ''}"> ${chevronIcon} </span>
664
- `;
665
-
666
- const toggleCollapsed = () => {
667
- const newOpenFacets = { ...this.openFacets };
668
- newOpenFacets[key] = !isOpen;
669
- this.openFacets = newOpenFacets;
670
- };
671
-
672
- // Added data-testid for Playwright testing
673
- // Using className and aria-labels is not ideal for Playwright locator
674
- const headerId = `facet-group-header-label-${facetGroup.key}`;
675
- return html`
676
- <section
677
- class="facet-group ${this.collapsableFacets ? 'mobile' : ''}"
678
- aria-labelledby=${headerId}
679
- data-testid=${headerId}
680
- >
681
- <div class="facet-group-header">
682
- <h3
683
- id=${headerId}
684
- @click=${toggleCollapsed}
685
- @keyup=${toggleCollapsed}
686
- >
687
- ${this.collapsableFacets ? collapser : nothing} ${facetGroup.title}
688
- <span class="sr-only">filters</span>
689
- </h3>
690
- </div>
691
- <div
692
- class="facet-group-content ${isOpen ? 'open' : ''}"
693
- data-testid="facet-group-content-${facetGroup.key}"
694
- >
695
- ${this.facetsLoading
696
- ? this.getTombstoneFacetGroupTemplate()
697
- : html`
698
- ${this.getFacetTemplate(facetGroup)}
699
- ${this.searchMoreFacetsLink(facetGroup)}
700
- `}
701
- </div>
702
- </section>
703
- `;
704
- }
705
-
706
- private getTombstoneFacetGroupTemplate(): TemplateResult {
707
- // Render five tombstone rows
708
- return html`
709
- ${map(
710
- Array(5).fill(null),
711
- () => html`<facet-tombstone-row></facet-tombstone-row>`,
712
- )}
713
- `;
714
- }
715
-
716
- /**
717
- * Generate the More... link button just below the facets group
718
- *
719
- * TODO: want to fire analytics?
720
- */
721
- private searchMoreFacetsLink(
722
- facetGroup: FacetGroup,
723
- ): TemplateResult | typeof nothing {
724
- // Don't render More... links for FTS searches
725
- if (!this.moreLinksVisible) {
726
- return nothing;
727
- }
728
-
729
- // Don't render More... links for lending facets
730
- if (facetGroup.key === 'lending') {
731
- return nothing;
732
- }
733
-
734
- // Don't render More... link if the number of facets < this.allowedFacetCount
735
- if (Object.keys(facetGroup.buckets).length < this.allowedFacetCount) {
736
- return nothing;
737
- }
738
-
739
- // We sort years in numeric order by default, rather than bucket count
740
- const facetSort = defaultFacetSort[facetGroup.key];
741
-
742
- // Added data-testid for Playwright testing
743
- // Using the className is not ideal for Playwright locator
744
- return html`<button
745
- class="more-link"
746
- @click=${() => {
747
- this.showMoreFacetsModal(facetGroup, facetSort);
748
- this.analyticsHandler?.sendEvent({
749
- category: analyticsCategories.default,
750
- action: analyticsActions.showMoreFacetsModal,
751
- label: facetGroup.key,
752
- });
753
- this.dispatchEvent(
754
- new CustomEvent('showMoreFacets', { detail: facetGroup.key }),
755
- );
756
- }}
757
- data-testid="more-link-btn"
758
- >
759
- More...
760
- </button>`;
761
- }
762
-
763
- async showMoreFacetsModal(
764
- facetGroup: FacetGroup,
765
- sortedBy: AggregationSortType,
766
- ): Promise<void> {
767
- const customModalContent = html`
768
- <more-facets-content
769
- .analyticsHandler=${this.analyticsHandler}
770
- .facetKey=${facetGroup.key}
771
- .query=${this.query}
772
- .filterMap=${this.filterMap}
773
- .pageSpecifierParams=${this.pageSpecifierParams}
774
- .modalManager=${this.modalManager}
775
- .searchService=${this.searchService}
776
- .searchType=${this.searchType}
777
- .collectionTitles=${this.collectionTitles}
778
- .tvChannelAliases=${this.tvChannelAliases}
779
- .selectedFacets=${this.selectedFacets}
780
- .sortedBy=${sortedBy}
781
- .isTvSearch=${this.isTvSearch}
782
- @facetsChanged=${(e: CustomEvent) => {
783
- const event = new CustomEvent<SelectedFacets>('facetsChanged', {
784
- detail: e.detail,
785
- bubbles: true,
786
- composed: true,
787
- });
788
- this.dispatchEvent(event);
789
- }}
790
- >
791
- </more-facets-content>
792
- `;
793
-
794
- const config = new ModalConfig({
795
- bodyColor: '#fff',
796
- headerColor: '#194880',
797
- showHeaderLogo: false,
798
- closeOnBackdropClick: true, // TODO: want to fire analytics
799
- title: html`Select filters`,
800
- });
801
- this.modalManager?.classList.add('more-search-facets');
802
- this.modalManager?.showModal({
803
- config,
804
- customModalContent,
805
- userClosedModalCallback: () => {
806
- this.modalManager?.classList.remove('more-search-facets');
807
- },
808
- });
809
- }
810
-
811
- /**
812
- * Generate the list template for each bucket in a facet group
813
- */
814
- private getFacetTemplate(facetGroup: FacetGroup): TemplateResult {
815
- return html`
816
- <facets-template
817
- .collectionPagePath=${this.collectionPagePath}
818
- .facetGroup=${facetGroup}
819
- .selectedFacets=${this.selectedFacets}
820
- .collectionTitles=${this.collectionTitles}
821
- @facetClick=${(e: CustomEvent<FacetEventDetails>) => {
822
- this.selectedFacets = updateSelectedFacetBucket(
823
- this.selectedFacets,
824
- facetGroup.key,
825
- e.detail.bucket,
826
- true,
827
- );
828
-
829
- const event = new CustomEvent<SelectedFacets>('facetsChanged', {
830
- detail: this.selectedFacets,
831
- bubbles: true,
832
- composed: true,
833
- });
834
- this.dispatchEvent(event);
835
- }}
836
- ></facets-template>
837
- `;
838
- }
839
-
840
- static get styles() {
841
- return [
842
- srOnlyStyle,
843
- css`
844
- a:link {
845
- text-decoration: none;
846
- color: var(--ia-theme-link-color, #4b64ff);
847
- }
848
- a:link:hover {
849
- text-decoration: underline;
850
- }
851
-
852
- #container.loading {
853
- opacity: 0.5;
854
- }
855
-
856
- #container.managing {
857
- opacity: 0.3;
858
- }
859
-
860
- .histogram-loading-indicator {
861
- width: 100%;
862
- height: 2.25rem;
863
- margin-top: 1.75rem;
864
- font-size: 1.4rem;
865
- text-align: center;
866
- }
867
-
868
- .collapser {
869
- display: inline-block;
870
- cursor: pointer;
871
- width: 10px;
872
- height: 10px;
873
- }
874
-
875
- .collapser svg {
876
- transition: transform 0.2s ease-in-out;
877
- }
878
-
879
- .collapser.open svg {
880
- transform: rotate(90deg);
881
- }
882
-
883
- .facet-group:not(:last-child) {
884
- margin-bottom: 2rem;
885
- }
886
-
887
- .facet-group h3 {
888
- margin-bottom: 0.7rem;
889
- }
890
-
891
- .facet-group.mobile h3 {
892
- cursor: pointer;
893
- }
894
-
895
- .facet-group-header {
896
- display: flex;
897
- margin-bottom: 0.7rem;
898
- justify-content: space-between;
899
- border-bottom: 1px solid rgb(232, 232, 232);
900
- }
901
-
902
- .facet-group-content {
903
- transition: max-height 0.2s ease-in-out;
904
- }
905
-
906
- .facet-group.mobile .facet-group-content {
907
- max-height: 0;
908
- overflow: hidden;
909
- }
910
-
911
- .facet-group.mobile .facet-group-content.open {
912
- max-height: 2000px;
913
- }
914
-
915
- .partof-collections ul {
916
- list-style-type: none;
917
- padding: 0;
918
- font-size: 1.2rem;
919
- }
920
-
921
- h3 {
922
- font-size: 1.4rem;
923
- margin: 0;
924
- }
925
-
926
- .more-link {
927
- font-size: 1.2rem;
928
- text-decoration: none;
929
- padding: 0;
930
- background: inherit;
931
- border: 0;
932
- color: var(--ia-theme-link-color, #4b64ff);
933
- cursor: pointer;
934
- }
935
-
936
- #date-picker-label {
937
- display: flex;
938
- justify-content: space-between;
939
- }
940
-
941
- .expand-date-picker-btn {
942
- margin: 0;
943
- padding: 0;
944
- border: 0;
945
- appearance: none;
946
- background: none;
947
- cursor: pointer;
948
- }
949
-
950
- .expand-date-picker-btn > svg {
951
- width: 14px;
952
- height: 14px;
953
- }
954
-
955
- .sorting-icon {
956
- height: 15px;
957
- cursor: pointer;
958
- }
959
-
960
- histogram-date-range.wide-inputs {
961
- --histogramDateRangeInputWidth: 4.8rem;
962
- }
963
- `,
964
- ];
965
- }
966
- }
1
+ import {
2
+ css,
3
+ html,
4
+ LitElement,
5
+ PropertyValues,
6
+ nothing,
7
+ TemplateResult,
8
+ } from 'lit';
9
+ import { customElement, property, state } from 'lit/decorators.js';
10
+ import { map } from 'lit/directives/map.js';
11
+ import { ref } from 'lit/directives/ref.js';
12
+ import { msg } from '@lit/localize';
13
+ import { classMap } from 'lit/directives/class-map.js';
14
+ import {
15
+ Aggregation,
16
+ AggregationSortType,
17
+ Bucket,
18
+ FilterMap,
19
+ SearchServiceInterface,
20
+ SearchType,
21
+ } from '@internetarchive/search-service';
22
+ import '@internetarchive/histogram-date-range';
23
+ import '@internetarchive/feature-feedback';
24
+ import {
25
+ ModalConfig,
26
+ ModalManagerInterface,
27
+ } from '@internetarchive/modal-manager';
28
+ import type { FeatureFeedbackServiceInterface } from '@internetarchive/feature-feedback';
29
+ import type { RecaptchaManagerInterface } from '@internetarchive/recaptcha-manager';
30
+ import type { AnalyticsManagerInterface } from '@internetarchive/analytics-manager';
31
+ import type { SharedResizeObserverInterface } from '@internetarchive/shared-resize-observer';
32
+ import type {
33
+ BarScalingOption,
34
+ BinSnappingInterval,
35
+ } from '@internetarchive/histogram-date-range';
36
+ import chevronIcon from './assets/img/icons/chevron';
37
+ import expandIcon from './assets/img/icons/expand';
38
+ import {
39
+ FacetOption,
40
+ SelectedFacets,
41
+ FacetGroup,
42
+ FacetBucket,
43
+ defaultFacetDisplayOrder,
44
+ facetTitles,
45
+ lendingFacetDisplayNames,
46
+ lendingFacetKeysVisibility,
47
+ LendingFacetKey,
48
+ suppressedCollections,
49
+ defaultFacetSort,
50
+ FacetEventDetails,
51
+ } from './models';
52
+ import type {
53
+ CollectionTitles,
54
+ PageSpecifierParams,
55
+ TVChannelAliases,
56
+ } from './data-source/models';
57
+ import {
58
+ analyticsActions,
59
+ analyticsCategories,
60
+ } from './utils/analytics-events';
61
+ import { srOnlyStyle } from './styles/sr-only';
62
+ import { ExpandedDatePicker } from './expanded-date-picker';
63
+ import {
64
+ sortBucketsBySelectionState,
65
+ updateSelectedFacetBucket,
66
+ } from './utils/facet-utils';
67
+
68
+ import '@internetarchive/histogram-date-range';
69
+ import './collection-facets/more-facets-content';
70
+ import './collection-facets/facets-template';
71
+ import './collection-facets/facet-tombstone-row';
72
+ import './expanded-date-picker';
73
+
74
+ @customElement('collection-facets')
75
+ export class CollectionFacets extends LitElement {
76
+ @property({ type: Object }) searchService?: SearchServiceInterface;
77
+
78
+ @property({ type: Number }) searchType?: SearchType;
79
+
80
+ @property({ type: Object }) aggregations?: Record<string, Aggregation>;
81
+
82
+ @property({ type: Object }) histogramAggregation?: Aggregation;
83
+
84
+ @property({ type: String }) minSelectedDate?: string;
85
+
86
+ @property({ type: String }) maxSelectedDate?: string;
87
+
88
+ @property({ type: Boolean }) moreLinksVisible = true;
89
+
90
+ @property({ type: Boolean }) facetsLoading = false;
91
+
92
+ @property({ type: Boolean }) histogramAggregationLoading = false;
93
+
94
+ @property({ type: Object }) selectedFacets?: SelectedFacets;
95
+
96
+ @property({ type: Boolean }) collapsableFacets = false;
97
+
98
+ @property({ type: Number }) contentWidth?: number;
99
+
100
+ @property({ type: Boolean }) showHistogramDatePicker = false;
101
+
102
+ @property({ type: Boolean }) allowExpandingDatePicker = false;
103
+
104
+ @property({ type: Boolean }) suppressMediatypeFacets = false;
105
+
106
+ @property({ type: String }) query?: string;
107
+
108
+ @property({ type: Object }) pageSpecifierParams?: PageSpecifierParams;
109
+
110
+ @property({ type: Array }) parentCollections: string[] = [];
111
+
112
+ @property({ type: Object }) filterMap?: FilterMap;
113
+
114
+ @property({ type: String }) baseNavigationUrl?: string;
115
+
116
+ @property({ type: String }) collectionPagePath: string = '/details/';
117
+
118
+ @property({ type: Boolean }) isManageView = false;
119
+
120
+ @property({ type: Boolean }) isTvSearch = false;
121
+
122
+ @property({ type: Array }) facetDisplayOrder: FacetOption[] =
123
+ defaultFacetDisplayOrder;
124
+
125
+ @property({ type: Object, attribute: false })
126
+ modalManager?: ModalManagerInterface;
127
+
128
+ @property({ type: Object, attribute: false })
129
+ resizeObserver?: SharedResizeObserverInterface;
130
+
131
+ @property({ type: Object, attribute: false })
132
+ featureFeedbackService?: FeatureFeedbackServiceInterface;
133
+
134
+ @property({ type: Object, attribute: false })
135
+ recaptchaManager?: RecaptchaManagerInterface;
136
+
137
+ @property({ type: Object, attribute: false })
138
+ analyticsHandler?: AnalyticsManagerInterface;
139
+
140
+ @property({ type: Object, attribute: false })
141
+ collectionTitles?: CollectionTitles;
142
+
143
+ @property({ type: Object, attribute: false })
144
+ tvChannelAliases?: TVChannelAliases;
145
+
146
+ @state() openFacets: Record<FacetOption, boolean> = {
147
+ subject: false,
148
+ lending: false,
149
+ mediatype: false,
150
+ language: false,
151
+ creator: false,
152
+ collection: false,
153
+ year: false,
154
+ program: false,
155
+ person: false,
156
+ sponsor: false,
157
+ };
158
+
159
+ /**
160
+ * Maximum # of facet buckets to render per facet group
161
+ */
162
+ private allowedFacetCount = 6;
163
+
164
+ render() {
165
+ const containerClasses = classMap({
166
+ loading: this.facetsLoading,
167
+ managing: this.isManageView,
168
+ });
169
+
170
+ // Added data-testid for Playwright testing
171
+ // Using facet-group class and aria-labels is not ideal for Playwright locator
172
+ const datePickerLabelId = 'date-picker-label';
173
+ return html`
174
+ <div id="container" class=${containerClasses}>
175
+ ${this.showHistogramDatePicker &&
176
+ (this.histogramAggregation || this.histogramAggregationLoading)
177
+ ? html`
178
+ <section
179
+ class="facet-group"
180
+ aria-labelledby=${datePickerLabelId}
181
+ data-testid="facet-group-header-label-date-picker"
182
+ >
183
+ <h3 id=${datePickerLabelId}>
184
+ Year Published <span class="sr-only">range filter</span>
185
+ ${this.expandDatePickerBtnTemplate}
186
+ </h3>
187
+ ${this.histogramTemplate}
188
+ </section>
189
+ `
190
+ : nothing}
191
+ ${this.collectionPartOfTemplate}
192
+ ${this.mergedFacets.map(facetGroup =>
193
+ this.getFacetGroupTemplate(facetGroup),
194
+ )}
195
+ </div>
196
+ `;
197
+ }
198
+
199
+ private get collectionPartOfTemplate(): TemplateResult | typeof nothing {
200
+ // We only display the "Part Of" section on collection pages
201
+ if (!this.parentCollections?.length) return nothing;
202
+
203
+ // Added data-testid for Playwright testing
204
+ // Using className and aria-labels is not ideal for Playwright locator
205
+ const headingId = 'partof-heading';
206
+ return html`
207
+ <section
208
+ class="facet-group partof-collections"
209
+ aria-labelledby=${headingId}
210
+ data-testid="facet-group-partof-collections"
211
+ >
212
+ <div class="facet-group-header">
213
+ <h3 id=${headingId}>${msg('Part Of')}</h3>
214
+ </div>
215
+ <ul>
216
+ ${map(this.parentCollections, collxn => {
217
+ const collectionURL = `${this.baseNavigationUrl}${this.collectionPagePath}${collxn}`;
218
+
219
+ return html` <li>
220
+ <a
221
+ href=${collectionURL}
222
+ data-id=${collxn}
223
+ @click=${this.partOfCollectionClicked}
224
+ >
225
+ ${this.collectionTitles?.get(collxn) ?? collxn}
226
+ </a>
227
+ </li>`;
228
+ })}
229
+ </ul>
230
+ </section>
231
+ `;
232
+ }
233
+
234
+ private partOfCollectionClicked(e: Event): void {
235
+ this.analyticsHandler?.sendEvent({
236
+ category: analyticsCategories.default,
237
+ action: analyticsActions.partOfCollectionClicked,
238
+ label: (e.target as HTMLElement).dataset.id,
239
+ });
240
+ }
241
+
242
+ /**
243
+ * Properties to pass into the date-picker histogram component
244
+ */
245
+ private get histogramProps() {
246
+ const { histogramAggregation: aggregation } = this;
247
+ if (!aggregation) return undefined;
248
+
249
+ // Normalize some properties from the raw aggregation
250
+ const firstYear =
251
+ aggregation.first_bucket_year ?? aggregation.first_bucket_key;
252
+ const lastYear =
253
+ aggregation.last_bucket_year ?? aggregation.last_bucket_key;
254
+ if (firstYear == null || lastYear == null) return undefined; // We at least need a start/end year defined
255
+
256
+ const firstMonth = aggregation.first_bucket_month ?? 1;
257
+ const lastMonth = aggregation.last_bucket_month ?? 12;
258
+
259
+ const yearInterval = aggregation.interval ?? 1;
260
+ const monthInterval = aggregation.interval_in_months ?? 12;
261
+
262
+ const zeroPadMonth = (month: number) => month.toString().padStart(2, '0');
263
+
264
+ // The date picker is configured differently for TV search, allowing month-level resolution
265
+ if (this.isTvSearch) {
266
+ // Whether the bucket interval is less than a year
267
+ // (i.e., requires individual months to be handled & labeled)
268
+ const mustHandleMonths = monthInterval < 12;
269
+
270
+ return {
271
+ buckets: aggregation.buckets as number[],
272
+ dateFormat: 'YYYY-MM',
273
+ tooltipDateFormat: mustHandleMonths ? 'MMM YYYY' : 'YYYY',
274
+ tooltipLabel: 'broadcast',
275
+ binSnapping: (mustHandleMonths
276
+ ? 'month'
277
+ : 'year') as BinSnappingInterval,
278
+ barScaling: 'linear' as BarScalingOption,
279
+ minDate: `${firstYear}-${zeroPadMonth(firstMonth)}`,
280
+ maxDate: `${lastYear}-${zeroPadMonth(lastMonth + monthInterval - 1)}`,
281
+ };
282
+ }
283
+
284
+ // All other search types use the same configuration
285
+ return {
286
+ buckets: aggregation.buckets as number[],
287
+ dateFormat: 'YYYY',
288
+ tooltipDateFormat: 'YYYY',
289
+ tooltipLabel: 'item',
290
+ binSnapping: 'year' as BinSnappingInterval,
291
+ barScaling: 'logarithmic' as BarScalingOption,
292
+ minDate: `${firstYear}`,
293
+ maxDate: `${lastYear + yearInterval - 1}`,
294
+ };
295
+ }
296
+
297
+ /**
298
+ * Opens a modal dialog containing an enlarged version of the date picker.
299
+ */
300
+ private showDatePickerModal(): void {
301
+ const { histogramProps } = this;
302
+ if (!histogramProps) return;
303
+
304
+ const {
305
+ buckets,
306
+ dateFormat,
307
+ tooltipDateFormat,
308
+ tooltipLabel,
309
+ binSnapping,
310
+ barScaling,
311
+ minDate,
312
+ maxDate,
313
+ } = histogramProps;
314
+
315
+ // Because the modal manager does not clear its DOM content after being closed,
316
+ // it may try to render the exact same date picker template when it is reopened.
317
+ // And because it isn't actually a descendent of this collection-facets component,
318
+ // changes to the template defined here may not trigger a reactive update to the date
319
+ // picker, resulting in it displaying a stale date range.
320
+ // This ref callback ensures that every time the date picker modal is opened, it will
321
+ // always propagate the most recent date range into the date picker regardless of
322
+ // whether Lit thinks the update is necessary.
323
+ const expandedDatePickerChanged = (elmt?: Element) => {
324
+ if (elmt && elmt instanceof ExpandedDatePicker) {
325
+ const expandedDatePicker = elmt as ExpandedDatePicker;
326
+ expandedDatePicker.minSelectedDate = this.minSelectedDate;
327
+ expandedDatePicker.maxSelectedDate = this.maxSelectedDate;
328
+ }
329
+ };
330
+
331
+ const customModalContent = html`
332
+ <expanded-date-picker
333
+ ${ref(expandedDatePickerChanged)}
334
+ .minDate=${minDate}
335
+ .maxDate=${maxDate}
336
+ .minSelectedDate=${this.minSelectedDate}
337
+ .maxSelectedDate=${this.maxSelectedDate}
338
+ .customDateFormat=${dateFormat}
339
+ .customTooltipDateFormat=${tooltipDateFormat}
340
+ .customTooltipLabel=${tooltipLabel}
341
+ .binSnapping=${binSnapping}
342
+ .barScaling=${barScaling}
343
+ .buckets=${buckets}
344
+ .modalManager=${this.modalManager}
345
+ .analyticsHandler=${this.analyticsHandler}
346
+ @histogramDateRangeApplied=${this.histogramDateRangeUpdated}
347
+ @modalClosed=${this.handleExpandedDatePickerClosed}
348
+ ></expanded-date-picker>
349
+ `;
350
+
351
+ const config = new ModalConfig({
352
+ bodyColor: '#fff',
353
+ headerColor: '#194880',
354
+ showHeaderLogo: false,
355
+ closeOnBackdropClick: true, // TODO: want to fire analytics
356
+ title: html`${msg('Select a date range')}`,
357
+ });
358
+
359
+ this.modalManager?.classList.add('expanded-date-picker');
360
+ this.modalManager?.showModal({
361
+ config,
362
+ customModalContent,
363
+ userClosedModalCallback: this.handleExpandedDatePickerClosed,
364
+ });
365
+
366
+ this.analyticsHandler?.sendEvent({
367
+ category: analyticsCategories.default,
368
+ action: analyticsActions.histogramExpanded,
369
+ label: window.location.href,
370
+ });
371
+ }
372
+
373
+ private handleExpandedDatePickerClosed = (): void => {
374
+ this.modalManager?.classList.remove('expanded-date-picker');
375
+ };
376
+
377
+ updated(changed: PropertyValues) {
378
+ if (changed.has('selectedFacets')) {
379
+ this.dispatchFacetsChangedEvent();
380
+ }
381
+ }
382
+
383
+ // TODO: want to fire analytics?
384
+ private dispatchFacetsChangedEvent() {
385
+ const event = new CustomEvent<SelectedFacets>('facetsChanged', {
386
+ detail: this.selectedFacets,
387
+ });
388
+ this.dispatchEvent(event);
389
+ }
390
+
391
+ /**
392
+ * Template for the "Expand" button to show the date picker modal, or
393
+ * `nothing` if that button should currently not be shown.
394
+ */
395
+ private get expandDatePickerBtnTemplate(): TemplateResult | typeof nothing {
396
+ return this.allowExpandingDatePicker && !this.facetsLoading
397
+ ? html`<button
398
+ class="expand-date-picker-btn"
399
+ aria-hidden="true"
400
+ @click=${this.showDatePickerModal}
401
+ >
402
+ ${expandIcon}
403
+ </button>`
404
+ : nothing;
405
+ }
406
+
407
+ private get histogramTemplate(): TemplateResult | typeof nothing {
408
+ if (this.histogramAggregationLoading) {
409
+ return html` <div class="histogram-loading-indicator">&hellip;</div> `;
410
+ }
411
+
412
+ const { histogramProps } = this;
413
+ if (!histogramProps) return nothing;
414
+
415
+ const {
416
+ buckets,
417
+ dateFormat,
418
+ tooltipDateFormat,
419
+ tooltipLabel,
420
+ binSnapping,
421
+ barScaling,
422
+ minDate,
423
+ maxDate,
424
+ } = histogramProps;
425
+
426
+ return html`
427
+ <histogram-date-range
428
+ class=${this.isTvSearch ? 'wide-inputs' : nothing}
429
+ .minDate=${minDate}
430
+ .maxDate=${maxDate}
431
+ .minSelectedDate=${this.minSelectedDate ?? minDate}
432
+ .maxSelectedDate=${this.maxSelectedDate ?? maxDate}
433
+ .updateDelay=${100}
434
+ .dateFormat=${dateFormat}
435
+ .tooltipDateFormat=${tooltipDateFormat}
436
+ .tooltipLabel=${tooltipLabel}
437
+ .binSnapping=${binSnapping}
438
+ .barScaling=${barScaling}
439
+ .bins=${buckets}
440
+ missingDataMessage="..."
441
+ .width=${this.collapsableFacets && this.contentWidth
442
+ ? this.contentWidth
443
+ : 180}
444
+ @histogramDateRangeUpdated=${this.histogramDateRangeUpdated}
445
+ ></histogram-date-range>
446
+ `;
447
+ }
448
+
449
+ /**
450
+ * Dispatches a `histogramDateRangeUpdated` event with the date range copied from the
451
+ * input event.
452
+ *
453
+ * Arrow function to ensure `this` is always bound to the current component.
454
+ */
455
+ private histogramDateRangeUpdated = (
456
+ e: CustomEvent<{
457
+ minDate: string;
458
+ maxDate: string;
459
+ }>,
460
+ ): void => {
461
+ const { minDate, maxDate } = e.detail;
462
+ const event = new CustomEvent('histogramDateRangeUpdated', {
463
+ detail: { minDate, maxDate },
464
+ });
465
+ this.dispatchEvent(event);
466
+ };
467
+
468
+ /**
469
+ * Combines the selected facets with the aggregations to create a single list of facets
470
+ */
471
+ private get mergedFacets(): FacetGroup[] {
472
+ const facetGroups: FacetGroup[] = [];
473
+
474
+ this.facetDisplayOrder.forEach(facetKey => {
475
+ if (facetKey === 'mediatype' && this.suppressMediatypeFacets) return;
476
+
477
+ const selectedFacetGroup = this.selectedFacetGroups.find(
478
+ group => group.key === facetKey,
479
+ );
480
+ const aggregateFacetGroup = this.aggregationFacetGroups.find(
481
+ group => group.key === facetKey,
482
+ );
483
+
484
+ // if the user selected a facet, but it's not in the aggregation, we add it as-is
485
+ if (selectedFacetGroup && !aggregateFacetGroup) {
486
+ facetGroups.push(selectedFacetGroup);
487
+ return;
488
+ }
489
+
490
+ // if we don't have an aggregate facet group, don't add this to the list
491
+ if (!aggregateFacetGroup) return;
492
+
493
+ // start with either the selected group if we have one, or the aggregate group
494
+ const facetGroup = selectedFacetGroup ?? aggregateFacetGroup;
495
+
496
+ // attach the counts to the selected buckets
497
+ let bucketsWithCount =
498
+ selectedFacetGroup?.buckets.map(bucket => {
499
+ const selectedBucket = aggregateFacetGroup.buckets.find(
500
+ b => b.key === bucket.key,
501
+ );
502
+ return selectedBucket
503
+ ? {
504
+ ...bucket,
505
+ count: selectedBucket.count,
506
+ }
507
+ : bucket;
508
+ }) ?? [];
509
+
510
+ // append any additional buckets that were not selected
511
+ aggregateFacetGroup.buckets.forEach(bucket => {
512
+ const existingBucket = bucketsWithCount.find(b => b.key === bucket.key);
513
+ if (existingBucket) return;
514
+ bucketsWithCount.push(bucket);
515
+ });
516
+
517
+ /**
518
+ * render limited facet items on page facet area
519
+ *
520
+ * - by-default we are showing 6 items
521
+ * - additionally want to show all items (selected/suppressed) in page facet area
522
+ */
523
+ let allowedFacetCount = Object.keys(
524
+ (selectedFacetGroup?.buckets as []) || [],
525
+ )?.length;
526
+ if (allowedFacetCount < this.allowedFacetCount) {
527
+ allowedFacetCount = this.allowedFacetCount; // splice start index from 0th
528
+ }
529
+
530
+ // For lending facets, only include a specific subset of buckets
531
+ if (facetKey === 'lending') {
532
+ bucketsWithCount = bucketsWithCount.filter(
533
+ bucket => lendingFacetKeysVisibility[bucket.key as LendingFacetKey],
534
+ );
535
+ }
536
+
537
+ // Sort the FacetBuckets so that selected and hidden buckets come before the rest
538
+ sortBucketsBySelectionState(bucketsWithCount, defaultFacetSort[facetKey]);
539
+
540
+ // For mediatype facets, ensure the collection bucket is always shown if present
541
+ if (facetKey === 'mediatype') {
542
+ const collectionIndex = bucketsWithCount.findIndex(
543
+ bucket => bucket.key === 'collection',
544
+ );
545
+
546
+ if (collectionIndex >= allowedFacetCount) {
547
+ const [collectionBucket] = bucketsWithCount.splice(
548
+ collectionIndex,
549
+ 1,
550
+ );
551
+
552
+ // If we're showing lots of selected facets, ensure we're not cutting off the last one
553
+ if (allowedFacetCount > this.allowedFacetCount) {
554
+ allowedFacetCount += 1;
555
+ }
556
+
557
+ bucketsWithCount.splice(allowedFacetCount - 1, 0, collectionBucket);
558
+ }
559
+ }
560
+
561
+ // For TV creator facets, uppercase the display text
562
+ if (facetKey === 'creator' && this.isTvSearch) {
563
+ bucketsWithCount.forEach(b => {
564
+ b.displayText = (b.displayText ?? b.key)?.toLocaleUpperCase();
565
+
566
+ const channelLabel = this.tvChannelAliases?.get(b.displayText);
567
+ if (channelLabel && channelLabel !== b.displayText) {
568
+ b.extraNote = `(${channelLabel})`;
569
+ }
570
+ });
571
+ }
572
+
573
+ // slice off how many items we want to show in page facet area
574
+ facetGroup.buckets = bucketsWithCount.slice(0, allowedFacetCount);
575
+
576
+ facetGroups.push(facetGroup);
577
+ });
578
+
579
+ return facetGroups;
580
+ }
581
+
582
+ /**
583
+ * Converts the selected facets to a `FacetGroup` array,
584
+ * which is easier to work with
585
+ */
586
+ private get selectedFacetGroups(): FacetGroup[] {
587
+ if (!this.selectedFacets) return [];
588
+
589
+ const facetGroups: FacetGroup[] = Object.entries(this.selectedFacets).map(
590
+ ([key, selectedFacets]) => {
591
+ const option = key as FacetOption;
592
+ const title = facetTitles[option];
593
+
594
+ const buckets: FacetBucket[] = Object.entries(selectedFacets).map(
595
+ ([value, facetData]) => {
596
+ let displayText: string = value;
597
+ // for lending facets, convert the key to a readable format
598
+ if (option === 'lending') {
599
+ displayText =
600
+ lendingFacetDisplayNames[value as LendingFacetKey] ?? value;
601
+ }
602
+ return {
603
+ displayText,
604
+ key: value,
605
+ count: facetData.count,
606
+ state: facetData.state,
607
+ };
608
+ },
609
+ );
610
+
611
+ return {
612
+ title,
613
+ key: option,
614
+ buckets,
615
+ };
616
+ },
617
+ );
618
+
619
+ return facetGroups;
620
+ }
621
+
622
+ /**
623
+ * Converts the raw `aggregations` to `FacetGroups`, which are easier to use
624
+ */
625
+ private get aggregationFacetGroups(): FacetGroup[] {
626
+ const facetGroups: FacetGroup[] = [];
627
+ Object.entries(this.aggregations ?? []).forEach(([key, aggregation]) => {
628
+ // the year_histogram and date_histogram data is in a different format so can't be handled here
629
+ if (['year_histogram', 'date_histogram'].includes(key)) return;
630
+
631
+ const option = key as FacetOption;
632
+ const title = facetTitles[option];
633
+ if (!title) return;
634
+
635
+ let castedBuckets = aggregation.getSortedBuckets(
636
+ defaultFacetSort[option],
637
+ ) as Bucket[];
638
+
639
+ if (option === 'collection') {
640
+ // we are not showing fav- collections or certain deemphasized collections in facets
641
+ castedBuckets = castedBuckets?.filter(bucket => {
642
+ const bucketKey = bucket?.key?.toString();
643
+ return (
644
+ !suppressedCollections[bucketKey] && !bucketKey?.startsWith('fav-')
645
+ );
646
+ });
647
+ }
648
+
649
+ const facetBuckets: FacetBucket[] = castedBuckets.map(bucket => {
650
+ const bucketKey = bucket.key;
651
+ let displayText = `${bucket.key}`;
652
+ // for lending facets, convert the bucket key to a readable format
653
+ if (option === 'lending') {
654
+ displayText =
655
+ lendingFacetDisplayNames[bucket.key as LendingFacetKey] ??
656
+ `${bucket.key}`;
657
+ }
658
+ return {
659
+ displayText,
660
+ key: `${bucketKey}`,
661
+ count: bucket.doc_count,
662
+ state: 'none',
663
+ };
664
+ });
665
+ const group: FacetGroup = {
666
+ title,
667
+ key: option,
668
+ buckets: facetBuckets,
669
+ };
670
+ facetGroups.push(group);
671
+ });
672
+ return facetGroups;
673
+ }
674
+
675
+ /**
676
+ * Generate the template for a facet group with a header and the collapsible
677
+ * chevron for the mobile view
678
+ */
679
+ private getFacetGroupTemplate(
680
+ facetGroup: FacetGroup,
681
+ ): TemplateResult | typeof nothing {
682
+ if (!this.facetsLoading && facetGroup.buckets.length === 0) return nothing;
683
+
684
+ const { key } = facetGroup;
685
+ const isOpen = this.openFacets[key];
686
+ const collapser = html`
687
+ <span class="collapser ${isOpen ? 'open' : ''}"> ${chevronIcon} </span>
688
+ `;
689
+
690
+ const toggleCollapsed = () => {
691
+ const newOpenFacets = { ...this.openFacets };
692
+ newOpenFacets[key] = !isOpen;
693
+ this.openFacets = newOpenFacets;
694
+ };
695
+
696
+ // Added data-testid for Playwright testing
697
+ // Using className and aria-labels is not ideal for Playwright locator
698
+ const headerId = `facet-group-header-label-${facetGroup.key}`;
699
+ return html`
700
+ <section
701
+ class="facet-group ${this.collapsableFacets ? 'mobile' : ''}"
702
+ aria-labelledby=${headerId}
703
+ data-testid=${headerId}
704
+ >
705
+ <div class="facet-group-header">
706
+ <h3
707
+ id=${headerId}
708
+ @click=${toggleCollapsed}
709
+ @keyup=${toggleCollapsed}
710
+ >
711
+ ${this.collapsableFacets ? collapser : nothing} ${facetGroup.title}
712
+ <span class="sr-only">filters</span>
713
+ </h3>
714
+ </div>
715
+ <div
716
+ class="facet-group-content ${isOpen ? 'open' : ''}"
717
+ data-testid="facet-group-content-${facetGroup.key}"
718
+ >
719
+ ${this.facetsLoading
720
+ ? this.getTombstoneFacetGroupTemplate()
721
+ : html`
722
+ ${this.getFacetTemplate(facetGroup)}
723
+ ${this.searchMoreFacetsLink(facetGroup)}
724
+ `}
725
+ </div>
726
+ </section>
727
+ `;
728
+ }
729
+
730
+ private getTombstoneFacetGroupTemplate(): TemplateResult {
731
+ // Render five tombstone rows
732
+ return html`
733
+ ${map(
734
+ Array(5).fill(null),
735
+ () => html`<facet-tombstone-row></facet-tombstone-row>`,
736
+ )}
737
+ `;
738
+ }
739
+
740
+ /**
741
+ * Generate the More... link button just below the facets group
742
+ *
743
+ * TODO: want to fire analytics?
744
+ */
745
+ private searchMoreFacetsLink(
746
+ facetGroup: FacetGroup,
747
+ ): TemplateResult | typeof nothing {
748
+ // Don't render More... links for FTS searches
749
+ if (!this.moreLinksVisible) {
750
+ return nothing;
751
+ }
752
+
753
+ // Don't render More... links for lending facets
754
+ if (facetGroup.key === 'lending') {
755
+ return nothing;
756
+ }
757
+
758
+ // Don't render More... link if the number of facets < this.allowedFacetCount
759
+ if (Object.keys(facetGroup.buckets).length < this.allowedFacetCount) {
760
+ return nothing;
761
+ }
762
+
763
+ // We sort years in numeric order by default, rather than bucket count
764
+ const facetSort = defaultFacetSort[facetGroup.key];
765
+
766
+ // Added data-testid for Playwright testing
767
+ // Using the className is not ideal for Playwright locator
768
+ return html`<button
769
+ class="more-link"
770
+ @click=${() => {
771
+ this.showMoreFacetsModal(facetGroup, facetSort);
772
+ this.analyticsHandler?.sendEvent({
773
+ category: analyticsCategories.default,
774
+ action: analyticsActions.showMoreFacetsModal,
775
+ label: facetGroup.key,
776
+ });
777
+ this.dispatchEvent(
778
+ new CustomEvent('showMoreFacets', { detail: facetGroup.key }),
779
+ );
780
+ }}
781
+ data-testid="more-link-btn"
782
+ >
783
+ More...
784
+ </button>`;
785
+ }
786
+
787
+ async showMoreFacetsModal(
788
+ facetGroup: FacetGroup,
789
+ sortedBy: AggregationSortType,
790
+ ): Promise<void> {
791
+ const customModalContent = html`
792
+ <more-facets-content
793
+ .analyticsHandler=${this.analyticsHandler}
794
+ .facetKey=${facetGroup.key}
795
+ .query=${this.query}
796
+ .filterMap=${this.filterMap}
797
+ .pageSpecifierParams=${this.pageSpecifierParams}
798
+ .modalManager=${this.modalManager}
799
+ .searchService=${this.searchService}
800
+ .searchType=${this.searchType}
801
+ .collectionTitles=${this.collectionTitles}
802
+ .tvChannelAliases=${this.tvChannelAliases}
803
+ .selectedFacets=${this.selectedFacets}
804
+ .sortedBy=${sortedBy}
805
+ .isTvSearch=${this.isTvSearch}
806
+ @facetsChanged=${(e: CustomEvent) => {
807
+ const event = new CustomEvent<SelectedFacets>('facetsChanged', {
808
+ detail: e.detail,
809
+ bubbles: true,
810
+ composed: true,
811
+ });
812
+ this.dispatchEvent(event);
813
+ }}
814
+ >
815
+ </more-facets-content>
816
+ `;
817
+
818
+ const config = new ModalConfig({
819
+ bodyColor: '#fff',
820
+ headerColor: '#194880',
821
+ showHeaderLogo: false,
822
+ closeOnBackdropClick: true, // TODO: want to fire analytics
823
+ title: html`Select filters`,
824
+ });
825
+ this.modalManager?.classList.add('more-search-facets');
826
+ this.modalManager?.showModal({
827
+ config,
828
+ customModalContent,
829
+ userClosedModalCallback: () => {
830
+ this.modalManager?.classList.remove('more-search-facets');
831
+ },
832
+ });
833
+ }
834
+
835
+ /**
836
+ * Generate the list template for each bucket in a facet group
837
+ */
838
+ private getFacetTemplate(facetGroup: FacetGroup): TemplateResult {
839
+ return html`
840
+ <facets-template
841
+ .collectionPagePath=${this.collectionPagePath}
842
+ .facetGroup=${facetGroup}
843
+ .selectedFacets=${this.selectedFacets}
844
+ .collectionTitles=${this.collectionTitles}
845
+ @facetClick=${(e: CustomEvent<FacetEventDetails>) => {
846
+ this.selectedFacets = updateSelectedFacetBucket(
847
+ this.selectedFacets,
848
+ facetGroup.key,
849
+ e.detail.bucket,
850
+ true,
851
+ );
852
+
853
+ const event = new CustomEvent<SelectedFacets>('facetsChanged', {
854
+ detail: this.selectedFacets,
855
+ bubbles: true,
856
+ composed: true,
857
+ });
858
+ this.dispatchEvent(event);
859
+ }}
860
+ ></facets-template>
861
+ `;
862
+ }
863
+
864
+ static get styles() {
865
+ return [
866
+ srOnlyStyle,
867
+ css`
868
+ a:link {
869
+ text-decoration: none;
870
+ color: var(--ia-theme-link-color, #4b64ff);
871
+ }
872
+ a:link:hover {
873
+ text-decoration: underline;
874
+ }
875
+
876
+ #container.loading {
877
+ opacity: 0.5;
878
+ }
879
+
880
+ #container.managing {
881
+ opacity: 0.3;
882
+ }
883
+
884
+ .histogram-loading-indicator {
885
+ width: 100%;
886
+ height: 2.25rem;
887
+ margin-top: 1.75rem;
888
+ font-size: 1.4rem;
889
+ text-align: center;
890
+ }
891
+
892
+ .collapser {
893
+ display: inline-block;
894
+ cursor: pointer;
895
+ width: 10px;
896
+ height: 10px;
897
+ }
898
+
899
+ .collapser svg {
900
+ transition: transform 0.2s ease-in-out;
901
+ }
902
+
903
+ .collapser.open svg {
904
+ transform: rotate(90deg);
905
+ }
906
+
907
+ .facet-group:not(:last-child) {
908
+ margin-bottom: 2rem;
909
+ }
910
+
911
+ .facet-group h3 {
912
+ margin-bottom: 0.7rem;
913
+ }
914
+
915
+ .facet-group.mobile h3 {
916
+ cursor: pointer;
917
+ }
918
+
919
+ .facet-group-header {
920
+ display: flex;
921
+ margin-bottom: 0.7rem;
922
+ justify-content: space-between;
923
+ border-bottom: 1px solid rgb(232, 232, 232);
924
+ }
925
+
926
+ .facet-group-content {
927
+ transition: max-height 0.2s ease-in-out;
928
+ }
929
+
930
+ .facet-group.mobile .facet-group-content {
931
+ max-height: 0;
932
+ overflow: hidden;
933
+ }
934
+
935
+ .facet-group.mobile .facet-group-content.open {
936
+ max-height: 2000px;
937
+ }
938
+
939
+ .partof-collections ul {
940
+ list-style-type: none;
941
+ padding: 0;
942
+ font-size: 1.2rem;
943
+ }
944
+
945
+ h3 {
946
+ font-size: 1.4rem;
947
+ margin: 0;
948
+ }
949
+
950
+ .more-link {
951
+ font-size: 1.2rem;
952
+ text-decoration: none;
953
+ padding: 0;
954
+ background: inherit;
955
+ border: 0;
956
+ color: var(--ia-theme-link-color, #4b64ff);
957
+ cursor: pointer;
958
+ }
959
+
960
+ #date-picker-label {
961
+ display: flex;
962
+ justify-content: space-between;
963
+ }
964
+
965
+ .expand-date-picker-btn {
966
+ margin: 0;
967
+ padding: 0;
968
+ border: 0;
969
+ appearance: none;
970
+ background: none;
971
+ cursor: pointer;
972
+ }
973
+
974
+ .expand-date-picker-btn > svg {
975
+ width: 14px;
976
+ height: 14px;
977
+ }
978
+
979
+ .sorting-icon {
980
+ height: 15px;
981
+ cursor: pointer;
982
+ }
983
+
984
+ histogram-date-range.wide-inputs {
985
+ --histogramDateRangeInputWidth: 4.8rem;
986
+ }
987
+ `,
988
+ ];
989
+ }
990
+ }