@internetarchive/collection-browser 3.3.1 → 3.3.3
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.
- package/.editorconfig +29 -29
- package/.github/workflows/ci.yml +27 -27
- package/.github/workflows/gh-pages-main.yml +39 -39
- package/.github/workflows/npm-publish.yml +39 -39
- package/.github/workflows/pr-preview.yml +38 -38
- package/.husky/pre-commit +4 -4
- package/.prettierignore +1 -1
- package/LICENSE +661 -661
- package/README.md +83 -83
- package/dist/src/collection-browser.js +683 -683
- package/dist/src/collection-browser.js.map +1 -1
- package/dist/src/collection-facets/more-facets-content.js +118 -118
- package/dist/src/collection-facets/more-facets-content.js.map +1 -1
- package/dist/src/collection-facets.js +265 -266
- package/dist/src/collection-facets.js.map +1 -1
- package/dist/src/data-source/collection-browser-data-source.js.map +1 -1
- package/dist/src/data-source/collection-browser-query-state.js.map +1 -1
- package/dist/src/data-source/models.js.map +1 -1
- package/dist/src/tiles/base-tile-component.js.map +1 -1
- package/dist/src/tiles/grid/account-tile.js +36 -36
- package/dist/src/tiles/grid/account-tile.js.map +1 -1
- package/dist/src/tiles/grid/collection-tile.js +77 -77
- package/dist/src/tiles/grid/collection-tile.js.map +1 -1
- package/dist/src/tiles/grid/item-tile.js +137 -137
- package/dist/src/tiles/grid/item-tile.js.map +1 -1
- package/dist/src/tiles/hover/hover-pane-controller.d.ts +9 -1
- package/dist/src/tiles/hover/hover-pane-controller.js +105 -37
- package/dist/src/tiles/hover/hover-pane-controller.js.map +1 -1
- package/dist/src/tiles/hover/tile-hover-pane.d.ts +1 -0
- package/dist/src/tiles/hover/tile-hover-pane.js +115 -112
- package/dist/src/tiles/hover/tile-hover-pane.js.map +1 -1
- package/dist/src/tiles/list/tile-list-compact.js +99 -99
- package/dist/src/tiles/list/tile-list-compact.js.map +1 -1
- package/dist/src/tiles/list/tile-list.js +297 -297
- package/dist/src/tiles/list/tile-list.js.map +1 -1
- package/dist/src/tiles/tile-dispatcher.d.ts +4 -1
- package/dist/src/tiles/tile-dispatcher.js +231 -204
- package/dist/src/tiles/tile-dispatcher.js.map +1 -1
- package/dist/src/utils/format-date.js.map +1 -1
- package/dist/test/collection-browser.test.js +189 -189
- package/dist/test/collection-browser.test.js.map +1 -1
- package/dist/test/tiles/grid/item-tile.test.js +77 -77
- package/dist/test/tiles/grid/item-tile.test.js.map +1 -1
- package/dist/test/tiles/hover/hover-pane-controller.test.js +68 -21
- package/dist/test/tiles/hover/hover-pane-controller.test.js.map +1 -1
- package/dist/test/tiles/list/tile-list-compact.test.js +70 -70
- package/dist/test/tiles/list/tile-list-compact.test.js.map +1 -1
- package/dist/test/tiles/list/tile-list.test.js +126 -126
- package/dist/test/tiles/list/tile-list.test.js.map +1 -1
- package/dist/test/tiles/tile-dispatcher.test.js +130 -52
- package/dist/test/tiles/tile-dispatcher.test.js.map +1 -1
- package/dist/test/utils/format-date.test.js.map +1 -1
- package/eslint.config.mjs +53 -53
- package/index.html +24 -24
- package/local.archive.org.cert +86 -86
- package/local.archive.org.key +27 -27
- package/package.json +118 -117
- package/renovate.json +6 -6
- package/src/collection-browser.ts +2829 -2829
- package/src/collection-facets/more-facets-content.ts +639 -639
- package/src/collection-facets.ts +994 -995
- package/src/data-source/collection-browser-data-source.ts +1401 -1401
- package/src/data-source/collection-browser-query-state.ts +65 -65
- package/src/data-source/models.ts +43 -43
- package/src/tiles/base-tile-component.ts +65 -65
- package/src/tiles/grid/account-tile.ts +113 -113
- package/src/tiles/grid/collection-tile.ts +163 -163
- package/src/tiles/grid/item-tile.ts +340 -340
- package/src/tiles/hover/hover-pane-controller.ts +613 -517
- package/src/tiles/hover/tile-hover-pane.ts +184 -180
- package/src/tiles/list/tile-list-compact.ts +239 -239
- package/src/tiles/list/tile-list.ts +700 -700
- package/src/tiles/tile-dispatcher.ts +517 -490
- package/src/utils/format-date.ts +62 -62
- package/test/collection-browser.test.ts +2403 -2403
- package/test/tiles/grid/item-tile.test.ts +520 -520
- package/test/tiles/hover/hover-pane-controller.test.ts +418 -353
- package/test/tiles/list/tile-list-compact.test.ts +282 -282
- package/test/tiles/list/tile-list.test.ts +552 -552
- package/test/tiles/tile-dispatcher.test.ts +283 -187
- package/test/utils/format-date.test.ts +89 -89
- package/tsconfig.json +20 -20
- package/web-dev-server.config.mjs +30 -30
- package/web-test-runner.config.mjs +41 -41
|
@@ -1,639 +1,639 @@
|
|
|
1
|
-
import {
|
|
2
|
-
css,
|
|
3
|
-
CSSResultGroup,
|
|
4
|
-
html,
|
|
5
|
-
LitElement,
|
|
6
|
-
nothing,
|
|
7
|
-
PropertyValues,
|
|
8
|
-
TemplateResult,
|
|
9
|
-
} from 'lit';
|
|
10
|
-
import { customElement, property, state } from 'lit/decorators.js';
|
|
11
|
-
import {
|
|
12
|
-
Aggregation,
|
|
13
|
-
Bucket,
|
|
14
|
-
SearchServiceInterface,
|
|
15
|
-
SearchParams,
|
|
16
|
-
SearchType,
|
|
17
|
-
AggregationSortType,
|
|
18
|
-
FilterMap,
|
|
19
|
-
PageType,
|
|
20
|
-
} from '@internetarchive/search-service';
|
|
21
|
-
import type { ModalManagerInterface } from '@internetarchive/modal-manager';
|
|
22
|
-
import type { AnalyticsManagerInterface } from '@internetarchive/analytics-manager';
|
|
23
|
-
import { msg } from '@lit/localize';
|
|
24
|
-
import {
|
|
25
|
-
SelectedFacets,
|
|
26
|
-
FacetGroup,
|
|
27
|
-
FacetBucket,
|
|
28
|
-
FacetOption,
|
|
29
|
-
facetTitles,
|
|
30
|
-
suppressedCollections,
|
|
31
|
-
valueFacetSort,
|
|
32
|
-
defaultFacetSort,
|
|
33
|
-
getDefaultSelectedFacets,
|
|
34
|
-
FacetEventDetails,
|
|
35
|
-
} from '../models';
|
|
36
|
-
import type {
|
|
37
|
-
CollectionTitles,
|
|
38
|
-
PageSpecifierParams,
|
|
39
|
-
TVChannelAliases,
|
|
40
|
-
} from '../data-source/models';
|
|
41
|
-
import '@internetarchive/ia-activity-indicator';
|
|
42
|
-
import './more-facets-pagination';
|
|
43
|
-
import './facets-template';
|
|
44
|
-
import {
|
|
45
|
-
analyticsActions,
|
|
46
|
-
analyticsCategories,
|
|
47
|
-
} from '../utils/analytics-events';
|
|
48
|
-
import './toggle-switch';
|
|
49
|
-
import { srOnlyStyle } from '../styles/sr-only';
|
|
50
|
-
import {
|
|
51
|
-
mergeSelectedFacets,
|
|
52
|
-
sortBucketsBySelectionState,
|
|
53
|
-
updateSelectedFacetBucket,
|
|
54
|
-
} from '../utils/facet-utils';
|
|
55
|
-
import {
|
|
56
|
-
MORE_FACETS__DEFAULT_PAGE_SIZE,
|
|
57
|
-
MORE_FACETS__MAX_AGGREGATIONS,
|
|
58
|
-
} from './models';
|
|
59
|
-
|
|
60
|
-
@customElement('more-facets-content')
|
|
61
|
-
export class MoreFacetsContent extends LitElement {
|
|
62
|
-
@property({ type: String }) facetKey?: FacetOption;
|
|
63
|
-
|
|
64
|
-
@property({ type: String }) query?: string;
|
|
65
|
-
|
|
66
|
-
@property({ type: Array }) identifiers?: string[];
|
|
67
|
-
|
|
68
|
-
@property({ type: Object }) filterMap?: FilterMap;
|
|
69
|
-
|
|
70
|
-
@property({ type: Number }) searchType?: SearchType;
|
|
71
|
-
|
|
72
|
-
@property({ type: Object }) pageSpecifierParams?: PageSpecifierParams;
|
|
73
|
-
|
|
74
|
-
@property({ type: Object })
|
|
75
|
-
collectionTitles?: CollectionTitles;
|
|
76
|
-
|
|
77
|
-
@property({ type: Object })
|
|
78
|
-
tvChannelAliases?: TVChannelAliases;
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* Maximum number of facets to show per page within the modal.
|
|
82
|
-
*/
|
|
83
|
-
@property({ type: Number }) facetsPerPage = MORE_FACETS__DEFAULT_PAGE_SIZE;
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* Whether we are waiting for facet data to load.
|
|
87
|
-
* We begin with this set to true so that we show an initial loading indicator.
|
|
88
|
-
*/
|
|
89
|
-
@property({ type: Boolean }) facetsLoading = true;
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* The set of pre-existing facet selections (including both selected & negated facets).
|
|
93
|
-
*/
|
|
94
|
-
@property({ type: Object }) selectedFacets?: SelectedFacets;
|
|
95
|
-
|
|
96
|
-
@property({ type: Number }) sortedBy: AggregationSortType =
|
|
97
|
-
AggregationSortType.COUNT;
|
|
98
|
-
|
|
99
|
-
@property({ type: Boolean }) isTvSearch = false;
|
|
100
|
-
|
|
101
|
-
@property({ type: Object }) modalManager?: ModalManagerInterface;
|
|
102
|
-
|
|
103
|
-
@property({ type: Object }) searchService?: SearchServiceInterface;
|
|
104
|
-
|
|
105
|
-
@property({ type: Object, attribute: false })
|
|
106
|
-
analyticsHandler?: AnalyticsManagerInterface;
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* The full set of aggregations received from the search service
|
|
110
|
-
*/
|
|
111
|
-
@state() private aggregations?: Record<string, Aggregation>;
|
|
112
|
-
|
|
113
|
-
/**
|
|
114
|
-
* A FacetGroup storing the full set of facet buckets to be shown on the dialog.
|
|
115
|
-
*/
|
|
116
|
-
@state() private facetGroup?: FacetGroup;
|
|
117
|
-
|
|
118
|
-
/**
|
|
119
|
-
* An object holding any changes the patron has made to their facet selections
|
|
120
|
-
* within the modal dialog but which they have not yet applied. These are
|
|
121
|
-
* eventually merged into the existing `selectedFacets` when the patron applies
|
|
122
|
-
* their changes, or discarded if they cancel/close the dialog.
|
|
123
|
-
*/
|
|
124
|
-
@state() private unappliedFacetChanges: SelectedFacets =
|
|
125
|
-
getDefaultSelectedFacets();
|
|
126
|
-
|
|
127
|
-
/**
|
|
128
|
-
* Which page of facets we are showing.
|
|
129
|
-
*/
|
|
130
|
-
@state() private pageNumber = 1;
|
|
131
|
-
|
|
132
|
-
willUpdate(changed: PropertyValues): void {
|
|
133
|
-
if (
|
|
134
|
-
changed.has('aggregations') ||
|
|
135
|
-
changed.has('facetsPerPage') ||
|
|
136
|
-
changed.has('sortedBy') ||
|
|
137
|
-
changed.has('selectedFacets') ||
|
|
138
|
-
changed.has('unappliedFacetChanges')
|
|
139
|
-
) {
|
|
140
|
-
// Convert the merged selected facets & aggregations into a facet group, and
|
|
141
|
-
// store it for reuse across pages.
|
|
142
|
-
this.facetGroup = this.mergedFacets;
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
updated(changed: PropertyValues): void {
|
|
147
|
-
// If any of the search properties change, it triggers a facet fetch
|
|
148
|
-
if (
|
|
149
|
-
changed.has('facetKey') ||
|
|
150
|
-
changed.has('query') ||
|
|
151
|
-
changed.has('searchType') ||
|
|
152
|
-
changed.has('filterMap')
|
|
153
|
-
) {
|
|
154
|
-
this.facetsLoading = true;
|
|
155
|
-
this.pageNumber = 1;
|
|
156
|
-
this.sortedBy = defaultFacetSort[this.facetKey as FacetOption];
|
|
157
|
-
|
|
158
|
-
this.updateSpecificFacets();
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
firstUpdated(): void {
|
|
163
|
-
this.setupEscapeListeners();
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
/**
|
|
167
|
-
* Close more facets modal on Escape click
|
|
168
|
-
*/
|
|
169
|
-
private setupEscapeListeners() {
|
|
170
|
-
if (this.modalManager) {
|
|
171
|
-
document.addEventListener('keydown', (e: KeyboardEvent) => {
|
|
172
|
-
if (e.key === 'Escape') {
|
|
173
|
-
this.modalManager?.closeModal();
|
|
174
|
-
}
|
|
175
|
-
});
|
|
176
|
-
} else {
|
|
177
|
-
document.removeEventListener('keydown', () => {});
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
/**
|
|
182
|
-
* Whether facet requests are for the search_results page type (either defaulted or explicitly).
|
|
183
|
-
*/
|
|
184
|
-
private get isSearchResultsPage(): boolean {
|
|
185
|
-
// Default page type is search_results when none is specified, so we check
|
|
186
|
-
// for undefined as well.
|
|
187
|
-
const pageType: PageType | undefined = this.pageSpecifierParams?.pageType;
|
|
188
|
-
return pageType === undefined || pageType === 'search_results';
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
/**
|
|
192
|
-
* Get specific facets data from search-service API based of currently query params
|
|
193
|
-
* - this.aggregations - hold result of search service and being used for further processing.
|
|
194
|
-
*/
|
|
195
|
-
async updateSpecificFacets(): Promise<void> {
|
|
196
|
-
if (!this.facetKey) return; // Can't fetch facets if we don't know what type of facets we need!
|
|
197
|
-
|
|
198
|
-
const trimmedQuery = this.query?.trim();
|
|
199
|
-
if (!trimmedQuery && this.isSearchResultsPage) return; // The search page _requires_ a query
|
|
200
|
-
|
|
201
|
-
const aggregations = {
|
|
202
|
-
simpleParams: [this.facetKey],
|
|
203
|
-
};
|
|
204
|
-
const aggregationsSize = MORE_FACETS__MAX_AGGREGATIONS; // Only request the 10K highest-count facets
|
|
205
|
-
|
|
206
|
-
const params: SearchParams = {
|
|
207
|
-
...this.pageSpecifierParams,
|
|
208
|
-
query: trimmedQuery || '',
|
|
209
|
-
identifiers: this.identifiers,
|
|
210
|
-
filters: this.filterMap,
|
|
211
|
-
aggregations,
|
|
212
|
-
aggregationsSize,
|
|
213
|
-
rows: 0, // todo - do we want server-side pagination with offset/page/limit flag?
|
|
214
|
-
};
|
|
215
|
-
|
|
216
|
-
const results = await this.searchService?.search(params, this.searchType);
|
|
217
|
-
this.aggregations = results?.success?.response.aggregations;
|
|
218
|
-
this.facetsLoading = false;
|
|
219
|
-
|
|
220
|
-
const collectionTitles = results?.success?.response?.collectionTitles;
|
|
221
|
-
if (collectionTitles) {
|
|
222
|
-
for (const [id, title] of Object.entries(collectionTitles)) {
|
|
223
|
-
this.collectionTitles?.set(id, title);
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
/**
|
|
229
|
-
* Handler for page number changes from the pagination widget.
|
|
230
|
-
*/
|
|
231
|
-
private pageNumberClicked(e: CustomEvent<{ page: number }>) {
|
|
232
|
-
const page = e?.detail?.page;
|
|
233
|
-
if (page) {
|
|
234
|
-
this.pageNumber = Number(page);
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
this.analyticsHandler?.sendEvent({
|
|
238
|
-
category: analyticsCategories.default,
|
|
239
|
-
action: analyticsActions.moreFacetsPageChange,
|
|
240
|
-
label: `${this.pageNumber}`,
|
|
241
|
-
});
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
/**
|
|
245
|
-
* Combines the selected facets with the aggregations to create a single list of facets
|
|
246
|
-
*/
|
|
247
|
-
private get mergedFacets(): FacetGroup | undefined {
|
|
248
|
-
if (!this.facetKey || !this.selectedFacets) return undefined;
|
|
249
|
-
|
|
250
|
-
const { selectedFacetGroup, aggregationFacetGroup } = this;
|
|
251
|
-
|
|
252
|
-
// If we don't have any aggregations, then there is nothing to show yet
|
|
253
|
-
if (!aggregationFacetGroup) return undefined;
|
|
254
|
-
|
|
255
|
-
// Start with either the selected group if we have one, or the aggregate group otherwise
|
|
256
|
-
const facetGroup = { ...(selectedFacetGroup ?? aggregationFacetGroup) };
|
|
257
|
-
|
|
258
|
-
// Attach the counts to the selected buckets
|
|
259
|
-
const bucketsWithCount =
|
|
260
|
-
selectedFacetGroup?.buckets.map(bucket => {
|
|
261
|
-
const selectedBucket = aggregationFacetGroup.buckets.find(
|
|
262
|
-
b => b.key === bucket.key,
|
|
263
|
-
);
|
|
264
|
-
return selectedBucket
|
|
265
|
-
? {
|
|
266
|
-
...bucket,
|
|
267
|
-
count: selectedBucket.count,
|
|
268
|
-
}
|
|
269
|
-
: bucket;
|
|
270
|
-
}) ?? [];
|
|
271
|
-
|
|
272
|
-
// Sort the buckets by selection state
|
|
273
|
-
// We do this *prior* to considering unapplied selections, because we want the facets
|
|
274
|
-
// to remain in position when they are selected/unselected, rather than re-sort themselves.
|
|
275
|
-
sortBucketsBySelectionState(bucketsWithCount, this.sortedBy);
|
|
276
|
-
|
|
277
|
-
// Append any additional buckets that were not selected
|
|
278
|
-
aggregationFacetGroup.buckets.forEach(bucket => {
|
|
279
|
-
const existingBucket = selectedFacetGroup?.buckets.find(
|
|
280
|
-
b => b.key === bucket.key,
|
|
281
|
-
);
|
|
282
|
-
if (existingBucket) return;
|
|
283
|
-
bucketsWithCount.push(bucket);
|
|
284
|
-
});
|
|
285
|
-
|
|
286
|
-
// Apply any unapplied selections that appear on this page
|
|
287
|
-
const unappliedBuckets = this.unappliedFacetChanges[this.facetKey];
|
|
288
|
-
for (const [index, bucket] of bucketsWithCount.entries()) {
|
|
289
|
-
const unappliedBucket = unappliedBuckets?.[bucket.key];
|
|
290
|
-
if (unappliedBucket) {
|
|
291
|
-
bucketsWithCount[index] = { ...unappliedBucket };
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
// For TV creator facets, uppercase the display text
|
|
296
|
-
if (this.facetKey === 'creator' && this.isTvSearch) {
|
|
297
|
-
bucketsWithCount.forEach(b => {
|
|
298
|
-
b.displayText = (b.displayText ?? b.key)?.toLocaleUpperCase();
|
|
299
|
-
|
|
300
|
-
const channelLabel = this.tvChannelAliases?.get(b.displayText);
|
|
301
|
-
if (channelLabel && channelLabel !== b.displayText) {
|
|
302
|
-
b.extraNote = `(${channelLabel})`;
|
|
303
|
-
}
|
|
304
|
-
});
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
facetGroup.buckets = bucketsWithCount;
|
|
308
|
-
return facetGroup;
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
/**
|
|
312
|
-
* Converts the selected facets for the current facet key to a `FacetGroup`,
|
|
313
|
-
* which is easier to work with.
|
|
314
|
-
*/
|
|
315
|
-
private get selectedFacetGroup(): FacetGroup | undefined {
|
|
316
|
-
if (!this.selectedFacets || !this.facetKey) return undefined;
|
|
317
|
-
|
|
318
|
-
const selectedFacetsForKey = this.selectedFacets[this.facetKey];
|
|
319
|
-
if (!selectedFacetsForKey) return undefined;
|
|
320
|
-
|
|
321
|
-
const facetGroupTitle = facetTitles[this.facetKey];
|
|
322
|
-
|
|
323
|
-
const buckets: FacetBucket[] = Object.entries(selectedFacetsForKey).map(
|
|
324
|
-
([value, data]) => {
|
|
325
|
-
const displayText: string = value;
|
|
326
|
-
return {
|
|
327
|
-
displayText,
|
|
328
|
-
key: value,
|
|
329
|
-
count: data?.count,
|
|
330
|
-
state: data?.state,
|
|
331
|
-
};
|
|
332
|
-
},
|
|
333
|
-
);
|
|
334
|
-
|
|
335
|
-
return {
|
|
336
|
-
title: facetGroupTitle,
|
|
337
|
-
key: this.facetKey,
|
|
338
|
-
buckets,
|
|
339
|
-
};
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
/**
|
|
343
|
-
* Converts the raw `aggregations` for the current facet key to a `FacetGroup`,
|
|
344
|
-
* which is easier to work with.
|
|
345
|
-
*/
|
|
346
|
-
private get aggregationFacetGroup(): FacetGroup | undefined {
|
|
347
|
-
if (!this.aggregations || !this.facetKey) return undefined;
|
|
348
|
-
|
|
349
|
-
const currentAggregation = this.aggregations[this.facetKey];
|
|
350
|
-
if (!currentAggregation) return undefined;
|
|
351
|
-
|
|
352
|
-
const facetGroupTitle = facetTitles[this.facetKey];
|
|
353
|
-
|
|
354
|
-
// Order the facets according to the current sort option
|
|
355
|
-
let sortedBuckets = currentAggregation.getSortedBuckets(
|
|
356
|
-
this.sortedBy,
|
|
357
|
-
) as Bucket[];
|
|
358
|
-
|
|
359
|
-
if (this.facetKey === 'collection') {
|
|
360
|
-
// we are not showing fav- collections or certain deemphasized collections in facets
|
|
361
|
-
sortedBuckets = sortedBuckets?.filter(bucket => {
|
|
362
|
-
const bucketKey = bucket?.key?.toString();
|
|
363
|
-
return (
|
|
364
|
-
!suppressedCollections[bucketKey] && !bucketKey?.startsWith('fav-')
|
|
365
|
-
);
|
|
366
|
-
});
|
|
367
|
-
}
|
|
368
|
-
|
|
369
|
-
// Construct the array of facet buckets from the aggregation buckets
|
|
370
|
-
const facetBuckets: FacetBucket[] = sortedBuckets.map(bucket => {
|
|
371
|
-
const bucketKeyStr = `${bucket.key}`;
|
|
372
|
-
return {
|
|
373
|
-
displayText: `${bucketKeyStr}`,
|
|
374
|
-
key: `${bucketKeyStr}`,
|
|
375
|
-
count: bucket.doc_count,
|
|
376
|
-
state: 'none',
|
|
377
|
-
};
|
|
378
|
-
});
|
|
379
|
-
|
|
380
|
-
return {
|
|
381
|
-
title: facetGroupTitle,
|
|
382
|
-
key: this.facetKey,
|
|
383
|
-
buckets: facetBuckets,
|
|
384
|
-
};
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
/**
|
|
388
|
-
* Returns a FacetGroup representing only the current page of facet buckets to show.
|
|
389
|
-
*/
|
|
390
|
-
private get facetGroupForCurrentPage(): FacetGroup | undefined {
|
|
391
|
-
const { facetGroup } = this;
|
|
392
|
-
if (!facetGroup) return undefined;
|
|
393
|
-
|
|
394
|
-
// Slice out only the current page of facet buckets
|
|
395
|
-
const firstBucketIndexOnPage = (this.pageNumber - 1) * this.facetsPerPage;
|
|
396
|
-
const truncatedBuckets = facetGroup.buckets.slice(
|
|
397
|
-
firstBucketIndexOnPage,
|
|
398
|
-
firstBucketIndexOnPage + this.facetsPerPage,
|
|
399
|
-
);
|
|
400
|
-
|
|
401
|
-
return {
|
|
402
|
-
...facetGroup,
|
|
403
|
-
buckets: truncatedBuckets,
|
|
404
|
-
};
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
private get moreFacetsTemplate(): TemplateResult {
|
|
408
|
-
return html`
|
|
409
|
-
<facets-template
|
|
410
|
-
.facetGroup=${this.facetGroupForCurrentPage}
|
|
411
|
-
.selectedFacets=${this.selectedFacets}
|
|
412
|
-
.collectionTitles=${this.collectionTitles}
|
|
413
|
-
@facetClick=${(e: CustomEvent<FacetEventDetails>) => {
|
|
414
|
-
if (this.facetKey) {
|
|
415
|
-
this.unappliedFacetChanges = updateSelectedFacetBucket(
|
|
416
|
-
this.unappliedFacetChanges,
|
|
417
|
-
this.facetKey,
|
|
418
|
-
e.detail.bucket,
|
|
419
|
-
);
|
|
420
|
-
}
|
|
421
|
-
}}
|
|
422
|
-
></facets-template>
|
|
423
|
-
`;
|
|
424
|
-
}
|
|
425
|
-
|
|
426
|
-
private get loaderTemplate(): TemplateResult {
|
|
427
|
-
return html`<div class="facets-loader">
|
|
428
|
-
<ia-activity-indicator .mode=${'processing'}></ia-activity-indicator>
|
|
429
|
-
</div> `;
|
|
430
|
-
}
|
|
431
|
-
|
|
432
|
-
/**
|
|
433
|
-
* How many pages of facets to show in the modal pagination widget
|
|
434
|
-
*/
|
|
435
|
-
private get paginationSize(): number {
|
|
436
|
-
if (!this.aggregations || !this.facetKey) return 0;
|
|
437
|
-
|
|
438
|
-
// Calculate the appropriate number of pages to show in the modal pagination widget
|
|
439
|
-
const length = this.aggregations[this.facetKey]?.buckets.length;
|
|
440
|
-
return Math.ceil(length / this.facetsPerPage);
|
|
441
|
-
}
|
|
442
|
-
|
|
443
|
-
// render pagination if more then 1 page
|
|
444
|
-
private get facetsPaginationTemplate() {
|
|
445
|
-
return this.paginationSize > 1
|
|
446
|
-
? html`<more-facets-pagination
|
|
447
|
-
.size=${this.paginationSize}
|
|
448
|
-
.currentPage=${1}
|
|
449
|
-
@pageNumberClicked=${this.pageNumberClicked}
|
|
450
|
-
></more-facets-pagination>`
|
|
451
|
-
: nothing;
|
|
452
|
-
}
|
|
453
|
-
|
|
454
|
-
private get footerTemplate() {
|
|
455
|
-
if (this.paginationSize > 0) {
|
|
456
|
-
return html`${this.facetsPaginationTemplate}
|
|
457
|
-
<div class="footer">
|
|
458
|
-
<button
|
|
459
|
-
class="btn btn-cancel"
|
|
460
|
-
type="button"
|
|
461
|
-
@click=${this.cancelClick}
|
|
462
|
-
>
|
|
463
|
-
Cancel
|
|
464
|
-
</button>
|
|
465
|
-
<button
|
|
466
|
-
class="btn btn-submit"
|
|
467
|
-
type="button"
|
|
468
|
-
@click=${this.applySearchFacetsClicked}
|
|
469
|
-
>
|
|
470
|
-
Apply filters
|
|
471
|
-
</button>
|
|
472
|
-
</div> `;
|
|
473
|
-
}
|
|
474
|
-
|
|
475
|
-
return nothing;
|
|
476
|
-
}
|
|
477
|
-
|
|
478
|
-
private sortFacetAggregation(facetSortType: AggregationSortType) {
|
|
479
|
-
this.sortedBy = facetSortType;
|
|
480
|
-
this.dispatchEvent(
|
|
481
|
-
new CustomEvent('sortedFacets', { detail: this.sortedBy }),
|
|
482
|
-
);
|
|
483
|
-
}
|
|
484
|
-
|
|
485
|
-
private get modalHeaderTemplate(): TemplateResult {
|
|
486
|
-
const facetSort =
|
|
487
|
-
this.sortedBy ?? defaultFacetSort[this.facetKey as FacetOption];
|
|
488
|
-
const defaultSwitchSide =
|
|
489
|
-
facetSort === AggregationSortType.COUNT ? 'left' : 'right';
|
|
490
|
-
|
|
491
|
-
return html`<span class="sr-only">${msg('More facets for:')}</span>
|
|
492
|
-
<span class="title">
|
|
493
|
-
${this.facetGroup?.title}
|
|
494
|
-
|
|
495
|
-
<label class="sort-label">${msg('Sort by:')}</label>
|
|
496
|
-
${this.facetKey
|
|
497
|
-
? html`<toggle-switch
|
|
498
|
-
class="sort-toggle"
|
|
499
|
-
leftValue=${AggregationSortType.COUNT}
|
|
500
|
-
leftLabel="Count"
|
|
501
|
-
rightValue=${valueFacetSort[this.facetKey]}
|
|
502
|
-
.rightLabel=${this.facetGroup?.title}
|
|
503
|
-
side=${defaultSwitchSide}
|
|
504
|
-
@change=${(e: CustomEvent<string>) => {
|
|
505
|
-
this.sortFacetAggregation(
|
|
506
|
-
Number(e.detail) as AggregationSortType,
|
|
507
|
-
);
|
|
508
|
-
}}
|
|
509
|
-
></toggle-switch>`
|
|
510
|
-
: nothing}
|
|
511
|
-
</span>`;
|
|
512
|
-
}
|
|
513
|
-
|
|
514
|
-
render() {
|
|
515
|
-
return html`
|
|
516
|
-
${this.facetsLoading
|
|
517
|
-
? this.loaderTemplate
|
|
518
|
-
: html`
|
|
519
|
-
<section id="more-facets">
|
|
520
|
-
<div class="header-content">${this.modalHeaderTemplate}</div>
|
|
521
|
-
<div class="facets-content">${this.moreFacetsTemplate}</div>
|
|
522
|
-
${this.footerTemplate}
|
|
523
|
-
</section>
|
|
524
|
-
`}
|
|
525
|
-
`;
|
|
526
|
-
}
|
|
527
|
-
|
|
528
|
-
private applySearchFacetsClicked() {
|
|
529
|
-
const mergedSelections = mergeSelectedFacets(
|
|
530
|
-
this.selectedFacets,
|
|
531
|
-
this.unappliedFacetChanges,
|
|
532
|
-
);
|
|
533
|
-
|
|
534
|
-
const event = new CustomEvent<SelectedFacets>('facetsChanged', {
|
|
535
|
-
detail: mergedSelections,
|
|
536
|
-
bubbles: true,
|
|
537
|
-
composed: true,
|
|
538
|
-
});
|
|
539
|
-
this.dispatchEvent(event);
|
|
540
|
-
|
|
541
|
-
// Reset the unapplied changes back to default, now that they have been applied
|
|
542
|
-
this.unappliedFacetChanges = getDefaultSelectedFacets();
|
|
543
|
-
|
|
544
|
-
this.modalManager?.closeModal();
|
|
545
|
-
this.analyticsHandler?.sendEvent({
|
|
546
|
-
category: analyticsCategories.default,
|
|
547
|
-
action: `${analyticsActions.applyMoreFacetsModal}`,
|
|
548
|
-
label: `${this.facetKey}`,
|
|
549
|
-
});
|
|
550
|
-
}
|
|
551
|
-
|
|
552
|
-
private cancelClick() {
|
|
553
|
-
// Reset the unapplied changes back to default
|
|
554
|
-
this.unappliedFacetChanges = getDefaultSelectedFacets();
|
|
555
|
-
|
|
556
|
-
this.modalManager?.closeModal();
|
|
557
|
-
this.analyticsHandler?.sendEvent({
|
|
558
|
-
category: analyticsCategories.default,
|
|
559
|
-
action: analyticsActions.closeMoreFacetsModal,
|
|
560
|
-
label: `${this.facetKey}`,
|
|
561
|
-
});
|
|
562
|
-
}
|
|
563
|
-
|
|
564
|
-
static get styles(): CSSResultGroup {
|
|
565
|
-
const modalSubmitButton = css`var(--primaryButtonBGColor, #194880)`;
|
|
566
|
-
|
|
567
|
-
return [
|
|
568
|
-
srOnlyStyle,
|
|
569
|
-
css`
|
|
570
|
-
section#more-facets {
|
|
571
|
-
overflow: auto;
|
|
572
|
-
padding: 10px; /* leaves room for scroll bar to appear without overlaying on content */
|
|
573
|
-
--facetsColumnCount: 3;
|
|
574
|
-
}
|
|
575
|
-
.header-content .title {
|
|
576
|
-
display: block;
|
|
577
|
-
text-align: left;
|
|
578
|
-
font-size: 1.8rem;
|
|
579
|
-
padding: 0 10px;
|
|
580
|
-
font-weight: bold;
|
|
581
|
-
}
|
|
582
|
-
|
|
583
|
-
.sort-label {
|
|
584
|
-
margin-left: 20px;
|
|
585
|
-
font-size: 1.3rem;
|
|
586
|
-
}
|
|
587
|
-
|
|
588
|
-
.sort-toggle {
|
|
589
|
-
font-weight: normal;
|
|
590
|
-
}
|
|
591
|
-
|
|
592
|
-
.facets-content {
|
|
593
|
-
font-size: 1.2rem;
|
|
594
|
-
max-height: 300px;
|
|
595
|
-
overflow: auto;
|
|
596
|
-
padding: 10px;
|
|
597
|
-
}
|
|
598
|
-
.facets-loader {
|
|
599
|
-
margin-bottom: 20px;
|
|
600
|
-
width: 70px;
|
|
601
|
-
display: block;
|
|
602
|
-
margin-left: auto;
|
|
603
|
-
margin-right: auto;
|
|
604
|
-
}
|
|
605
|
-
.btn {
|
|
606
|
-
border: none;
|
|
607
|
-
padding: 10px;
|
|
608
|
-
margin-bottom: 10px;
|
|
609
|
-
width: auto;
|
|
610
|
-
border-radius: 4px;
|
|
611
|
-
cursor: pointer;
|
|
612
|
-
}
|
|
613
|
-
.btn-cancel {
|
|
614
|
-
background-color: #2c2c2c;
|
|
615
|
-
color: white;
|
|
616
|
-
}
|
|
617
|
-
.btn-submit {
|
|
618
|
-
background-color: ${modalSubmitButton};
|
|
619
|
-
color: white;
|
|
620
|
-
}
|
|
621
|
-
.footer {
|
|
622
|
-
text-align: center;
|
|
623
|
-
margin-top: 10px;
|
|
624
|
-
}
|
|
625
|
-
|
|
626
|
-
@media (max-width: 560px) {
|
|
627
|
-
section#more-facets {
|
|
628
|
-
max-height: 450px;
|
|
629
|
-
--facetsColumnCount: 1;
|
|
630
|
-
}
|
|
631
|
-
.facets-content {
|
|
632
|
-
overflow-y: auto;
|
|
633
|
-
height: 300px;
|
|
634
|
-
}
|
|
635
|
-
}
|
|
636
|
-
`,
|
|
637
|
-
];
|
|
638
|
-
}
|
|
639
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
css,
|
|
3
|
+
CSSResultGroup,
|
|
4
|
+
html,
|
|
5
|
+
LitElement,
|
|
6
|
+
nothing,
|
|
7
|
+
PropertyValues,
|
|
8
|
+
TemplateResult,
|
|
9
|
+
} from 'lit';
|
|
10
|
+
import { customElement, property, state } from 'lit/decorators.js';
|
|
11
|
+
import {
|
|
12
|
+
Aggregation,
|
|
13
|
+
Bucket,
|
|
14
|
+
SearchServiceInterface,
|
|
15
|
+
SearchParams,
|
|
16
|
+
SearchType,
|
|
17
|
+
AggregationSortType,
|
|
18
|
+
FilterMap,
|
|
19
|
+
PageType,
|
|
20
|
+
} from '@internetarchive/search-service';
|
|
21
|
+
import type { ModalManagerInterface } from '@internetarchive/modal-manager';
|
|
22
|
+
import type { AnalyticsManagerInterface } from '@internetarchive/analytics-manager';
|
|
23
|
+
import { msg } from '@lit/localize';
|
|
24
|
+
import {
|
|
25
|
+
SelectedFacets,
|
|
26
|
+
FacetGroup,
|
|
27
|
+
FacetBucket,
|
|
28
|
+
FacetOption,
|
|
29
|
+
facetTitles,
|
|
30
|
+
suppressedCollections,
|
|
31
|
+
valueFacetSort,
|
|
32
|
+
defaultFacetSort,
|
|
33
|
+
getDefaultSelectedFacets,
|
|
34
|
+
FacetEventDetails,
|
|
35
|
+
} from '../models';
|
|
36
|
+
import type {
|
|
37
|
+
CollectionTitles,
|
|
38
|
+
PageSpecifierParams,
|
|
39
|
+
TVChannelAliases,
|
|
40
|
+
} from '../data-source/models';
|
|
41
|
+
import '@internetarchive/ia-activity-indicator';
|
|
42
|
+
import './more-facets-pagination';
|
|
43
|
+
import './facets-template';
|
|
44
|
+
import {
|
|
45
|
+
analyticsActions,
|
|
46
|
+
analyticsCategories,
|
|
47
|
+
} from '../utils/analytics-events';
|
|
48
|
+
import './toggle-switch';
|
|
49
|
+
import { srOnlyStyle } from '../styles/sr-only';
|
|
50
|
+
import {
|
|
51
|
+
mergeSelectedFacets,
|
|
52
|
+
sortBucketsBySelectionState,
|
|
53
|
+
updateSelectedFacetBucket,
|
|
54
|
+
} from '../utils/facet-utils';
|
|
55
|
+
import {
|
|
56
|
+
MORE_FACETS__DEFAULT_PAGE_SIZE,
|
|
57
|
+
MORE_FACETS__MAX_AGGREGATIONS,
|
|
58
|
+
} from './models';
|
|
59
|
+
|
|
60
|
+
@customElement('more-facets-content')
|
|
61
|
+
export class MoreFacetsContent extends LitElement {
|
|
62
|
+
@property({ type: String }) facetKey?: FacetOption;
|
|
63
|
+
|
|
64
|
+
@property({ type: String }) query?: string;
|
|
65
|
+
|
|
66
|
+
@property({ type: Array }) identifiers?: string[];
|
|
67
|
+
|
|
68
|
+
@property({ type: Object }) filterMap?: FilterMap;
|
|
69
|
+
|
|
70
|
+
@property({ type: Number }) searchType?: SearchType;
|
|
71
|
+
|
|
72
|
+
@property({ type: Object }) pageSpecifierParams?: PageSpecifierParams;
|
|
73
|
+
|
|
74
|
+
@property({ type: Object })
|
|
75
|
+
collectionTitles?: CollectionTitles;
|
|
76
|
+
|
|
77
|
+
@property({ type: Object })
|
|
78
|
+
tvChannelAliases?: TVChannelAliases;
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Maximum number of facets to show per page within the modal.
|
|
82
|
+
*/
|
|
83
|
+
@property({ type: Number }) facetsPerPage = MORE_FACETS__DEFAULT_PAGE_SIZE;
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Whether we are waiting for facet data to load.
|
|
87
|
+
* We begin with this set to true so that we show an initial loading indicator.
|
|
88
|
+
*/
|
|
89
|
+
@property({ type: Boolean }) facetsLoading = true;
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* The set of pre-existing facet selections (including both selected & negated facets).
|
|
93
|
+
*/
|
|
94
|
+
@property({ type: Object }) selectedFacets?: SelectedFacets;
|
|
95
|
+
|
|
96
|
+
@property({ type: Number }) sortedBy: AggregationSortType =
|
|
97
|
+
AggregationSortType.COUNT;
|
|
98
|
+
|
|
99
|
+
@property({ type: Boolean }) isTvSearch = false;
|
|
100
|
+
|
|
101
|
+
@property({ type: Object }) modalManager?: ModalManagerInterface;
|
|
102
|
+
|
|
103
|
+
@property({ type: Object }) searchService?: SearchServiceInterface;
|
|
104
|
+
|
|
105
|
+
@property({ type: Object, attribute: false })
|
|
106
|
+
analyticsHandler?: AnalyticsManagerInterface;
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* The full set of aggregations received from the search service
|
|
110
|
+
*/
|
|
111
|
+
@state() private aggregations?: Record<string, Aggregation>;
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* A FacetGroup storing the full set of facet buckets to be shown on the dialog.
|
|
115
|
+
*/
|
|
116
|
+
@state() private facetGroup?: FacetGroup;
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* An object holding any changes the patron has made to their facet selections
|
|
120
|
+
* within the modal dialog but which they have not yet applied. These are
|
|
121
|
+
* eventually merged into the existing `selectedFacets` when the patron applies
|
|
122
|
+
* their changes, or discarded if they cancel/close the dialog.
|
|
123
|
+
*/
|
|
124
|
+
@state() private unappliedFacetChanges: SelectedFacets =
|
|
125
|
+
getDefaultSelectedFacets();
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Which page of facets we are showing.
|
|
129
|
+
*/
|
|
130
|
+
@state() private pageNumber = 1;
|
|
131
|
+
|
|
132
|
+
willUpdate(changed: PropertyValues): void {
|
|
133
|
+
if (
|
|
134
|
+
changed.has('aggregations') ||
|
|
135
|
+
changed.has('facetsPerPage') ||
|
|
136
|
+
changed.has('sortedBy') ||
|
|
137
|
+
changed.has('selectedFacets') ||
|
|
138
|
+
changed.has('unappliedFacetChanges')
|
|
139
|
+
) {
|
|
140
|
+
// Convert the merged selected facets & aggregations into a facet group, and
|
|
141
|
+
// store it for reuse across pages.
|
|
142
|
+
this.facetGroup = this.mergedFacets;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
updated(changed: PropertyValues): void {
|
|
147
|
+
// If any of the search properties change, it triggers a facet fetch
|
|
148
|
+
if (
|
|
149
|
+
changed.has('facetKey') ||
|
|
150
|
+
changed.has('query') ||
|
|
151
|
+
changed.has('searchType') ||
|
|
152
|
+
changed.has('filterMap')
|
|
153
|
+
) {
|
|
154
|
+
this.facetsLoading = true;
|
|
155
|
+
this.pageNumber = 1;
|
|
156
|
+
this.sortedBy = defaultFacetSort[this.facetKey as FacetOption];
|
|
157
|
+
|
|
158
|
+
this.updateSpecificFacets();
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
firstUpdated(): void {
|
|
163
|
+
this.setupEscapeListeners();
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Close more facets modal on Escape click
|
|
168
|
+
*/
|
|
169
|
+
private setupEscapeListeners() {
|
|
170
|
+
if (this.modalManager) {
|
|
171
|
+
document.addEventListener('keydown', (e: KeyboardEvent) => {
|
|
172
|
+
if (e.key === 'Escape') {
|
|
173
|
+
this.modalManager?.closeModal();
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
} else {
|
|
177
|
+
document.removeEventListener('keydown', () => {});
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Whether facet requests are for the search_results page type (either defaulted or explicitly).
|
|
183
|
+
*/
|
|
184
|
+
private get isSearchResultsPage(): boolean {
|
|
185
|
+
// Default page type is search_results when none is specified, so we check
|
|
186
|
+
// for undefined as well.
|
|
187
|
+
const pageType: PageType | undefined = this.pageSpecifierParams?.pageType;
|
|
188
|
+
return pageType === undefined || pageType === 'search_results';
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Get specific facets data from search-service API based of currently query params
|
|
193
|
+
* - this.aggregations - hold result of search service and being used for further processing.
|
|
194
|
+
*/
|
|
195
|
+
async updateSpecificFacets(): Promise<void> {
|
|
196
|
+
if (!this.facetKey) return; // Can't fetch facets if we don't know what type of facets we need!
|
|
197
|
+
|
|
198
|
+
const trimmedQuery = this.query?.trim();
|
|
199
|
+
if (!trimmedQuery && this.isSearchResultsPage) return; // The search page _requires_ a query
|
|
200
|
+
|
|
201
|
+
const aggregations = {
|
|
202
|
+
simpleParams: [this.facetKey],
|
|
203
|
+
};
|
|
204
|
+
const aggregationsSize = MORE_FACETS__MAX_AGGREGATIONS; // Only request the 10K highest-count facets
|
|
205
|
+
|
|
206
|
+
const params: SearchParams = {
|
|
207
|
+
...this.pageSpecifierParams,
|
|
208
|
+
query: trimmedQuery || '',
|
|
209
|
+
identifiers: this.identifiers,
|
|
210
|
+
filters: this.filterMap,
|
|
211
|
+
aggregations,
|
|
212
|
+
aggregationsSize,
|
|
213
|
+
rows: 0, // todo - do we want server-side pagination with offset/page/limit flag?
|
|
214
|
+
};
|
|
215
|
+
|
|
216
|
+
const results = await this.searchService?.search(params, this.searchType);
|
|
217
|
+
this.aggregations = results?.success?.response.aggregations;
|
|
218
|
+
this.facetsLoading = false;
|
|
219
|
+
|
|
220
|
+
const collectionTitles = results?.success?.response?.collectionTitles;
|
|
221
|
+
if (collectionTitles) {
|
|
222
|
+
for (const [id, title] of Object.entries(collectionTitles)) {
|
|
223
|
+
this.collectionTitles?.set(id, title);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Handler for page number changes from the pagination widget.
|
|
230
|
+
*/
|
|
231
|
+
private pageNumberClicked(e: CustomEvent<{ page: number }>) {
|
|
232
|
+
const page = e?.detail?.page;
|
|
233
|
+
if (page) {
|
|
234
|
+
this.pageNumber = Number(page);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
this.analyticsHandler?.sendEvent({
|
|
238
|
+
category: analyticsCategories.default,
|
|
239
|
+
action: analyticsActions.moreFacetsPageChange,
|
|
240
|
+
label: `${this.pageNumber}`,
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Combines the selected facets with the aggregations to create a single list of facets
|
|
246
|
+
*/
|
|
247
|
+
private get mergedFacets(): FacetGroup | undefined {
|
|
248
|
+
if (!this.facetKey || !this.selectedFacets) return undefined;
|
|
249
|
+
|
|
250
|
+
const { selectedFacetGroup, aggregationFacetGroup } = this;
|
|
251
|
+
|
|
252
|
+
// If we don't have any aggregations, then there is nothing to show yet
|
|
253
|
+
if (!aggregationFacetGroup) return undefined;
|
|
254
|
+
|
|
255
|
+
// Start with either the selected group if we have one, or the aggregate group otherwise
|
|
256
|
+
const facetGroup = { ...(selectedFacetGroup ?? aggregationFacetGroup) };
|
|
257
|
+
|
|
258
|
+
// Attach the counts to the selected buckets
|
|
259
|
+
const bucketsWithCount =
|
|
260
|
+
selectedFacetGroup?.buckets.map(bucket => {
|
|
261
|
+
const selectedBucket = aggregationFacetGroup.buckets.find(
|
|
262
|
+
b => b.key === bucket.key,
|
|
263
|
+
);
|
|
264
|
+
return selectedBucket
|
|
265
|
+
? {
|
|
266
|
+
...bucket,
|
|
267
|
+
count: selectedBucket.count,
|
|
268
|
+
}
|
|
269
|
+
: bucket;
|
|
270
|
+
}) ?? [];
|
|
271
|
+
|
|
272
|
+
// Sort the buckets by selection state
|
|
273
|
+
// We do this *prior* to considering unapplied selections, because we want the facets
|
|
274
|
+
// to remain in position when they are selected/unselected, rather than re-sort themselves.
|
|
275
|
+
sortBucketsBySelectionState(bucketsWithCount, this.sortedBy);
|
|
276
|
+
|
|
277
|
+
// Append any additional buckets that were not selected
|
|
278
|
+
aggregationFacetGroup.buckets.forEach(bucket => {
|
|
279
|
+
const existingBucket = selectedFacetGroup?.buckets.find(
|
|
280
|
+
b => b.key === bucket.key,
|
|
281
|
+
);
|
|
282
|
+
if (existingBucket) return;
|
|
283
|
+
bucketsWithCount.push(bucket);
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
// Apply any unapplied selections that appear on this page
|
|
287
|
+
const unappliedBuckets = this.unappliedFacetChanges[this.facetKey];
|
|
288
|
+
for (const [index, bucket] of bucketsWithCount.entries()) {
|
|
289
|
+
const unappliedBucket = unappliedBuckets?.[bucket.key];
|
|
290
|
+
if (unappliedBucket) {
|
|
291
|
+
bucketsWithCount[index] = { ...unappliedBucket };
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
// For TV creator facets, uppercase the display text
|
|
296
|
+
if (this.facetKey === 'creator' && this.isTvSearch) {
|
|
297
|
+
bucketsWithCount.forEach(b => {
|
|
298
|
+
b.displayText = (b.displayText ?? b.key)?.toLocaleUpperCase();
|
|
299
|
+
|
|
300
|
+
const channelLabel = this.tvChannelAliases?.get(b.displayText);
|
|
301
|
+
if (channelLabel && channelLabel !== b.displayText) {
|
|
302
|
+
b.extraNote = `(${channelLabel})`;
|
|
303
|
+
}
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
facetGroup.buckets = bucketsWithCount;
|
|
308
|
+
return facetGroup;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* Converts the selected facets for the current facet key to a `FacetGroup`,
|
|
313
|
+
* which is easier to work with.
|
|
314
|
+
*/
|
|
315
|
+
private get selectedFacetGroup(): FacetGroup | undefined {
|
|
316
|
+
if (!this.selectedFacets || !this.facetKey) return undefined;
|
|
317
|
+
|
|
318
|
+
const selectedFacetsForKey = this.selectedFacets[this.facetKey];
|
|
319
|
+
if (!selectedFacetsForKey) return undefined;
|
|
320
|
+
|
|
321
|
+
const facetGroupTitle = facetTitles[this.facetKey];
|
|
322
|
+
|
|
323
|
+
const buckets: FacetBucket[] = Object.entries(selectedFacetsForKey).map(
|
|
324
|
+
([value, data]) => {
|
|
325
|
+
const displayText: string = value;
|
|
326
|
+
return {
|
|
327
|
+
displayText,
|
|
328
|
+
key: value,
|
|
329
|
+
count: data?.count,
|
|
330
|
+
state: data?.state,
|
|
331
|
+
};
|
|
332
|
+
},
|
|
333
|
+
);
|
|
334
|
+
|
|
335
|
+
return {
|
|
336
|
+
title: facetGroupTitle,
|
|
337
|
+
key: this.facetKey,
|
|
338
|
+
buckets,
|
|
339
|
+
};
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* Converts the raw `aggregations` for the current facet key to a `FacetGroup`,
|
|
344
|
+
* which is easier to work with.
|
|
345
|
+
*/
|
|
346
|
+
private get aggregationFacetGroup(): FacetGroup | undefined {
|
|
347
|
+
if (!this.aggregations || !this.facetKey) return undefined;
|
|
348
|
+
|
|
349
|
+
const currentAggregation = this.aggregations[this.facetKey];
|
|
350
|
+
if (!currentAggregation) return undefined;
|
|
351
|
+
|
|
352
|
+
const facetGroupTitle = facetTitles[this.facetKey];
|
|
353
|
+
|
|
354
|
+
// Order the facets according to the current sort option
|
|
355
|
+
let sortedBuckets = currentAggregation.getSortedBuckets(
|
|
356
|
+
this.sortedBy,
|
|
357
|
+
) as Bucket[];
|
|
358
|
+
|
|
359
|
+
if (this.facetKey === 'collection') {
|
|
360
|
+
// we are not showing fav- collections or certain deemphasized collections in facets
|
|
361
|
+
sortedBuckets = sortedBuckets?.filter(bucket => {
|
|
362
|
+
const bucketKey = bucket?.key?.toString();
|
|
363
|
+
return (
|
|
364
|
+
!suppressedCollections[bucketKey] && !bucketKey?.startsWith('fav-')
|
|
365
|
+
);
|
|
366
|
+
});
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
// Construct the array of facet buckets from the aggregation buckets
|
|
370
|
+
const facetBuckets: FacetBucket[] = sortedBuckets.map(bucket => {
|
|
371
|
+
const bucketKeyStr = `${bucket.key}`;
|
|
372
|
+
return {
|
|
373
|
+
displayText: `${bucketKeyStr}`,
|
|
374
|
+
key: `${bucketKeyStr}`,
|
|
375
|
+
count: bucket.doc_count,
|
|
376
|
+
state: 'none',
|
|
377
|
+
};
|
|
378
|
+
});
|
|
379
|
+
|
|
380
|
+
return {
|
|
381
|
+
title: facetGroupTitle,
|
|
382
|
+
key: this.facetKey,
|
|
383
|
+
buckets: facetBuckets,
|
|
384
|
+
};
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/**
|
|
388
|
+
* Returns a FacetGroup representing only the current page of facet buckets to show.
|
|
389
|
+
*/
|
|
390
|
+
private get facetGroupForCurrentPage(): FacetGroup | undefined {
|
|
391
|
+
const { facetGroup } = this;
|
|
392
|
+
if (!facetGroup) return undefined;
|
|
393
|
+
|
|
394
|
+
// Slice out only the current page of facet buckets
|
|
395
|
+
const firstBucketIndexOnPage = (this.pageNumber - 1) * this.facetsPerPage;
|
|
396
|
+
const truncatedBuckets = facetGroup.buckets.slice(
|
|
397
|
+
firstBucketIndexOnPage,
|
|
398
|
+
firstBucketIndexOnPage + this.facetsPerPage,
|
|
399
|
+
);
|
|
400
|
+
|
|
401
|
+
return {
|
|
402
|
+
...facetGroup,
|
|
403
|
+
buckets: truncatedBuckets,
|
|
404
|
+
};
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
private get moreFacetsTemplate(): TemplateResult {
|
|
408
|
+
return html`
|
|
409
|
+
<facets-template
|
|
410
|
+
.facetGroup=${this.facetGroupForCurrentPage}
|
|
411
|
+
.selectedFacets=${this.selectedFacets}
|
|
412
|
+
.collectionTitles=${this.collectionTitles}
|
|
413
|
+
@facetClick=${(e: CustomEvent<FacetEventDetails>) => {
|
|
414
|
+
if (this.facetKey) {
|
|
415
|
+
this.unappliedFacetChanges = updateSelectedFacetBucket(
|
|
416
|
+
this.unappliedFacetChanges,
|
|
417
|
+
this.facetKey,
|
|
418
|
+
e.detail.bucket,
|
|
419
|
+
);
|
|
420
|
+
}
|
|
421
|
+
}}
|
|
422
|
+
></facets-template>
|
|
423
|
+
`;
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
private get loaderTemplate(): TemplateResult {
|
|
427
|
+
return html`<div class="facets-loader">
|
|
428
|
+
<ia-activity-indicator .mode=${'processing'}></ia-activity-indicator>
|
|
429
|
+
</div> `;
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
/**
|
|
433
|
+
* How many pages of facets to show in the modal pagination widget
|
|
434
|
+
*/
|
|
435
|
+
private get paginationSize(): number {
|
|
436
|
+
if (!this.aggregations || !this.facetKey) return 0;
|
|
437
|
+
|
|
438
|
+
// Calculate the appropriate number of pages to show in the modal pagination widget
|
|
439
|
+
const length = this.aggregations[this.facetKey]?.buckets.length;
|
|
440
|
+
return Math.ceil(length / this.facetsPerPage);
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
// render pagination if more then 1 page
|
|
444
|
+
private get facetsPaginationTemplate() {
|
|
445
|
+
return this.paginationSize > 1
|
|
446
|
+
? html`<more-facets-pagination
|
|
447
|
+
.size=${this.paginationSize}
|
|
448
|
+
.currentPage=${1}
|
|
449
|
+
@pageNumberClicked=${this.pageNumberClicked}
|
|
450
|
+
></more-facets-pagination>`
|
|
451
|
+
: nothing;
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
private get footerTemplate() {
|
|
455
|
+
if (this.paginationSize > 0) {
|
|
456
|
+
return html`${this.facetsPaginationTemplate}
|
|
457
|
+
<div class="footer">
|
|
458
|
+
<button
|
|
459
|
+
class="btn btn-cancel"
|
|
460
|
+
type="button"
|
|
461
|
+
@click=${this.cancelClick}
|
|
462
|
+
>
|
|
463
|
+
Cancel
|
|
464
|
+
</button>
|
|
465
|
+
<button
|
|
466
|
+
class="btn btn-submit"
|
|
467
|
+
type="button"
|
|
468
|
+
@click=${this.applySearchFacetsClicked}
|
|
469
|
+
>
|
|
470
|
+
Apply filters
|
|
471
|
+
</button>
|
|
472
|
+
</div> `;
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
return nothing;
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
private sortFacetAggregation(facetSortType: AggregationSortType) {
|
|
479
|
+
this.sortedBy = facetSortType;
|
|
480
|
+
this.dispatchEvent(
|
|
481
|
+
new CustomEvent('sortedFacets', { detail: this.sortedBy }),
|
|
482
|
+
);
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
private get modalHeaderTemplate(): TemplateResult {
|
|
486
|
+
const facetSort =
|
|
487
|
+
this.sortedBy ?? defaultFacetSort[this.facetKey as FacetOption];
|
|
488
|
+
const defaultSwitchSide =
|
|
489
|
+
facetSort === AggregationSortType.COUNT ? 'left' : 'right';
|
|
490
|
+
|
|
491
|
+
return html`<span class="sr-only">${msg('More facets for:')}</span>
|
|
492
|
+
<span class="title">
|
|
493
|
+
${this.facetGroup?.title}
|
|
494
|
+
|
|
495
|
+
<label class="sort-label">${msg('Sort by:')}</label>
|
|
496
|
+
${this.facetKey
|
|
497
|
+
? html`<toggle-switch
|
|
498
|
+
class="sort-toggle"
|
|
499
|
+
leftValue=${AggregationSortType.COUNT}
|
|
500
|
+
leftLabel="Count"
|
|
501
|
+
rightValue=${valueFacetSort[this.facetKey]}
|
|
502
|
+
.rightLabel=${this.facetGroup?.title}
|
|
503
|
+
side=${defaultSwitchSide}
|
|
504
|
+
@change=${(e: CustomEvent<string>) => {
|
|
505
|
+
this.sortFacetAggregation(
|
|
506
|
+
Number(e.detail) as AggregationSortType,
|
|
507
|
+
);
|
|
508
|
+
}}
|
|
509
|
+
></toggle-switch>`
|
|
510
|
+
: nothing}
|
|
511
|
+
</span>`;
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
render() {
|
|
515
|
+
return html`
|
|
516
|
+
${this.facetsLoading
|
|
517
|
+
? this.loaderTemplate
|
|
518
|
+
: html`
|
|
519
|
+
<section id="more-facets">
|
|
520
|
+
<div class="header-content">${this.modalHeaderTemplate}</div>
|
|
521
|
+
<div class="facets-content">${this.moreFacetsTemplate}</div>
|
|
522
|
+
${this.footerTemplate}
|
|
523
|
+
</section>
|
|
524
|
+
`}
|
|
525
|
+
`;
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
private applySearchFacetsClicked() {
|
|
529
|
+
const mergedSelections = mergeSelectedFacets(
|
|
530
|
+
this.selectedFacets,
|
|
531
|
+
this.unappliedFacetChanges,
|
|
532
|
+
);
|
|
533
|
+
|
|
534
|
+
const event = new CustomEvent<SelectedFacets>('facetsChanged', {
|
|
535
|
+
detail: mergedSelections,
|
|
536
|
+
bubbles: true,
|
|
537
|
+
composed: true,
|
|
538
|
+
});
|
|
539
|
+
this.dispatchEvent(event);
|
|
540
|
+
|
|
541
|
+
// Reset the unapplied changes back to default, now that they have been applied
|
|
542
|
+
this.unappliedFacetChanges = getDefaultSelectedFacets();
|
|
543
|
+
|
|
544
|
+
this.modalManager?.closeModal();
|
|
545
|
+
this.analyticsHandler?.sendEvent({
|
|
546
|
+
category: analyticsCategories.default,
|
|
547
|
+
action: `${analyticsActions.applyMoreFacetsModal}`,
|
|
548
|
+
label: `${this.facetKey}`,
|
|
549
|
+
});
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
private cancelClick() {
|
|
553
|
+
// Reset the unapplied changes back to default
|
|
554
|
+
this.unappliedFacetChanges = getDefaultSelectedFacets();
|
|
555
|
+
|
|
556
|
+
this.modalManager?.closeModal();
|
|
557
|
+
this.analyticsHandler?.sendEvent({
|
|
558
|
+
category: analyticsCategories.default,
|
|
559
|
+
action: analyticsActions.closeMoreFacetsModal,
|
|
560
|
+
label: `${this.facetKey}`,
|
|
561
|
+
});
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
static get styles(): CSSResultGroup {
|
|
565
|
+
const modalSubmitButton = css`var(--primaryButtonBGColor, #194880)`;
|
|
566
|
+
|
|
567
|
+
return [
|
|
568
|
+
srOnlyStyle,
|
|
569
|
+
css`
|
|
570
|
+
section#more-facets {
|
|
571
|
+
overflow: auto;
|
|
572
|
+
padding: 10px; /* leaves room for scroll bar to appear without overlaying on content */
|
|
573
|
+
--facetsColumnCount: 3;
|
|
574
|
+
}
|
|
575
|
+
.header-content .title {
|
|
576
|
+
display: block;
|
|
577
|
+
text-align: left;
|
|
578
|
+
font-size: 1.8rem;
|
|
579
|
+
padding: 0 10px;
|
|
580
|
+
font-weight: bold;
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
.sort-label {
|
|
584
|
+
margin-left: 20px;
|
|
585
|
+
font-size: 1.3rem;
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
.sort-toggle {
|
|
589
|
+
font-weight: normal;
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
.facets-content {
|
|
593
|
+
font-size: 1.2rem;
|
|
594
|
+
max-height: 300px;
|
|
595
|
+
overflow: auto;
|
|
596
|
+
padding: 10px;
|
|
597
|
+
}
|
|
598
|
+
.facets-loader {
|
|
599
|
+
margin-bottom: 20px;
|
|
600
|
+
width: 70px;
|
|
601
|
+
display: block;
|
|
602
|
+
margin-left: auto;
|
|
603
|
+
margin-right: auto;
|
|
604
|
+
}
|
|
605
|
+
.btn {
|
|
606
|
+
border: none;
|
|
607
|
+
padding: 10px;
|
|
608
|
+
margin-bottom: 10px;
|
|
609
|
+
width: auto;
|
|
610
|
+
border-radius: 4px;
|
|
611
|
+
cursor: pointer;
|
|
612
|
+
}
|
|
613
|
+
.btn-cancel {
|
|
614
|
+
background-color: #2c2c2c;
|
|
615
|
+
color: white;
|
|
616
|
+
}
|
|
617
|
+
.btn-submit {
|
|
618
|
+
background-color: ${modalSubmitButton};
|
|
619
|
+
color: white;
|
|
620
|
+
}
|
|
621
|
+
.footer {
|
|
622
|
+
text-align: center;
|
|
623
|
+
margin-top: 10px;
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
@media (max-width: 560px) {
|
|
627
|
+
section#more-facets {
|
|
628
|
+
max-height: 450px;
|
|
629
|
+
--facetsColumnCount: 1;
|
|
630
|
+
}
|
|
631
|
+
.facets-content {
|
|
632
|
+
overflow-y: auto;
|
|
633
|
+
height: 300px;
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
`,
|
|
637
|
+
];
|
|
638
|
+
}
|
|
639
|
+
}
|