@@ -16,13 +16,15 @@ import type { CollectionTitles } from '../../data-source/models';
16
16
import type { FacetOption , SelectedFacets } from '../../models' ;
17
17
import { updateSelectedFacetBucket } from '../../utils/facet-utils' ;
18
18
import { SmartQueryHeuristicGroup } from './smart-facet-heuristics' ;
19
+ import type { SmartFacetDropdown } from './smart-facet-dropdown' ;
19
20
import type { SmartFacet , SmartFacetEvent } from './models' ;
21
+ import { smartFacetEquals } from './smart-facet-equals' ;
22
+ import { dedupe } from './dedupe' ;
20
23
import filterIcon from '../../assets/img/icons/filter' ;
21
24
22
25
import './smart-facet-button' ;
23
26
import './smart-facet-dropdown' ;
24
- import { smartFacetEquals } from './smart-facet-equals' ;
25
- import { dedupe } from './dedupe' ;
27
+ import { log } from '../../utils/log' ;
26
28
27
29
const fieldPrefixes : Partial < Record < FacetOption , string > > = {
28
30
collection : 'Collection: ' ,
@@ -73,9 +75,12 @@ export class SmartFacetBar extends LitElement {
73
75
}
74
76
75
77
protected willUpdate ( changed : PropertyValues ) : void {
78
+ let shouldUpdateSmartFacets = false ;
79
+
76
80
if ( changed . has ( 'query' ) ) {
77
- this . updateSmartFacets ( ) ;
81
+ log ( 'query change' , changed . get ( 'query' ) , this . query ) ;
78
82
this . lastAggregations = undefined ;
83
+ shouldUpdateSmartFacets = true ;
79
84
}
80
85
81
86
if (
@@ -84,16 +89,25 @@ export class SmartFacetBar extends LitElement {
84
89
this . aggregations &&
85
90
Object . keys ( this . aggregations ) . length > 0
86
91
) {
92
+ log ( 'aggs change' , changed . get ( 'aggregations' ) , this . aggregations ) ;
87
93
this . lastAggregations = this . aggregations ;
94
+ shouldUpdateSmartFacets = true ;
95
+ }
96
+
97
+ if ( shouldUpdateSmartFacets ) {
98
+ log ( 'should update smart facets, doing so...' ) ;
99
+ this . updateSmartFacets ( ) ;
88
100
}
89
101
}
90
102
91
103
private async updateSmartFacets ( ) : Promise < void > {
92
- console . log ( 'updating smart facets' ) ;
104
+ log ( 'updating smart facets' ) ;
93
105
if ( this . query ) {
94
106
this . heuristicRecs =
95
107
await new SmartQueryHeuristicGroup ( ) . getRecommendedFacets ( this . query ) ;
108
+ log ( 'heuristic recs are' , this . heuristicRecs ) ;
96
109
this . smartFacets = dedupe ( this . facetsToDisplay ) ;
110
+ log ( 'smart facets are' , this . smartFacets ) ;
97
111
}
98
112
}
99
113
@@ -129,6 +143,7 @@ export class SmartFacetBar extends LitElement {
129
143
.labelPrefix = ${ fieldPrefixes [ facets [ 0 ] . facets [ 0 ] . facetType ] }
130
144
.activeFacetRef = ${ facets [ 0 ] . facets [ 0 ] }
131
145
@facetClick = ${ this . facetDropdownClicked }
146
+ @dropdownClick = ${ this . onDropdownClick }
132
147
> </ smart- facet- dropdown>
133
148
` ;
134
149
}
@@ -147,8 +162,6 @@ export class SmartFacetBar extends LitElement {
147
162
}
148
163
149
164
private get facetsToDisplay ( ) : SmartFacet [ ] [ ] {
150
- if ( ! this . lastAggregations ) return [ ] ;
151
-
152
165
const facets : SmartFacet [ ] [ ] = [ ] ;
153
166
154
167
if ( this . heuristicRecs . length > 0 ) {
@@ -157,52 +170,54 @@ export class SmartFacetBar extends LitElement {
157
170
}
158
171
}
159
172
160
- const keys = [
161
- 'mediatype' ,
162
- 'year' ,
163
- 'language' ,
164
- 'creator' ,
165
- 'subject' ,
166
- 'collection' ,
167
- ] ;
168
- for ( const key of keys ) {
169
- const agg = this . lastAggregations [ key ] ;
170
- if ( ! agg ) continue ;
171
- if ( agg . buckets . length === 0 ) continue ;
172
- if ( [ 'lending' , 'year_histogram' ] . includes ( key ) ) continue ;
173
- if ( typeof agg . buckets [ 0 ] === 'number' ) continue ;
174
-
175
- if (
176
- key === 'mediatype' &&
177
- this . selectedFacets &&
178
- Object . values ( this . selectedFacets . mediatype ) . some (
179
- bucket => bucket . state !== 'none'
180
- )
181
- ) {
182
- continue ;
183
- }
184
-
185
- const facetType = key as FacetOption ;
186
- const buckets = agg . buckets as Bucket [ ] ;
187
-
188
- const unusedBuckets = buckets . filter ( b => {
189
- const selectedFacetBucket = this . selectedFacets ?. [ facetType ] [ b . key ] ;
190
- if ( selectedFacetBucket && selectedFacetBucket . state !== 'none' ) {
191
- return false ;
173
+ if ( this . lastAggregations ) {
174
+ const keys = [
175
+ 'mediatype' ,
176
+ 'year' ,
177
+ 'language' ,
178
+ 'creator' ,
179
+ 'subject' ,
180
+ 'collection' ,
181
+ ] ;
182
+ for ( const key of keys ) {
183
+ const agg = this . lastAggregations [ key ] ;
184
+ if ( ! agg ) continue ;
185
+ if ( agg . buckets . length === 0 ) continue ;
186
+ if ( [ 'lending' , 'year_histogram' ] . includes ( key ) ) continue ;
187
+ if ( typeof agg . buckets [ 0 ] === 'number' ) continue ;
188
+
189
+ if (
190
+ key === 'mediatype' &&
191
+ this . selectedFacets &&
192
+ Object . values ( this . selectedFacets . mediatype ) . some (
193
+ bucket => bucket . state !== 'none'
194
+ )
195
+ ) {
196
+ continue ;
192
197
}
193
- return true ;
194
- } ) ;
195
198
196
- if ( facetType === 'mediatype' ) {
197
- facets . push (
198
- [ this . toSmartFacet ( facetType , [ unusedBuckets [ 0 ] ] ) ] ,
199
- [ this . toSmartFacet ( facetType , [ unusedBuckets [ 1 ] ] ) ]
200
- ) ;
201
- } else if ( facetType === 'collection' || facetType === 'subject' ) {
202
- const topBuckets = unusedBuckets . slice ( 0 , 5 ) ;
203
- facets . push ( topBuckets . map ( b => this . toSmartFacet ( facetType , [ b ] ) ) ) ;
204
- } else {
205
- facets . push ( [ this . toSmartFacet ( facetType , [ unusedBuckets [ 0 ] ] ) ] ) ;
199
+ const facetType = key as FacetOption ;
200
+ const buckets = agg . buckets as Bucket [ ] ;
201
+
202
+ const unusedBuckets = buckets . filter ( b => {
203
+ const selectedFacetBucket = this . selectedFacets ?. [ facetType ] [ b . key ] ;
204
+ if ( selectedFacetBucket && selectedFacetBucket . state !== 'none' ) {
205
+ return false ;
206
+ }
207
+ return true ;
208
+ } ) ;
209
+
210
+ if ( facetType === 'mediatype' ) {
211
+ facets . push (
212
+ [ this . toSmartFacet ( facetType , [ unusedBuckets [ 0 ] ] ) ] ,
213
+ [ this . toSmartFacet ( facetType , [ unusedBuckets [ 1 ] ] ) ]
214
+ ) ;
215
+ } else if ( facetType === 'collection' || facetType === 'subject' ) {
216
+ const topBuckets = unusedBuckets . slice ( 0 , 5 ) ;
217
+ facets . push ( topBuckets . map ( b => this . toSmartFacet ( facetType , [ b ] ) ) ) ;
218
+ } else {
219
+ facets . push ( [ this . toSmartFacet ( facetType , [ unusedBuckets [ 0 ] ] ) ] ) ;
220
+ }
206
221
}
207
222
}
208
223
@@ -222,10 +237,6 @@ export class SmartFacetBar extends LitElement {
222
237
if ( title ) displayText = title ;
223
238
}
224
239
225
- // if (prefix && fieldPrefixes[facetType]) {
226
- // displayText = fieldPrefixes[facetType] + displayText;
227
- // }
228
-
229
240
return {
230
241
facetType,
231
242
bucketKey : bucket . key . toString ( ) ,
@@ -236,10 +247,12 @@ export class SmartFacetBar extends LitElement {
236
247
}
237
248
238
249
private facetClicked ( e : CustomEvent < SmartFacetEvent > ) : void {
239
- this . smartFacets = [
240
- [ { ...e . detail . smartFacet , selected : ! e . detail . smartFacet . selected } ] ,
241
- ...this . smartFacets . filter ( f => f [ 0 ] !== e . detail . smartFacet ) ,
242
- ] ;
250
+ if ( ! e . detail . smartFacet . selected ) {
251
+ this . smartFacets = [
252
+ [ { ...e . detail . smartFacet , selected : true } ] ,
253
+ ...this . smartFacets . filter ( f => f [ 0 ] !== e . detail . smartFacet ) ,
254
+ ] ;
255
+ }
243
256
244
257
for ( const facet of e . detail . details ) {
245
258
this . selectedFacets = updateSelectedFacetBucket (
@@ -283,6 +296,18 @@ export class SmartFacetBar extends LitElement {
283
296
this . dispatchEvent ( event ) ;
284
297
}
285
298
299
+ private onDropdownClick ( e : CustomEvent < SmartFacetDropdown > ) : void {
300
+ log ( 'smart bar: onDropdownClick' , e . detail ) ;
301
+ this . shadowRoot
302
+ ?. querySelectorAll ( 'smart-facet-dropdown' )
303
+ . forEach ( dropdown => {
304
+ if ( dropdown !== e . detail ) {
305
+ log ( 'closing' , dropdown ) ;
306
+ ( dropdown as SmartFacetDropdown ) . close ( ) ;
307
+ }
308
+ } ) ;
309
+ }
310
+
286
311
private filterToggleClicked ( ) : void {
287
312
this . dispatchEvent ( new CustomEvent ( 'filtersToggled' ) ) ;
288
313
}
0 commit comments