@internetarchive/collection-browser 3.5.2-alpha-webdev8164.0 → 3.5.2-webdev-8162.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 (45) 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/.prettierignore +1 -1
  7. package/LICENSE +661 -661
  8. package/README.md +83 -83
  9. package/dist/src/app-root.js +606 -606
  10. package/dist/src/app-root.js.map +1 -1
  11. package/dist/src/collection-facets/facets-template.js +23 -28
  12. package/dist/src/collection-facets/facets-template.js.map +1 -1
  13. package/dist/src/collection-facets/more-facets-content.d.ts +8 -34
  14. package/dist/src/collection-facets/more-facets-content.js +159 -358
  15. package/dist/src/collection-facets/more-facets-content.js.map +1 -1
  16. package/dist/src/tiles/grid/collection-tile.js +77 -77
  17. package/dist/src/tiles/grid/collection-tile.js.map +1 -1
  18. package/dist/src/tiles/grid/item-tile.js +137 -137
  19. package/dist/src/tiles/grid/item-tile.js.map +1 -1
  20. package/dist/src/tiles/models.js.map +1 -1
  21. package/dist/src/tiles/tile-dispatcher.js +215 -215
  22. package/dist/src/tiles/tile-dispatcher.js.map +1 -1
  23. package/dist/test/collection-browser.test.js +1 -1
  24. package/dist/test/collection-browser.test.js.map +1 -1
  25. package/dist/test/collection-facets/more-facets-content.test.js +31 -137
  26. package/dist/test/collection-facets/more-facets-content.test.js.map +1 -1
  27. package/eslint.config.mjs +53 -53
  28. package/index.html +24 -24
  29. package/local.archive.org.cert +86 -86
  30. package/local.archive.org.key +27 -27
  31. package/package.json +121 -119
  32. package/renovate.json +6 -6
  33. package/src/app-root.ts +1140 -1140
  34. package/src/collection-facets/facets-template.ts +83 -88
  35. package/src/collection-facets/more-facets-content.ts +642 -856
  36. package/src/tiles/grid/collection-tile.ts +163 -163
  37. package/src/tiles/grid/item-tile.ts +340 -340
  38. package/src/tiles/models.ts +1 -1
  39. package/src/tiles/tile-dispatcher.ts +517 -517
  40. package/test/collection-browser.test.ts +1 -1
  41. package/test/collection-facets/more-facets-content.test.ts +231 -378
  42. package/tsconfig.json +25 -25
  43. package/web-dev-server.config.mjs +30 -30
  44. package/web-test-runner.config.mjs +52 -41
  45. package/.husky/pre-commit +0 -4
@@ -1,856 +1,642 @@
1
- import {
2
- css,
3
- CSSResultGroup,
4
- html,
5
- LitElement,
6
- nothing,
7
- PropertyValues,
8
- TemplateResult,
9
- } from 'lit';
10
- import { customElement, property, state } from 'lit/decorators.js';
11
- import {
12
- Aggregation,
13
- Bucket,
14
- SearchServiceInterface,
15
- SearchParams,
16
- SearchType,
17
- AggregationSortType,
18
- FilterMap,
19
- PageType,
20
- } from '@internetarchive/search-service';
21
- import type { ModalManagerInterface } from '@internetarchive/modal-manager';
22
- import type { AnalyticsManagerInterface } from '@internetarchive/analytics-manager';
23
- import { msg } from '@lit/localize';
24
- import {
25
- SelectedFacets,
26
- FacetGroup,
27
- FacetBucket,
28
- FacetOption,
29
- facetTitles,
30
- suppressedCollections,
31
- valueFacetSort,
32
- defaultFacetSort,
33
- getDefaultSelectedFacets,
34
- FacetEventDetails,
35
- } from '../models';
36
- import type {
37
- CollectionTitles,
38
- PageSpecifierParams,
39
- TVChannelAliases,
40
- } from '../data-source/models';
41
- import '@internetarchive/elements/ia-status-indicator/ia-status-indicator';
42
- import './facets-template';
43
- import {
44
- analyticsActions,
45
- analyticsCategories,
46
- } from '../utils/analytics-events';
47
- import './toggle-switch';
48
- import './more-facets-pagination';
49
- import { srOnlyStyle } from '../styles/sr-only';
50
- import {
51
- mergeSelectedFacets,
52
- sortBucketsBySelectionState,
53
- updateSelectedFacetBucket,
54
- } from '../utils/facet-utils';
55
- import {
56
- MORE_FACETS__DEFAULT_PAGE_SIZE,
57
- MORE_FACETS__MAX_AGGREGATIONS,
58
- } from './models';
59
-
60
- /**
61
- * Threshold for switching from horizontal scroll to pagination.
62
- * If facet count >= this value, use pagination. Otherwise use horizontal scroll.
63
- */
64
- const PAGINATION_THRESHOLD = 1000;
65
-
66
- @customElement('more-facets-content')
67
- export class MoreFacetsContent extends LitElement {
68
- @property({ type: String }) facetKey?: FacetOption;
69
-
70
- @property({ type: String }) query?: string;
71
-
72
- @property({ type: Array }) identifiers?: string[];
73
-
74
- @property({ type: Object }) filterMap?: FilterMap;
75
-
76
- @property({ type: Number }) searchType?: SearchType;
77
-
78
- @property({ type: Object }) pageSpecifierParams?: PageSpecifierParams;
79
-
80
- @property({ type: Object })
81
- collectionTitles?: CollectionTitles;
82
-
83
- @property({ type: Object })
84
- tvChannelAliases?: TVChannelAliases;
85
-
86
- /**
87
- * Maximum number of facets to show per page within the modal.
88
- */
89
- @property({ type: Number }) facetsPerPage = MORE_FACETS__DEFAULT_PAGE_SIZE;
90
-
91
- /**
92
- * Whether we are waiting for facet data to load.
93
- * We begin with this set to true so that we show an initial loading indicator.
94
- */
95
- @property({ type: Boolean }) facetsLoading = true;
96
-
97
- /**
98
- * The set of pre-existing facet selections (including both selected & negated facets).
99
- */
100
- @property({ type: Object }) selectedFacets?: SelectedFacets;
101
-
102
- @property({ type: Number }) sortedBy: AggregationSortType =
103
- AggregationSortType.COUNT;
104
-
105
- @property({ type: Boolean }) isTvSearch = false;
106
-
107
- @property({ type: Object }) modalManager?: ModalManagerInterface;
108
-
109
- @property({ type: Object }) searchService?: SearchServiceInterface;
110
-
111
- @property({ type: Object, attribute: false })
112
- analyticsHandler?: AnalyticsManagerInterface;
113
-
114
- /**
115
- * The full set of aggregations received from the search service
116
- */
117
- @state() private aggregations?: Record<string, Aggregation>;
118
-
119
- /**
120
- * A FacetGroup storing the full set of facet buckets to be shown on the dialog.
121
- */
122
- @state() private facetGroup?: FacetGroup;
123
-
124
- /**
125
- * An object holding any changes the patron has made to their facet selections
126
- * within the modal dialog but which they have not yet applied. These are
127
- * eventually merged into the existing `selectedFacets` when the patron applies
128
- * their changes, or discarded if they cancel/close the dialog.
129
- */
130
- @state() private unappliedFacetChanges: SelectedFacets =
131
- getDefaultSelectedFacets();
132
-
133
- /**
134
- * Text entered by the user to filter facet buckets.
135
- * Applied to bucket.key for case-insensitive matching.
136
- */
137
- @state() private filterText = '';
138
-
139
- /**
140
- * Current page number for pagination (when facet count >= PAGINATION_THRESHOLD).
141
- */
142
- @state() private pageNumber = 1;
143
-
144
- willUpdate(changed: PropertyValues): void {
145
- if (
146
- changed.has('aggregations') ||
147
- changed.has('facetsPerPage') ||
148
- changed.has('sortedBy') ||
149
- changed.has('selectedFacets') ||
150
- changed.has('unappliedFacetChanges')
151
- ) {
152
- // Convert the merged selected facets & aggregations into a facet group, and
153
- // store it for reuse across pages.
154
- this.facetGroup = this.mergedFacets;
155
- }
156
-
157
- // Reset to page 1 when filter text changes (only matters for pagination mode)
158
- if (changed.has('filterText')) {
159
- this.pageNumber = 1;
160
- }
161
- }
162
-
163
- updated(changed: PropertyValues): void {
164
- // If any of the search properties change, it triggers a facet fetch
165
- if (
166
- changed.has('facetKey') ||
167
- changed.has('query') ||
168
- changed.has('searchType') ||
169
- changed.has('filterMap')
170
- ) {
171
- this.facetsLoading = true;
172
- this.pageNumber = 1; // Reset to page 1 on new search
173
- this.sortedBy = defaultFacetSort[this.facetKey as FacetOption];
174
-
175
- this.updateSpecificFacets();
176
- }
177
- }
178
-
179
- firstUpdated(): void {
180
- this.setupEscapeListeners();
181
- }
182
-
183
- /**
184
- * Close more facets modal on Escape click
185
- */
186
- private setupEscapeListeners() {
187
- if (this.modalManager) {
188
- document.addEventListener('keydown', (e: KeyboardEvent) => {
189
- if (e.key === 'Escape') {
190
- this.modalManager?.closeModal();
191
- }
192
- });
193
- } else {
194
- document.removeEventListener('keydown', () => {});
195
- }
196
- }
197
-
198
- /**
199
- * Whether facet requests are for the search_results page type (either defaulted or explicitly).
200
- */
201
- private get isSearchResultsPage(): boolean {
202
- // Default page type is search_results when none is specified, so we check
203
- // for undefined as well.
204
- const pageType: PageType | undefined = this.pageSpecifierParams?.pageType;
205
- return pageType === undefined || pageType === 'search_results';
206
- }
207
-
208
- /**
209
- * Get specific facets data from search-service API based of currently query params
210
- * - this.aggregations - hold result of search service and being used for further processing.
211
- */
212
- async updateSpecificFacets(): Promise<void> {
213
- if (!this.facetKey) return; // Can't fetch facets if we don't know what type of facets we need!
214
-
215
- const trimmedQuery = this.query?.trim();
216
- if (!trimmedQuery && this.isSearchResultsPage) return; // The search page _requires_ a query
217
-
218
- const aggregations = {
219
- simpleParams: [this.facetKey],
220
- };
221
- const aggregationsSize = MORE_FACETS__MAX_AGGREGATIONS; // Only request the 10K highest-count facets
222
-
223
- const params: SearchParams = {
224
- ...this.pageSpecifierParams,
225
- query: trimmedQuery || '',
226
- identifiers: this.identifiers,
227
- filters: this.filterMap,
228
- aggregations,
229
- aggregationsSize,
230
- rows: 0, // todo - do we want server-side pagination with offset/page/limit flag?
231
- };
232
-
233
- const results = await this.searchService?.search(params, this.searchType);
234
- this.aggregations = results?.success?.response.aggregations;
235
- this.facetsLoading = false;
236
-
237
- const collectionTitles = results?.success?.response?.collectionTitles;
238
- if (collectionTitles) {
239
- for (const [id, title] of Object.entries(collectionTitles)) {
240
- this.collectionTitles?.set(id, title);
241
- }
242
- }
243
- }
244
-
245
- /**
246
- * Combines the selected facets with the aggregations to create a single list of facets
247
- */
248
- private get mergedFacets(): FacetGroup | undefined {
249
- if (!this.facetKey || !this.selectedFacets) return undefined;
250
-
251
- const { selectedFacetGroup, aggregationFacetGroup } = this;
252
-
253
- // If we don't have any aggregations, then there is nothing to show yet
254
- if (!aggregationFacetGroup) return undefined;
255
-
256
- // Start with either the selected group if we have one, or the aggregate group otherwise
257
- const facetGroup = { ...(selectedFacetGroup ?? aggregationFacetGroup) };
258
-
259
- // Attach the counts to the selected buckets
260
- const bucketsWithCount =
261
- selectedFacetGroup?.buckets.map(bucket => {
262
- const selectedBucket = aggregationFacetGroup.buckets.find(
263
- b => b.key === bucket.key,
264
- );
265
- return selectedBucket
266
- ? {
267
- ...bucket,
268
- count: selectedBucket.count,
269
- }
270
- : bucket;
271
- }) ?? [];
272
-
273
- // Sort the buckets by selection state
274
- // We do this *prior* to considering unapplied selections, because we want the facets
275
- // to remain in position when they are selected/unselected, rather than re-sort themselves.
276
- sortBucketsBySelectionState(bucketsWithCount, this.sortedBy);
277
-
278
- // Append any additional buckets that were not selected
279
- aggregationFacetGroup.buckets.forEach(bucket => {
280
- const existingBucket = selectedFacetGroup?.buckets.find(
281
- b => b.key === bucket.key,
282
- );
283
- if (existingBucket) return;
284
- bucketsWithCount.push(bucket);
285
- });
286
-
287
- // Apply any unapplied selections that appear on this page
288
- const unappliedBuckets = this.unappliedFacetChanges[this.facetKey];
289
- for (const [index, bucket] of bucketsWithCount.entries()) {
290
- const unappliedBucket = unappliedBuckets?.[bucket.key];
291
- if (unappliedBucket) {
292
- bucketsWithCount[index] = { ...unappliedBucket };
293
- }
294
- }
295
-
296
- // For TV creator facets, uppercase the display text
297
- if (this.facetKey === 'creator' && this.isTvSearch) {
298
- bucketsWithCount.forEach(b => {
299
- b.displayText = (b.displayText ?? b.key)?.toLocaleUpperCase();
300
-
301
- const channelLabel = this.tvChannelAliases?.get(b.displayText);
302
- if (channelLabel && channelLabel !== b.displayText) {
303
- b.extraNote = `(${channelLabel})`;
304
- }
305
- });
306
- }
307
-
308
- facetGroup.buckets = bucketsWithCount;
309
- return facetGroup;
310
- }
311
-
312
- /**
313
- * Converts the selected facets for the current facet key to a `FacetGroup`,
314
- * which is easier to work with.
315
- */
316
- private get selectedFacetGroup(): FacetGroup | undefined {
317
- if (!this.selectedFacets || !this.facetKey) return undefined;
318
-
319
- const selectedFacetsForKey = this.selectedFacets[this.facetKey];
320
- if (!selectedFacetsForKey) return undefined;
321
-
322
- const facetGroupTitle = facetTitles[this.facetKey];
323
-
324
- const buckets: FacetBucket[] = Object.entries(selectedFacetsForKey).map(
325
- ([value, data]) => {
326
- const displayText: string = value;
327
- return {
328
- displayText,
329
- key: value,
330
- count: data?.count,
331
- state: data?.state,
332
- };
333
- },
334
- );
335
-
336
- return {
337
- title: facetGroupTitle,
338
- key: this.facetKey,
339
- buckets,
340
- };
341
- }
342
-
343
- /**
344
- * Converts the raw `aggregations` for the current facet key to a `FacetGroup`,
345
- * which is easier to work with.
346
- */
347
- private get aggregationFacetGroup(): FacetGroup | undefined {
348
- if (!this.aggregations || !this.facetKey) return undefined;
349
-
350
- const currentAggregation = this.aggregations[this.facetKey];
351
- if (!currentAggregation) return undefined;
352
-
353
- const facetGroupTitle = facetTitles[this.facetKey];
354
-
355
- // Order the facets according to the current sort option
356
- let sortedBuckets = currentAggregation.getSortedBuckets(
357
- this.sortedBy,
358
- ) as Bucket[];
359
-
360
- if (this.facetKey === 'collection') {
361
- // we are not showing fav- collections or certain deemphasized collections in facets
362
- sortedBuckets = sortedBuckets?.filter(bucket => {
363
- const bucketKey = bucket?.key?.toString();
364
- return (
365
- !suppressedCollections[bucketKey] && !bucketKey?.startsWith('fav-')
366
- );
367
- });
368
- }
369
-
370
- // Construct the array of facet buckets from the aggregation buckets
371
- const facetBuckets: FacetBucket[] = sortedBuckets.map(bucket => {
372
- const bucketKeyStr = `${bucket.key}`;
373
- return {
374
- displayText: `${bucketKeyStr}`,
375
- key: `${bucketKeyStr}`,
376
- count: bucket.doc_count,
377
- state: 'none',
378
- };
379
- });
380
-
381
- return {
382
- title: facetGroupTitle,
383
- key: this.facetKey,
384
- buckets: facetBuckets,
385
- };
386
- }
387
-
388
- /**
389
- * Returns the facet group with buckets filtered by the current filter text.
390
- * Filters are applied to the full bucket list before pagination.
391
- */
392
- private get filteredFacetGroup(): FacetGroup | undefined {
393
- const { facetGroup, filterText } = this;
394
- if (!facetGroup) return undefined;
395
-
396
- // If no filter text, return the full group
397
- if (!filterText.trim()) {
398
- return facetGroup;
399
- }
400
-
401
- // Filter buckets case-insensitively by bucket key
402
- const lowerFilter = filterText.toLowerCase().trim();
403
- const filteredBuckets = facetGroup.buckets.filter(bucket =>
404
- bucket.key.toLowerCase().includes(lowerFilter),
405
- );
406
-
407
- return {
408
- ...facetGroup,
409
- buckets: filteredBuckets,
410
- };
411
- }
412
-
413
- /**
414
- * Determines whether to use pagination based on the number of filtered facets.
415
- * Returns true if facet count >= PAGINATION_THRESHOLD, false otherwise.
416
- */
417
- private get usePagination(): boolean {
418
- const facetCount = this.filteredFacetGroup?.buckets.length ?? 0;
419
- return facetCount >= PAGINATION_THRESHOLD;
420
- }
421
-
422
- /**
423
- * Returns the facet group for the current page.
424
- * If using pagination (>= 1000 facets), slices to show only the current page.
425
- * Otherwise, returns all facets for horizontal scrolling.
426
- */
427
- private get facetGroupForCurrentPage(): FacetGroup | undefined {
428
- const filteredGroup = this.filteredFacetGroup;
429
- if (!filteredGroup) return undefined;
430
-
431
- // If facet count is below threshold, show all facets with horizontal scroll
432
- if (!this.usePagination) {
433
- return filteredGroup;
434
- }
435
-
436
- // Otherwise, use pagination - slice to current page
437
- const startIndex = (this.pageNumber - 1) * this.facetsPerPage;
438
- const endIndex = startIndex + this.facetsPerPage;
439
- const slicedBuckets = filteredGroup.buckets.slice(startIndex, endIndex);
440
-
441
- return {
442
- ...filteredGroup,
443
- buckets: slicedBuckets,
444
- };
445
- }
446
-
447
- private get moreFacetsTemplate(): TemplateResult {
448
- const facetGroup = this.facetGroupForCurrentPage;
449
-
450
- // Show empty state if filtering returned no results
451
- if (
452
- this.filterText.trim() &&
453
- (!facetGroup || facetGroup.buckets.length === 0)
454
- ) {
455
- return this.emptyFilterResultsTemplate;
456
- }
457
-
458
- return html`
459
- <facets-template
460
- .facetGroup=${facetGroup}
461
- .selectedFacets=${this.selectedFacets}
462
- .collectionTitles=${this.collectionTitles}
463
- @facetClick=${(e: CustomEvent<FacetEventDetails>) => {
464
- if (this.facetKey) {
465
- this.unappliedFacetChanges = updateSelectedFacetBucket(
466
- this.unappliedFacetChanges,
467
- this.facetKey,
468
- e.detail.bucket,
469
- );
470
- }
471
- }}
472
- ></facets-template>
473
- `;
474
- }
475
-
476
- private get loaderTemplate(): TemplateResult {
477
- return html`
478
- <ia-status-indicator
479
- class="facets-loader"
480
- mode="loading"
481
- ></ia-status-indicator>
482
- `;
483
- }
484
-
485
- private get emptyFilterResultsTemplate(): TemplateResult {
486
- return html`
487
- <div class="empty-results">
488
- <p>${msg('No matching values found.')}</p>
489
- <p class="hint">${msg('Try a different search term.')}</p>
490
- </div>
491
- `;
492
- }
493
-
494
- /**
495
- * Number of pages for pagination (only used when facet count >= PAGINATION_THRESHOLD).
496
- */
497
- private get paginationSize(): number {
498
- const filteredBuckets = this.filteredFacetGroup?.buckets ?? [];
499
- return Math.ceil(filteredBuckets.length / this.facetsPerPage);
500
- }
501
-
502
- /**
503
- * Template for pagination component (only shown when facet count >= PAGINATION_THRESHOLD).
504
- */
505
- private get facetsPaginationTemplate() {
506
- if (!this.usePagination) return nothing;
507
-
508
- return html`<more-facets-pagination
509
- .size=${this.paginationSize}
510
- .currentPage=${this.pageNumber}
511
- @pageNumberClicked=${this.pageNumberClicked}
512
- ></more-facets-pagination>`;
513
- }
514
-
515
- private get footerTemplate() {
516
- return html`
517
- ${this.facetsPaginationTemplate}
518
- <div class="footer">
519
- <button class="btn btn-cancel" type="button" @click=${this.cancelClick}>
520
- Cancel
521
- </button>
522
- <button
523
- class="btn btn-submit"
524
- type="button"
525
- @click=${this.applySearchFacetsClicked}
526
- >
527
- Apply filters
528
- </button>
529
- </div>
530
- `;
531
- }
532
-
533
- private sortFacetAggregation(facetSortType: AggregationSortType) {
534
- this.sortedBy = facetSortType;
535
- this.dispatchEvent(
536
- new CustomEvent('sortedFacets', { detail: this.sortedBy }),
537
- );
538
- }
539
-
540
- /**
541
- * Handler for filter input changes. Updates the filter text and triggers re-render.
542
- */
543
- private handleFilterInput(e: Event): void {
544
- const input = e.target as HTMLInputElement;
545
- this.filterText = input.value;
546
- }
547
-
548
- /**
549
- * Handler for pagination page number clicks.
550
- * Only used when facet count >= PAGINATION_THRESHOLD.
551
- */
552
- private pageNumberClicked(e: CustomEvent<{ page: number }>) {
553
- this.pageNumber = e.detail.page;
554
-
555
- // Track page navigation in analytics
556
- this.analyticsHandler?.sendEvent({
557
- category: analyticsCategories.default,
558
- action: analyticsActions.moreFacetsPageChange,
559
- label: `${this.pageNumber}`,
560
- });
561
-
562
- this.dispatchEvent(
563
- new CustomEvent('pageChanged', { detail: this.pageNumber }),
564
- );
565
- }
566
-
567
- private get modalHeaderTemplate(): TemplateResult {
568
- const facetSort =
569
- this.sortedBy ?? defaultFacetSort[this.facetKey as FacetOption];
570
- const defaultSwitchSide =
571
- facetSort === AggregationSortType.COUNT ? 'left' : 'right';
572
-
573
- return html`<span class="sr-only">${msg('More facets for:')}</span>
574
- <span class="title">
575
- ${this.facetGroup?.title}
576
-
577
- <label class="sort-label">${msg('Sort by:')}</label>
578
- ${this.facetKey
579
- ? html`<toggle-switch
580
- class="sort-toggle"
581
- leftValue=${AggregationSortType.COUNT}
582
- leftLabel="Count"
583
- rightValue=${valueFacetSort[this.facetKey]}
584
- .rightLabel=${this.facetGroup?.title}
585
- side=${defaultSwitchSide}
586
- @change=${(e: CustomEvent<string>) => {
587
- this.sortFacetAggregation(
588
- Number(e.detail) as AggregationSortType,
589
- );
590
- }}
591
- ></toggle-switch>`
592
- : nothing}
593
-
594
- <label class="filter-label" for="facet-filter"
595
- >${msg('Filter by:')}</label
596
- >
597
- <input
598
- id="facet-filter"
599
- type="text"
600
- class="filter-input"
601
- .value=${this.filterText}
602
- @input=${this.handleFilterInput}
603
- placeholder=${msg('Search...')}
604
- aria-label=${msg('Filter facets')}
605
- />
606
- </span>`;
607
- }
608
-
609
- render() {
610
- const contentClass = this.usePagination
611
- ? 'facets-content pagination-mode'
612
- : 'facets-content horizontal-scroll-mode';
613
- const sectionClass = this.usePagination
614
- ? 'pagination-mode'
615
- : 'horizontal-scroll-mode';
616
-
617
- return html`
618
- ${this.facetsLoading
619
- ? this.loaderTemplate
620
- : html`
621
- <section id="more-facets" class="${sectionClass}">
622
- <div class="header-content">${this.modalHeaderTemplate}</div>
623
- <div class="${contentClass}">
624
- ${this.usePagination
625
- ? this.moreFacetsTemplate
626
- : html`<div class="facets-horizontal-container">
627
- ${this.moreFacetsTemplate}
628
- </div>`}
629
- </div>
630
- ${this.footerTemplate}
631
- </section>
632
- `}
633
- `;
634
- }
635
-
636
- private applySearchFacetsClicked() {
637
- const mergedSelections = mergeSelectedFacets(
638
- this.selectedFacets,
639
- this.unappliedFacetChanges,
640
- );
641
-
642
- const event = new CustomEvent<SelectedFacets>('facetsChanged', {
643
- detail: mergedSelections,
644
- bubbles: true,
645
- composed: true,
646
- });
647
- this.dispatchEvent(event);
648
-
649
- // Reset the unapplied changes back to default, now that they have been applied
650
- this.unappliedFacetChanges = getDefaultSelectedFacets();
651
-
652
- // Reset filter text
653
- this.filterText = '';
654
-
655
- this.modalManager?.closeModal();
656
- this.analyticsHandler?.sendEvent({
657
- category: analyticsCategories.default,
658
- action: `${analyticsActions.applyMoreFacetsModal}`,
659
- label: `${this.facetKey}`,
660
- });
661
- }
662
-
663
- private cancelClick() {
664
- // Reset the unapplied changes back to default
665
- this.unappliedFacetChanges = getDefaultSelectedFacets();
666
-
667
- // Reset filter text
668
- this.filterText = '';
669
-
670
- this.modalManager?.closeModal();
671
- this.analyticsHandler?.sendEvent({
672
- category: analyticsCategories.default,
673
- action: analyticsActions.closeMoreFacetsModal,
674
- label: `${this.facetKey}`,
675
- });
676
- }
677
-
678
- static get styles(): CSSResultGroup {
679
- const modalSubmitButton = css`var(--primaryButtonBGColor, #194880)`;
680
-
681
- return [
682
- srOnlyStyle,
683
- css`
684
- section#more-facets {
685
- overflow: auto;
686
- padding: 10px; /* leaves room for scroll bar to appear without overlaying on content */
687
- --facetsColumnCount: 3;
688
- }
689
-
690
- /* Horizontal scroll mode: fixed column height for horizontal overflow */
691
- section#more-facets.horizontal-scroll-mode {
692
- --facetsColumnCount: 3;
693
- --facetsMaxHeight: 280px;
694
- }
695
-
696
- /* Pagination mode: set height for proper column layout with vertical scroll */
697
- section#more-facets.pagination-mode {
698
- --facetsColumnCount: 3;
699
- --facetsMaxHeight: 280px; /* Columns need height constraint to flow properly */
700
- }
701
- .header-content .title {
702
- display: block;
703
- text-align: left;
704
- font-size: 1.8rem;
705
- padding: 0 10px;
706
- font-weight: bold;
707
- }
708
-
709
- .sort-label {
710
- margin-left: 20px;
711
- font-size: 1.3rem;
712
- }
713
-
714
- .sort-toggle {
715
- font-weight: normal;
716
- }
717
-
718
- .filter-label {
719
- margin-left: 20px;
720
- font-size: 1.3rem;
721
- }
722
-
723
- .filter-input {
724
- font-size: 1.3rem;
725
- padding: 4px 8px;
726
- border: 1px solid #ccc;
727
- border-radius: 4px;
728
- margin-left: 5px;
729
- width: 150px;
730
- font-family: inherit;
731
- }
732
-
733
- .filter-input:focus {
734
- outline: 2px solid #194880;
735
- outline-offset: 1px;
736
- border-color: #194880;
737
- }
738
-
739
- .empty-results {
740
- text-align: center;
741
- padding: 40px 20px;
742
- color: #666;
743
- }
744
-
745
- .empty-results .hint {
746
- font-size: 1.1rem;
747
- margin-top: 10px;
748
- }
749
-
750
- .facets-content {
751
- font-size: 1.2rem;
752
- max-height: 300px;
753
- padding: 10px;
754
- /* Force scrollbar to always be visible */
755
- scrollbar-width: thin; /* Firefox */
756
- scrollbar-color: #888 #f1f1f1; /* Firefox - thumb and track colors */
757
- }
758
-
759
- /* Pagination mode: vertical scrolling, allow taller height for multiple columns */
760
- .facets-content.pagination-mode {
761
- overflow-y: auto;
762
- overflow-x: hidden;
763
- max-height: none; /* Remove height constraint to allow columns to flow properly */
764
- height: 300px; /* Fixed height to enable vertical scroll */
765
- }
766
-
767
- /* Horizontal scroll mode: horizontal scrolling only */
768
- .facets-content.horizontal-scroll-mode {
769
- overflow-x: auto;
770
- overflow-y: hidden;
771
- }
772
-
773
- /* Webkit browsers scrollbar styling - always visible */
774
- .facets-content::-webkit-scrollbar {
775
- width: 12px; /* Vertical scrollbar width */
776
- height: 12px; /* Horizontal scrollbar height */
777
- }
778
-
779
- .facets-content::-webkit-scrollbar-track {
780
- background: #f1f1f1;
781
- border-radius: 6px;
782
- }
783
-
784
- .facets-content::-webkit-scrollbar-thumb {
785
- background: #888;
786
- border-radius: 6px;
787
- min-height: 30px; /* Ensure thumb is always visible when scrolling is possible */
788
- }
789
-
790
- .facets-content::-webkit-scrollbar-thumb:hover {
791
- background: #555;
792
- }
793
-
794
- /* Force corner to match track color */
795
- .facets-content::-webkit-scrollbar-corner {
796
- background: #f1f1f1;
797
- }
798
-
799
- .facets-horizontal-container {
800
- display: inline-block;
801
- min-width: 100%;
802
- /* Allow natural width expansion based on content */
803
- width: fit-content;
804
- }
805
- .facets-loader {
806
- --icon-width: 70px;
807
- margin-bottom: 20px;
808
- display: block;
809
- margin-left: auto;
810
- margin-right: auto;
811
- }
812
- .btn {
813
- border: none;
814
- padding: 10px;
815
- margin-bottom: 10px;
816
- width: auto;
817
- border-radius: 4px;
818
- cursor: pointer;
819
- }
820
- .btn-cancel {
821
- background-color: #2c2c2c;
822
- color: white;
823
- }
824
- .btn-submit {
825
- background-color: ${modalSubmitButton};
826
- color: white;
827
- }
828
- .footer {
829
- text-align: center;
830
- margin-top: 10px;
831
- }
832
-
833
- @media (max-width: 560px) {
834
- section#more-facets.horizontal-scroll-mode,
835
- section#more-facets.pagination-mode {
836
- max-height: 450px;
837
- --facetsColumnCount: 1; /* Single column on mobile */
838
- --facetsMaxHeight: none; /* Remove fixed height for vertical scrolling */
839
- }
840
- /* On mobile, always use vertical scrolling regardless of mode */
841
- .facets-content,
842
- .facets-content.pagination-mode,
843
- .facets-content.horizontal-scroll-mode {
844
- overflow-y: auto;
845
- overflow-x: hidden;
846
- height: 300px;
847
- }
848
- .filter-input {
849
- width: 120px;
850
- font-size: 1.2rem;
851
- }
852
- }
853
- `,
854
- ];
855
- }
856
- }
1
+ import {
2
+ css,
3
+ CSSResultGroup,
4
+ html,
5
+ LitElement,
6
+ nothing,
7
+ PropertyValues,
8
+ TemplateResult,
9
+ } from 'lit';
10
+ import { customElement, property, state } from 'lit/decorators.js';
11
+ import {
12
+ Aggregation,
13
+ Bucket,
14
+ SearchServiceInterface,
15
+ SearchParams,
16
+ SearchType,
17
+ AggregationSortType,
18
+ FilterMap,
19
+ PageType,
20
+ } from '@internetarchive/search-service';
21
+ import type { ModalManagerInterface } from '@internetarchive/modal-manager';
22
+ import type { AnalyticsManagerInterface } from '@internetarchive/analytics-manager';
23
+ import { msg } from '@lit/localize';
24
+ import {
25
+ SelectedFacets,
26
+ FacetGroup,
27
+ FacetBucket,
28
+ FacetOption,
29
+ facetTitles,
30
+ suppressedCollections,
31
+ valueFacetSort,
32
+ defaultFacetSort,
33
+ getDefaultSelectedFacets,
34
+ FacetEventDetails,
35
+ } from '../models';
36
+ import type {
37
+ CollectionTitles,
38
+ PageSpecifierParams,
39
+ TVChannelAliases,
40
+ } from '../data-source/models';
41
+ import '@internetarchive/elements/ia-status-indicator/ia-status-indicator';
42
+ import './more-facets-pagination';
43
+ import './facets-template';
44
+ import {
45
+ analyticsActions,
46
+ analyticsCategories,
47
+ } from '../utils/analytics-events';
48
+ import './toggle-switch';
49
+ import { srOnlyStyle } from '../styles/sr-only';
50
+ import {
51
+ mergeSelectedFacets,
52
+ sortBucketsBySelectionState,
53
+ updateSelectedFacetBucket,
54
+ } from '../utils/facet-utils';
55
+ import {
56
+ MORE_FACETS__DEFAULT_PAGE_SIZE,
57
+ MORE_FACETS__MAX_AGGREGATIONS,
58
+ } from './models';
59
+
60
+ @customElement('more-facets-content')
61
+ export class MoreFacetsContent extends LitElement {
62
+ @property({ type: String }) facetKey?: FacetOption;
63
+
64
+ @property({ type: String }) query?: string;
65
+
66
+ @property({ type: Array }) identifiers?: string[];
67
+
68
+ @property({ type: Object }) filterMap?: FilterMap;
69
+
70
+ @property({ type: Number }) searchType?: SearchType;
71
+
72
+ @property({ type: Object }) pageSpecifierParams?: PageSpecifierParams;
73
+
74
+ @property({ type: Object })
75
+ collectionTitles?: CollectionTitles;
76
+
77
+ @property({ type: Object })
78
+ tvChannelAliases?: TVChannelAliases;
79
+
80
+ /**
81
+ * Maximum number of facets to show per page within the modal.
82
+ */
83
+ @property({ type: Number }) facetsPerPage = MORE_FACETS__DEFAULT_PAGE_SIZE;
84
+
85
+ /**
86
+ * Whether we are waiting for facet data to load.
87
+ * We begin with this set to true so that we show an initial loading indicator.
88
+ */
89
+ @property({ type: Boolean }) facetsLoading = true;
90
+
91
+ /**
92
+ * The set of pre-existing facet selections (including both selected & negated facets).
93
+ */
94
+ @property({ type: Object }) selectedFacets?: SelectedFacets;
95
+
96
+ @property({ type: Number }) sortedBy: AggregationSortType =
97
+ AggregationSortType.COUNT;
98
+
99
+ @property({ type: Boolean }) isTvSearch = false;
100
+
101
+ @property({ type: Object }) modalManager?: ModalManagerInterface;
102
+
103
+ @property({ type: Object }) searchService?: SearchServiceInterface;
104
+
105
+ @property({ type: Object, attribute: false })
106
+ analyticsHandler?: AnalyticsManagerInterface;
107
+
108
+ /**
109
+ * The full set of aggregations received from the search service
110
+ */
111
+ @state() private aggregations?: Record<string, Aggregation>;
112
+
113
+ /**
114
+ * A FacetGroup storing the full set of facet buckets to be shown on the dialog.
115
+ */
116
+ @state() private facetGroup?: FacetGroup;
117
+
118
+ /**
119
+ * An object holding any changes the patron has made to their facet selections
120
+ * within the modal dialog but which they have not yet applied. These are
121
+ * eventually merged into the existing `selectedFacets` when the patron applies
122
+ * their changes, or discarded if they cancel/close the dialog.
123
+ */
124
+ @state() private unappliedFacetChanges: SelectedFacets =
125
+ getDefaultSelectedFacets();
126
+
127
+ /**
128
+ * Which page of facets we are showing.
129
+ */
130
+ @state() private pageNumber = 1;
131
+
132
+ willUpdate(changed: PropertyValues): void {
133
+ if (
134
+ changed.has('aggregations') ||
135
+ changed.has('facetsPerPage') ||
136
+ changed.has('sortedBy') ||
137
+ changed.has('selectedFacets') ||
138
+ changed.has('unappliedFacetChanges')
139
+ ) {
140
+ // Convert the merged selected facets & aggregations into a facet group, and
141
+ // store it for reuse across pages.
142
+ this.facetGroup = this.mergedFacets;
143
+ }
144
+ }
145
+
146
+ updated(changed: PropertyValues): void {
147
+ // If any of the search properties change, it triggers a facet fetch
148
+ if (
149
+ changed.has('facetKey') ||
150
+ changed.has('query') ||
151
+ changed.has('searchType') ||
152
+ changed.has('filterMap')
153
+ ) {
154
+ this.facetsLoading = true;
155
+ this.pageNumber = 1;
156
+ this.sortedBy = defaultFacetSort[this.facetKey as FacetOption];
157
+
158
+ this.updateSpecificFacets();
159
+ }
160
+ }
161
+
162
+ firstUpdated(): void {
163
+ this.setupEscapeListeners();
164
+ }
165
+
166
+ /**
167
+ * Close more facets modal on Escape click
168
+ */
169
+ private setupEscapeListeners() {
170
+ if (this.modalManager) {
171
+ document.addEventListener('keydown', (e: KeyboardEvent) => {
172
+ if (e.key === 'Escape') {
173
+ this.modalManager?.closeModal();
174
+ }
175
+ });
176
+ } else {
177
+ document.removeEventListener('keydown', () => {});
178
+ }
179
+ }
180
+
181
+ /**
182
+ * Whether facet requests are for the search_results page type (either defaulted or explicitly).
183
+ */
184
+ private get isSearchResultsPage(): boolean {
185
+ // Default page type is search_results when none is specified, so we check
186
+ // for undefined as well.
187
+ const pageType: PageType | undefined = this.pageSpecifierParams?.pageType;
188
+ return pageType === undefined || pageType === 'search_results';
189
+ }
190
+
191
+ /**
192
+ * Get specific facets data from search-service API based of currently query params
193
+ * - this.aggregations - hold result of search service and being used for further processing.
194
+ */
195
+ async updateSpecificFacets(): Promise<void> {
196
+ if (!this.facetKey) return; // Can't fetch facets if we don't know what type of facets we need!
197
+
198
+ const trimmedQuery = this.query?.trim();
199
+ if (!trimmedQuery && this.isSearchResultsPage) return; // The search page _requires_ a query
200
+
201
+ const aggregations = {
202
+ simpleParams: [this.facetKey],
203
+ };
204
+ const aggregationsSize = MORE_FACETS__MAX_AGGREGATIONS; // Only request the 10K highest-count facets
205
+
206
+ const params: SearchParams = {
207
+ ...this.pageSpecifierParams,
208
+ query: trimmedQuery || '',
209
+ identifiers: this.identifiers,
210
+ filters: this.filterMap,
211
+ aggregations,
212
+ aggregationsSize,
213
+ rows: 0, // todo - do we want server-side pagination with offset/page/limit flag?
214
+ };
215
+
216
+ const results = await this.searchService?.search(params, this.searchType);
217
+ this.aggregations = results?.success?.response.aggregations;
218
+ this.facetsLoading = false;
219
+
220
+ const collectionTitles = results?.success?.response?.collectionTitles;
221
+ if (collectionTitles) {
222
+ for (const [id, title] of Object.entries(collectionTitles)) {
223
+ this.collectionTitles?.set(id, title);
224
+ }
225
+ }
226
+ }
227
+
228
+ /**
229
+ * Handler for page number changes from the pagination widget.
230
+ */
231
+ private pageNumberClicked(e: CustomEvent<{ page: number }>) {
232
+ const page = e?.detail?.page;
233
+ if (page) {
234
+ this.pageNumber = Number(page);
235
+ }
236
+
237
+ this.analyticsHandler?.sendEvent({
238
+ category: analyticsCategories.default,
239
+ action: analyticsActions.moreFacetsPageChange,
240
+ label: `${this.pageNumber}`,
241
+ });
242
+ }
243
+
244
+ /**
245
+ * Combines the selected facets with the aggregations to create a single list of facets
246
+ */
247
+ private get mergedFacets(): FacetGroup | undefined {
248
+ if (!this.facetKey || !this.selectedFacets) return undefined;
249
+
250
+ const { selectedFacetGroup, aggregationFacetGroup } = this;
251
+
252
+ // If we don't have any aggregations, then there is nothing to show yet
253
+ if (!aggregationFacetGroup) return undefined;
254
+
255
+ // Start with either the selected group if we have one, or the aggregate group otherwise
256
+ const facetGroup = { ...(selectedFacetGroup ?? aggregationFacetGroup) };
257
+
258
+ // Attach the counts to the selected buckets
259
+ const bucketsWithCount =
260
+ selectedFacetGroup?.buckets.map(bucket => {
261
+ const selectedBucket = aggregationFacetGroup.buckets.find(
262
+ b => b.key === bucket.key,
263
+ );
264
+ return selectedBucket
265
+ ? {
266
+ ...bucket,
267
+ count: selectedBucket.count,
268
+ }
269
+ : bucket;
270
+ }) ?? [];
271
+
272
+ // Sort the buckets by selection state
273
+ // We do this *prior* to considering unapplied selections, because we want the facets
274
+ // to remain in position when they are selected/unselected, rather than re-sort themselves.
275
+ sortBucketsBySelectionState(bucketsWithCount, this.sortedBy);
276
+
277
+ // Append any additional buckets that were not selected
278
+ aggregationFacetGroup.buckets.forEach(bucket => {
279
+ const existingBucket = selectedFacetGroup?.buckets.find(
280
+ b => b.key === bucket.key,
281
+ );
282
+ if (existingBucket) return;
283
+ bucketsWithCount.push(bucket);
284
+ });
285
+
286
+ // Apply any unapplied selections that appear on this page
287
+ const unappliedBuckets = this.unappliedFacetChanges[this.facetKey];
288
+ for (const [index, bucket] of bucketsWithCount.entries()) {
289
+ const unappliedBucket = unappliedBuckets?.[bucket.key];
290
+ if (unappliedBucket) {
291
+ bucketsWithCount[index] = { ...unappliedBucket };
292
+ }
293
+ }
294
+
295
+ // For TV creator facets, uppercase the display text
296
+ if (this.facetKey === 'creator' && this.isTvSearch) {
297
+ bucketsWithCount.forEach(b => {
298
+ b.displayText = (b.displayText ?? b.key)?.toLocaleUpperCase();
299
+
300
+ const channelLabel = this.tvChannelAliases?.get(b.displayText);
301
+ if (channelLabel && channelLabel !== b.displayText) {
302
+ b.extraNote = `(${channelLabel})`;
303
+ }
304
+ });
305
+ }
306
+
307
+ facetGroup.buckets = bucketsWithCount;
308
+ return facetGroup;
309
+ }
310
+
311
+ /**
312
+ * Converts the selected facets for the current facet key to a `FacetGroup`,
313
+ * which is easier to work with.
314
+ */
315
+ private get selectedFacetGroup(): FacetGroup | undefined {
316
+ if (!this.selectedFacets || !this.facetKey) return undefined;
317
+
318
+ const selectedFacetsForKey = this.selectedFacets[this.facetKey];
319
+ if (!selectedFacetsForKey) return undefined;
320
+
321
+ const facetGroupTitle = facetTitles[this.facetKey];
322
+
323
+ const buckets: FacetBucket[] = Object.entries(selectedFacetsForKey).map(
324
+ ([value, data]) => {
325
+ const displayText: string = value;
326
+ return {
327
+ displayText,
328
+ key: value,
329
+ count: data?.count,
330
+ state: data?.state,
331
+ };
332
+ },
333
+ );
334
+
335
+ return {
336
+ title: facetGroupTitle,
337
+ key: this.facetKey,
338
+ buckets,
339
+ };
340
+ }
341
+
342
+ /**
343
+ * Converts the raw `aggregations` for the current facet key to a `FacetGroup`,
344
+ * which is easier to work with.
345
+ */
346
+ private get aggregationFacetGroup(): FacetGroup | undefined {
347
+ if (!this.aggregations || !this.facetKey) return undefined;
348
+
349
+ const currentAggregation = this.aggregations[this.facetKey];
350
+ if (!currentAggregation) return undefined;
351
+
352
+ const facetGroupTitle = facetTitles[this.facetKey];
353
+
354
+ // Order the facets according to the current sort option
355
+ let sortedBuckets = currentAggregation.getSortedBuckets(
356
+ this.sortedBy,
357
+ ) as Bucket[];
358
+
359
+ if (this.facetKey === 'collection') {
360
+ // we are not showing fav- collections or certain deemphasized collections in facets
361
+ sortedBuckets = sortedBuckets?.filter(bucket => {
362
+ const bucketKey = bucket?.key?.toString();
363
+ return (
364
+ !suppressedCollections[bucketKey] && !bucketKey?.startsWith('fav-')
365
+ );
366
+ });
367
+ }
368
+
369
+ // Construct the array of facet buckets from the aggregation buckets
370
+ const facetBuckets: FacetBucket[] = sortedBuckets.map(bucket => {
371
+ const bucketKeyStr = `${bucket.key}`;
372
+ return {
373
+ displayText: `${bucketKeyStr}`,
374
+ key: `${bucketKeyStr}`,
375
+ count: bucket.doc_count,
376
+ state: 'none',
377
+ };
378
+ });
379
+
380
+ return {
381
+ title: facetGroupTitle,
382
+ key: this.facetKey,
383
+ buckets: facetBuckets,
384
+ };
385
+ }
386
+
387
+ /**
388
+ * Returns a FacetGroup representing only the current page of facet buckets to show.
389
+ */
390
+ private get facetGroupForCurrentPage(): FacetGroup | undefined {
391
+ const { facetGroup } = this;
392
+ if (!facetGroup) return undefined;
393
+
394
+ // Slice out only the current page of facet buckets
395
+ const firstBucketIndexOnPage = (this.pageNumber - 1) * this.facetsPerPage;
396
+ const truncatedBuckets = facetGroup.buckets.slice(
397
+ firstBucketIndexOnPage,
398
+ firstBucketIndexOnPage + this.facetsPerPage,
399
+ );
400
+
401
+ return {
402
+ ...facetGroup,
403
+ buckets: truncatedBuckets,
404
+ };
405
+ }
406
+
407
+ private get moreFacetsTemplate(): TemplateResult {
408
+ return html`
409
+ <facets-template
410
+ .facetGroup=${this.facetGroupForCurrentPage}
411
+ .selectedFacets=${this.selectedFacets}
412
+ .collectionTitles=${this.collectionTitles}
413
+ @facetClick=${(e: CustomEvent<FacetEventDetails>) => {
414
+ if (this.facetKey) {
415
+ this.unappliedFacetChanges = updateSelectedFacetBucket(
416
+ this.unappliedFacetChanges,
417
+ this.facetKey,
418
+ e.detail.bucket,
419
+ );
420
+ }
421
+ }}
422
+ ></facets-template>
423
+ `;
424
+ }
425
+
426
+ private get loaderTemplate(): TemplateResult {
427
+ return html`
428
+ <ia-status-indicator
429
+ class="facets-loader"
430
+ mode="loading"
431
+ ></ia-status-indicator>
432
+ `;
433
+ }
434
+
435
+ /**
436
+ * How many pages of facets to show in the modal pagination widget
437
+ */
438
+ private get paginationSize(): number {
439
+ if (!this.aggregations || !this.facetKey) return 0;
440
+
441
+ // Calculate the appropriate number of pages to show in the modal pagination widget
442
+ const length = this.aggregations[this.facetKey]?.buckets.length;
443
+ return Math.ceil(length / this.facetsPerPage);
444
+ }
445
+
446
+ // render pagination if more then 1 page
447
+ private get facetsPaginationTemplate() {
448
+ return this.paginationSize > 1
449
+ ? html`<more-facets-pagination
450
+ .size=${this.paginationSize}
451
+ .currentPage=${1}
452
+ @pageNumberClicked=${this.pageNumberClicked}
453
+ ></more-facets-pagination>`
454
+ : nothing;
455
+ }
456
+
457
+ private get footerTemplate() {
458
+ if (this.paginationSize > 0) {
459
+ return html`${this.facetsPaginationTemplate}
460
+ <div class="footer">
461
+ <button
462
+ class="btn btn-cancel"
463
+ type="button"
464
+ @click=${this.cancelClick}
465
+ >
466
+ Cancel
467
+ </button>
468
+ <button
469
+ class="btn btn-submit"
470
+ type="button"
471
+ @click=${this.applySearchFacetsClicked}
472
+ >
473
+ Apply filters
474
+ </button>
475
+ </div> `;
476
+ }
477
+
478
+ return nothing;
479
+ }
480
+
481
+ private sortFacetAggregation(facetSortType: AggregationSortType) {
482
+ this.sortedBy = facetSortType;
483
+ this.dispatchEvent(
484
+ new CustomEvent('sortedFacets', { detail: this.sortedBy }),
485
+ );
486
+ }
487
+
488
+ private get modalHeaderTemplate(): TemplateResult {
489
+ const facetSort =
490
+ this.sortedBy ?? defaultFacetSort[this.facetKey as FacetOption];
491
+ const defaultSwitchSide =
492
+ facetSort === AggregationSortType.COUNT ? 'left' : 'right';
493
+
494
+ return html`<span class="sr-only">${msg('More facets for:')}</span>
495
+ <span class="title">
496
+ ${this.facetGroup?.title}
497
+
498
+ <label class="sort-label">${msg('Sort by:')}</label>
499
+ ${this.facetKey
500
+ ? html`<toggle-switch
501
+ class="sort-toggle"
502
+ leftValue=${AggregationSortType.COUNT}
503
+ leftLabel="Count"
504
+ rightValue=${valueFacetSort[this.facetKey]}
505
+ .rightLabel=${this.facetGroup?.title}
506
+ side=${defaultSwitchSide}
507
+ @change=${(e: CustomEvent<string>) => {
508
+ this.sortFacetAggregation(
509
+ Number(e.detail) as AggregationSortType,
510
+ );
511
+ }}
512
+ ></toggle-switch>`
513
+ : nothing}
514
+ </span>`;
515
+ }
516
+
517
+ render() {
518
+ return html`
519
+ ${this.facetsLoading
520
+ ? this.loaderTemplate
521
+ : html`
522
+ <section id="more-facets">
523
+ <div class="header-content">${this.modalHeaderTemplate}</div>
524
+ <div class="facets-content">${this.moreFacetsTemplate}</div>
525
+ ${this.footerTemplate}
526
+ </section>
527
+ `}
528
+ `;
529
+ }
530
+
531
+ private applySearchFacetsClicked() {
532
+ const mergedSelections = mergeSelectedFacets(
533
+ this.selectedFacets,
534
+ this.unappliedFacetChanges,
535
+ );
536
+
537
+ const event = new CustomEvent<SelectedFacets>('facetsChanged', {
538
+ detail: mergedSelections,
539
+ bubbles: true,
540
+ composed: true,
541
+ });
542
+ this.dispatchEvent(event);
543
+
544
+ // Reset the unapplied changes back to default, now that they have been applied
545
+ this.unappliedFacetChanges = getDefaultSelectedFacets();
546
+
547
+ this.modalManager?.closeModal();
548
+ this.analyticsHandler?.sendEvent({
549
+ category: analyticsCategories.default,
550
+ action: `${analyticsActions.applyMoreFacetsModal}`,
551
+ label: `${this.facetKey}`,
552
+ });
553
+ }
554
+
555
+ private cancelClick() {
556
+ // Reset the unapplied changes back to default
557
+ this.unappliedFacetChanges = getDefaultSelectedFacets();
558
+
559
+ this.modalManager?.closeModal();
560
+ this.analyticsHandler?.sendEvent({
561
+ category: analyticsCategories.default,
562
+ action: analyticsActions.closeMoreFacetsModal,
563
+ label: `${this.facetKey}`,
564
+ });
565
+ }
566
+
567
+ static get styles(): CSSResultGroup {
568
+ const modalSubmitButton = css`var(--primaryButtonBGColor, #194880)`;
569
+
570
+ return [
571
+ srOnlyStyle,
572
+ css`
573
+ section#more-facets {
574
+ overflow: auto;
575
+ padding: 10px; /* leaves room for scroll bar to appear without overlaying on content */
576
+ --facetsColumnCount: 3;
577
+ }
578
+ .header-content .title {
579
+ display: block;
580
+ text-align: left;
581
+ font-size: 1.8rem;
582
+ padding: 0 10px;
583
+ font-weight: bold;
584
+ }
585
+
586
+ .sort-label {
587
+ margin-left: 20px;
588
+ font-size: 1.3rem;
589
+ }
590
+
591
+ .sort-toggle {
592
+ font-weight: normal;
593
+ }
594
+
595
+ .facets-content {
596
+ font-size: 1.2rem;
597
+ max-height: 300px;
598
+ overflow: auto;
599
+ padding: 10px;
600
+ }
601
+ .facets-loader {
602
+ --icon-width: 70px;
603
+ margin-bottom: 20px;
604
+ display: block;
605
+ margin-left: auto;
606
+ margin-right: auto;
607
+ }
608
+ .btn {
609
+ border: none;
610
+ padding: 10px;
611
+ margin-bottom: 10px;
612
+ width: auto;
613
+ border-radius: 4px;
614
+ cursor: pointer;
615
+ }
616
+ .btn-cancel {
617
+ background-color: #2c2c2c;
618
+ color: white;
619
+ }
620
+ .btn-submit {
621
+ background-color: ${modalSubmitButton};
622
+ color: white;
623
+ }
624
+ .footer {
625
+ text-align: center;
626
+ margin-top: 10px;
627
+ }
628
+
629
+ @media (max-width: 560px) {
630
+ section#more-facets {
631
+ max-height: 450px;
632
+ --facetsColumnCount: 1;
633
+ }
634
+ .facets-content {
635
+ overflow-y: auto;
636
+ height: 300px;
637
+ }
638
+ }
639
+ `,
640
+ ];
641
+ }
642
+ }