@internetarchive/collection-browser 4.7.1-alpha-webdev9189.0 → 4.8.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.
Files changed (87) hide show
  1. package/dist/src/app-root.js +0 -2
  2. package/dist/src/app-root.js.map +1 -1
  3. package/dist/src/assets/img/icons/check.d.ts +2 -0
  4. package/dist/src/assets/img/icons/check.js +5 -0
  5. package/dist/src/assets/img/icons/check.js.map +1 -0
  6. package/dist/src/collection-browser.js +2 -24
  7. package/dist/src/collection-browser.js.map +1 -1
  8. package/dist/src/collection-facets/facet-row-template.d.ts +38 -0
  9. package/dist/src/collection-facets/facet-row-template.js +200 -0
  10. package/dist/src/collection-facets/facet-row-template.js.map +1 -0
  11. package/dist/src/collection-facets/facet-row.js +11 -186
  12. package/dist/src/collection-facets/facet-row.js.map +1 -1
  13. package/dist/src/collection-facets/models.d.ts +7 -2
  14. package/dist/src/collection-facets/models.js +7 -2
  15. package/dist/src/collection-facets/models.js.map +1 -1
  16. package/dist/src/collection-facets/more-facets-content.d.ts +87 -21
  17. package/dist/src/collection-facets/more-facets-content.js +477 -200
  18. package/dist/src/collection-facets/more-facets-content.js.map +1 -1
  19. package/dist/src/collection-facets/more-facets-scroller.d.ts +85 -0
  20. package/dist/src/collection-facets/more-facets-scroller.js +428 -0
  21. package/dist/src/collection-facets/more-facets-scroller.js.map +1 -0
  22. package/dist/src/collection-facets/page-window.d.ts +73 -0
  23. package/dist/src/collection-facets/page-window.js +202 -0
  24. package/dist/src/collection-facets/page-window.js.map +1 -0
  25. package/dist/src/collection-facets/toggle-switch.js +1 -0
  26. package/dist/src/collection-facets/toggle-switch.js.map +1 -1
  27. package/dist/src/models.d.ts +4 -0
  28. package/dist/src/models.js +16 -0
  29. package/dist/src/models.js.map +1 -1
  30. package/dist/src/sort-filter-bar/sort-filter-bar.js +0 -1
  31. package/dist/src/sort-filter-bar/sort-filter-bar.js.map +1 -1
  32. package/dist/src/utils/log.d.ts +1 -4
  33. package/dist/src/utils/normalize-filter-text.d.ts +28 -0
  34. package/dist/src/utils/normalize-filter-text.js +35 -0
  35. package/dist/src/utils/normalize-filter-text.js.map +1 -0
  36. package/dist/test/collection-browser.test.js +0 -34
  37. package/dist/test/collection-browser.test.js.map +1 -1
  38. package/dist/test/collection-facets/more-facets-content.test.js +367 -17
  39. package/dist/test/collection-facets/more-facets-content.test.js.map +1 -1
  40. package/dist/test/collection-facets/more-facets-scroller.test.d.ts +1 -0
  41. package/dist/test/collection-facets/more-facets-scroller.test.js +288 -0
  42. package/dist/test/collection-facets/more-facets-scroller.test.js.map +1 -0
  43. package/dist/test/collection-facets/page-window.test.d.ts +1 -0
  44. package/dist/test/collection-facets/page-window.test.js +175 -0
  45. package/dist/test/collection-facets/page-window.test.js.map +1 -0
  46. package/dist/test/mocks/mock-search-responses.d.ts +6 -0
  47. package/dist/test/mocks/mock-search-responses.js +52 -0
  48. package/dist/test/mocks/mock-search-responses.js.map +1 -1
  49. package/dist/test/mocks/mock-search-service.js +2 -1
  50. package/dist/test/mocks/mock-search-service.js.map +1 -1
  51. package/dist/test/sort-filter-bar/sort-filter-bar.test.js +0 -22
  52. package/dist/test/sort-filter-bar/sort-filter-bar.test.js.map +1 -1
  53. package/dist/test/utils/normalize-filter-text.test.d.ts +1 -0
  54. package/dist/test/utils/normalize-filter-text.test.js +69 -0
  55. package/dist/test/utils/normalize-filter-text.test.js.map +1 -0
  56. package/docs/design/column-virtualization.md +214 -0
  57. package/package.json +3 -1
  58. package/src/app-root.ts +0 -2
  59. package/src/assets/img/icons/check.ts +5 -0
  60. package/src/collection-browser.ts +4 -26
  61. package/src/collection-facets/facet-row-template.ts +242 -0
  62. package/src/collection-facets/facet-row.ts +15 -202
  63. package/src/collection-facets/models.ts +8 -2
  64. package/src/collection-facets/more-facets-content.ts +528 -221
  65. package/src/collection-facets/more-facets-scroller.ts +485 -0
  66. package/src/collection-facets/page-window.ts +247 -0
  67. package/src/collection-facets/toggle-switch.ts +1 -0
  68. package/src/models.ts +17 -0
  69. package/src/sort-filter-bar/sort-filter-bar.ts +0 -1
  70. package/src/utils/normalize-filter-text.ts +54 -0
  71. package/test/collection-browser.test.ts +0 -55
  72. package/test/collection-facets/more-facets-content.test.ts +509 -24
  73. package/test/collection-facets/more-facets-scroller.test.ts +401 -0
  74. package/test/collection-facets/page-window.test.ts +208 -0
  75. package/test/mocks/mock-search-responses.ts +56 -0
  76. package/test/mocks/mock-search-service.ts +2 -0
  77. package/test/sort-filter-bar/sort-filter-bar.test.ts +0 -29
  78. package/test/utils/normalize-filter-text.test.ts +87 -0
  79. package/web-test-runner.config.mjs +7 -2
  80. package/dist/src/collection-facets/more-facets-pagination.d.ts +0 -36
  81. package/dist/src/collection-facets/more-facets-pagination.js +0 -260
  82. package/dist/src/collection-facets/more-facets-pagination.js.map +0 -1
  83. package/dist/test/collection-facets/more-facets-pagination.test.d.ts +0 -1
  84. package/dist/test/collection-facets/more-facets-pagination.test.js +0 -126
  85. package/dist/test/collection-facets/more-facets-pagination.test.js.map +0 -1
  86. package/src/collection-facets/more-facets-pagination.ts +0 -294
  87. package/test/collection-facets/more-facets-pagination.test.ts +0 -201
@@ -0,0 +1,485 @@
1
+ import {
2
+ css,
3
+ CSSResultGroup,
4
+ html,
5
+ LitElement,
6
+ PropertyValues,
7
+ TemplateResult,
8
+ unsafeCSS,
9
+ } from 'lit';
10
+ import { customElement, property, query } from 'lit/decorators.js';
11
+ import { classMap } from 'lit/directives/class-map.js';
12
+ import { ifDefined } from 'lit/directives/if-defined.js';
13
+ import { guard } from 'lit/directives/guard.js';
14
+ import { ref } from 'lit/directives/ref.js';
15
+ import { repeat } from 'lit/directives/repeat.js';
16
+ import { msg } from '@lit/localize';
17
+ import type { FacetBucket, FacetEventDetails, FacetOption } from '../models';
18
+ import type { CollectionTitles } from '../data-source/models';
19
+ import arrowLeftIcon from '../assets/img/icons/arrow-left';
20
+ import arrowRightIcon from '../assets/img/icons/arrow-right';
21
+ import { srOnlyStyle } from '../styles/sr-only';
22
+ import {
23
+ facetRowStyles,
24
+ facetRowTemplate,
25
+ getFacetState,
26
+ } from './facet-row-template';
27
+ import { PageWindow } from './page-window';
28
+ import {
29
+ MORE_FACETS__COLUMN_WIDTH,
30
+ MORE_FACETS__ROWS_PER_COLUMN,
31
+ } from './models';
32
+
33
+ /** Height in px reserved below the columns for the horizontal scrollbar */
34
+ const SCROLLBAR_SIZE = 12;
35
+
36
+ /**
37
+ * A long list of facet buckets laid out top to bottom in fixed-width columns
38
+ * that scroll horizontally and snap to whole pages. Only the pages around the
39
+ * viewport are in the DOM, so thousands of values stay responsive.
40
+ *
41
+ * Rows are plain DOM rather than `<facet-row>` elements, and one listener
42
+ * handles every row's checkbox clicks.
43
+ *
44
+ * See docs/design/column-virtualization.md before changing the windowing.
45
+ *
46
+ * @fires facetClick - A row's checkbox was clicked. Detail: `FacetEventDetails`
47
+ * @fires pageChanged - The scroller came to rest on a different page. Detail:
48
+ * the 0-based page index
49
+ */
50
+ @customElement('more-facets-scroller')
51
+ export class MoreFacetsScroller extends LitElement {
52
+ /** The name of the facet group the buckets belong to (e.g., "subject") */
53
+ @property({ type: String }) facetType?: FacetOption;
54
+
55
+ /** Every bucket to show, in display order */
56
+ @property({ type: Array }) buckets: FacetBucket[] = [];
57
+
58
+ /** The collection name cache for converting collection identifiers to titles */
59
+ @property({ type: Object }) collectionTitles?: CollectionTitles;
60
+
61
+ /** How many buckets to stack in each column before starting the next one */
62
+ @property({ type: Number }) rowsPerColumn = MORE_FACETS__ROWS_PER_COLUMN;
63
+
64
+ /**
65
+ * Whether rows leave out their hide (eye) button, unless the value is
66
+ * already hidden. Set it when the facet has only one value in all, since
67
+ * hiding that would leave no results. Don't base it on how many rows are
68
+ * showing: a filter that leaves a single row still needs its hide button.
69
+ */
70
+ @property({ type: Boolean }) omitHideButtons = false;
71
+
72
+ /** Accessible name for the columns, e.g. "Subject values" */
73
+ @property({ type: String }) label?: string;
74
+
75
+ private win = new PageWindow(this, {
76
+ colWidth: MORE_FACETS__COLUMN_WIDTH,
77
+ total: 0,
78
+ onRest: page => this.rested(page),
79
+ });
80
+
81
+ /** The page an arrow button is scrolling to, until the scroller rests */
82
+ private pendingPage?: number;
83
+
84
+ /** The last page reported in a `pageChanged` event */
85
+ private reportedPage = 0;
86
+
87
+ @query('.scroller')
88
+ private scroller?: HTMLElement;
89
+
90
+ /** The page resting at the start of the viewport */
91
+ get currentPage(): number {
92
+ return this.win.currentPage;
93
+ }
94
+
95
+ /** How many pages the buckets span at the current width */
96
+ get pageCount(): number {
97
+ return this.win.pageCount;
98
+ }
99
+
100
+ /** Scrolls so that `page` rests at the start of the viewport */
101
+ scrollToPage(page: number, behavior: ScrollBehavior = 'auto'): void {
102
+ this.win.scrollToPage(page, behavior);
103
+ }
104
+
105
+ willUpdate(changed: PropertyValues): void {
106
+ if (changed.has('buckets') || changed.has('rowsPerColumn')) {
107
+ this.win.setTotal(Math.ceil(this.buckets.length / this.rowsPerColumn));
108
+ }
109
+ this.keepFocusOnScreen();
110
+ }
111
+
112
+ render(): TemplateResult {
113
+ const w = this.win;
114
+ const live: number[] = [];
115
+ for (let i = w.first; i <= w.last; i++) live.push(i);
116
+
117
+ return html`
118
+ <div class="scroll-nav">
119
+ ${this.arrowTemplate('prev')}
120
+ <div class="frame">
121
+ <div
122
+ class="scroller"
123
+ role="group"
124
+ aria-label=${ifDefined(this.label)}
125
+ tabindex=${w.pageCount > 0 ? 0 : -1}
126
+ style="--rowsPerColumn: ${this.rowsPerColumn}"
127
+ ${ref(w.attach)}
128
+ @click=${this.rowClicked}
129
+ @focusin=${this.rowFocused}
130
+ >
131
+ <div class="sizer" style="width:${w.totalWidth}px">
132
+ ${guard([w.pageCount, w.pageWidth, w.totalWidth], () =>
133
+ this.snapStubsTemplate(),
134
+ )}
135
+ ${repeat(
136
+ live,
137
+ i => i,
138
+ i =>
139
+ html`<div
140
+ class="page"
141
+ data-page=${i}
142
+ style="left:${i * w.pageWidth}px;width:${w.pageSpan(i)}px"
143
+ >
144
+ ${this.pageTemplate(i)}
145
+ </div>`,
146
+ )}
147
+ </div>
148
+ </div>
149
+ </div>
150
+ ${this.arrowTemplate('next')}
151
+ </div>
152
+ `;
153
+ }
154
+
155
+ /**
156
+ * One empty snap target per page, always mounted. The browser keeps
157
+ * re-picking its snap target while a fling decelerates, so the targets must
158
+ * not come and go with the window of rendered pages.
159
+ */
160
+ private snapStubsTemplate(): TemplateResult {
161
+ const w = this.win;
162
+ const all = Array.from({ length: w.pageCount }, (_, i) => i);
163
+ return html`${repeat(
164
+ all,
165
+ i => i,
166
+ i =>
167
+ html`<div
168
+ class="snap-stub"
169
+ style="left:${i * w.pageWidth}px;width:${w.pageSpan(i)}px"
170
+ ></div>`,
171
+ )}`;
172
+ }
173
+
174
+ /** The columns of rows on page `page` */
175
+ private pageTemplate(page: number): TemplateResult[] {
176
+ const { facetType, buckets, rowsPerColumn } = this;
177
+ if (!facetType) return [];
178
+
179
+ const { colsPerPage } = this.win;
180
+ const firstCol = page * colsPerPage;
181
+ const endCol = Math.min(
182
+ firstCol + colsPerPage,
183
+ Math.ceil(buckets.length / rowsPerColumn),
184
+ );
185
+
186
+ const columns: TemplateResult[] = [];
187
+ for (let col = firstCol; col < endCol; col++) {
188
+ const start = col * rowsPerColumn;
189
+ const rows = buckets.slice(start, start + rowsPerColumn);
190
+ columns.push(
191
+ html`<div class="column">
192
+ ${rows.map(bucket =>
193
+ facetRowTemplate({
194
+ facetType,
195
+ bucket,
196
+ collectionTitles: this.collectionTitles,
197
+ omitHideButton: this.omitHideButtons,
198
+ }),
199
+ )}
200
+ </div>`,
201
+ );
202
+ }
203
+ return columns;
204
+ }
205
+
206
+ private arrowTemplate(direction: 'prev' | 'next'): TemplateResult {
207
+ const { currentPage, pageCount } = this.win;
208
+ const isPrev = direction === 'prev';
209
+ const disabled = isPrev ? currentPage <= 0 : currentPage >= pageCount - 1;
210
+
211
+ // Arrows keep their space even when there's nothing to scroll to, so that
212
+ // showing them never changes how many columns fit.
213
+ const classes = classMap({
214
+ 'scroll-arrow': true,
215
+ [direction]: true,
216
+ unneeded: pageCount <= 1,
217
+ });
218
+
219
+ return html`<button
220
+ type="button"
221
+ class=${classes}
222
+ ?disabled=${disabled}
223
+ aria-label=${isPrev
224
+ ? msg('Show previous page of values')
225
+ : msg('Show next page of values')}
226
+ @click=${() => this.arrowClicked(isPrev ? -1 : 1)}
227
+ >
228
+ ${isPrev ? arrowLeftIcon : arrowRightIcon}
229
+ </button>`;
230
+ }
231
+
232
+ /**
233
+ * Handles clicks on any row's checkboxes. The rows have no listeners of
234
+ * their own.
235
+ */
236
+ private rowClicked(e: Event): void {
237
+ const input = e.target;
238
+ const { facetType } = this;
239
+ if (!(input instanceof HTMLInputElement) || !facetType) return;
240
+
241
+ const bucket = this.buckets.find(b => b.key === input.value);
242
+ if (!bucket) return;
243
+
244
+ const negative = input.classList.contains('hide-facet-checkbox');
245
+ this.dispatchEvent(
246
+ new CustomEvent<FacetEventDetails>('facetClick', {
247
+ detail: {
248
+ facetType,
249
+ bucket: { ...bucket, state: getFacetState(input.checked, negative) },
250
+ negative,
251
+ },
252
+ }),
253
+ );
254
+ }
255
+
256
+ /**
257
+ * When focus comes into the rows from outside them, or from the scroller
258
+ * itself (a tab stop just before the rows), keeps it on the page being
259
+ * shown. Otherwise Tab lands on the mounted page before this one, and
260
+ * Shift+Tab on the last one after it, and the browser scrolls there.
261
+ * Moving from row to row is left alone, so Tab and Shift+Tab still carry on
262
+ * into the next or previous page.
263
+ */
264
+ private rowFocused(e: FocusEvent): void {
265
+ const { scroller } = this;
266
+ const row = e.target;
267
+ if (!scroller || !(row instanceof HTMLElement) || row === scroller) return;
268
+
269
+ const from = e.relatedTarget;
270
+ const fromRows =
271
+ from instanceof Node && from !== scroller && scroller.contains(from);
272
+ if (fromRows) return;
273
+
274
+ const page = Number(row.closest<HTMLElement>('.page')?.dataset.page);
275
+ const { currentPage } = this.win;
276
+ if (Number.isNaN(page) || page === currentPage) return;
277
+
278
+ const rowsOnPage = [
279
+ ...scroller.querySelectorAll<HTMLElement>(
280
+ `.page[data-page="${currentPage}"] input`,
281
+ ),
282
+ ].filter(input => !input.closest('[hidden]'));
283
+ const target =
284
+ page < currentPage ? rowsOnPage[0] : rowsOnPage[rowsOnPage.length - 1];
285
+ target?.focus({ preventScroll: true });
286
+
287
+ // Whatever moved focus may also scroll to the row it picked: in the
288
+ // dialog, modal-manager's focus trap already has, before this event; the
289
+ // browser's own Tab does just after this handler returns. Put the page
290
+ // back now, and again before the next frame is painted (and before the
291
+ // window is recomputed, so this page stays mounted).
292
+ this.win.scrollToPage(currentPage);
293
+ requestAnimationFrame(() => this.win.scrollToPage(currentPage));
294
+ }
295
+
296
+ /**
297
+ * If a row has focus and its page is about to leave the window, moves focus
298
+ * to the scroller itself. Otherwise focus would fall back to the document
299
+ * when the page is removed, and the arrow keys would stop scrolling. The
300
+ * scroller being a tab stop matters here: modal-manager's focus trap only
301
+ * moves Tab between tabbable elements, and from anything else it goes back
302
+ * to the start of the dialog.
303
+ */
304
+ private keepFocusOnScreen(): void {
305
+ const focusedPage =
306
+ this.shadowRoot?.activeElement?.closest<HTMLElement>('.page');
307
+ if (!focusedPage) return;
308
+ const page = Number(focusedPage.dataset.page);
309
+ if (page >= this.win.first && page <= this.win.last) return;
310
+ this.scroller?.focus({ preventScroll: true });
311
+ }
312
+
313
+ private arrowClicked(step: 1 | -1): void {
314
+ // Count from where an earlier click is already headed, so that clicking
315
+ // twice quickly moves two pages.
316
+ const from = this.pendingPage ?? this.win.currentPage;
317
+ const target = Math.max(0, Math.min(from + step, this.win.pageCount - 1));
318
+ this.pendingPage = target;
319
+
320
+ const reduceMotion = window.matchMedia?.(
321
+ '(prefers-reduced-motion: reduce)',
322
+ ).matches;
323
+ this.win.scrollToPage(target, reduceMotion ? 'auto' : 'smooth');
324
+ }
325
+
326
+ private rested(page: number): void {
327
+ this.pendingPage = undefined;
328
+ if (page === this.reportedPage) return;
329
+ this.reportedPage = page;
330
+ this.dispatchEvent(
331
+ new CustomEvent<number>('pageChanged', { detail: page }),
332
+ );
333
+ }
334
+
335
+ static get styles(): CSSResultGroup {
336
+ const colWidth = unsafeCSS(`${MORE_FACETS__COLUMN_WIDTH}px`);
337
+ const scrollbarSize = unsafeCSS(`${SCROLLBAR_SIZE}px`);
338
+
339
+ const ownCss = css`
340
+ :host {
341
+ display: block;
342
+ --colWidth: ${colWidth};
343
+ --facetRowHeight: 2.4rem;
344
+ }
345
+
346
+ .scroll-nav {
347
+ display: flex;
348
+ align-items: center;
349
+ }
350
+
351
+ .frame {
352
+ flex: 1 1 auto;
353
+ min-width: 0;
354
+ container-type: inline-size;
355
+ }
356
+
357
+ .scroller {
358
+ /* Fallback where round() is unsupported: fill the frame */
359
+ width: 100%;
360
+ /* Otherwise round down to whole columns, so none is ever cut off */
361
+ width: max(
362
+ round(down, 100cqi, var(--colWidth)),
363
+ min(100cqi, var(--colWidth))
364
+ );
365
+ height: calc(
366
+ var(--rowsPerColumn) * var(--facetRowHeight) + ${scrollbarSize}
367
+ );
368
+ margin: 0 auto;
369
+ outline-offset: 2px;
370
+ overflow-x: auto;
371
+ overflow-y: hidden;
372
+ contain: strict;
373
+ scroll-snap-type: x mandatory;
374
+ /* scroll-snap-stop stays at its default 'normal' so a fling can cross
375
+ many pages before resting. 'always' would force one page per gesture. */
376
+ }
377
+
378
+ /* Keep the scrollbar visible, so it's clear there is more to see */
379
+ .scroller::-webkit-scrollbar {
380
+ height: ${scrollbarSize};
381
+ }
382
+ .scroller::-webkit-scrollbar-track {
383
+ background: #f1f1f1;
384
+ border-radius: 6px;
385
+ }
386
+ .scroller::-webkit-scrollbar-thumb {
387
+ background: #888;
388
+ border-radius: 6px;
389
+ }
390
+ .scroller::-webkit-scrollbar-thumb:hover {
391
+ background: #555;
392
+ }
393
+ /* Chrome ignores the rules above once these are set, so only Firefox gets them */
394
+ @supports not selector(::-webkit-scrollbar) {
395
+ .scroller {
396
+ scrollbar-width: thin;
397
+ scrollbar-color: #888 #f1f1f1;
398
+ }
399
+ }
400
+
401
+ .sizer {
402
+ position: relative;
403
+ height: 100%;
404
+ }
405
+
406
+ .snap-stub {
407
+ position: absolute;
408
+ top: 0;
409
+ height: 100%;
410
+ scroll-snap-align: start;
411
+ pointer-events: none;
412
+ contain: strict;
413
+ }
414
+
415
+ .page {
416
+ position: absolute;
417
+ top: 0;
418
+ height: 100%;
419
+ display: flex;
420
+ contain: content;
421
+ content-visibility: auto;
422
+ contain-intrinsic-size: auto none;
423
+ }
424
+
425
+ .column {
426
+ flex: none;
427
+ width: var(--colWidth);
428
+ box-sizing: border-box;
429
+ padding: 0 0.75rem;
430
+ }
431
+
432
+ /* Rows are a fixed height with one line of text, so every column holds
433
+ exactly rowsPerColumn rows. The full text is in the row's tooltip. */
434
+ .facet-row-container {
435
+ height: var(--facetRowHeight);
436
+ box-sizing: border-box;
437
+ align-items: center;
438
+ }
439
+ .facet-info-display {
440
+ flex-wrap: nowrap;
441
+ min-width: 0;
442
+ }
443
+ .facet-title {
444
+ min-width: 0;
445
+ overflow: hidden;
446
+ white-space: nowrap;
447
+ text-overflow: ellipsis;
448
+ word-break: normal;
449
+ }
450
+ .facet-count {
451
+ flex: none;
452
+ padding-left: 0.5rem;
453
+ }
454
+
455
+ .scroll-arrow {
456
+ flex: none;
457
+ width: 2.4rem;
458
+ padding: 0.5rem;
459
+ background: none;
460
+ border: none;
461
+ cursor: pointer;
462
+ }
463
+ .scroll-arrow svg {
464
+ height: 14px;
465
+ fill: #2c2c2c;
466
+ }
467
+ .scroll-arrow:disabled {
468
+ opacity: 0.3;
469
+ cursor: default;
470
+ }
471
+ .scroll-arrow.unneeded {
472
+ visibility: hidden;
473
+ }
474
+
475
+ /* Touch screens swipe; the space is better spent on the column */
476
+ @media (max-width: 560px) {
477
+ .scroll-arrow {
478
+ display: none;
479
+ }
480
+ }
481
+ `;
482
+
483
+ return [srOnlyStyle, facetRowStyles, ownCss];
484
+ }
485
+ }