@internetarchive/collection-browser 2.18.3-alpha-webdev7768.8 → 2.19.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.
@@ -1,428 +1,437 @@
1
- import {
2
- css,
3
- html,
4
- LitElement,
5
- TemplateResult,
6
- CSSResultGroup,
7
- nothing,
8
- PropertyValues,
9
- } from 'lit';
10
- import { repeat } from 'lit/directives/repeat.js';
11
- import { customElement, property, state } from 'lit/decorators.js';
12
- import type { Aggregation, Bucket } from '@internetarchive/search-service';
13
- import type { CollectionTitles } from '../../data-source/models';
14
- import type {
15
- FacetEventDetails,
16
- FacetOption,
17
- FacetState,
18
- SelectedFacets,
19
- } from '../../models';
20
- import { updateSelectedFacetBucket } from '../../utils/facet-utils';
21
- import { SmartQueryHeuristicGroup } from './smart-facet-heuristics';
22
- import type { SmartFacetDropdown } from './smart-facet-dropdown';
23
- import type { SmartFacet, SmartFacetEvent } from './models';
24
- import { smartFacetEquals } from './smart-facet-equals';
25
- import { dedupe } from './dedupe';
26
- import { log } from '../../utils/log';
27
- import filterIcon from '../../assets/img/icons/filter';
28
-
29
- import './smart-facet-button';
30
- import './smart-facet-dropdown';
31
-
32
- const fieldPrefixes: Partial<Record<FacetOption, string>> = {
33
- collection: 'Collection: ',
34
- creator: 'By: ',
35
- subject: 'About: ',
36
- };
37
-
38
- function capitalize(str: string) {
39
- return str.charAt(0).toUpperCase() + str.slice(1);
40
- }
41
-
42
- @customElement('smart-facet-bar')
43
- export class SmartFacetBar extends LitElement {
44
- @property({ type: String }) query?: string;
45
-
46
- @property({ type: Object }) aggregations?: Record<string, Aggregation>;
47
-
48
- @property({ type: Object }) selectedFacets?: SelectedFacets;
49
-
50
- /** The map from collection identifiers to their titles */
51
- @property({ type: Object })
52
- collectionTitles?: CollectionTitles;
53
-
54
- @property({ type: Boolean }) filterToggleShown = false;
55
-
56
- @property({ type: Boolean }) filterToggleActive = false;
57
-
58
- @property({ type: String }) label?: string;
59
-
60
- @state() private heuristicRecs: SmartFacet[] = [];
61
-
62
- @state() private smartFacets: SmartFacet[][] = [];
63
-
64
- @state() private lastAggregations?: Record<string, Aggregation>;
65
-
66
- //
67
- // COMPONENT LIFECYCLE METHODS
68
- //
69
-
70
- render() {
71
- if (!this.query) return nothing;
72
-
73
- const shouldShowLabel = !!this.label && this.smartFacets.length > 0;
74
- return html`
75
- <div id="smart-facets-container">
76
- ${this.filtersToggleTemplate}
77
- ${shouldShowLabel
78
- ? html`<p id="filters-label">${this.label}</p>`
79
- : nothing}
80
- ${repeat(
81
- this.smartFacets,
82
- f =>
83
- `${f[0].label}|${f[0].facets[0].facetType}|${f[0].facets[0].bucketKey}`,
84
- facet => this.makeSmartFacet(facet),
85
- )}
86
- </div>
87
- `;
88
- }
89
-
90
- protected willUpdate(changed: PropertyValues): void {
91
- let shouldUpdateSmartFacets = false;
92
-
93
- if (changed.has('query')) {
94
- log('query change', changed.get('query'), this.query);
95
- this.lastAggregations = undefined;
96
- shouldUpdateSmartFacets = true;
97
- }
98
-
99
- if (
100
- changed.has('aggregations') &&
101
- !this.lastAggregations &&
102
- this.aggregations &&
103
- Object.keys(this.aggregations).length > 0
104
- ) {
105
- log('aggs change', changed.get('aggregations'), this.aggregations);
106
- this.lastAggregations = this.aggregations;
107
- shouldUpdateSmartFacets = true;
108
- }
109
-
110
- if (shouldUpdateSmartFacets) {
111
- log('should update smart facets, doing so...');
112
- this.updateSmartFacets();
113
- }
114
- }
115
-
116
- refresh(): void {
117
- this.lastAggregations = this.aggregations;
118
- this.updateSmartFacets();
119
- }
120
-
121
- private async updateSmartFacets(): Promise<void> {
122
- log('updating smart facets');
123
- if (this.query) {
124
- this.heuristicRecs =
125
- await new SmartQueryHeuristicGroup().getRecommendedFacets(this.query);
126
- log('heuristic recs are', this.heuristicRecs);
127
- this.smartFacets = dedupe(this.facetsToDisplay);
128
- log('smart facets are', this.smartFacets);
129
- }
130
- }
131
-
132
- //
133
- // OTHER METHODS
134
- //
135
-
136
- private makeSmartFacet(facets: SmartFacet[]) {
137
- if (facets.length === 0) {
138
- return nothing;
139
- }
140
- if (facets.length === 1) {
141
- return this.smartFacetButton(facets[0]);
142
- }
143
- return this.smartFacetDropdown(facets);
144
- }
145
-
146
- private smartFacetButton(facet: SmartFacet) {
147
- return html`
148
- <smart-facet-button
149
- .facetInfo=${facet}
150
- .labelPrefix=${fieldPrefixes[facet.facets[0].facetType]}
151
- .selected=${facet.selected ?? false}
152
- @facetClick=${this.facetClicked}
153
- ></smart-facet-button>
154
- `;
155
- }
156
-
157
- private smartFacetDropdown(facets: SmartFacet[]) {
158
- return html`
159
- <smart-facet-dropdown
160
- .facetInfo=${facets}
161
- .labelPrefix=${fieldPrefixes[facets[0].facets[0].facetType]}
162
- .activeFacetRef=${facets[0].facets[0]}
163
- @facetClick=${this.dropdownOptionClicked}
164
- @dropdownClick=${this.dropdownClicked}
165
- ></smart-facet-dropdown>
166
- `;
167
- }
168
-
169
- private get filtersToggleTemplate(): TemplateResult | typeof nothing {
170
- if (!this.filterToggleShown) return nothing;
171
-
172
- return html`
173
- <button
174
- id="filters-toggle"
175
- class=${this.filterToggleActive ? 'active' : ''}
176
- title="${this.filterToggleActive ? 'Hide' : 'Show'} filters pane"
177
- @click=${this.filterToggleClicked}
178
- >
179
- ${filterIcon}
180
- </button>
181
- `;
182
- }
183
-
184
- private get facetsToDisplay(): SmartFacet[][] {
185
- const facets: SmartFacet[][] = [];
186
-
187
- if (this.heuristicRecs.length > 0) {
188
- for (const rec of this.heuristicRecs) {
189
- // Suppress mediatype-only facets for now.
190
- if (rec.facets.length === 1 && rec.facets[0].facetType === 'mediatype')
191
- continue;
192
-
193
- facets.push([rec]);
194
- }
195
- }
196
-
197
- if (this.lastAggregations) {
198
- const keys = [
199
- 'mediatype',
200
- 'year',
201
- 'language',
202
- 'creator',
203
- 'subject',
204
- 'collection',
205
- ];
206
- for (const key of keys) {
207
- const agg = this.lastAggregations[key];
208
- if (!agg) continue;
209
- if (agg.buckets.length === 0) continue;
210
- if (['lending', 'year_histogram', 'date_histogram'].includes(key))
211
- continue;
212
- if (typeof agg.buckets[0] === 'number') continue;
213
-
214
- if (
215
- key === 'mediatype' &&
216
- this.selectedFacets &&
217
- Object.values(this.selectedFacets.mediatype ?? {}).some(
218
- bucket => bucket.state !== 'none',
219
- )
220
- ) {
221
- continue;
222
- }
223
-
224
- const facetType = key as FacetOption;
225
- const buckets = agg.buckets as Bucket[];
226
-
227
- const unusedBuckets = buckets.filter(b => {
228
- const selectedFacetBucket = this.selectedFacets?.[facetType]?.[b.key];
229
- if (selectedFacetBucket && selectedFacetBucket.state !== 'none') {
230
- return false;
231
- }
232
- return true;
233
- });
234
-
235
- if (facetType === 'mediatype') {
236
- continue;
237
- // Don't include mediatype bubbles
238
- } else if (facetType === 'collection' || facetType === 'subject') {
239
- const topBuckets = unusedBuckets.slice(0, 5);
240
- facets.push(topBuckets.map(b => this.toSmartFacet(facetType, [b])));
241
- } else {
242
- facets.push([this.toSmartFacet(facetType, [unusedBuckets[0]])]);
243
- }
244
- }
245
- }
246
-
247
- return facets;
248
- }
249
-
250
- private toSmartFacet(
251
- facetType: FacetOption,
252
- buckets: Bucket[],
253
- // prefix = true
254
- ): SmartFacet {
255
- return {
256
- facets: buckets.map(bucket => {
257
- let displayText = capitalize(bucket.key.toString());
258
- if (facetType === 'collection') {
259
- const title = this.collectionTitles?.get(bucket.key.toString());
260
- if (title) displayText = title;
261
- }
262
-
263
- return {
264
- facetType,
265
- bucketKey: bucket.key.toString(),
266
- displayText,
267
- };
268
- }),
269
- } as SmartFacet;
270
- }
271
-
272
- /**
273
- * Toggles the state of the given smart facet, and updates the selected facets accordingly.
274
- */
275
- private toggleSmartFacet(
276
- facet: SmartFacet,
277
- details: FacetEventDetails[],
278
- ): void {
279
- let newState: FacetState;
280
- if (facet.selected) {
281
- // When deselected, leave the smart facet where it is
282
- newState = 'none';
283
- this.smartFacets = this.smartFacets.map(f => {
284
- if (f[0] === facet) return [{ ...facet, selected: false }];
285
- return f;
286
- });
287
- } else {
288
- // When selected, move the toggled smart facet to the front of the list
289
- newState = 'selected';
290
- this.smartFacets = [
291
- [{ ...facet, selected: true }],
292
- ...this.smartFacets.filter(f => f[0] !== facet),
293
- ];
294
- }
295
-
296
- this.updateSelectedFacets(
297
- details.map(facet => ({
298
- ...facet,
299
- bucket: {
300
- ...facet.bucket,
301
- state: newState,
302
- },
303
- })),
304
- );
305
- }
306
-
307
- /**
308
- * Updates the selected facet buckets for each of the given facets,
309
- * and emits a `facetsChanged` event to notify parent components of
310
- * the new state.
311
- */
312
- private updateSelectedFacets(facets: FacetEventDetails[]): void {
313
- for (const facet of facets) {
314
- this.selectedFacets = updateSelectedFacetBucket(
315
- this.selectedFacets,
316
- facet.facetType,
317
- facet.bucket,
318
- true,
319
- );
320
- }
321
-
322
- const event = new CustomEvent<SelectedFacets>('facetsChanged', {
323
- detail: this.selectedFacets,
324
- });
325
- this.dispatchEvent(event);
326
- }
327
-
328
- /**
329
- * Handler for when a smart facet button is clicked
330
- */
331
- private facetClicked(e: CustomEvent<SmartFacetEvent>): void {
332
- this.toggleSmartFacet(e.detail.smartFacet, e.detail.details);
333
- }
334
-
335
- /**
336
- * Handler for when an option in a smart facet dropdown menu is selected
337
- */
338
- private dropdownOptionClicked(e: CustomEvent<SmartFacetEvent>): void {
339
- const existingFacet = this.smartFacets.find(
340
- sf => sf.length === 1 && smartFacetEquals(sf[0], e.detail.smartFacet),
341
- );
342
- if (existingFacet) {
343
- // The facet already exists outside the dropdown, so just select it there
344
- this.toggleSmartFacet(existingFacet[0], e.detail.details);
345
- return;
346
- }
347
-
348
- // Otherwise, prepend a new smart facet for the selected option
349
- this.smartFacets = [
350
- [{ ...e.detail.smartFacet, selected: true }],
351
- ...this.smartFacets,
352
- ];
353
-
354
- this.updateSelectedFacets(e.detail.details);
355
- }
356
-
357
- /**
358
- * Handler for when any dropdown is clicked (whether button, caret, or menu item)
359
- */
360
- private dropdownClicked(e: CustomEvent<SmartFacetDropdown>): void {
361
- log('smart bar: onDropdownClick', e.detail);
362
- this.shadowRoot
363
- ?.querySelectorAll('smart-facet-dropdown')
364
- .forEach(dropdown => {
365
- if (dropdown !== e.detail) {
366
- log('closing', dropdown);
367
- (dropdown as SmartFacetDropdown).close();
368
- }
369
- });
370
- }
371
-
372
- private filterToggleClicked(): void {
373
- this.dispatchEvent(new CustomEvent('filtersToggled'));
374
- }
375
-
376
- //
377
- // STYLES
378
- //
379
-
380
- static get styles(): CSSResultGroup {
381
- return css`
382
- #smart-facets-container {
383
- display: flex;
384
- align-items: center;
385
- gap: 5px 10px;
386
- padding: 10px 0;
387
- white-space: nowrap;
388
- overflow: scroll hidden;
389
- scrollbar-width: none;
390
- }
391
-
392
- #filters-toggle {
393
- margin: 0;
394
- border: 0;
395
- padding: 5px 8px;
396
- border-radius: 5px;
397
- background: white;
398
- color: #2c2c2c;
399
- border: 1px solid #194880;
400
- font-size: 1.4rem;
401
- font-family: inherit;
402
- text-decoration: none;
403
- cursor: pointer;
404
- }
405
-
406
- #filters-toggle.active {
407
- background: #194880;
408
- color: white;
409
- }
410
-
411
- #filters-toggle > svg {
412
- width: 12px;
413
- filter: invert(0.16667);
414
- vertical-align: -1px;
415
- }
416
-
417
- #filters-toggle.active > svg {
418
- filter: invert(1);
419
- }
420
-
421
- #filters-label {
422
- font-size: 1.4rem;
423
- font-weight: var(--smartFacetLabelFontWeight, normal);
424
- margin: 0 -5px 0 0;
425
- }
426
- `;
427
- }
428
- }
1
+ import {
2
+ css,
3
+ html,
4
+ LitElement,
5
+ TemplateResult,
6
+ CSSResultGroup,
7
+ nothing,
8
+ PropertyValues,
9
+ } from 'lit';
10
+ import { repeat } from 'lit/directives/repeat.js';
11
+ import { customElement, property, state } from 'lit/decorators.js';
12
+ import type { Aggregation, Bucket } from '@internetarchive/search-service';
13
+ import type { CollectionTitles } from '../../data-source/models';
14
+ import type {
15
+ FacetEventDetails,
16
+ FacetOption,
17
+ FacetState,
18
+ SelectedFacets,
19
+ } from '../../models';
20
+ import { updateSelectedFacetBucket } from '../../utils/facet-utils';
21
+ import { SmartQueryHeuristicGroup } from './smart-facet-heuristics';
22
+ import type { SmartFacetDropdown } from './smart-facet-dropdown';
23
+ import type { SmartFacet, SmartFacetEvent } from './models';
24
+ import { smartFacetEquals } from './smart-facet-equals';
25
+ import { dedupe } from './dedupe';
26
+ import { log } from '../../utils/log';
27
+ import filterIcon from '../../assets/img/icons/filter';
28
+
29
+ import './smart-facet-button';
30
+ import './smart-facet-dropdown';
31
+
32
+ const fieldPrefixes: Partial<Record<FacetOption, string>> = {
33
+ collection: 'Collection: ',
34
+ creator: 'By: ',
35
+ subject: 'About: ',
36
+ };
37
+
38
+ function capitalize(str: string) {
39
+ return str.charAt(0).toUpperCase() + str.slice(1);
40
+ }
41
+
42
+ @customElement('smart-facet-bar')
43
+ export class SmartFacetBar extends LitElement {
44
+ @property({ type: String }) query?: string;
45
+
46
+ @property({ type: Object }) aggregations?: Record<string, Aggregation>;
47
+
48
+ @property({ type: Object }) selectedFacets?: SelectedFacets;
49
+
50
+ /** The map from collection identifiers to their titles */
51
+ @property({ type: Object })
52
+ collectionTitles?: CollectionTitles;
53
+
54
+ @property({ type: Boolean }) filterToggleShown = false;
55
+
56
+ @property({ type: Boolean }) filterToggleActive = false;
57
+
58
+ @property({ type: String }) label?: string;
59
+
60
+ @state() private heuristicRecs: SmartFacet[] = [];
61
+
62
+ @state() private smartFacets: SmartFacet[][] = [];
63
+
64
+ @state() private lastAggregations?: Record<string, Aggregation>;
65
+
66
+ //
67
+ // COMPONENT LIFECYCLE METHODS
68
+ //
69
+
70
+ render() {
71
+ if (!this.query) return nothing;
72
+
73
+ const shouldShowLabel = !!this.label && this.smartFacets.length > 0;
74
+ return html`
75
+ <div id="smart-facets-container">
76
+ ${this.filtersToggleTemplate}
77
+ ${shouldShowLabel
78
+ ? html`<p id="filters-label">${this.label}</p>`
79
+ : nothing}
80
+ ${repeat(
81
+ this.smartFacets,
82
+ f =>
83
+ `${f[0].label}|${f[0].facets[0].facetType}|${f[0].facets[0].bucketKey}`,
84
+ facet => this.makeSmartFacet(facet),
85
+ )}
86
+ </div>
87
+ `;
88
+ }
89
+
90
+ protected willUpdate(changed: PropertyValues): void {
91
+ let shouldUpdateSmartFacets = false;
92
+
93
+ if (changed.has('query')) {
94
+ log('query change', changed.get('query'), this.query);
95
+ this.lastAggregations = undefined;
96
+ shouldUpdateSmartFacets = true;
97
+ }
98
+
99
+ if (
100
+ changed.has('aggregations') &&
101
+ !this.lastAggregations &&
102
+ this.aggregations &&
103
+ Object.keys(this.aggregations).length > 0
104
+ ) {
105
+ log('aggs change', changed.get('aggregations'), this.aggregations);
106
+ this.lastAggregations = this.aggregations;
107
+ shouldUpdateSmartFacets = true;
108
+ }
109
+
110
+ if (shouldUpdateSmartFacets) {
111
+ log('should update smart facets, doing so...');
112
+ this.updateSmartFacets();
113
+ }
114
+ }
115
+
116
+ refresh(): void {
117
+ this.lastAggregations = this.aggregations;
118
+ this.updateSmartFacets();
119
+ }
120
+
121
+ deselectAll(): void {
122
+ for (const sf of this.smartFacets) {
123
+ for (const facet of sf) {
124
+ facet.selected = false;
125
+ }
126
+ }
127
+ this.requestUpdate();
128
+ }
129
+
130
+ private async updateSmartFacets(): Promise<void> {
131
+ log('updating smart facets');
132
+ if (this.query) {
133
+ this.heuristicRecs =
134
+ await new SmartQueryHeuristicGroup().getRecommendedFacets(this.query);
135
+ log('heuristic recs are', this.heuristicRecs);
136
+ this.smartFacets = dedupe(this.facetsToDisplay);
137
+ log('smart facets are', this.smartFacets);
138
+ }
139
+ }
140
+
141
+ //
142
+ // OTHER METHODS
143
+ //
144
+
145
+ private makeSmartFacet(facets: SmartFacet[]) {
146
+ if (facets.length === 0) {
147
+ return nothing;
148
+ }
149
+ if (facets.length === 1) {
150
+ return this.smartFacetButton(facets[0]);
151
+ }
152
+ return this.smartFacetDropdown(facets);
153
+ }
154
+
155
+ private smartFacetButton(facet: SmartFacet) {
156
+ return html`
157
+ <smart-facet-button
158
+ .facetInfo=${facet}
159
+ .labelPrefix=${fieldPrefixes[facet.facets[0].facetType]}
160
+ .selected=${facet.selected ?? false}
161
+ @facetClick=${this.facetClicked}
162
+ ></smart-facet-button>
163
+ `;
164
+ }
165
+
166
+ private smartFacetDropdown(facets: SmartFacet[]) {
167
+ return html`
168
+ <smart-facet-dropdown
169
+ .facetInfo=${facets}
170
+ .labelPrefix=${fieldPrefixes[facets[0].facets[0].facetType]}
171
+ .activeFacetRef=${facets[0].facets[0]}
172
+ @facetClick=${this.dropdownOptionClicked}
173
+ @dropdownClick=${this.dropdownClicked}
174
+ ></smart-facet-dropdown>
175
+ `;
176
+ }
177
+
178
+ private get filtersToggleTemplate(): TemplateResult | typeof nothing {
179
+ if (!this.filterToggleShown) return nothing;
180
+
181
+ return html`
182
+ <button
183
+ id="filters-toggle"
184
+ class=${this.filterToggleActive ? 'active' : ''}
185
+ title="${this.filterToggleActive ? 'Hide' : 'Show'} filters pane"
186
+ @click=${this.filterToggleClicked}
187
+ >
188
+ ${filterIcon}
189
+ </button>
190
+ `;
191
+ }
192
+
193
+ private get facetsToDisplay(): SmartFacet[][] {
194
+ const facets: SmartFacet[][] = [];
195
+
196
+ if (this.heuristicRecs.length > 0) {
197
+ for (const rec of this.heuristicRecs) {
198
+ // Suppress mediatype-only facets for now.
199
+ if (rec.facets.length === 1 && rec.facets[0].facetType === 'mediatype')
200
+ continue;
201
+
202
+ facets.push([rec]);
203
+ }
204
+ }
205
+
206
+ if (this.lastAggregations) {
207
+ const keys = [
208
+ 'mediatype',
209
+ 'year',
210
+ 'language',
211
+ 'creator',
212
+ 'subject',
213
+ 'collection',
214
+ ];
215
+ for (const key of keys) {
216
+ const agg = this.lastAggregations[key];
217
+ if (!agg) continue;
218
+ if (agg.buckets.length === 0) continue;
219
+ if (['lending', 'year_histogram', 'date_histogram'].includes(key))
220
+ continue;
221
+ if (typeof agg.buckets[0] === 'number') continue;
222
+
223
+ if (
224
+ key === 'mediatype' &&
225
+ this.selectedFacets &&
226
+ Object.values(this.selectedFacets.mediatype ?? {}).some(
227
+ bucket => bucket.state !== 'none',
228
+ )
229
+ ) {
230
+ continue;
231
+ }
232
+
233
+ const facetType = key as FacetOption;
234
+ const buckets = agg.buckets as Bucket[];
235
+
236
+ const unusedBuckets = buckets.filter(b => {
237
+ const selectedFacetBucket = this.selectedFacets?.[facetType]?.[b.key];
238
+ if (selectedFacetBucket && selectedFacetBucket.state !== 'none') {
239
+ return false;
240
+ }
241
+ return true;
242
+ });
243
+
244
+ if (facetType === 'mediatype') {
245
+ continue;
246
+ // Don't include mediatype bubbles
247
+ } else if (facetType === 'collection' || facetType === 'subject') {
248
+ const topBuckets = unusedBuckets.slice(0, 5);
249
+ facets.push(topBuckets.map(b => this.toSmartFacet(facetType, [b])));
250
+ } else {
251
+ facets.push([this.toSmartFacet(facetType, [unusedBuckets[0]])]);
252
+ }
253
+ }
254
+ }
255
+
256
+ return facets;
257
+ }
258
+
259
+ private toSmartFacet(
260
+ facetType: FacetOption,
261
+ buckets: Bucket[],
262
+ // prefix = true
263
+ ): SmartFacet {
264
+ return {
265
+ facets: buckets.map(bucket => {
266
+ let displayText = capitalize(bucket.key.toString());
267
+ if (facetType === 'collection') {
268
+ const title = this.collectionTitles?.get(bucket.key.toString());
269
+ if (title) displayText = title;
270
+ }
271
+
272
+ return {
273
+ facetType,
274
+ bucketKey: bucket.key.toString(),
275
+ displayText,
276
+ };
277
+ }),
278
+ } as SmartFacet;
279
+ }
280
+
281
+ /**
282
+ * Toggles the state of the given smart facet, and updates the selected facets accordingly.
283
+ */
284
+ private toggleSmartFacet(
285
+ facet: SmartFacet,
286
+ details: FacetEventDetails[],
287
+ ): void {
288
+ let newState: FacetState;
289
+ if (facet.selected) {
290
+ // When deselected, leave the smart facet where it is
291
+ newState = 'none';
292
+ this.smartFacets = this.smartFacets.map(f => {
293
+ if (f[0] === facet) return [{ ...facet, selected: false }];
294
+ return f;
295
+ });
296
+ } else {
297
+ // When selected, move the toggled smart facet to the front of the list
298
+ newState = 'selected';
299
+ this.smartFacets = [
300
+ [{ ...facet, selected: true }],
301
+ ...this.smartFacets.filter(f => f[0] !== facet),
302
+ ];
303
+ }
304
+
305
+ this.updateSelectedFacets(
306
+ details.map(facet => ({
307
+ ...facet,
308
+ bucket: {
309
+ ...facet.bucket,
310
+ state: newState,
311
+ },
312
+ })),
313
+ );
314
+ }
315
+
316
+ /**
317
+ * Updates the selected facet buckets for each of the given facets,
318
+ * and emits a `facetsChanged` event to notify parent components of
319
+ * the new state.
320
+ */
321
+ private updateSelectedFacets(facets: FacetEventDetails[]): void {
322
+ for (const facet of facets) {
323
+ this.selectedFacets = updateSelectedFacetBucket(
324
+ this.selectedFacets,
325
+ facet.facetType,
326
+ facet.bucket,
327
+ true,
328
+ );
329
+ }
330
+
331
+ const event = new CustomEvent<SelectedFacets>('facetsChanged', {
332
+ detail: this.selectedFacets,
333
+ });
334
+ this.dispatchEvent(event);
335
+ }
336
+
337
+ /**
338
+ * Handler for when a smart facet button is clicked
339
+ */
340
+ private facetClicked(e: CustomEvent<SmartFacetEvent>): void {
341
+ this.toggleSmartFacet(e.detail.smartFacet, e.detail.details);
342
+ }
343
+
344
+ /**
345
+ * Handler for when an option in a smart facet dropdown menu is selected
346
+ */
347
+ private dropdownOptionClicked(e: CustomEvent<SmartFacetEvent>): void {
348
+ const existingFacet = this.smartFacets.find(
349
+ sf => sf.length === 1 && smartFacetEquals(sf[0], e.detail.smartFacet),
350
+ );
351
+ if (existingFacet) {
352
+ // The facet already exists outside the dropdown, so just select it there
353
+ this.toggleSmartFacet(existingFacet[0], e.detail.details);
354
+ return;
355
+ }
356
+
357
+ // Otherwise, prepend a new smart facet for the selected option
358
+ this.smartFacets = [
359
+ [{ ...e.detail.smartFacet, selected: true }],
360
+ ...this.smartFacets,
361
+ ];
362
+
363
+ this.updateSelectedFacets(e.detail.details);
364
+ }
365
+
366
+ /**
367
+ * Handler for when any dropdown is clicked (whether button, caret, or menu item)
368
+ */
369
+ private dropdownClicked(e: CustomEvent<SmartFacetDropdown>): void {
370
+ log('smart bar: onDropdownClick', e.detail);
371
+ this.shadowRoot
372
+ ?.querySelectorAll('smart-facet-dropdown')
373
+ .forEach(dropdown => {
374
+ if (dropdown !== e.detail) {
375
+ log('closing', dropdown);
376
+ (dropdown as SmartFacetDropdown).close();
377
+ }
378
+ });
379
+ }
380
+
381
+ private filterToggleClicked(): void {
382
+ this.dispatchEvent(new CustomEvent('filtersToggled'));
383
+ }
384
+
385
+ //
386
+ // STYLES
387
+ //
388
+
389
+ static get styles(): CSSResultGroup {
390
+ return css`
391
+ #smart-facets-container {
392
+ display: flex;
393
+ align-items: center;
394
+ gap: 5px 10px;
395
+ padding: 10px 0;
396
+ white-space: nowrap;
397
+ overflow: scroll hidden;
398
+ scrollbar-width: none;
399
+ }
400
+
401
+ #filters-toggle {
402
+ margin: 0;
403
+ border: 0;
404
+ padding: 5px 8px;
405
+ border-radius: 5px;
406
+ background: white;
407
+ color: #2c2c2c;
408
+ border: 1px solid #194880;
409
+ font-size: 1.4rem;
410
+ font-family: inherit;
411
+ text-decoration: none;
412
+ cursor: pointer;
413
+ }
414
+
415
+ #filters-toggle.active {
416
+ background: #194880;
417
+ color: white;
418
+ }
419
+
420
+ #filters-toggle > svg {
421
+ width: 12px;
422
+ filter: invert(0.16667);
423
+ vertical-align: -1px;
424
+ }
425
+
426
+ #filters-toggle.active > svg {
427
+ filter: invert(1);
428
+ }
429
+
430
+ #filters-label {
431
+ font-size: 1.4rem;
432
+ font-weight: var(--smartFacetLabelFontWeight, normal);
433
+ margin: 0 -5px 0 0;
434
+ }
435
+ `;
436
+ }
437
+ }