@internetarchive/collection-browser 4.0.1 → 4.1.0-alpha-webdev8164.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -25,6 +25,12 @@ export class MoreFacetsPagination extends LitElement {
25
25
 
26
26
  @property({ type: Number }) currentPage: number = 1;
27
27
 
28
+ /**
29
+ * When true, shows a more compact set of page numbers
30
+ * (only 1 neighbor on each side of the current page).
31
+ */
32
+ @property({ type: Boolean }) compact = false;
33
+
28
34
  @state() pages?: number[] = [];
29
35
 
30
36
  firstUpdated() {
@@ -33,7 +39,7 @@ export class MoreFacetsPagination extends LitElement {
33
39
 
34
40
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
35
41
  override updated(changed: Map<string, any>) {
36
- if (changed.has('size')) {
42
+ if (changed.has('size') || changed.has('compact')) {
37
43
  this.observePageCount();
38
44
  }
39
45
  if (changed.has('currentPage')) {
@@ -53,6 +59,11 @@ export class MoreFacetsPagination extends LitElement {
53
59
  observePageCount() {
54
60
  this.pages = []; /* `0` is elipses marker */
55
61
 
62
+ if (this.compact) {
63
+ this.observePageCountCompact();
64
+ return;
65
+ }
66
+
56
67
  const paginatorMaxPagesToShow = 7;
57
68
  const atMinThreshold = this.size <= paginatorMaxPagesToShow;
58
69
 
@@ -166,6 +177,52 @@ export class MoreFacetsPagination extends LitElement {
166
177
  }
167
178
  }
168
179
 
180
+ /**
181
+ * Compact page calculation: shows first, ..., prev, current, next, ..., last.
182
+ * Only 1 neighbor on each side of the current page for minimal width.
183
+ */
184
+ private observePageCountCompact() {
185
+ if (this.size <= 3) {
186
+ this.pages = [...Array(this.size).keys()].map(i => i + 1);
187
+ return;
188
+ }
189
+
190
+ const pages: number[] = [];
191
+
192
+ // First page
193
+ pages.push(1);
194
+
195
+ // Ellipsis after first if current is far enough away
196
+ if (this.currentPage > 3) {
197
+ pages.push(0);
198
+ }
199
+
200
+ // Previous page (if not already shown as first)
201
+ if (this.currentPage - 1 > 1) {
202
+ pages.push(this.currentPage - 1);
203
+ }
204
+
205
+ // Current page (if not first or last)
206
+ if (this.currentPage !== 1 && this.currentPage !== this.size) {
207
+ pages.push(this.currentPage);
208
+ }
209
+
210
+ // Next page (if not already the last)
211
+ if (this.currentPage + 1 < this.size) {
212
+ pages.push(this.currentPage + 1);
213
+ }
214
+
215
+ // Ellipsis before last if current is far enough away
216
+ if (this.currentPage < this.size - 2) {
217
+ pages.push(0);
218
+ }
219
+
220
+ // Last page
221
+ pages.push(this.size);
222
+
223
+ this.pages = pages;
224
+ }
225
+
169
226
  private get getEllipsisTemplate() {
170
227
  return html`<i class="ellipses">...</i>`;
171
228
  }
@@ -250,6 +307,10 @@ export class MoreFacetsPagination extends LitElement {
250
307
  margin-top: 10px;
251
308
  background-color: #eee;
252
309
  text-align: center;
310
+ display: flex;
311
+ align-items: center;
312
+ justify-content: center;
313
+ flex-wrap: nowrap;
253
314
  }
254
315
  .facets-pagination button {
255
316
  border: none;
@@ -276,6 +337,7 @@ export class MoreFacetsPagination extends LitElement {
276
337
  vertical-align: baseline;
277
338
  display: inline-block;
278
339
  min-width: 2.5rem;
340
+ font-family: inherit;
279
341
  }
280
342
  .facets-pagination i {
281
343
  cursor: auto;
@@ -288,6 +350,16 @@ export class MoreFacetsPagination extends LitElement {
288
350
  .page-numbers {
289
351
  display: inline-block;
290
352
  }
353
+
354
+ @media (max-width: 560px) {
355
+ .facets-pagination button,
356
+ .facets-pagination i {
357
+ margin: 5px 2px;
358
+ padding: 3px;
359
+ min-width: 2rem;
360
+ font-size: 1.2rem;
361
+ }
362
+ }
291
363
  `,
292
364
  ];
293
365
  }
@@ -6,6 +6,7 @@ import { MockSearchService } from '../mocks/mock-search-service';
6
6
  import { MockAnalyticsHandler } from '../mocks/mock-analytics-handler';
7
7
  import type { FacetsTemplate } from '../../src/collection-facets/facets-template';
8
8
  import type { SelectedFacets } from '../../src/models';
9
+ import { Aggregation, type Bucket } from '@internetarchive/search-service';
9
10
 
10
11
  const selectedFacetsGroup = {
11
12
  title: 'Media Type',
@@ -54,7 +55,7 @@ describe('More facets content', () => {
54
55
  expect(el.shadowRoot?.querySelector('.facets-loader')).to.exist;
55
56
  });
56
57
 
57
- it('should render pagination for more facets', async () => {
58
+ it('should NOT render pagination when facet count < 1000', async () => {
58
59
  const searchService = new MockSearchService();
59
60
 
60
61
  const el = await fixture<MoreFacetsContent>(
@@ -64,11 +65,22 @@ describe('More facets content', () => {
64
65
  );
65
66
 
66
67
  el.facetKey = 'year';
67
- el.query = 'more-facets'; // Produces a response with 40+ aggregations for multiple pages
68
+ el.query = 'more-facets'; // Produces a response with 45 aggregations (< 1000)
68
69
  await el.updateComplete;
69
70
  await aTimeout(50); // Give it a moment to perform the (mock) search query after the initial update
70
71
 
71
- expect(el.shadowRoot?.querySelectorAll('more-facets-pagination')).to.exist;
72
+ // Verify pagination component is NOT present (horizontal scroll mode)
73
+ expect(el.shadowRoot?.querySelector('more-facets-pagination')).to.not.exist;
74
+
75
+ // Verify horizontal scroll mode CSS class is applied
76
+ expect(
77
+ el.shadowRoot?.querySelector('.facets-content.horizontal-scroll-mode'),
78
+ ).to.exist;
79
+
80
+ // Verify footer still exists with buttons
81
+ expect(el.shadowRoot?.querySelector('.footer')).to.exist;
82
+ expect(el.shadowRoot?.querySelector('.btn-cancel')).to.exist;
83
+ expect(el.shadowRoot?.querySelector('.btn-submit')).to.exist;
72
84
  });
73
85
 
74
86
  it('query for more facets content using search service', async () => {
@@ -228,4 +240,207 @@ describe('More facets content', () => {
228
240
  expect(mockAnalyticsHandler.callAction).to.equal('applyMoreFacetsModal');
229
241
  expect(mockAnalyticsHandler.callLabel).to.equal('collection');
230
242
  });
243
+
244
+ it('should have horizontal scrolling enabled', async () => {
245
+ const searchService = new MockSearchService();
246
+
247
+ const el = await fixture<MoreFacetsContent>(
248
+ html`<more-facets-content
249
+ .searchService=${searchService}
250
+ ></more-facets-content>`,
251
+ );
252
+
253
+ el.facetKey = 'year';
254
+ el.query = 'more-facets';
255
+ await el.updateComplete;
256
+ await aTimeout(50);
257
+
258
+ const facetsContent = el.shadowRoot?.querySelector(
259
+ '.facets-content',
260
+ ) as HTMLElement;
261
+ const styles = window.getComputedStyle(facetsContent);
262
+
263
+ expect(styles.overflowX).to.equal('auto');
264
+ expect(styles.overflowY).to.equal('hidden');
265
+ });
266
+
267
+ it('should have horizontal container wrapper', async () => {
268
+ const searchService = new MockSearchService();
269
+
270
+ const el = await fixture<MoreFacetsContent>(
271
+ html`<more-facets-content
272
+ .searchService=${searchService}
273
+ ></more-facets-content>`,
274
+ );
275
+
276
+ el.facetKey = 'year';
277
+ el.query = 'more-facets';
278
+ await el.updateComplete;
279
+ await aTimeout(50);
280
+
281
+ const container = el.shadowRoot?.querySelector(
282
+ '.facets-horizontal-container',
283
+ );
284
+ expect(container).to.exist;
285
+
286
+ const facetsTemplate = container?.querySelector('facets-template');
287
+ expect(facetsTemplate).to.exist;
288
+ });
289
+
290
+ it('should render pagination when facet count >= 1000', async () => {
291
+ // Manually create aggregations with 1000+ facets
292
+ const buckets: Bucket[] = [];
293
+ for (let i = 0; i < 1000; i++) {
294
+ buckets.push({ key: `value-${i}`, doc_count: i + 1 });
295
+ }
296
+
297
+ const el = await fixture<MoreFacetsContent>(
298
+ html`<more-facets-content
299
+ .facetKey=${'subject'}
300
+ .selectedFacets=${{
301
+ mediatype: {},
302
+ lending: {},
303
+ year: {},
304
+ subject: {},
305
+ collection: {},
306
+ creator: {},
307
+ language: {},
308
+ }}
309
+ ></more-facets-content>`,
310
+ );
311
+
312
+ // @ts-expect-error - accessing private property for testing
313
+ el.aggregations = {
314
+ subject: new Aggregation({ buckets }),
315
+ };
316
+ el.facetsLoading = false;
317
+ await el.updateComplete;
318
+
319
+ // Verify pagination component IS present
320
+ expect(el.shadowRoot?.querySelector('more-facets-pagination')).to.exist;
321
+
322
+ // Verify pagination mode CSS class is applied
323
+ expect(el.shadowRoot?.querySelector('.facets-content.pagination-mode')).to
324
+ .exist;
325
+
326
+ // Verify horizontal container wrapper does NOT exist in pagination mode
327
+ expect(el.shadowRoot?.querySelector('.facets-horizontal-container')).to.not
328
+ .exist;
329
+ });
330
+
331
+ it('pagination page change should send analytics event', async () => {
332
+ const mockAnalyticsHandler = new MockAnalyticsHandler();
333
+
334
+ // Manually create aggregations with 1000+ facets
335
+ const buckets: Bucket[] = [];
336
+ for (let i = 0; i < 1000; i++) {
337
+ buckets.push({ key: `value-${i}`, doc_count: i + 1 });
338
+ }
339
+
340
+ const el = await fixture<MoreFacetsContent>(
341
+ html`<more-facets-content
342
+ .facetKey=${'subject'}
343
+ .selectedFacets=${{
344
+ mediatype: {},
345
+ lending: {},
346
+ year: {},
347
+ subject: {},
348
+ collection: {},
349
+ creator: {},
350
+ language: {},
351
+ }}
352
+ .analyticsHandler=${mockAnalyticsHandler}
353
+ ></more-facets-content>`,
354
+ );
355
+
356
+ // @ts-expect-error - accessing private property for testing
357
+ el.aggregations = {
358
+ subject: new Aggregation({ buckets }),
359
+ };
360
+ el.facetsLoading = false;
361
+ await el.updateComplete;
362
+
363
+ // Get the pagination component
364
+ const pagination = el.shadowRoot?.querySelector(
365
+ 'more-facets-pagination',
366
+ ) as any;
367
+ expect(pagination).to.exist;
368
+
369
+ // Simulate clicking page 2
370
+ pagination.currentPage = 2;
371
+ await pagination.updateComplete;
372
+
373
+ // Verify analytics event was sent
374
+ expect(mockAnalyticsHandler.callCategory).to.equal('collection-browser');
375
+ expect(mockAnalyticsHandler.callAction).to.equal('moreFacetsPageChange');
376
+ expect(mockAnalyticsHandler.callLabel).to.equal('2');
377
+ });
378
+
379
+ it('should show clear button when filter text is present', async () => {
380
+ const searchService = new MockSearchService();
381
+
382
+ const el = await fixture<MoreFacetsContent>(
383
+ html`<more-facets-content
384
+ .facetKey=${'year'}
385
+ .query=${'more-facets'}
386
+ .searchService=${searchService}
387
+ .selectedFacets=${yearSelectedFacets}
388
+ ></more-facets-content>`,
389
+ );
390
+
391
+ await el.updateComplete;
392
+ await aTimeout(50);
393
+
394
+ // Initially, no clear button should exist
395
+ expect(el.shadowRoot?.querySelector('.filter-clear-btn')).to.not.exist;
396
+
397
+ // Type into the filter input
398
+ const filterInput = el.shadowRoot?.querySelector(
399
+ '#facet-filter',
400
+ ) as HTMLInputElement;
401
+ expect(filterInput).to.exist;
402
+
403
+ filterInput.value = 'test';
404
+ filterInput.dispatchEvent(new Event('input'));
405
+ await el.updateComplete;
406
+
407
+ // Now clear button should exist
408
+ expect(el.shadowRoot?.querySelector('.filter-clear-btn')).to.exist;
409
+ });
410
+
411
+ it('should clear filter text when clear button is clicked', async () => {
412
+ const searchService = new MockSearchService();
413
+
414
+ const el = await fixture<MoreFacetsContent>(
415
+ html`<more-facets-content
416
+ .facetKey=${'year'}
417
+ .query=${'more-facets'}
418
+ .searchService=${searchService}
419
+ .selectedFacets=${yearSelectedFacets}
420
+ ></more-facets-content>`,
421
+ );
422
+
423
+ await el.updateComplete;
424
+ await aTimeout(50);
425
+
426
+ // Type into the filter input
427
+ const filterInput = el.shadowRoot?.querySelector(
428
+ '#facet-filter',
429
+ ) as HTMLInputElement;
430
+ filterInput.value = 'test';
431
+ filterInput.dispatchEvent(new Event('input'));
432
+ await el.updateComplete;
433
+
434
+ // Click the clear button
435
+ const clearBtn = el.shadowRoot?.querySelector(
436
+ '.filter-clear-btn',
437
+ ) as HTMLButtonElement;
438
+ expect(clearBtn).to.exist;
439
+ clearBtn.click();
440
+ await el.updateComplete;
441
+
442
+ // Filter input should be empty and clear button should be gone
443
+ expect(filterInput.value).to.equal('');
444
+ expect(el.shadowRoot?.querySelector('.filter-clear-btn')).to.not.exist;
445
+ });
231
446
  });
@@ -198,4 +198,88 @@ describe('More facets pagination', () => {
198
198
  expect(el.currentPage).to.equal(6); // brings us forward 1 page
199
199
  });
200
200
  });
201
+
202
+ describe('Compact mode', () => {
203
+ it('shows all pages when size <= 3', async () => {
204
+ const el = await fixture<MoreFacetsPagination>(
205
+ html`<more-facets-pagination
206
+ .size=${3}
207
+ .compact=${true}
208
+ ></more-facets-pagination>`,
209
+ );
210
+
211
+ await el.updateComplete;
212
+ expect(el.pages).to.deep.equal([1, 2, 3]);
213
+ });
214
+
215
+ it('shows first, prev, current, next, ..., last for middle page', async () => {
216
+ const el = await fixture<MoreFacetsPagination>(
217
+ html`<more-facets-pagination
218
+ .size=${20}
219
+ .currentPage=${10}
220
+ .compact=${true}
221
+ ></more-facets-pagination>`,
222
+ );
223
+
224
+ await el.updateComplete;
225
+ // first, ..., prev, current, next, ..., last
226
+ expect(el.pages).to.deep.equal([1, 0, 9, 10, 11, 0, 20]);
227
+ });
228
+
229
+ it('shows correct pages when on page 1', async () => {
230
+ const el = await fixture<MoreFacetsPagination>(
231
+ html`<more-facets-pagination
232
+ .size=${20}
233
+ .currentPage=${1}
234
+ .compact=${true}
235
+ ></more-facets-pagination>`,
236
+ );
237
+
238
+ await el.updateComplete;
239
+ // first (current), next, ..., last
240
+ expect(el.pages).to.deep.equal([1, 2, 0, 20]);
241
+ });
242
+
243
+ it('shows correct pages when on last page', async () => {
244
+ const el = await fixture<MoreFacetsPagination>(
245
+ html`<more-facets-pagination
246
+ .size=${20}
247
+ .currentPage=${20}
248
+ .compact=${true}
249
+ ></more-facets-pagination>`,
250
+ );
251
+
252
+ await el.updateComplete;
253
+ // first, ..., prev, last (current)
254
+ expect(el.pages).to.deep.equal([1, 0, 19, 20]);
255
+ });
256
+
257
+ it('shows correct pages when on page 2', async () => {
258
+ const el = await fixture<MoreFacetsPagination>(
259
+ html`<more-facets-pagination
260
+ .size=${20}
261
+ .currentPage=${2}
262
+ .compact=${true}
263
+ ></more-facets-pagination>`,
264
+ );
265
+
266
+ await el.updateComplete;
267
+ // first, current, next, ..., last
268
+ expect(el.pages).to.deep.equal([1, 2, 3, 0, 20]);
269
+ });
270
+
271
+ it('shows correct pages when on second-to-last page', async () => {
272
+ const el = await fixture<MoreFacetsPagination>(
273
+ html`<more-facets-pagination
274
+ .size=${20}
275
+ .currentPage=${19}
276
+ .compact=${true}
277
+ ></more-facets-pagination>`,
278
+ );
279
+
280
+ await el.updateComplete;
281
+ // first, ..., prev, current, last
282
+ expect(el.pages).to.deep.equal([1, 0, 18, 19, 20]);
283
+ });
284
+ });
201
285
  });