@internetarchive/collection-browser 3.2.0 → 3.3.1-alpha1

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 (76) hide show
  1. package/.editorconfig +29 -29
  2. package/.github/workflows/ci.yml +27 -27
  3. package/.github/workflows/gh-pages-main.yml +39 -39
  4. package/.github/workflows/npm-publish.yml +39 -39
  5. package/.github/workflows/pr-preview.yml +38 -38
  6. package/.husky/pre-commit +4 -4
  7. package/.prettierignore +1 -1
  8. package/LICENSE +661 -661
  9. package/README.md +83 -83
  10. package/dist/src/collection-browser.d.ts +4 -0
  11. package/dist/src/collection-browser.js +702 -682
  12. package/dist/src/collection-browser.js.map +1 -1
  13. package/dist/src/collection-facets/more-facets-content.js +118 -118
  14. package/dist/src/collection-facets/more-facets-content.js.map +1 -1
  15. package/dist/src/collection-facets.js +266 -266
  16. package/dist/src/collection-facets.js.map +1 -1
  17. package/dist/src/data-source/collection-browser-data-source.js.map +1 -1
  18. package/dist/src/data-source/collection-browser-query-state.js.map +1 -1
  19. package/dist/src/data-source/models.js.map +1 -1
  20. package/dist/src/tiles/base-tile-component.d.ts +7 -0
  21. package/dist/src/tiles/base-tile-component.js +13 -0
  22. package/dist/src/tiles/base-tile-component.js.map +1 -1
  23. package/dist/src/tiles/grid/account-tile.js +37 -36
  24. package/dist/src/tiles/grid/account-tile.js.map +1 -1
  25. package/dist/src/tiles/grid/collection-tile.js +78 -77
  26. package/dist/src/tiles/grid/collection-tile.js.map +1 -1
  27. package/dist/src/tiles/grid/item-tile.d.ts +1 -1
  28. package/dist/src/tiles/grid/item-tile.js +139 -140
  29. package/dist/src/tiles/grid/item-tile.js.map +1 -1
  30. package/dist/src/tiles/list/tile-list-compact.js +100 -98
  31. package/dist/src/tiles/list/tile-list-compact.js.map +1 -1
  32. package/dist/src/tiles/list/tile-list.js +300 -300
  33. package/dist/src/tiles/list/tile-list.js.map +1 -1
  34. package/dist/src/tiles/tile-dispatcher.js +204 -200
  35. package/dist/src/tiles/tile-dispatcher.js.map +1 -1
  36. package/dist/src/utils/format-date.d.ts +15 -1
  37. package/dist/src/utils/format-date.js +8 -3
  38. package/dist/src/utils/format-date.js.map +1 -1
  39. package/dist/test/collection-browser.test.js +189 -189
  40. package/dist/test/collection-browser.test.js.map +1 -1
  41. package/dist/test/tiles/grid/item-tile.test.js +115 -64
  42. package/dist/test/tiles/grid/item-tile.test.js.map +1 -1
  43. package/dist/test/tiles/list/tile-list-compact.test.js +108 -57
  44. package/dist/test/tiles/list/tile-list-compact.test.js.map +1 -1
  45. package/dist/test/tiles/list/tile-list.test.js +164 -113
  46. package/dist/test/tiles/list/tile-list.test.js.map +1 -1
  47. package/dist/test/utils/format-date.test.js +31 -1
  48. package/dist/test/utils/format-date.test.js.map +1 -1
  49. package/eslint.config.mjs +53 -53
  50. package/index.html +24 -24
  51. package/local.archive.org.cert +86 -86
  52. package/local.archive.org.key +27 -27
  53. package/package.json +117 -117
  54. package/renovate.json +6 -6
  55. package/src/collection-browser.ts +2829 -2804
  56. package/src/collection-facets/more-facets-content.ts +639 -639
  57. package/src/collection-facets.ts +995 -995
  58. package/src/data-source/collection-browser-data-source.ts +1401 -1401
  59. package/src/data-source/collection-browser-query-state.ts +65 -65
  60. package/src/data-source/models.ts +43 -43
  61. package/src/tiles/base-tile-component.ts +65 -53
  62. package/src/tiles/grid/account-tile.ts +113 -112
  63. package/src/tiles/grid/collection-tile.ts +163 -162
  64. package/src/tiles/grid/item-tile.ts +340 -339
  65. package/src/tiles/list/tile-list-compact.ts +239 -236
  66. package/src/tiles/list/tile-list.ts +700 -696
  67. package/src/tiles/tile-dispatcher.ts +490 -486
  68. package/src/utils/format-date.ts +62 -42
  69. package/test/collection-browser.test.ts +2403 -2403
  70. package/test/tiles/grid/item-tile.test.ts +520 -464
  71. package/test/tiles/list/tile-list-compact.test.ts +282 -228
  72. package/test/tiles/list/tile-list.test.ts +552 -497
  73. package/test/utils/format-date.test.ts +89 -39
  74. package/tsconfig.json +20 -20
  75. package/web-dev-server.config.mjs +30 -30
  76. package/web-test-runner.config.mjs +41 -41
@@ -1,696 +1,700 @@
1
- import { css, html, nothing, PropertyValues, TemplateResult } from 'lit';
2
- import { ifDefined } from 'lit/directives/if-defined.js';
3
- import { join } from 'lit/directives/join.js';
4
- import { map } from 'lit/directives/map.js';
5
- import { unsafeHTML } from 'lit/directives/unsafe-html.js';
6
- import { customElement, property, state } from 'lit/decorators.js';
7
- import { msg, str } from '@lit/localize';
8
- import DOMPurify from 'dompurify';
9
-
10
- import type { SortParam } from '@internetarchive/search-service';
11
- import { suppressedCollections } from '../../models';
12
- import type { CollectionTitles } from '../../data-source/models';
13
- import { BaseTileComponent } from '../base-tile-component';
14
-
15
- import { formatCount, NumberFormat } from '../../utils/format-count';
16
- import { formatDate, DateFormat } from '../../utils/format-date';
17
- import { isFirstMillisecondOfUTCYear } from '../../utils/local-date-from-utc';
18
-
19
- import '../image-block';
20
- import '../review-block';
21
- import '../text-snippet-block';
22
- import '../tile-mediatype-icon';
23
-
24
- @customElement('tile-list')
25
- export class TileList extends BaseTileComponent {
26
- /*
27
- * Reactive properties inherited from BaseTileComponent:
28
- * - model?: TileModel;
29
- * - currentWidth?: number;
30
- * - currentHeight?: number;
31
- * - baseNavigationUrl?: string;
32
- * - baseImageUrl?: string;
33
- * - collectionPagePath?: string;
34
- * - sortParam: SortParam | null = null;
35
- * - defaultSortParam: SortParam | null = null;
36
- * - creatorFilter?: string;
37
- * - mobileBreakpoint?: number;
38
- * - loggedIn = false;
39
- * - suppressBlurring = false;
40
- */
41
-
42
- @property({ type: Object })
43
- collectionTitles?: CollectionTitles;
44
-
45
- @state() private collectionLinks: (TemplateResult | typeof nothing)[] = [];
46
-
47
- render() {
48
- return html`
49
- <div id="list-line" class="${this.classSize}">
50
- ${this.classSize === 'mobile'
51
- ? this.mobileTemplate
52
- : this.desktopTemplate}
53
- </div>
54
- `;
55
- }
56
-
57
- /**
58
- * Templates
59
- */
60
- private get mobileTemplate() {
61
- return html`
62
- <div id="list-line-top">
63
- <div id="list-line-left">${this.imageBlockTemplate}</div>
64
- <div id="list-line-right">
65
- <div id="title-line">
66
- <div id="title">${this.titleTemplate}</div>
67
- ${this.iconRightTemplate}
68
- </div>
69
- </div>
70
- </div>
71
- <div id="list-line-bottom">${this.detailsTemplate}</div>
72
- `;
73
- }
74
-
75
- private get desktopTemplate() {
76
- return html`
77
- <div id="list-line-left">${this.imageBlockTemplate}</div>
78
- <div id="list-line-right">
79
- <div id="title-line">
80
- <div id="title">${this.titleTemplate}</div>
81
- ${this.iconRightTemplate}
82
- </div>
83
- ${this.detailsTemplate}
84
- </div>
85
- `;
86
- }
87
-
88
- private get imageBlockTemplate() {
89
- if (!this.model) return nothing;
90
-
91
- const isCollection = this.model.mediatype === 'collection';
92
- const href = this.displayValueProvider.itemPageUrl(
93
- this.model.identifier,
94
- isCollection,
95
- );
96
-
97
- return html`<a
98
- id="image-link"
99
- title=${msg(str`View ${this.model?.title}`)}
100
- href=${href}
101
- >
102
- <image-block
103
- .model=${this.model}
104
- .baseImageUrl=${this.baseImageUrl}
105
- .isCompactTile=${false}
106
- .isListTile=${true}
107
- .viewSize=${this.classSize}
108
- .loggedIn=${this.loggedIn}
109
- .suppressBlurring=${this.suppressBlurring}
110
- >
111
- </image-block>
112
- </a> `;
113
- }
114
-
115
- private get detailsTemplate() {
116
- return html`
117
- ${this.itemLineTemplate} ${this.creatorTemplate}
118
- <div id="dates-line">
119
- ${this.datePublishedTemplate} ${this.dateSortByTemplate}
120
- ${this.webArchivesCaptureDatesTemplate}
121
- </div>
122
- <div id="views-line">
123
- ${this.viewsTemplate} ${this.ratingTemplate} ${this.reviewsTemplate}
124
- </div>
125
- ${this.topicsTemplate} ${this.collectionsTemplate}
126
- ${this.descriptionTemplate} ${this.textSnippetsTemplate}
127
- ${this.reviewBlockTemplate}
128
- `;
129
- }
130
-
131
- // Data templates
132
- private get iconRightTemplate() {
133
- return html`
134
- <a
135
- id="icon-right"
136
- href=${this.mediatypeURL}
137
- title=${msg(str`See more: ${this.model?.mediatype}`)}
138
- >
139
- <tile-mediatype-icon .model=${this.model}> </tile-mediatype-icon>
140
- </a>
141
- `;
142
- }
143
-
144
- private get titleTemplate() {
145
- if (!this.model?.title) {
146
- return nothing;
147
- }
148
-
149
- // If the model has a server-specified href, use it
150
- // Otherwise construct a details link using the identifier
151
- return this.model?.href
152
- ? html`<a href="${this.baseNavigationUrl}${this.model.href}"
153
- >${this.model.title ?? this.model.identifier}</a
154
- >`
155
- : this.detailsLink(
156
- this.model.identifier,
157
- this.model.title,
158
- this.model.mediatype === 'collection',
159
- );
160
- }
161
-
162
- private get itemLineTemplate() {
163
- const source = this.sourceTemplate;
164
- const volume = this.volumeTemplate;
165
- const issue = this.issueTemplate;
166
- if (!source && !volume && !issue) {
167
- return nothing;
168
- }
169
- return html` <div id="item-line">${source} ${volume} ${issue}</div> `;
170
- }
171
-
172
- private get sourceTemplate() {
173
- if (!this.model?.source) {
174
- return nothing;
175
- }
176
- return html`
177
- <div id="source" class="metadata">
178
- ${this.labelTemplate(msg('Source'))}
179
- ${this.searchLink('source', this.model.source)}
180
- </div>
181
- `;
182
- }
183
-
184
- private get volumeTemplate() {
185
- return this.metadataTemplate(this.model?.volume, msg('Volume'));
186
- }
187
-
188
- private get issueTemplate() {
189
- return this.metadataTemplate(this.model?.issue, msg('Issue'));
190
- }
191
-
192
- private get creatorTemplate() {
193
- // "Archivist since" if account
194
- if (this.model?.mediatype === 'account') {
195
- return html`
196
- <div id="creator" class="metadata">
197
- <span class="label"
198
- >${this.displayValueProvider.accountLabel ?? nothing}</span
199
- >
200
- </div>
201
- `;
202
- }
203
- // "Creator" if not account tile
204
- if (!this.model?.creators || this.model.creators.length === 0) {
205
- return nothing;
206
- }
207
- return html`
208
- <div id="creator" class="metadata">
209
- ${this.labelTemplate(msg('By'))}
210
- ${join(
211
- map(this.model.creators, id => this.searchLink('creator', id)),
212
- ', ',
213
- )}
214
- </div>
215
- `;
216
- }
217
-
218
- private get datePublishedTemplate() {
219
- // If we're showing a date published of Jan 1 at midnight, only show the year.
220
- // This is because items with only a year for their publication date are normalized to
221
- // Jan 1 at midnight timestamps in the search engine documents.
222
- const date: Date | undefined = this.model?.datePublished;
223
- let format: DateFormat = 'long';
224
- if (isFirstMillisecondOfUTCYear(date)) {
225
- format = 'year-only';
226
- }
227
-
228
- return this.metadataTemplate(formatDate(date, format), msg('Published'));
229
- }
230
-
231
- // Show date label/value when sorted by date type
232
- // Except datePublished which is always shown
233
- private get dateSortByTemplate() {
234
- if (
235
- this.effectiveSort &&
236
- (this.effectiveSort.field === 'addeddate' ||
237
- this.effectiveSort.field === 'reviewdate' ||
238
- this.effectiveSort.field === 'publicdate')
239
- ) {
240
- return this.metadataTemplate(
241
- formatDate(this.date, 'long'),
242
- this.displayValueProvider.dateLabel,
243
- );
244
- }
245
- return nothing;
246
- }
247
-
248
- private get viewsTemplate() {
249
- const viewCount =
250
- this.effectiveSort?.field === 'week'
251
- ? this.model?.weeklyViewCount // weekly views
252
- : this.model?.viewCount; // all-time views
253
- if (viewCount == null) return nothing;
254
-
255
- // when its a search-tile, we don't have any stats to show
256
- if (this.model?.mediatype === 'search') {
257
- return this.metadataTemplate('(Favorited search query)', '');
258
- }
259
-
260
- return this.metadataTemplate(
261
- `${formatCount(viewCount, this.formatSize)}`,
262
- msg('Views'),
263
- );
264
- }
265
-
266
- private get ratingTemplate() {
267
- return this.metadataTemplate(this.model?.averageRating, msg('Avg Rating'));
268
- }
269
-
270
- private get reviewsTemplate() {
271
- return this.metadataTemplate(this.model?.commentCount, msg('Reviews'));
272
- }
273
-
274
- private get topicsTemplate() {
275
- if (!this.model?.subjects || this.model.subjects.length === 0) {
276
- return nothing;
277
- }
278
- return html`
279
- <div id="topics" class="metadata">
280
- ${this.labelTemplate(msg('Topics'))}
281
- ${join(
282
- map(this.model.subjects, id => this.searchLink('subject', id)),
283
- ', ',
284
- )}
285
- </div>
286
- `;
287
- }
288
-
289
- private get collectionsTemplate() {
290
- if (!this.collectionLinks || this.collectionLinks.length === 0) {
291
- return nothing;
292
- }
293
- return html`
294
- <div id="collections" class="metadata">
295
- ${this.labelTemplate(msg('Collections'))}
296
- ${join(this.collectionLinks, ', ')}
297
- </div>
298
- `;
299
- }
300
-
301
- private get descriptionTemplate() {
302
- return this.metadataTemplate(
303
- // Sanitize away any HTML tags and convert line breaks to spaces.
304
- unsafeHTML(
305
- DOMPurify.sanitize(this.model?.description?.replace(/\n/g, ' ') ?? ''),
306
- ),
307
- '',
308
- 'description',
309
- );
310
- }
311
-
312
- private get reviewBlockTemplate(): TemplateResult | typeof nothing {
313
- if (!this.model?.review) return nothing;
314
-
315
- const { reviewtitle, reviewbody, stars } = this.model.review;
316
- return html`
317
- <review-block
318
- viewsize="list"
319
- title=${ifDefined(reviewtitle)}
320
- body=${ifDefined(reviewbody)}
321
- starRating=${ifDefined(stars)}
322
- >
323
- </review-block>
324
- `;
325
- }
326
-
327
- private get textSnippetsTemplate(): TemplateResult | typeof nothing {
328
- if (!this.hasSnippets) return nothing;
329
-
330
- return html`<text-snippet-block
331
- viewsize="list"
332
- .snippets=${this.model?.snippets}
333
- ></text-snippet-block>`;
334
- }
335
-
336
- private get hasSnippets(): boolean {
337
- return !!this.model?.snippets?.length;
338
- }
339
-
340
- private get webArchivesCaptureDatesTemplate():
341
- | TemplateResult
342
- | typeof nothing {
343
- if (!this.model?.captureDates || !this.model.title) return nothing;
344
-
345
- return html`
346
- <ul class="capture-dates">
347
- ${map(
348
- this.model.captureDates,
349
- date =>
350
- html`<li>
351
- ${this.displayValueProvider.webArchivesCaptureLink(
352
- this.model!.title,
353
- date,
354
- )}
355
- </li>`,
356
- )}
357
- </ul>
358
- `;
359
- }
360
-
361
- // Utility functions
362
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
363
- private metadataTemplate(text: any, label = '', id?: string) {
364
- if (!text) return nothing;
365
- return html`
366
- <div id=${ifDefined(id)} class="metadata">
367
- ${this.labelTemplate(label)} ${text}
368
- </div>
369
- `;
370
- }
371
-
372
- private labelTemplate(label: string) {
373
- return html` ${label
374
- ? html`<span class="label">${label}: </span>`
375
- : nothing}`;
376
- }
377
-
378
- private searchLink(field: string, searchTerm: string) {
379
- if (!field || !searchTerm) {
380
- return nothing;
381
- }
382
- const query = encodeURIComponent(`${field}:"${searchTerm}"`);
383
- // No whitespace after closing tag
384
- // Note: single ' for href='' to wrap " in query var gets changed back by yarn format
385
- return html`<a
386
- href="${this.baseNavigationUrl}/search?query=${query}"
387
- rel="nofollow"
388
- >
389
- ${DOMPurify.sanitize(searchTerm)}</a
390
- >`;
391
- }
392
-
393
- private detailsLink(
394
- identifier?: string,
395
- text?: string,
396
- isCollection = false,
397
- ): TemplateResult | typeof nothing {
398
- if (!identifier) return nothing;
399
-
400
- const linkText = text ?? identifier;
401
- const linkHref = this.displayValueProvider.itemPageUrl(
402
- identifier,
403
- isCollection,
404
- );
405
-
406
- return html`<a href=${linkHref}> ${DOMPurify.sanitize(linkText)} </a>`;
407
- }
408
-
409
- /** The URL of this item's mediatype collection, if defined. */
410
- private get mediatypeURL(): string | typeof nothing {
411
- // NB: baseNavigationUrl can be an empty string
412
- if (this.baseNavigationUrl === undefined || !this.model?.mediatype)
413
- return nothing;
414
-
415
- // Need special handling for certain mediatypes that don't have a top-level collection page
416
- switch (this.model.mediatype) {
417
- case 'collection':
418
- return `${this.baseNavigationUrl}/search?query=mediatype:collection&sort=-downloads`;
419
- case 'account':
420
- return nothing;
421
- default:
422
- return this.displayValueProvider.itemPageUrl(
423
- this.model.mediatype,
424
- true,
425
- );
426
- }
427
- }
428
-
429
- protected updated(changed: PropertyValues): void {
430
- if (changed.has('model') || changed.has('collectionTitles')) {
431
- this.buildCollectionLinks();
432
- }
433
- }
434
-
435
- private async buildCollectionLinks() {
436
- if (!this.model?.collections || this.model.collections.length === 0) {
437
- return;
438
- }
439
-
440
- // Note: quirk of Lit: need to replace collectionLinks array,
441
- // otherwise it will not re-render. Can't simply alter the array.
442
- this.collectionLinks = [];
443
- const newCollectionLinks: (TemplateResult | typeof nothing)[] = [];
444
- for (const collection of this.model.collections) {
445
- // Don't include favorites or collections that are meant to be suppressed
446
- if (
447
- !suppressedCollections[collection] &&
448
- !collection.startsWith('fav-')
449
- ) {
450
- newCollectionLinks.push(
451
- this.detailsLink(
452
- collection,
453
- this.collectionTitles?.get(collection) ?? collection,
454
- true,
455
- ),
456
- );
457
- }
458
- }
459
- this.collectionLinks = newCollectionLinks;
460
- }
461
-
462
- /*
463
- * TODO: fix field names to match model in src/collection-browser.ts
464
- * private get dateSortSelector()
465
- * @see src/models.ts
466
- */
467
- private get date(): Date | undefined {
468
- switch (this.effectiveSort?.field) {
469
- case 'date':
470
- return this.model?.datePublished;
471
- case 'reviewdate':
472
- return this.model?.dateReviewed;
473
- case 'addeddate':
474
- return this.model?.dateAdded;
475
- default:
476
- return this.model?.dateArchived; // publicdate
477
- }
478
- }
479
-
480
- /**
481
- * Returns the active sort param if one is set, or the default sort param otherwise.
482
- */
483
- private get effectiveSort(): SortParam | null {
484
- return this.sortParam ?? this.defaultSortParam;
485
- }
486
-
487
- private get classSize(): string {
488
- if (
489
- this.mobileBreakpoint &&
490
- this.currentWidth &&
491
- this.currentWidth < this.mobileBreakpoint
492
- ) {
493
- return 'mobile';
494
- }
495
- return 'desktop';
496
- }
497
-
498
- private get formatSize(): NumberFormat {
499
- if (
500
- this.mobileBreakpoint &&
501
- this.currentWidth &&
502
- this.currentWidth < this.mobileBreakpoint
503
- ) {
504
- return 'short';
505
- }
506
- return 'long';
507
- }
508
-
509
- static get styles() {
510
- return css`
511
- html {
512
- font-size: unset;
513
- }
514
-
515
- div {
516
- font-size: 14px;
517
- }
518
-
519
- div a {
520
- text-decoration: none;
521
- }
522
-
523
- div a:link {
524
- color: var(--ia-theme-link-color, #4b64ff);
525
- }
526
-
527
- .label {
528
- font-weight: bold;
529
- }
530
-
531
- #list-line.mobile {
532
- --infiniteScrollerRowGap: 20px;
533
- --infiniteScrollerRowHeight: auto;
534
- }
535
-
536
- #list-line.desktop {
537
- --infiniteScrollerRowGap: 30px;
538
- --infiniteScrollerRowHeight: auto;
539
- }
540
-
541
- /* fields */
542
- #icon-right {
543
- width: 20px;
544
- padding-top: 5px;
545
- --iconHeight: 20px;
546
- --iconWidth: 20px;
547
- --iconTextAlign: right;
548
- margin-top: -8px;
549
- text-align: right;
550
- }
551
-
552
- #title {
553
- color: #4b64ff;
554
- text-decoration: none;
555
- font-size: 22px;
556
- font-weight: bold;
557
- /* align top of text with image */
558
- line-height: 25px;
559
- margin-top: -4px;
560
- padding-bottom: 2px;
561
- flex-grow: 1;
562
-
563
- display: -webkit-box;
564
- -webkit-box-orient: vertical;
565
- -webkit-line-clamp: 3;
566
- overflow: hidden;
567
- overflow-wrap: anywhere;
568
- }
569
-
570
- .metadata {
571
- line-height: 20px;
572
- }
573
-
574
- #description,
575
- #creator,
576
- #topics,
577
- #source {
578
- text-align: left;
579
- overflow: hidden;
580
- text-overflow: ellipsis;
581
- -webkit-box-orient: vertical;
582
- display: -webkit-box;
583
- word-break: break-word;
584
- -webkit-line-clamp: 3; /* number of lines to show */
585
- line-clamp: 3;
586
-
587
- /*
588
- * Safari doesn't always respect the line-clamping rules above,
589
- * so we add this to ensure these fields still get truncated
590
- */
591
- max-height: 60px;
592
- }
593
-
594
- #collections {
595
- display: -webkit-box;
596
- -webkit-box-orient: vertical;
597
- -webkit-line-clamp: 3;
598
- overflow: hidden;
599
- overflow-wrap: anywhere;
600
- }
601
-
602
- #collections > a {
603
- display: inline-block;
604
- }
605
-
606
- #icon {
607
- padding-top: 5px;
608
- }
609
-
610
- #description {
611
- padding-top: 10px;
612
- }
613
-
614
- /* Top level container */
615
- #list-line {
616
- display: flex;
617
- }
618
-
619
- #list-line.mobile {
620
- flex-direction: column;
621
- }
622
-
623
- #list-line.desktop {
624
- column-gap: 10px;
625
- }
626
-
627
- #list-line-top {
628
- display: flex;
629
- column-gap: 7px;
630
- }
631
-
632
- #list-line-bottom {
633
- padding-top: 4px;
634
- }
635
-
636
- #list-line-right,
637
- #list-line-top,
638
- #list-line-bottom {
639
- width: 100%;
640
- }
641
-
642
- /*
643
- * If the container becomes very tiny, don't let the thumbnail side take
644
- * up too much space. Shouldn't make a difference on ordinary viewport sizes.
645
- */
646
- #list-line-left {
647
- max-width: 25%;
648
-
649
- display: flex;
650
- flex-direction: column;
651
- row-gap: 5px;
652
- }
653
-
654
- div a:hover {
655
- text-decoration: underline;
656
- }
657
-
658
- /* Lines containing multiple div as row */
659
- #item-line,
660
- #dates-line,
661
- #views-line,
662
- #title-line {
663
- display: flex;
664
- flex-direction: row;
665
- column-gap: 10px;
666
- }
667
-
668
- /*
669
- * With the exception of the title line, allow these to wrap if
670
- * the space becomes too small to accommodate them together.
671
- *
672
- * The title line is excluded because it contains the mediatype icon
673
- * which we don't want to wrap.
674
- */
675
- #item-line,
676
- #dates-line,
677
- #views-line {
678
- flex-wrap: wrap;
679
- }
680
-
681
- .capture-dates {
682
- margin: 0;
683
- padding: 0;
684
- list-style-type: none;
685
- }
686
-
687
- .capture-dates a:link {
688
- text-decoration: none;
689
- color: var(--ia-theme-link-color, #4b64ff);
690
- }
691
- .capture-dates a:hover {
692
- text-decoration: underline;
693
- }
694
- `;
695
- }
696
- }
1
+ import { css, html, nothing, PropertyValues, TemplateResult } from 'lit';
2
+ import { ifDefined } from 'lit/directives/if-defined.js';
3
+ import { join } from 'lit/directives/join.js';
4
+ import { map } from 'lit/directives/map.js';
5
+ import { unsafeHTML } from 'lit/directives/unsafe-html.js';
6
+ import { customElement, property, state } from 'lit/decorators.js';
7
+ import { msg, str } from '@lit/localize';
8
+ import DOMPurify from 'dompurify';
9
+
10
+ import type { SortParam } from '@internetarchive/search-service';
11
+ import { suppressedCollections } from '../../models';
12
+ import type { CollectionTitles } from '../../data-source/models';
13
+ import { BaseTileComponent } from '../base-tile-component';
14
+
15
+ import { formatCount, NumberFormat } from '../../utils/format-count';
16
+ import type { DateFormat } from '../../utils/format-date';
17
+ import { isFirstMillisecondOfUTCYear } from '../../utils/local-date-from-utc';
18
+
19
+ import '../image-block';
20
+ import '../review-block';
21
+ import '../text-snippet-block';
22
+ import '../tile-mediatype-icon';
23
+
24
+ @customElement('tile-list')
25
+ export class TileList extends BaseTileComponent {
26
+ /*
27
+ * Reactive properties inherited from BaseTileComponent:
28
+ * - model?: TileModel;
29
+ * - currentWidth?: number;
30
+ * - currentHeight?: number;
31
+ * - baseNavigationUrl?: string;
32
+ * - baseImageUrl?: string;
33
+ * - collectionPagePath?: string;
34
+ * - sortParam: SortParam | null = null;
35
+ * - defaultSortParam: SortParam | null = null;
36
+ * - creatorFilter?: string;
37
+ * - mobileBreakpoint?: number;
38
+ * - loggedIn = false;
39
+ * - suppressBlurring = false;
40
+ * - useLocalTime = false;
41
+ */
42
+
43
+ @property({ type: Object })
44
+ collectionTitles?: CollectionTitles;
45
+
46
+ @state() private collectionLinks: (TemplateResult | typeof nothing)[] = [];
47
+
48
+ render() {
49
+ return html`
50
+ <div id="list-line" class="${this.classSize}">
51
+ ${this.classSize === 'mobile'
52
+ ? this.mobileTemplate
53
+ : this.desktopTemplate}
54
+ </div>
55
+ `;
56
+ }
57
+
58
+ /**
59
+ * Templates
60
+ */
61
+ private get mobileTemplate() {
62
+ return html`
63
+ <div id="list-line-top">
64
+ <div id="list-line-left">${this.imageBlockTemplate}</div>
65
+ <div id="list-line-right">
66
+ <div id="title-line">
67
+ <div id="title">${this.titleTemplate}</div>
68
+ ${this.iconRightTemplate}
69
+ </div>
70
+ </div>
71
+ </div>
72
+ <div id="list-line-bottom">${this.detailsTemplate}</div>
73
+ `;
74
+ }
75
+
76
+ private get desktopTemplate() {
77
+ return html`
78
+ <div id="list-line-left">${this.imageBlockTemplate}</div>
79
+ <div id="list-line-right">
80
+ <div id="title-line">
81
+ <div id="title">${this.titleTemplate}</div>
82
+ ${this.iconRightTemplate}
83
+ </div>
84
+ ${this.detailsTemplate}
85
+ </div>
86
+ `;
87
+ }
88
+
89
+ private get imageBlockTemplate() {
90
+ if (!this.model) return nothing;
91
+
92
+ const isCollection = this.model.mediatype === 'collection';
93
+ const href = this.displayValueProvider.itemPageUrl(
94
+ this.model.identifier,
95
+ isCollection,
96
+ );
97
+
98
+ return html`<a
99
+ id="image-link"
100
+ title=${msg(str`View ${this.model?.title}`)}
101
+ href=${href}
102
+ >
103
+ <image-block
104
+ .model=${this.model}
105
+ .baseImageUrl=${this.baseImageUrl}
106
+ .isCompactTile=${false}
107
+ .isListTile=${true}
108
+ .viewSize=${this.classSize}
109
+ .loggedIn=${this.loggedIn}
110
+ .suppressBlurring=${this.suppressBlurring}
111
+ >
112
+ </image-block>
113
+ </a> `;
114
+ }
115
+
116
+ private get detailsTemplate() {
117
+ return html`
118
+ ${this.itemLineTemplate} ${this.creatorTemplate}
119
+ <div id="dates-line">
120
+ ${this.datePublishedTemplate} ${this.dateSortByTemplate}
121
+ ${this.webArchivesCaptureDatesTemplate}
122
+ </div>
123
+ <div id="views-line">
124
+ ${this.viewsTemplate} ${this.ratingTemplate} ${this.reviewsTemplate}
125
+ </div>
126
+ ${this.topicsTemplate} ${this.collectionsTemplate}
127
+ ${this.descriptionTemplate} ${this.textSnippetsTemplate}
128
+ ${this.reviewBlockTemplate}
129
+ `;
130
+ }
131
+
132
+ // Data templates
133
+ private get iconRightTemplate() {
134
+ return html`
135
+ <a
136
+ id="icon-right"
137
+ href=${this.mediatypeURL}
138
+ title=${msg(str`See more: ${this.model?.mediatype}`)}
139
+ >
140
+ <tile-mediatype-icon .model=${this.model}> </tile-mediatype-icon>
141
+ </a>
142
+ `;
143
+ }
144
+
145
+ private get titleTemplate() {
146
+ if (!this.model?.title) {
147
+ return nothing;
148
+ }
149
+
150
+ // If the model has a server-specified href, use it
151
+ // Otherwise construct a details link using the identifier
152
+ return this.model?.href
153
+ ? html`<a href="${this.baseNavigationUrl}${this.model.href}"
154
+ >${this.model.title ?? this.model.identifier}</a
155
+ >`
156
+ : this.detailsLink(
157
+ this.model.identifier,
158
+ this.model.title,
159
+ this.model.mediatype === 'collection',
160
+ );
161
+ }
162
+
163
+ private get itemLineTemplate() {
164
+ const source = this.sourceTemplate;
165
+ const volume = this.volumeTemplate;
166
+ const issue = this.issueTemplate;
167
+ if (!source && !volume && !issue) {
168
+ return nothing;
169
+ }
170
+ return html` <div id="item-line">${source} ${volume} ${issue}</div> `;
171
+ }
172
+
173
+ private get sourceTemplate() {
174
+ if (!this.model?.source) {
175
+ return nothing;
176
+ }
177
+ return html`
178
+ <div id="source" class="metadata">
179
+ ${this.labelTemplate(msg('Source'))}
180
+ ${this.searchLink('source', this.model.source)}
181
+ </div>
182
+ `;
183
+ }
184
+
185
+ private get volumeTemplate() {
186
+ return this.metadataTemplate(this.model?.volume, msg('Volume'));
187
+ }
188
+
189
+ private get issueTemplate() {
190
+ return this.metadataTemplate(this.model?.issue, msg('Issue'));
191
+ }
192
+
193
+ private get creatorTemplate() {
194
+ // "Archivist since" if account
195
+ if (this.model?.mediatype === 'account') {
196
+ return html`
197
+ <div id="creator" class="metadata">
198
+ <span class="label"
199
+ >${this.displayValueProvider.accountLabel ?? nothing}</span
200
+ >
201
+ </div>
202
+ `;
203
+ }
204
+ // "Creator" if not account tile
205
+ if (!this.model?.creators || this.model.creators.length === 0) {
206
+ return nothing;
207
+ }
208
+ return html`
209
+ <div id="creator" class="metadata">
210
+ ${this.labelTemplate(msg('By'))}
211
+ ${join(
212
+ map(this.model.creators, id => this.searchLink('creator', id)),
213
+ ', ',
214
+ )}
215
+ </div>
216
+ `;
217
+ }
218
+
219
+ private get datePublishedTemplate() {
220
+ // If we're showing a date published of Jan 1 at midnight, only show the year.
221
+ // This is because items with only a year for their publication date are normalized to
222
+ // Jan 1 at midnight timestamps in the search engine documents.
223
+ const date: Date | undefined = this.model?.datePublished;
224
+ let format: DateFormat = 'long';
225
+ if (isFirstMillisecondOfUTCYear(date)) {
226
+ format = 'year-only';
227
+ }
228
+
229
+ return this.metadataTemplate(
230
+ this.getFormattedDate(date, format),
231
+ msg('Published'),
232
+ );
233
+ }
234
+
235
+ // Show date label/value when sorted by date type
236
+ // Except datePublished which is always shown
237
+ private get dateSortByTemplate() {
238
+ if (
239
+ this.effectiveSort &&
240
+ (this.effectiveSort.field === 'addeddate' ||
241
+ this.effectiveSort.field === 'reviewdate' ||
242
+ this.effectiveSort.field === 'publicdate')
243
+ ) {
244
+ return this.metadataTemplate(
245
+ this.getFormattedDate(this.date, 'long'),
246
+ this.displayValueProvider.dateLabel,
247
+ );
248
+ }
249
+ return nothing;
250
+ }
251
+
252
+ private get viewsTemplate() {
253
+ const viewCount =
254
+ this.effectiveSort?.field === 'week'
255
+ ? this.model?.weeklyViewCount // weekly views
256
+ : this.model?.viewCount; // all-time views
257
+ if (viewCount == null) return nothing;
258
+
259
+ // when its a search-tile, we don't have any stats to show
260
+ if (this.model?.mediatype === 'search') {
261
+ return this.metadataTemplate('(Favorited search query)', '');
262
+ }
263
+
264
+ return this.metadataTemplate(
265
+ `${formatCount(viewCount, this.formatSize)}`,
266
+ msg('Views'),
267
+ );
268
+ }
269
+
270
+ private get ratingTemplate() {
271
+ return this.metadataTemplate(this.model?.averageRating, msg('Avg Rating'));
272
+ }
273
+
274
+ private get reviewsTemplate() {
275
+ return this.metadataTemplate(this.model?.commentCount, msg('Reviews'));
276
+ }
277
+
278
+ private get topicsTemplate() {
279
+ if (!this.model?.subjects || this.model.subjects.length === 0) {
280
+ return nothing;
281
+ }
282
+ return html`
283
+ <div id="topics" class="metadata">
284
+ ${this.labelTemplate(msg('Topics'))}
285
+ ${join(
286
+ map(this.model.subjects, id => this.searchLink('subject', id)),
287
+ ', ',
288
+ )}
289
+ </div>
290
+ `;
291
+ }
292
+
293
+ private get collectionsTemplate() {
294
+ if (!this.collectionLinks || this.collectionLinks.length === 0) {
295
+ return nothing;
296
+ }
297
+ return html`
298
+ <div id="collections" class="metadata">
299
+ ${this.labelTemplate(msg('Collections'))}
300
+ ${join(this.collectionLinks, ', ')}
301
+ </div>
302
+ `;
303
+ }
304
+
305
+ private get descriptionTemplate() {
306
+ return this.metadataTemplate(
307
+ // Sanitize away any HTML tags and convert line breaks to spaces.
308
+ unsafeHTML(
309
+ DOMPurify.sanitize(this.model?.description?.replace(/\n/g, ' ') ?? ''),
310
+ ),
311
+ '',
312
+ 'description',
313
+ );
314
+ }
315
+
316
+ private get reviewBlockTemplate(): TemplateResult | typeof nothing {
317
+ if (!this.model?.review) return nothing;
318
+
319
+ const { reviewtitle, reviewbody, stars } = this.model.review;
320
+ return html`
321
+ <review-block
322
+ viewsize="list"
323
+ title=${ifDefined(reviewtitle)}
324
+ body=${ifDefined(reviewbody)}
325
+ starRating=${ifDefined(stars)}
326
+ >
327
+ </review-block>
328
+ `;
329
+ }
330
+
331
+ private get textSnippetsTemplate(): TemplateResult | typeof nothing {
332
+ if (!this.hasSnippets) return nothing;
333
+
334
+ return html`<text-snippet-block
335
+ viewsize="list"
336
+ .snippets=${this.model?.snippets}
337
+ ></text-snippet-block>`;
338
+ }
339
+
340
+ private get hasSnippets(): boolean {
341
+ return !!this.model?.snippets?.length;
342
+ }
343
+
344
+ private get webArchivesCaptureDatesTemplate():
345
+ | TemplateResult
346
+ | typeof nothing {
347
+ if (!this.model?.captureDates || !this.model.title) return nothing;
348
+
349
+ return html`
350
+ <ul class="capture-dates">
351
+ ${map(
352
+ this.model.captureDates,
353
+ date =>
354
+ html`<li>
355
+ ${this.displayValueProvider.webArchivesCaptureLink(
356
+ this.model!.title,
357
+ date,
358
+ )}
359
+ </li>`,
360
+ )}
361
+ </ul>
362
+ `;
363
+ }
364
+
365
+ // Utility functions
366
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
367
+ private metadataTemplate(text: any, label = '', id?: string) {
368
+ if (!text) return nothing;
369
+ return html`
370
+ <div id=${ifDefined(id)} class="metadata">
371
+ ${this.labelTemplate(label)} ${text}
372
+ </div>
373
+ `;
374
+ }
375
+
376
+ private labelTemplate(label: string) {
377
+ return html` ${label
378
+ ? html`<span class="label">${label}: </span>`
379
+ : nothing}`;
380
+ }
381
+
382
+ private searchLink(field: string, searchTerm: string) {
383
+ if (!field || !searchTerm) {
384
+ return nothing;
385
+ }
386
+ const query = encodeURIComponent(`${field}:"${searchTerm}"`);
387
+ // No whitespace after closing tag
388
+ // Note: single ' for href='' to wrap " in query var gets changed back by yarn format
389
+ return html`<a
390
+ href="${this.baseNavigationUrl}/search?query=${query}"
391
+ rel="nofollow"
392
+ >
393
+ ${DOMPurify.sanitize(searchTerm)}</a
394
+ >`;
395
+ }
396
+
397
+ private detailsLink(
398
+ identifier?: string,
399
+ text?: string,
400
+ isCollection = false,
401
+ ): TemplateResult | typeof nothing {
402
+ if (!identifier) return nothing;
403
+
404
+ const linkText = text ?? identifier;
405
+ const linkHref = this.displayValueProvider.itemPageUrl(
406
+ identifier,
407
+ isCollection,
408
+ );
409
+
410
+ return html`<a href=${linkHref}> ${DOMPurify.sanitize(linkText)} </a>`;
411
+ }
412
+
413
+ /** The URL of this item's mediatype collection, if defined. */
414
+ private get mediatypeURL(): string | typeof nothing {
415
+ // NB: baseNavigationUrl can be an empty string
416
+ if (this.baseNavigationUrl === undefined || !this.model?.mediatype)
417
+ return nothing;
418
+
419
+ // Need special handling for certain mediatypes that don't have a top-level collection page
420
+ switch (this.model.mediatype) {
421
+ case 'collection':
422
+ return `${this.baseNavigationUrl}/search?query=mediatype:collection&sort=-downloads`;
423
+ case 'account':
424
+ return nothing;
425
+ default:
426
+ return this.displayValueProvider.itemPageUrl(
427
+ this.model.mediatype,
428
+ true,
429
+ );
430
+ }
431
+ }
432
+
433
+ protected updated(changed: PropertyValues): void {
434
+ if (changed.has('model') || changed.has('collectionTitles')) {
435
+ this.buildCollectionLinks();
436
+ }
437
+ }
438
+
439
+ private async buildCollectionLinks() {
440
+ if (!this.model?.collections || this.model.collections.length === 0) {
441
+ return;
442
+ }
443
+
444
+ // Note: quirk of Lit: need to replace collectionLinks array,
445
+ // otherwise it will not re-render. Can't simply alter the array.
446
+ this.collectionLinks = [];
447
+ const newCollectionLinks: (TemplateResult | typeof nothing)[] = [];
448
+ for (const collection of this.model.collections) {
449
+ // Don't include favorites or collections that are meant to be suppressed
450
+ if (
451
+ !suppressedCollections[collection] &&
452
+ !collection.startsWith('fav-')
453
+ ) {
454
+ newCollectionLinks.push(
455
+ this.detailsLink(
456
+ collection,
457
+ this.collectionTitles?.get(collection) ?? collection,
458
+ true,
459
+ ),
460
+ );
461
+ }
462
+ }
463
+ this.collectionLinks = newCollectionLinks;
464
+ }
465
+
466
+ /*
467
+ * TODO: fix field names to match model in src/collection-browser.ts
468
+ * private get dateSortSelector()
469
+ * @see src/models.ts
470
+ */
471
+ private get date(): Date | undefined {
472
+ switch (this.effectiveSort?.field) {
473
+ case 'date':
474
+ return this.model?.datePublished;
475
+ case 'reviewdate':
476
+ return this.model?.dateReviewed;
477
+ case 'addeddate':
478
+ return this.model?.dateAdded;
479
+ default:
480
+ return this.model?.dateArchived; // publicdate
481
+ }
482
+ }
483
+
484
+ /**
485
+ * Returns the active sort param if one is set, or the default sort param otherwise.
486
+ */
487
+ private get effectiveSort(): SortParam | null {
488
+ return this.sortParam ?? this.defaultSortParam;
489
+ }
490
+
491
+ private get classSize(): string {
492
+ if (
493
+ this.mobileBreakpoint &&
494
+ this.currentWidth &&
495
+ this.currentWidth < this.mobileBreakpoint
496
+ ) {
497
+ return 'mobile';
498
+ }
499
+ return 'desktop';
500
+ }
501
+
502
+ private get formatSize(): NumberFormat {
503
+ if (
504
+ this.mobileBreakpoint &&
505
+ this.currentWidth &&
506
+ this.currentWidth < this.mobileBreakpoint
507
+ ) {
508
+ return 'short';
509
+ }
510
+ return 'long';
511
+ }
512
+
513
+ static get styles() {
514
+ return css`
515
+ html {
516
+ font-size: unset;
517
+ }
518
+
519
+ div {
520
+ font-size: 14px;
521
+ }
522
+
523
+ div a {
524
+ text-decoration: none;
525
+ }
526
+
527
+ div a:link {
528
+ color: var(--ia-theme-link-color, #4b64ff);
529
+ }
530
+
531
+ .label {
532
+ font-weight: bold;
533
+ }
534
+
535
+ #list-line.mobile {
536
+ --infiniteScrollerRowGap: 20px;
537
+ --infiniteScrollerRowHeight: auto;
538
+ }
539
+
540
+ #list-line.desktop {
541
+ --infiniteScrollerRowGap: 30px;
542
+ --infiniteScrollerRowHeight: auto;
543
+ }
544
+
545
+ /* fields */
546
+ #icon-right {
547
+ width: 20px;
548
+ padding-top: 5px;
549
+ --iconHeight: 20px;
550
+ --iconWidth: 20px;
551
+ --iconTextAlign: right;
552
+ margin-top: -8px;
553
+ text-align: right;
554
+ }
555
+
556
+ #title {
557
+ color: #4b64ff;
558
+ text-decoration: none;
559
+ font-size: 22px;
560
+ font-weight: bold;
561
+ /* align top of text with image */
562
+ line-height: 25px;
563
+ margin-top: -4px;
564
+ padding-bottom: 2px;
565
+ flex-grow: 1;
566
+
567
+ display: -webkit-box;
568
+ -webkit-box-orient: vertical;
569
+ -webkit-line-clamp: 3;
570
+ overflow: hidden;
571
+ overflow-wrap: anywhere;
572
+ }
573
+
574
+ .metadata {
575
+ line-height: 20px;
576
+ }
577
+
578
+ #description,
579
+ #creator,
580
+ #topics,
581
+ #source {
582
+ text-align: left;
583
+ overflow: hidden;
584
+ text-overflow: ellipsis;
585
+ -webkit-box-orient: vertical;
586
+ display: -webkit-box;
587
+ word-break: break-word;
588
+ -webkit-line-clamp: 3; /* number of lines to show */
589
+ line-clamp: 3;
590
+
591
+ /*
592
+ * Safari doesn't always respect the line-clamping rules above,
593
+ * so we add this to ensure these fields still get truncated
594
+ */
595
+ max-height: 60px;
596
+ }
597
+
598
+ #collections {
599
+ display: -webkit-box;
600
+ -webkit-box-orient: vertical;
601
+ -webkit-line-clamp: 3;
602
+ overflow: hidden;
603
+ overflow-wrap: anywhere;
604
+ }
605
+
606
+ #collections > a {
607
+ display: inline-block;
608
+ }
609
+
610
+ #icon {
611
+ padding-top: 5px;
612
+ }
613
+
614
+ #description {
615
+ padding-top: 10px;
616
+ }
617
+
618
+ /* Top level container */
619
+ #list-line {
620
+ display: flex;
621
+ }
622
+
623
+ #list-line.mobile {
624
+ flex-direction: column;
625
+ }
626
+
627
+ #list-line.desktop {
628
+ column-gap: 10px;
629
+ }
630
+
631
+ #list-line-top {
632
+ display: flex;
633
+ column-gap: 7px;
634
+ }
635
+
636
+ #list-line-bottom {
637
+ padding-top: 4px;
638
+ }
639
+
640
+ #list-line-right,
641
+ #list-line-top,
642
+ #list-line-bottom {
643
+ width: 100%;
644
+ }
645
+
646
+ /*
647
+ * If the container becomes very tiny, don't let the thumbnail side take
648
+ * up too much space. Shouldn't make a difference on ordinary viewport sizes.
649
+ */
650
+ #list-line-left {
651
+ max-width: 25%;
652
+
653
+ display: flex;
654
+ flex-direction: column;
655
+ row-gap: 5px;
656
+ }
657
+
658
+ div a:hover {
659
+ text-decoration: underline;
660
+ }
661
+
662
+ /* Lines containing multiple div as row */
663
+ #item-line,
664
+ #dates-line,
665
+ #views-line,
666
+ #title-line {
667
+ display: flex;
668
+ flex-direction: row;
669
+ column-gap: 10px;
670
+ }
671
+
672
+ /*
673
+ * With the exception of the title line, allow these to wrap if
674
+ * the space becomes too small to accommodate them together.
675
+ *
676
+ * The title line is excluded because it contains the mediatype icon
677
+ * which we don't want to wrap.
678
+ */
679
+ #item-line,
680
+ #dates-line,
681
+ #views-line {
682
+ flex-wrap: wrap;
683
+ }
684
+
685
+ .capture-dates {
686
+ margin: 0;
687
+ padding: 0;
688
+ list-style-type: none;
689
+ }
690
+
691
+ .capture-dates a:link {
692
+ text-decoration: none;
693
+ color: var(--ia-theme-link-color, #4b64ff);
694
+ }
695
+ .capture-dates a:hover {
696
+ text-decoration: underline;
697
+ }
698
+ `;
699
+ }
700
+ }