@@ -16,13 +16,15 @@ import type { CollectionTitles } from '../../data-source/models';
1616import type { FacetOption , SelectedFacets } from '../../models' ;
1717import { updateSelectedFacetBucket } from '../../utils/facet-utils' ;
1818import { SmartQueryHeuristicGroup } from './smart-facet-heuristics' ;
19+ import type { SmartFacetDropdown } from './smart-facet-dropdown' ;
1920import type { SmartFacet , SmartFacetEvent } from './models' ;
21+ import { smartFacetEquals } from './smart-facet-equals' ;
22+ import { dedupe } from './dedupe' ;
2023import filterIcon from '../../assets/img/icons/filter' ;
2124
2225import './smart-facet-button' ;
2326import './smart-facet-dropdown' ;
24- import { smartFacetEquals } from './smart-facet-equals' ;
25- import { dedupe } from './dedupe' ;
27+ import { log } from '../../utils/log' ;
2628
2729const fieldPrefixes : Partial < Record < FacetOption , string > > = {
2830 collection : 'Collection: ' ,
@@ -73,9 +75,12 @@ export class SmartFacetBar extends LitElement {
7375 }
7476
7577 protected willUpdate ( changed : PropertyValues ) : void {
78+ let shouldUpdateSmartFacets = false ;
79+
7680 if ( changed . has ( 'query' ) ) {
77- this . updateSmartFacets ( ) ;
81+ log ( 'query change' , changed . get ( 'query' ) , this . query ) ;
7882 this . lastAggregations = undefined ;
83+ shouldUpdateSmartFacets = true ;
7984 }
8085
8186 if (
@@ -84,16 +89,25 @@ export class SmartFacetBar extends LitElement {
8489 this . aggregations &&
8590 Object . keys ( this . aggregations ) . length > 0
8691 ) {
92+ log ( 'aggs change' , changed . get ( 'aggregations' ) , this . aggregations ) ;
8793 this . lastAggregations = this . aggregations ;
94+ shouldUpdateSmartFacets = true ;
95+ }
96+
97+ if ( shouldUpdateSmartFacets ) {
98+ log ( 'should update smart facets, doing so...' ) ;
99+ this . updateSmartFacets ( ) ;
88100 }
89101 }
90102
91103 private async updateSmartFacets ( ) : Promise < void > {
92- console . log ( 'updating smart facets' ) ;
104+ log ( 'updating smart facets' ) ;
93105 if ( this . query ) {
94106 this . heuristicRecs =
95107 await new SmartQueryHeuristicGroup ( ) . getRecommendedFacets ( this . query ) ;
108+ log ( 'heuristic recs are' , this . heuristicRecs ) ;
96109 this . smartFacets = dedupe ( this . facetsToDisplay ) ;
110+ log ( 'smart facets are' , this . smartFacets ) ;
97111 }
98112 }
99113
@@ -129,6 +143,7 @@ export class SmartFacetBar extends LitElement {
129143 .labelPrefix = ${ fieldPrefixes [ facets [ 0 ] . facets [ 0 ] . facetType ] }
130144 .activeFacetRef = ${ facets [ 0 ] . facets [ 0 ] }
131145 @facetClick = ${ this . facetDropdownClicked }
146+ @dropdownClick = ${ this . onDropdownClick }
132147 > </ smart- facet- dropdown>
133148 ` ;
134149 }
@@ -147,8 +162,6 @@ export class SmartFacetBar extends LitElement {
147162 }
148163
149164 private get facetsToDisplay ( ) : SmartFacet [ ] [ ] {
150- if ( ! this . lastAggregations ) return [ ] ;
151-
152165 const facets : SmartFacet [ ] [ ] = [ ] ;
153166
154167 if ( this . heuristicRecs . length > 0 ) {
@@ -157,52 +170,54 @@ export class SmartFacetBar extends LitElement {
157170 }
158171 }
159172
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 ;
192197 }
193- return true ;
194- } ) ;
195198
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+ }
206221 }
207222 }
208223
@@ -222,10 +237,6 @@ export class SmartFacetBar extends LitElement {
222237 if ( title ) displayText = title ;
223238 }
224239
225- // if (prefix && fieldPrefixes[facetType]) {
226- // displayText = fieldPrefixes[facetType] + displayText;
227- // }
228-
229240 return {
230241 facetType,
231242 bucketKey : bucket . key . toString ( ) ,
@@ -236,10 +247,12 @@ export class SmartFacetBar extends LitElement {
236247 }
237248
238249 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+ }
243256
244257 for ( const facet of e . detail . details ) {
245258 this . selectedFacets = updateSelectedFacetBucket (
@@ -283,6 +296,18 @@ export class SmartFacetBar extends LitElement {
283296 this . dispatchEvent ( event ) ;
284297 }
285298
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+
286311 private filterToggleClicked ( ) : void {
287312 this . dispatchEvent ( new CustomEvent ( 'filtersToggled' ) ) ;
288313 }
0 commit comments