@internetarchive/collection-browser 0.3.1-alpha.3 → 0.3.2-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (30) hide show
  1. package/dist/src/collection-browser.d.ts +6 -0
  2. package/dist/src/collection-browser.js +346 -341
  3. package/dist/src/collection-browser.js.map +1 -1
  4. package/dist/src/collection-facets/facets-template.js +150 -150
  5. package/dist/src/collection-facets/facets-template.js.map +1 -1
  6. package/dist/src/collection-facets/more-facets-content.js +134 -134
  7. package/dist/src/collection-facets/more-facets-content.js.map +1 -1
  8. package/dist/src/collection-facets.d.ts +2 -0
  9. package/dist/src/collection-facets.js +158 -147
  10. package/dist/src/collection-facets.js.map +1 -1
  11. package/dist/src/models.js.map +1 -1
  12. package/dist/src/restoration-state-handler.js.map +1 -1
  13. package/dist/src/tiles/list/tile-list.js +204 -204
  14. package/dist/src/tiles/list/tile-list.js.map +1 -1
  15. package/dist/src/utils/format-count.js.map +1 -1
  16. package/dist/test/collection-browser.test.js +26 -26
  17. package/dist/test/collection-browser.test.js.map +1 -1
  18. package/dist/test/collection-facets.test.js +2 -2
  19. package/dist/test/collection-facets.test.js.map +1 -1
  20. package/package.json +1 -1
  21. package/src/collection-browser.ts +1539 -1530
  22. package/src/collection-facets/facets-template.ts +294 -294
  23. package/src/collection-facets/more-facets-content.ts +518 -518
  24. package/src/collection-facets.ts +582 -569
  25. package/src/models.ts +216 -216
  26. package/src/restoration-state-handler.ts +302 -302
  27. package/src/tiles/list/tile-list.ts +509 -509
  28. package/src/utils/format-count.ts +96 -96
  29. package/test/collection-browser.test.ts +490 -490
  30. package/test/collection-facets.test.ts +510 -510
@@ -1,569 +1,582 @@
1
- /* eslint-disable import/no-duplicates */
2
- import {
3
- css,
4
- html,
5
- LitElement,
6
- PropertyValues,
7
- nothing,
8
- TemplateResult,
9
- } from 'lit';
10
- import { customElement, property, state } from 'lit/decorators.js';
11
- import {
12
- Aggregation,
13
- Bucket,
14
- SearchServiceInterface,
15
- SearchType,
16
- } from '@internetarchive/search-service';
17
- import '@internetarchive/histogram-date-range';
18
- import '@internetarchive/feature-feedback';
19
- import '@internetarchive/collection-name-cache';
20
- import type { CollectionNameCacheInterface } from '@internetarchive/collection-name-cache';
21
- import {
22
- ModalConfig,
23
- ModalManagerInterface,
24
- } from '@internetarchive/modal-manager';
25
- import chevronIcon from './assets/img/icons/chevron';
26
- import {
27
- FacetOption,
28
- SelectedFacets,
29
- FacetGroup,
30
- FacetBucket,
31
- facetDisplayOrder,
32
- facetTitles,
33
- lendingFacetDisplayNames,
34
- lendingFacetKeysVisibility,
35
- LendingFacetKey,
36
- } from './models';
37
- import type { LanguageCodeHandlerInterface } from './language-code-handler/language-code-handler';
38
- import './collection-facets/more-facets-content';
39
- import './collection-facets/facets-template';
40
-
41
- @customElement('collection-facets')
42
- export class CollectionFacets extends LitElement {
43
- @property({ type: Object }) searchService?: SearchServiceInterface;
44
-
45
- @property({ type: String }) searchType?: SearchType;
46
-
47
- @property({ type: Object }) aggregations?: Record<string, Aggregation>;
48
-
49
- @property({ type: Object }) fullYearsHistogramAggregation?: Aggregation;
50
-
51
- @property({ type: String }) minSelectedDate?: string;
52
-
53
- @property({ type: String }) maxSelectedDate?: string;
54
-
55
- @property({ type: Boolean }) facetsLoading = false;
56
-
57
- @property({ type: Boolean }) fullYearAggregationLoading = false;
58
-
59
- @property({ type: Object }) selectedFacets?: SelectedFacets;
60
-
61
- @property({ type: Boolean }) collapsableFacets = false;
62
-
63
- @property({ type: Boolean }) showHistogramDatePicker = false;
64
-
65
- @property({ type: String }) fullQuery?: string;
66
-
67
- @property({ type: Object }) modalManager?: ModalManagerInterface;
68
-
69
- @property({ type: Object })
70
- languageCodeHandler?: LanguageCodeHandlerInterface;
71
-
72
- @property({ type: Object })
73
- collectionNameCache?: CollectionNameCacheInterface;
74
-
75
- /** Fires when a facet is clicked */
76
- @property({ type: Function }) onFacetClick?: (
77
- name: FacetOption,
78
- facetChecked: boolean,
79
- negative: boolean
80
- ) => void;
81
-
82
- @state() openFacets: Record<FacetOption, boolean> = {
83
- subject: false,
84
- lending: false,
85
- mediatype: false,
86
- language: false,
87
- creator: false,
88
- collection: false,
89
- year: false,
90
- };
91
-
92
- @property({ type: Object, attribute: false })
93
-
94
- /**
95
- * render number of facet items
96
- */
97
- private allowedFacetCount = 6;
98
-
99
- render() {
100
- return html`
101
- <div id="container" class="${this.facetsLoading ? 'loading' : ''}">
102
- ${this.showHistogramDatePicker && this.fullYearsHistogramAggregation
103
- ? html`
104
- <div class="facet-group">
105
- <h1>Year Published <feature-feedback></feature-feedback></h1>
106
- ${this.histogramTemplate}
107
- </div>
108
- `
109
- : nothing}
110
- ${this.mergedFacets.map(facetGroup =>
111
- this.getFacetGroupTemplate(facetGroup)
112
- )}
113
- </div>
114
- `;
115
- }
116
-
117
- updated(changed: PropertyValues) {
118
- if (changed.has('selectedFacets')) {
119
- this.dispatchFacetsChangedEvent();
120
- }
121
- }
122
-
123
- // TODO: want to fire analytics?
124
- private dispatchFacetsChangedEvent() {
125
- const event = new CustomEvent<SelectedFacets>('facetsChanged', {
126
- detail: this.selectedFacets,
127
- });
128
- this.dispatchEvent(event);
129
- }
130
-
131
- private get currentYearsHistogramAggregation(): Aggregation | undefined {
132
- return this.aggregations?.year_histogram;
133
- }
134
-
135
- private get histogramTemplate() {
136
- const { fullYearsHistogramAggregation } = this;
137
- return html`
138
- <histogram-date-range
139
- .minDate=${fullYearsHistogramAggregation?.first_bucket_key}
140
- .maxDate=${fullYearsHistogramAggregation?.last_bucket_key}
141
- .minSelectedDate=${this.minSelectedDate}
142
- .maxSelectedDate=${this.maxSelectedDate}
143
- .updateDelay=${100}
144
- missingDataMessage="..."
145
- .width=${180}
146
- .bins=${fullYearsHistogramAggregation?.buckets as number[]}
147
- @histogramDateRangeUpdated=${this.histogramDateRangeUpdated}
148
- ></histogram-date-range>
149
- `;
150
- }
151
-
152
- private histogramDateRangeUpdated(
153
- e: CustomEvent<{
154
- minDate: string;
155
- maxDate: string;
156
- }>
157
- ) {
158
- const { minDate, maxDate } = e.detail;
159
- const event = new CustomEvent('histogramDateRangeUpdated', {
160
- detail: { minDate, maxDate },
161
- });
162
- this.dispatchEvent(event);
163
- }
164
-
165
- /**
166
- * Combines the selected facets with the aggregations to create a single list of facets
167
- */
168
- private get mergedFacets(): FacetGroup[] {
169
- const facetGroups: FacetGroup[] = [];
170
-
171
- facetDisplayOrder.forEach(facetKey => {
172
- const selectedFacetGroup = this.selectedFacetGroups.find(
173
- group => group.key === facetKey
174
- );
175
- const aggregateFacetGroup = this.aggregationFacetGroups.find(
176
- group => group.key === facetKey
177
- );
178
-
179
- // if the user selected a facet, but it's not in the aggregation, we add it as-is
180
- if (selectedFacetGroup && !aggregateFacetGroup) {
181
- facetGroups.push(selectedFacetGroup);
182
- return;
183
- }
184
-
185
- // if we don't have an aggregate facet group, don't add this to the list
186
- if (!aggregateFacetGroup) return;
187
-
188
- // start with either the selected group if we have one, or the aggregate group
189
- const facetGroup = selectedFacetGroup ?? aggregateFacetGroup;
190
-
191
- // attach the counts to the selected buckets
192
- let bucketsWithCount =
193
- selectedFacetGroup?.buckets.map(bucket => {
194
- const selectedBucket = aggregateFacetGroup.buckets.find(
195
- b => b.key === bucket.key
196
- );
197
- return selectedBucket
198
- ? {
199
- ...bucket,
200
- count: selectedBucket.count,
201
- }
202
- : bucket;
203
- }) ?? [];
204
-
205
- // append any additional buckets that were not selected
206
- aggregateFacetGroup.buckets.forEach(bucket => {
207
- const existingBucket = bucketsWithCount.find(b => b.key === bucket.key);
208
- if (existingBucket) return;
209
- bucketsWithCount.push(bucket);
210
- });
211
-
212
- // For lending facets, only include a specific subset of buckets
213
- if (facetKey === 'lending') {
214
- bucketsWithCount = bucketsWithCount.filter(
215
- bucket => lendingFacetKeysVisibility[bucket.key as LendingFacetKey]
216
- );
217
- }
218
-
219
- /**
220
- * render limited facet items on page facet area
221
- *
222
- * - by-default we are showing 6 items
223
- * - additionally want to show all items (selected/suppressed) in page facet area
224
- */
225
- let allowedFacetCount = Object.keys(
226
- (selectedFacetGroup?.buckets as []) || []
227
- )?.length;
228
- if (allowedFacetCount < this.allowedFacetCount) {
229
- allowedFacetCount = this.allowedFacetCount; // splice start index from 0th
230
- }
231
-
232
- // splice how many items we want to show in page facet area
233
- facetGroup.buckets = bucketsWithCount.splice(0, allowedFacetCount);
234
-
235
- facetGroups.push(facetGroup);
236
- });
237
-
238
- return facetGroups;
239
- }
240
-
241
- /**
242
- * Converts the selected facets to a `FacetGroup` array,
243
- * which is easier to work with
244
- */
245
- private get selectedFacetGroups(): FacetGroup[] {
246
- if (!this.selectedFacets) return [];
247
-
248
- const facetGroups: FacetGroup[] = Object.entries(this.selectedFacets).map(
249
- ([key, selectedFacets]) => {
250
- const option = key as FacetOption;
251
- const title = facetTitles[option];
252
-
253
- const buckets: FacetBucket[] = Object.entries(selectedFacets).map(
254
- ([value, facetData]) => {
255
- let displayText = value;
256
- // for selected languages, we store the language code instead of the
257
- // display name, so look up the name from the mapping
258
- if (option === 'language') {
259
- displayText =
260
- this.languageCodeHandler?.getLanguageNameFromCodeString(
261
- value
262
- ) ?? value;
263
- }
264
- // for lending facets, convert the key to a readable format
265
- if (option === 'lending') {
266
- displayText =
267
- lendingFacetDisplayNames[value as LendingFacetKey] ?? value;
268
- }
269
- return {
270
- displayText,
271
- key: value,
272
- count: facetData.count,
273
- state: facetData.state,
274
- };
275
- }
276
- );
277
-
278
- return {
279
- title,
280
- key: option,
281
- buckets,
282
- };
283
- }
284
- );
285
-
286
- return facetGroups;
287
- }
288
-
289
- /**
290
- * Converts the raw `aggregations` to `FacetGroups`, which are easier to use
291
- */
292
- private get aggregationFacetGroups(): FacetGroup[] {
293
- const facetGroups: FacetGroup[] = [];
294
- Object.entries(this.aggregations ?? []).forEach(([key, buckets]) => {
295
- // the year_histogram data is in a different format so can't be handled here
296
- if (key === 'year_histogram') return;
297
-
298
- const option = key as FacetOption;
299
- const title = facetTitles[option];
300
- if (!title) return;
301
-
302
- const castedBuckets = buckets.buckets as Bucket[];
303
-
304
- // we are not showing fav- items in facets
305
- castedBuckets?.filter(
306
- bucket => bucket?.key?.toString()?.startsWith('fav-') === false
307
- );
308
-
309
- const facetBuckets: FacetBucket[] = castedBuckets.map(bucket => {
310
- let bucketKey = bucket.key;
311
- let displayText = `${bucket.key}`;
312
- // for languages, we need to search by language code instead of the
313
- // display name, which is what we get from the search engine result
314
- if (option === 'language') {
315
- // const languageCodeKey = languageToCodeMap[bucket.key];
316
- bucketKey =
317
- this.languageCodeHandler?.getCodeStringFromLanguageName(
318
- `${bucket.key}`
319
- ) ?? bucket.key;
320
- // bucketKey = languageCodeKey ?? bucket.key;
321
- }
322
- // for lending facets, convert the bucket key to a readable format
323
- if (option === 'lending') {
324
- displayText =
325
- lendingFacetDisplayNames[bucket.key as LendingFacetKey] ??
326
- `${bucket.key}`;
327
- }
328
- return {
329
- displayText,
330
- key: `${bucketKey}`,
331
- count: bucket.doc_count,
332
- state: 'none',
333
- };
334
- });
335
- const group: FacetGroup = {
336
- title,
337
- key: option,
338
- buckets: facetBuckets,
339
- };
340
- facetGroups.push(group);
341
- });
342
- return facetGroups;
343
- }
344
-
345
- /**
346
- * Generate the template for a facet group with a header and the collapsible
347
- * chevron for the mobile view
348
- */
349
- private getFacetGroupTemplate(
350
- facetGroup: FacetGroup
351
- ): TemplateResult | typeof nothing {
352
- if (facetGroup.buckets.length === 0) return nothing;
353
- const { key } = facetGroup;
354
- const isOpen = this.openFacets[key];
355
- const collapser = html`
356
- <span class="collapser ${isOpen ? 'open' : ''}"> ${chevronIcon} </span>
357
- `;
358
-
359
- return html`
360
- <div class="facet-group ${this.collapsableFacets ? 'mobile' : ''}">
361
- <div class="facet-group-header">
362
- <h1
363
- @click=${() => {
364
- const newOpenFacets = { ...this.openFacets };
365
- newOpenFacets[key] = !isOpen;
366
- this.openFacets = newOpenFacets;
367
- }}
368
- @keyup=${() => {
369
- const newOpenFacets = { ...this.openFacets };
370
- newOpenFacets[key] = !isOpen;
371
- this.openFacets = newOpenFacets;
372
- }}
373
- >
374
- ${this.collapsableFacets ? collapser : nothing} ${facetGroup.title}
375
- </h1>
376
- <input
377
- class="sorting-icon"
378
- type="image"
379
- @click=${() => this.showMoreFacetsModal(facetGroup, 'alpha')}
380
- src="https://archive.org/images/filter-count.png"
381
- alt="Sort by alphabetically"
382
- />
383
- </div>
384
- <div class="facet-group-content ${isOpen ? 'open' : ''}">
385
- ${this.getFacetTemplate(facetGroup)}
386
- ${this.searchMoreFacetsLink(facetGroup)}
387
- </div>
388
- </div>
389
- `;
390
- }
391
-
392
- /**
393
- * Generate the More... link button just below the facets group
394
- *
395
- * TODO: want to fire analytics?
396
- */
397
- private searchMoreFacetsLink(
398
- facetGroup: FacetGroup
399
- ): TemplateResult | typeof nothing {
400
- // Don't render More... links for FTS searches
401
- if (this.searchType === SearchType.FULLTEXT) {
402
- return nothing;
403
- }
404
-
405
- // Don't render More... links for lending facets
406
- if (facetGroup.key === 'lending') {
407
- return nothing;
408
- }
409
-
410
- // Don't render More... link if the number of facets < this.allowedFacetCount
411
- if (Object.keys(facetGroup.buckets).length < this.allowedFacetCount) {
412
- return nothing;
413
- }
414
-
415
- return html`<button
416
- class="more-link"
417
- @click=${() => {
418
- this.showMoreFacetsModal(facetGroup, 'count');
419
- this.dispatchEvent(
420
- new CustomEvent('showMoreFacets', { detail: facetGroup.key })
421
- );
422
- }}
423
- >
424
- More...
425
- </button>`;
426
- }
427
-
428
- async showMoreFacetsModal(
429
- facetGroup: FacetGroup,
430
- sortedBy: string
431
- ): Promise<void> {
432
- const facetAggrKey = facetGroup.key;
433
-
434
- const customModalContent = html`
435
- <more-facets-content
436
- .facetKey=${facetGroup.key}
437
- .facetAggregationKey=${facetAggrKey}
438
- .fullQuery=${this.fullQuery}
439
- .modalManager=${this.modalManager}
440
- .searchService=${this.searchService}
441
- .searchType=${this.searchType}
442
- .collectionNameCache=${this.collectionNameCache}
443
- .languageCodeHandler=${this.languageCodeHandler}
444
- .selectedFacets=${this.selectedFacets}
445
- .sortedBy=${sortedBy}
446
- @facetsChanged=${(e: CustomEvent) => {
447
- const event = new CustomEvent<SelectedFacets>('facetsChanged', {
448
- detail: e.detail,
449
- bubbles: true,
450
- composed: true,
451
- });
452
- this.dispatchEvent(event);
453
- }}
454
- >
455
- </more-facets-content>
456
- `;
457
-
458
- const config = new ModalConfig({
459
- bodyColor: '#fff',
460
- headerColor: '#194880',
461
- showHeaderLogo: false,
462
- closeOnBackdropClick: true, // TODO: want to fire analytics
463
- title: html`Select filters`,
464
- });
465
- this.modalManager?.classList.add('more-search-facets');
466
- this.modalManager?.showModal({
467
- config,
468
- customModalContent,
469
- });
470
- }
471
-
472
- /**
473
- * Generate the list template for each bucket in a facet group
474
- */
475
- private getFacetTemplate(facetGroup: FacetGroup): TemplateResult {
476
- return html`
477
- <facets-template
478
- .facetGroup=${facetGroup}
479
- .selectedFacets=${this.selectedFacets}
480
- .renderOn=${'page'}
481
- .collectionNameCache=${this.collectionNameCache}
482
- @selectedFacetsChanged=${(e: CustomEvent) => {
483
- const event = new CustomEvent<SelectedFacets>('facetsChanged', {
484
- detail: e.detail,
485
- bubbles: true,
486
- composed: true,
487
- });
488
- this.dispatchEvent(event);
489
- }}
490
- ></facets-template>
491
- `;
492
- }
493
-
494
- static get styles() {
495
- return css`
496
- #container.loading {
497
- opacity: 0.5;
498
- }
499
-
500
- .collapser {
501
- display: inline-block;
502
- cursor: pointer;
503
- width: 10px;
504
- height: 10px;
505
- }
506
-
507
- .collapser svg {
508
- transition: transform 0.2s ease-in-out;
509
- }
510
-
511
- .collapser.open svg {
512
- transform: rotate(90deg);
513
- }
514
-
515
- .facet-group {
516
- margin-bottom: 2rem;
517
- }
518
-
519
- .facet-group h1 {
520
- margin-bottom: 0.7rem;
521
- }
522
-
523
- .facet-group.mobile h1 {
524
- cursor: pointer;
525
- }
526
-
527
- .facet-group-header {
528
- display: flex;
529
- margin-bottom: 0.7rem;
530
- justify-content: space-between;
531
- border-bottom: 1px solid rgb(232, 232, 232);
532
- }
533
-
534
- .facet-group-content {
535
- transition: max-height 0.2s ease-in-out;
536
- }
537
-
538
- .facet-group.mobile .facet-group-content {
539
- max-height: 0;
540
- overflow: hidden;
541
- }
542
-
543
- .facet-group.mobile .facet-group-content.open {
544
- max-height: 2000px;
545
- }
546
-
547
- h1 {
548
- font-size: 1.4rem;
549
- font-weight: 200
550
- padding-bottom: 3px;
551
- margin: 0;
552
- }
553
-
554
- .more-link {
555
- font-size: 1.2rem;
556
- text-decoration: none;
557
- padding: 0;
558
- background: inherit;
559
- border: 0;
560
- color: blue;
561
- cursor: pointer;
562
- }
563
- .sorting-icon {
564
- height: 15px;
565
- cursor: pointer;
566
- }
567
- `;
568
- }
569
- }
1
+ /* eslint-disable import/no-duplicates */
2
+ import {
3
+ css,
4
+ html,
5
+ LitElement,
6
+ PropertyValues,
7
+ nothing,
8
+ TemplateResult,
9
+ } from 'lit';
10
+ import { customElement, property, state } from 'lit/decorators.js';
11
+ import {
12
+ Aggregation,
13
+ Bucket,
14
+ SearchServiceInterface,
15
+ SearchType,
16
+ } from '@internetarchive/search-service';
17
+ import '@internetarchive/histogram-date-range';
18
+ import '@internetarchive/feature-feedback';
19
+ import '@internetarchive/collection-name-cache';
20
+ import type { CollectionNameCacheInterface } from '@internetarchive/collection-name-cache';
21
+ import {
22
+ ModalConfig,
23
+ ModalManagerInterface,
24
+ } from '@internetarchive/modal-manager';
25
+ import chevronIcon from './assets/img/icons/chevron';
26
+ import {
27
+ FacetOption,
28
+ SelectedFacets,
29
+ FacetGroup,
30
+ FacetBucket,
31
+ facetDisplayOrder,
32
+ facetTitles,
33
+ lendingFacetDisplayNames,
34
+ lendingFacetKeysVisibility,
35
+ LendingFacetKey,
36
+ } from './models';
37
+ import type { LanguageCodeHandlerInterface } from './language-code-handler/language-code-handler';
38
+ import './collection-facets/more-facets-content';
39
+ import './collection-facets/facets-template';
40
+
41
+ @customElement('collection-facets')
42
+ export class CollectionFacets extends LitElement {
43
+ @property({ type: Object }) searchService?: SearchServiceInterface;
44
+
45
+ @property({ type: String }) searchType?: SearchType;
46
+
47
+ @property({ type: Object }) aggregations?: Record<string, Aggregation>;
48
+
49
+ @property({ type: Object }) fullYearsHistogramAggregation?: Aggregation;
50
+
51
+ @property({ type: Number }) previousSearchType?: SearchType;
52
+
53
+ @property({ type: String }) minSelectedDate?: string;
54
+
55
+ @property({ type: String }) maxSelectedDate?: string;
56
+
57
+ @property({ type: Boolean }) facetsLoading = false;
58
+
59
+ @property({ type: Boolean }) fullYearAggregationLoading = false;
60
+
61
+ @property({ type: Object }) selectedFacets?: SelectedFacets;
62
+
63
+ @property({ type: Boolean }) collapsableFacets = false;
64
+
65
+ @property({ type: Boolean }) showHistogramDatePicker = false;
66
+
67
+ @property({ type: String }) fullQuery?: string;
68
+
69
+ @property({ type: Object }) modalManager?: ModalManagerInterface;
70
+
71
+ @property({ type: Object })
72
+ languageCodeHandler?: LanguageCodeHandlerInterface;
73
+
74
+ @property({ type: Object })
75
+ collectionNameCache?: CollectionNameCacheInterface;
76
+
77
+ /** Fires when a facet is clicked */
78
+ @property({ type: Function }) onFacetClick?: (
79
+ name: FacetOption,
80
+ facetChecked: boolean,
81
+ negative: boolean
82
+ ) => void;
83
+
84
+ @state() openFacets: Record<FacetOption, boolean> = {
85
+ subject: false,
86
+ lending: false,
87
+ mediatype: false,
88
+ language: false,
89
+ creator: false,
90
+ collection: false,
91
+ year: false,
92
+ };
93
+
94
+ @property({ type: Object, attribute: false })
95
+
96
+ /**
97
+ * render number of facet items
98
+ */
99
+ private allowedFacetCount = 6;
100
+
101
+ render() {
102
+ return html`
103
+ <div id="container" class="${this.facetsLoading ? 'loading' : ''}">
104
+ ${this.showHistogramDatePicker && this.fullYearsHistogramAggregation
105
+ ? html`
106
+ <div class="facet-group">
107
+ <h1>Year Published <feature-feedback></feature-feedback></h1>
108
+ ${this.histogramTemplate}
109
+ </div>
110
+ `
111
+ : nothing}
112
+ ${this.mergedFacets.map(facetGroup =>
113
+ this.getFacetGroupTemplate(facetGroup)
114
+ )}
115
+ </div>
116
+ `;
117
+ }
118
+
119
+ updated(changed: PropertyValues) {
120
+ if (changed.has('selectedFacets')) {
121
+ this.dispatchFacetsChangedEvent();
122
+ }
123
+ }
124
+
125
+ // TODO: want to fire analytics?
126
+ private dispatchFacetsChangedEvent() {
127
+ const event = new CustomEvent<SelectedFacets>('facetsChanged', {
128
+ detail: this.selectedFacets,
129
+ });
130
+ this.dispatchEvent(event);
131
+ }
132
+
133
+ private get currentYearsHistogramAggregation(): Aggregation | undefined {
134
+ return this.aggregations?.year_histogram;
135
+ }
136
+
137
+ private get histogramTemplate() {
138
+ const { fullYearsHistogramAggregation } = this;
139
+ return html`
140
+ <histogram-date-range
141
+ .minDate=${fullYearsHistogramAggregation?.first_bucket_key}
142
+ .maxDate=${fullYearsHistogramAggregation?.last_bucket_key}
143
+ .minSelectedDate=${this.minSelectedDate}
144
+ .maxSelectedDate=${this.maxSelectedDate}
145
+ .updateDelay=${100}
146
+ missingDataMessage="..."
147
+ .width=${180}
148
+ .bins=${fullYearsHistogramAggregation?.buckets as number[]}
149
+ @histogramDateRangeUpdated=${this.histogramDateRangeUpdated}
150
+ ></histogram-date-range>
151
+ `;
152
+ }
153
+
154
+ private histogramDateRangeUpdated(
155
+ e: CustomEvent<{
156
+ minDate: string;
157
+ maxDate: string;
158
+ }>
159
+ ) {
160
+ const { minDate, maxDate } = e.detail;
161
+ const event = new CustomEvent('histogramDateRangeUpdated', {
162
+ detail: { minDate, maxDate },
163
+ });
164
+ this.dispatchEvent(event);
165
+ }
166
+
167
+ /**
168
+ * Combines the selected facets with the aggregations to create a single list of facets
169
+ */
170
+ private get mergedFacets(): FacetGroup[] {
171
+ const facetGroups: FacetGroup[] = [];
172
+
173
+ facetDisplayOrder.forEach(facetKey => {
174
+ const selectedFacetGroup = this.selectedFacetGroups.find(
175
+ group => group.key === facetKey
176
+ );
177
+ const aggregateFacetGroup = this.aggregationFacetGroups.find(
178
+ group => group.key === facetKey
179
+ );
180
+
181
+ // if the user selected a facet, but it's not in the aggregation, we add it as-is
182
+ if (selectedFacetGroup && !aggregateFacetGroup) {
183
+ facetGroups.push(selectedFacetGroup);
184
+ return;
185
+ }
186
+
187
+ // if we don't have an aggregate facet group, don't add this to the list
188
+ if (!aggregateFacetGroup) return;
189
+
190
+ // start with either the selected group if we have one, or the aggregate group
191
+ const facetGroup = selectedFacetGroup ?? aggregateFacetGroup;
192
+
193
+ // attach the counts to the selected buckets
194
+ let bucketsWithCount =
195
+ selectedFacetGroup?.buckets.map(bucket => {
196
+ const selectedBucket = aggregateFacetGroup.buckets.find(
197
+ b => b.key === bucket.key
198
+ );
199
+ return selectedBucket
200
+ ? {
201
+ ...bucket,
202
+ count: selectedBucket.count,
203
+ }
204
+ : bucket;
205
+ }) ?? [];
206
+
207
+ // append any additional buckets that were not selected
208
+ aggregateFacetGroup.buckets.forEach(bucket => {
209
+ const existingBucket = bucketsWithCount.find(b => b.key === bucket.key);
210
+ if (existingBucket) return;
211
+ bucketsWithCount.push(bucket);
212
+ });
213
+
214
+ // For lending facets, only include a specific subset of buckets
215
+ if (facetKey === 'lending') {
216
+ bucketsWithCount = bucketsWithCount.filter(
217
+ bucket => lendingFacetKeysVisibility[bucket.key as LendingFacetKey]
218
+ );
219
+ }
220
+
221
+ /**
222
+ * render limited facet items on page facet area
223
+ *
224
+ * - by-default we are showing 6 items
225
+ * - additionally want to show all items (selected/suppressed) in page facet area
226
+ */
227
+ let allowedFacetCount = Object.keys(
228
+ (selectedFacetGroup?.buckets as []) || []
229
+ )?.length;
230
+ if (allowedFacetCount < this.allowedFacetCount) {
231
+ allowedFacetCount = this.allowedFacetCount; // splice start index from 0th
232
+ }
233
+
234
+ // splice how many items we want to show in page facet area
235
+ facetGroup.buckets = bucketsWithCount.splice(0, allowedFacetCount);
236
+
237
+ facetGroups.push(facetGroup);
238
+ });
239
+
240
+ return facetGroups;
241
+ }
242
+
243
+ /**
244
+ * Converts the selected facets to a `FacetGroup` array,
245
+ * which is easier to work with
246
+ */
247
+ private get selectedFacetGroups(): FacetGroup[] {
248
+ if (!this.selectedFacets) return [];
249
+
250
+ const facetGroups: FacetGroup[] = Object.entries(this.selectedFacets).map(
251
+ ([key, selectedFacets]) => {
252
+ const option = key as FacetOption;
253
+ const title = facetTitles[option];
254
+
255
+ const buckets: FacetBucket[] = Object.entries(selectedFacets).map(
256
+ ([value, facetData]) => {
257
+ let displayText = value;
258
+ // for selected languages, we store the language code instead of the
259
+ // display name, so look up the name from the mapping
260
+ if (option === 'language') {
261
+ displayText =
262
+ this.languageCodeHandler?.getLanguageNameFromCodeString(
263
+ value
264
+ ) ?? value;
265
+ }
266
+ // for lending facets, convert the key to a readable format
267
+ if (option === 'lending') {
268
+ displayText =
269
+ lendingFacetDisplayNames[value as LendingFacetKey] ?? value;
270
+ }
271
+ return {
272
+ displayText,
273
+ key: value,
274
+ count: facetData.count,
275
+ state: facetData.state,
276
+ };
277
+ }
278
+ );
279
+
280
+ return {
281
+ title,
282
+ key: option,
283
+ buckets,
284
+ };
285
+ }
286
+ );
287
+
288
+ return facetGroups;
289
+ }
290
+
291
+ /**
292
+ * Converts the raw `aggregations` to `FacetGroups`, which are easier to use
293
+ */
294
+ private get aggregationFacetGroups(): FacetGroup[] {
295
+ const facetGroups: FacetGroup[] = [];
296
+ Object.entries(this.aggregations ?? []).forEach(([key, buckets]) => {
297
+ // the year_histogram data is in a different format so can't be handled here
298
+ if (key === 'year_histogram') return;
299
+
300
+ const option = key as FacetOption;
301
+ const title = facetTitles[option];
302
+ if (!title) return;
303
+
304
+ const castedBuckets = buckets.buckets as Bucket[];
305
+
306
+ // we are not showing fav- items in facets
307
+ castedBuckets?.filter(
308
+ bucket => bucket?.key?.toString()?.startsWith('fav-') === false
309
+ );
310
+
311
+ const facetBuckets: FacetBucket[] = castedBuckets.map(bucket => {
312
+ let bucketKey = bucket.key;
313
+ let displayText = `${bucket.key}`;
314
+ // for languages, we need to search by language code instead of the
315
+ // display name, which is what we get from the search engine result
316
+ if (option === 'language') {
317
+ // const languageCodeKey = languageToCodeMap[bucket.key];
318
+ bucketKey =
319
+ this.languageCodeHandler?.getCodeStringFromLanguageName(
320
+ `${bucket.key}`
321
+ ) ?? bucket.key;
322
+ // bucketKey = languageCodeKey ?? bucket.key;
323
+ }
324
+ // for lending facets, convert the bucket key to a readable format
325
+ if (option === 'lending') {
326
+ displayText =
327
+ lendingFacetDisplayNames[bucket.key as LendingFacetKey] ??
328
+ `${bucket.key}`;
329
+ }
330
+ return {
331
+ displayText,
332
+ key: `${bucketKey}`,
333
+ count: bucket.doc_count,
334
+ state: 'none',
335
+ };
336
+ });
337
+ const group: FacetGroup = {
338
+ title,
339
+ key: option,
340
+ buckets: facetBuckets,
341
+ };
342
+ facetGroups.push(group);
343
+ });
344
+ return facetGroups;
345
+ }
346
+
347
+ /**
348
+ * Generate the template for a facet group with a header and the collapsible
349
+ * chevron for the mobile view
350
+ */
351
+ private getFacetGroupTemplate(
352
+ facetGroup: FacetGroup
353
+ ): TemplateResult | typeof nothing {
354
+ if (facetGroup.buckets.length === 0) return nothing;
355
+ const { key } = facetGroup;
356
+ const isOpen = this.openFacets[key];
357
+ const collapser = html`
358
+ <span class="collapser ${isOpen ? 'open' : ''}"> ${chevronIcon} </span>
359
+ `;
360
+
361
+ return html`
362
+ <div class="facet-group ${this.collapsableFacets ? 'mobile' : ''}">
363
+ <div class="facet-group-header">
364
+ <h1
365
+ @click=${() => {
366
+ const newOpenFacets = { ...this.openFacets };
367
+ newOpenFacets[key] = !isOpen;
368
+ this.openFacets = newOpenFacets;
369
+ }}
370
+ @keyup=${() => {
371
+ const newOpenFacets = { ...this.openFacets };
372
+ newOpenFacets[key] = !isOpen;
373
+ this.openFacets = newOpenFacets;
374
+ }}
375
+ >
376
+ ${this.collapsableFacets ? collapser : nothing} ${facetGroup.title}
377
+ </h1>
378
+ ${this.moreFacetsSortingIcon(facetGroup)}
379
+ </div>
380
+ <div class="facet-group-content ${isOpen ? 'open' : ''}">
381
+ ${this.getFacetTemplate(facetGroup)}
382
+ ${this.searchMoreFacetsLink(facetGroup)}
383
+ </div>
384
+ </div>
385
+ `;
386
+ }
387
+
388
+ private moreFacetsSortingIcon(
389
+ facetGroup: FacetGroup
390
+ ): TemplateResult | typeof nothing {
391
+ // Display the sorting icon for every facet group except lending
392
+ return facetGroup.key === 'lending'
393
+ ? nothing
394
+ : html`
395
+ <input
396
+ class="sorting-icon"
397
+ type="image"
398
+ @click=${() => this.showMoreFacetsModal(facetGroup, 'alpha')}
399
+ src="https://archive.org/images/filter-count.png"
400
+ alt="Sort alphabetically"
401
+ />
402
+ `;
403
+ }
404
+
405
+ /**
406
+ * Generate the More... link button just below the facets group
407
+ *
408
+ * TODO: want to fire analytics?
409
+ */
410
+ private searchMoreFacetsLink(
411
+ facetGroup: FacetGroup
412
+ ): TemplateResult | typeof nothing {
413
+ // Don't render More... links for FTS searches
414
+ if (this.previousSearchType === SearchType.FULLTEXT) {
415
+ return nothing;
416
+ }
417
+
418
+ // Don't render More... links for lending facets
419
+ if (facetGroup.key === 'lending') {
420
+ return nothing;
421
+ }
422
+
423
+ // Don't render More... link if the number of facets < this.allowedFacetCount
424
+ if (Object.keys(facetGroup.buckets).length < this.allowedFacetCount) {
425
+ return nothing;
426
+ }
427
+
428
+ return html`<button
429
+ class="more-link"
430
+ @click=${() => {
431
+ this.showMoreFacetsModal(facetGroup, 'count');
432
+ this.dispatchEvent(
433
+ new CustomEvent('showMoreFacets', { detail: facetGroup.key })
434
+ );
435
+ }}
436
+ >
437
+ More...
438
+ </button>`;
439
+ }
440
+
441
+ async showMoreFacetsModal(
442
+ facetGroup: FacetGroup,
443
+ sortedBy: string
444
+ ): Promise<void> {
445
+ const facetAggrKey = facetGroup.key;
446
+
447
+ const customModalContent = html`
448
+ <more-facets-content
449
+ .facetKey=${facetGroup.key}
450
+ .facetAggregationKey=${facetAggrKey}
451
+ .fullQuery=${this.fullQuery}
452
+ .modalManager=${this.modalManager}
453
+ .searchService=${this.searchService}
454
+ .searchType=${this.searchType}
455
+ .collectionNameCache=${this.collectionNameCache}
456
+ .languageCodeHandler=${this.languageCodeHandler}
457
+ .selectedFacets=${this.selectedFacets}
458
+ .sortedBy=${sortedBy}
459
+ @facetsChanged=${(e: CustomEvent) => {
460
+ const event = new CustomEvent<SelectedFacets>('facetsChanged', {
461
+ detail: e.detail,
462
+ bubbles: true,
463
+ composed: true,
464
+ });
465
+ this.dispatchEvent(event);
466
+ }}
467
+ >
468
+ </more-facets-content>
469
+ `;
470
+
471
+ const config = new ModalConfig({
472
+ bodyColor: '#fff',
473
+ headerColor: '#194880',
474
+ showHeaderLogo: false,
475
+ closeOnBackdropClick: true, // TODO: want to fire analytics
476
+ title: html`Select filters`,
477
+ });
478
+ this.modalManager?.classList.add('more-search-facets');
479
+ this.modalManager?.showModal({
480
+ config,
481
+ customModalContent,
482
+ });
483
+ }
484
+
485
+ /**
486
+ * Generate the list template for each bucket in a facet group
487
+ */
488
+ private getFacetTemplate(facetGroup: FacetGroup): TemplateResult {
489
+ return html`
490
+ <facets-template
491
+ .facetGroup=${facetGroup}
492
+ .selectedFacets=${this.selectedFacets}
493
+ .renderOn=${'page'}
494
+ .collectionNameCache=${this.collectionNameCache}
495
+ @selectedFacetsChanged=${(e: CustomEvent) => {
496
+ const event = new CustomEvent<SelectedFacets>('facetsChanged', {
497
+ detail: e.detail,
498
+ bubbles: true,
499
+ composed: true,
500
+ });
501
+ this.dispatchEvent(event);
502
+ }}
503
+ ></facets-template>
504
+ `;
505
+ }
506
+
507
+ static get styles() {
508
+ return css`
509
+ #container.loading {
510
+ opacity: 0.5;
511
+ }
512
+
513
+ .collapser {
514
+ display: inline-block;
515
+ cursor: pointer;
516
+ width: 10px;
517
+ height: 10px;
518
+ }
519
+
520
+ .collapser svg {
521
+ transition: transform 0.2s ease-in-out;
522
+ }
523
+
524
+ .collapser.open svg {
525
+ transform: rotate(90deg);
526
+ }
527
+
528
+ .facet-group {
529
+ margin-bottom: 2rem;
530
+ }
531
+
532
+ .facet-group h1 {
533
+ margin-bottom: 0.7rem;
534
+ }
535
+
536
+ .facet-group.mobile h1 {
537
+ cursor: pointer;
538
+ }
539
+
540
+ .facet-group-header {
541
+ display: flex;
542
+ margin-bottom: 0.7rem;
543
+ justify-content: space-between;
544
+ border-bottom: 1px solid rgb(232, 232, 232);
545
+ }
546
+
547
+ .facet-group-content {
548
+ transition: max-height 0.2s ease-in-out;
549
+ }
550
+
551
+ .facet-group.mobile .facet-group-content {
552
+ max-height: 0;
553
+ overflow: hidden;
554
+ }
555
+
556
+ .facet-group.mobile .facet-group-content.open {
557
+ max-height: 2000px;
558
+ }
559
+
560
+ h1 {
561
+ font-size: 1.4rem;
562
+ font-weight: 200
563
+ padding-bottom: 3px;
564
+ margin: 0;
565
+ }
566
+
567
+ .more-link {
568
+ font-size: 1.2rem;
569
+ text-decoration: none;
570
+ padding: 0;
571
+ background: inherit;
572
+ border: 0;
573
+ color: blue;
574
+ cursor: pointer;
575
+ }
576
+ .sorting-icon {
577
+ height: 15px;
578
+ cursor: pointer;
579
+ }
580
+ `;
581
+ }
582
+ }