@internetarchive/collection-browser 2.18.3-alpha-webdev7768.5 → 2.18.3-alpha-webdev7768.7

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