@internetarchive/collection-browser 3.3.2 → 3.3.4-alpha-webdev7761.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 (543) 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/index.d.ts +16 -0
  11. package/dist/index.js.map +1 -0
  12. package/dist/src/app-root.d.ts +105 -0
  13. package/dist/src/app-root.js +1076 -0
  14. package/dist/src/app-root.js.map +1 -0
  15. package/dist/src/assets/img/icons/arrow-left.d.ts +2 -0
  16. package/dist/src/assets/img/icons/arrow-left.js +10 -0
  17. package/dist/src/assets/img/icons/arrow-left.js.map +1 -0
  18. package/dist/src/assets/img/icons/arrow-right.d.ts +2 -0
  19. package/dist/src/assets/img/icons/arrow-right.js +10 -0
  20. package/dist/src/assets/img/icons/arrow-right.js.map +1 -0
  21. package/dist/src/assets/img/icons/chevron.d.ts +2 -0
  22. package/dist/src/assets/img/icons/chevron.js +4 -0
  23. package/dist/src/assets/img/icons/chevron.js.map +1 -0
  24. package/dist/src/assets/img/icons/close-circle-dark.d.ts +2 -0
  25. package/dist/src/assets/img/icons/close-circle-dark.js +5 -0
  26. package/dist/src/assets/img/icons/close-circle-dark.js.map +1 -0
  27. package/dist/src/assets/img/icons/contract.d.ts +2 -0
  28. package/dist/src/assets/img/icons/contract.js +9 -0
  29. package/dist/src/assets/img/icons/contract.js.map +1 -0
  30. package/dist/src/assets/img/icons/empty-query.d.ts +2 -0
  31. package/dist/src/assets/img/icons/empty-query.js +5 -0
  32. package/dist/src/assets/img/icons/empty-query.js.map +1 -0
  33. package/dist/src/assets/img/icons/expand.d.ts +2 -0
  34. package/dist/src/assets/img/icons/expand.js +9 -0
  35. package/dist/src/assets/img/icons/expand.js.map +1 -0
  36. package/dist/src/assets/img/icons/eye-closed.d.ts +2 -0
  37. package/dist/src/assets/img/icons/eye-closed.js +5 -0
  38. package/dist/src/assets/img/icons/eye-closed.js.map +1 -0
  39. package/dist/src/assets/img/icons/eye.d.ts +2 -0
  40. package/dist/src/assets/img/icons/eye.js +5 -0
  41. package/dist/src/assets/img/icons/eye.js.map +1 -0
  42. package/dist/src/assets/img/icons/favorite-filled.d.ts +1 -0
  43. package/dist/src/assets/img/icons/favorite-filled.js +10 -0
  44. package/dist/src/assets/img/icons/favorite-filled.js.map +1 -0
  45. package/dist/src/assets/img/icons/favorite-unfilled.d.ts +1 -0
  46. package/dist/src/assets/img/icons/favorite-unfilled.js +9 -0
  47. package/dist/src/assets/img/icons/favorite-unfilled.js.map +1 -0
  48. package/dist/src/assets/img/icons/filter.d.ts +2 -0
  49. package/dist/src/assets/img/icons/filter.js +10 -0
  50. package/dist/src/assets/img/icons/filter.js.map +1 -0
  51. package/dist/src/assets/img/icons/login-required.d.ts +1 -0
  52. package/dist/src/assets/img/icons/login-required.js +18 -0
  53. package/dist/src/assets/img/icons/login-required.js.map +1 -0
  54. package/dist/src/assets/img/icons/mediatype/account.d.ts +1 -0
  55. package/dist/src/assets/img/icons/mediatype/account.js +14 -0
  56. package/dist/src/assets/img/icons/mediatype/account.js.map +1 -0
  57. package/dist/src/assets/img/icons/mediatype/audio.d.ts +1 -0
  58. package/dist/src/assets/img/icons/mediatype/audio.js +14 -0
  59. package/dist/src/assets/img/icons/mediatype/audio.js.map +1 -0
  60. package/dist/src/assets/img/icons/mediatype/collection.d.ts +1 -0
  61. package/dist/src/assets/img/icons/mediatype/collection.js +12 -0
  62. package/dist/src/assets/img/icons/mediatype/collection.js.map +1 -0
  63. package/dist/src/assets/img/icons/mediatype/data.d.ts +1 -0
  64. package/dist/src/assets/img/icons/mediatype/data.js +15 -0
  65. package/dist/src/assets/img/icons/mediatype/data.js.map +1 -0
  66. package/dist/src/assets/img/icons/mediatype/etree.d.ts +1 -0
  67. package/dist/src/assets/img/icons/mediatype/etree.js +14 -0
  68. package/dist/src/assets/img/icons/mediatype/etree.js.map +1 -0
  69. package/dist/src/assets/img/icons/mediatype/film.d.ts +1 -0
  70. package/dist/src/assets/img/icons/mediatype/film.js +14 -0
  71. package/dist/src/assets/img/icons/mediatype/film.js.map +1 -0
  72. package/dist/src/assets/img/icons/mediatype/images.d.ts +1 -0
  73. package/dist/src/assets/img/icons/mediatype/images.js +13 -0
  74. package/dist/src/assets/img/icons/mediatype/images.js.map +1 -0
  75. package/dist/src/assets/img/icons/mediatype/radio.d.ts +1 -0
  76. package/dist/src/assets/img/icons/mediatype/radio.js +15 -0
  77. package/dist/src/assets/img/icons/mediatype/radio.js.map +1 -0
  78. package/dist/src/assets/img/icons/mediatype/search.d.ts +1 -0
  79. package/dist/src/assets/img/icons/mediatype/search.js +14 -0
  80. package/dist/src/assets/img/icons/mediatype/search.js.map +1 -0
  81. package/dist/src/assets/img/icons/mediatype/software.d.ts +1 -0
  82. package/dist/src/assets/img/icons/mediatype/software.js +13 -0
  83. package/dist/src/assets/img/icons/mediatype/software.js.map +1 -0
  84. package/dist/src/assets/img/icons/mediatype/texts.d.ts +1 -0
  85. package/dist/src/assets/img/icons/mediatype/texts.js +13 -0
  86. package/dist/src/assets/img/icons/mediatype/texts.js.map +1 -0
  87. package/dist/src/assets/img/icons/mediatype/tv-commercial.d.ts +1 -0
  88. package/dist/src/assets/img/icons/mediatype/tv-commercial.js +12 -0
  89. package/dist/src/assets/img/icons/mediatype/tv-commercial.js.map +1 -0
  90. package/dist/src/assets/img/icons/mediatype/tv-fact-check.d.ts +1 -0
  91. package/dist/src/assets/img/icons/mediatype/tv-fact-check.js +12 -0
  92. package/dist/src/assets/img/icons/mediatype/tv-fact-check.js.map +1 -0
  93. package/dist/src/assets/img/icons/mediatype/tv-quote.d.ts +1 -0
  94. package/dist/src/assets/img/icons/mediatype/tv-quote.js +12 -0
  95. package/dist/src/assets/img/icons/mediatype/tv-quote.js.map +1 -0
  96. package/dist/src/assets/img/icons/mediatype/tv.d.ts +1 -0
  97. package/dist/src/assets/img/icons/mediatype/tv.js +14 -0
  98. package/dist/src/assets/img/icons/mediatype/tv.js.map +1 -0
  99. package/dist/src/assets/img/icons/mediatype/video.d.ts +1 -0
  100. package/dist/src/assets/img/icons/mediatype/video.js +14 -0
  101. package/dist/src/assets/img/icons/mediatype/video.js.map +1 -0
  102. package/dist/src/assets/img/icons/mediatype/web.d.ts +1 -0
  103. package/dist/src/assets/img/icons/mediatype/web.js +13 -0
  104. package/dist/src/assets/img/icons/mediatype/web.js.map +1 -0
  105. package/dist/src/assets/img/icons/null-result.d.ts +2 -0
  106. package/dist/src/assets/img/icons/null-result.js +5 -0
  107. package/dist/src/assets/img/icons/null-result.js.map +1 -0
  108. package/dist/src/assets/img/icons/quote.d.ts +1 -0
  109. package/dist/src/assets/img/icons/quote.js +7 -0
  110. package/dist/src/assets/img/icons/quote.js.map +1 -0
  111. package/dist/src/assets/img/icons/restricted.d.ts +1 -0
  112. package/dist/src/assets/img/icons/restricted.js +13 -0
  113. package/dist/src/assets/img/icons/restricted.js.map +1 -0
  114. package/dist/src/assets/img/icons/reviews.d.ts +1 -0
  115. package/dist/src/assets/img/icons/reviews.js +11 -0
  116. package/dist/src/assets/img/icons/reviews.js.map +1 -0
  117. package/dist/src/assets/img/icons/upload.d.ts +1 -0
  118. package/dist/src/assets/img/icons/upload.js +12 -0
  119. package/dist/src/assets/img/icons/upload.js.map +1 -0
  120. package/dist/src/assets/img/icons/views.d.ts +1 -0
  121. package/dist/src/assets/img/icons/views.js +11 -0
  122. package/dist/src/assets/img/icons/views.js.map +1 -0
  123. package/dist/src/circular-activity-indicator.d.ts +5 -0
  124. package/dist/src/circular-activity-indicator.js +66 -0
  125. package/dist/src/circular-activity-indicator.js.map +1 -0
  126. package/dist/src/collection-browser.d.ts +692 -0
  127. package/dist/src/collection-browser.js +2669 -0
  128. package/dist/src/collection-browser.js.map +1 -0
  129. package/dist/src/collection-facets/facet-row.d.ts +30 -0
  130. package/dist/src/collection-facets/facet-row.js +266 -0
  131. package/dist/src/collection-facets/facet-row.js.map +1 -0
  132. package/dist/src/collection-facets/facet-tombstone-row.d.ts +5 -0
  133. package/dist/src/collection-facets/facet-tombstone-row.js +43 -0
  134. package/dist/src/collection-facets/facet-tombstone-row.js.map +1 -0
  135. package/dist/src/collection-facets/facets-template.d.ts +13 -0
  136. package/dist/src/collection-facets/facets-template.js +68 -0
  137. package/dist/src/collection-facets/facets-template.js.map +1 -0
  138. package/dist/src/collection-facets/models.d.ts +9 -0
  139. package/dist/src/collection-facets/models.js +10 -0
  140. package/dist/src/collection-facets/models.js.map +1 -0
  141. package/dist/src/collection-facets/more-facets-content.d.ts +109 -0
  142. package/dist/src/collection-facets/more-facets-content.js +547 -0
  143. package/dist/src/collection-facets/more-facets-content.js.map +1 -0
  144. package/dist/src/collection-facets/more-facets-pagination.d.ts +36 -0
  145. package/dist/src/collection-facets/more-facets-pagination.js +264 -0
  146. package/dist/src/collection-facets/more-facets-pagination.js.map +1 -0
  147. package/dist/src/collection-facets/smart-facets/dedupe.d.ts +10 -0
  148. package/dist/src/collection-facets/smart-facets/dedupe.js +35 -0
  149. package/dist/src/collection-facets/smart-facets/dedupe.js.map +1 -0
  150. package/dist/src/collection-facets/smart-facets/heuristics/browser-language/browser-language-heuristic.d.ts +5 -0
  151. package/dist/src/collection-facets/smart-facets/heuristics/browser-language/browser-language-heuristic.js +24 -0
  152. package/dist/src/collection-facets/smart-facets/heuristics/browser-language/browser-language-heuristic.js.map +1 -0
  153. package/dist/src/collection-facets/smart-facets/heuristics/index.d.ts +3 -0
  154. package/dist/src/collection-facets/smart-facets/heuristics/index.js +4 -0
  155. package/dist/src/collection-facets/smart-facets/heuristics/index.js.map +1 -0
  156. package/dist/src/collection-facets/smart-facets/heuristics/query-keywords/query-keywords-heuristic.d.ts +4 -0
  157. package/dist/src/collection-facets/smart-facets/heuristics/query-keywords/query-keywords-heuristic.js +14 -0
  158. package/dist/src/collection-facets/smart-facets/heuristics/query-keywords/query-keywords-heuristic.js.map +1 -0
  159. package/dist/src/collection-facets/smart-facets/heuristics/query-keywords/query-keywords-map.d.ts +6 -0
  160. package/dist/src/collection-facets/smart-facets/heuristics/query-keywords/query-keywords-map.js +68 -0
  161. package/dist/src/collection-facets/smart-facets/heuristics/query-keywords/query-keywords-map.js.map +1 -0
  162. package/dist/src/collection-facets/smart-facets/heuristics/wikidata/wikidata-entity-map.d.ts +9 -0
  163. package/dist/src/collection-facets/smart-facets/heuristics/wikidata/wikidata-entity-map.js +69 -0
  164. package/dist/src/collection-facets/smart-facets/heuristics/wikidata/wikidata-entity-map.js.map +1 -0
  165. package/dist/src/collection-facets/smart-facets/heuristics/wikidata/wikidata-heuristic.d.ts +21 -0
  166. package/dist/src/collection-facets/smart-facets/heuristics/wikidata/wikidata-heuristic.js +76 -0
  167. package/dist/src/collection-facets/smart-facets/heuristics/wikidata/wikidata-heuristic.js.map +1 -0
  168. package/dist/src/collection-facets/smart-facets/models.d.ts +30 -0
  169. package/dist/src/collection-facets/smart-facets/models.js +2 -0
  170. package/dist/src/collection-facets/smart-facets/models.js.map +1 -0
  171. package/dist/src/collection-facets/smart-facets/smart-facet-bar.d.ts +54 -0
  172. package/dist/src/collection-facets/smart-facets/smart-facet-bar.js +383 -0
  173. package/dist/src/collection-facets/smart-facets/smart-facet-bar.js.map +1 -0
  174. package/dist/src/collection-facets/smart-facets/smart-facet-button.d.ts +11 -0
  175. package/dist/src/collection-facets/smart-facets/smart-facet-button.js +133 -0
  176. package/dist/src/collection-facets/smart-facets/smart-facet-button.js.map +1 -0
  177. package/dist/src/collection-facets/smart-facets/smart-facet-dropdown.d.ts +28 -0
  178. package/dist/src/collection-facets/smart-facets/smart-facet-dropdown.js +172 -0
  179. package/dist/src/collection-facets/smart-facets/smart-facet-dropdown.js.map +1 -0
  180. package/dist/src/collection-facets/smart-facets/smart-facet-equals.d.ts +2 -0
  181. package/dist/src/collection-facets/smart-facets/smart-facet-equals.js +13 -0
  182. package/dist/src/collection-facets/smart-facets/smart-facet-equals.js.map +1 -0
  183. package/dist/src/collection-facets/smart-facets/smart-facet-heuristics.d.ts +5 -0
  184. package/dist/src/collection-facets/smart-facets/smart-facet-heuristics.js +18 -0
  185. package/dist/src/collection-facets/smart-facets/smart-facet-heuristics.js.map +1 -0
  186. package/dist/src/collection-facets/toggle-switch.d.ts +41 -0
  187. package/dist/src/collection-facets/toggle-switch.js +174 -0
  188. package/dist/src/collection-facets/toggle-switch.js.map +1 -0
  189. package/dist/src/collection-facets.d.ts +113 -0
  190. package/dist/src/collection-facets.js +884 -0
  191. package/dist/src/collection-facets.js.map +1 -0
  192. package/dist/src/data-source/collection-browser-data-source-interface.d.ts +270 -0
  193. package/dist/src/data-source/collection-browser-data-source-interface.js +2 -0
  194. package/dist/src/data-source/collection-browser-data-source-interface.js.map +1 -0
  195. package/dist/src/data-source/collection-browser-data-source.d.ts +418 -0
  196. package/dist/src/data-source/collection-browser-data-source.js +1121 -0
  197. package/dist/src/data-source/collection-browser-data-source.js.map +1 -0
  198. package/dist/src/data-source/collection-browser-query-state.d.ts +48 -0
  199. package/dist/src/data-source/collection-browser-query-state.js +2 -0
  200. package/dist/src/data-source/collection-browser-query-state.js.map +1 -0
  201. package/dist/src/data-source/models.d.ts +43 -0
  202. package/dist/src/data-source/models.js +9 -0
  203. package/dist/src/data-source/models.js.map +1 -0
  204. package/dist/src/empty-placeholder.d.ts +23 -0
  205. package/dist/src/empty-placeholder.js +165 -0
  206. package/dist/src/empty-placeholder.js.map +1 -0
  207. package/dist/src/expanded-date-picker.d.ts +50 -0
  208. package/dist/src/expanded-date-picker.js +182 -0
  209. package/dist/src/expanded-date-picker.js.map +1 -0
  210. package/dist/src/language-code-handler/language-code-handler.d.ts +37 -0
  211. package/dist/src/language-code-handler/language-code-handler.js +27 -0
  212. package/dist/src/language-code-handler/language-code-handler.js.map +1 -0
  213. package/dist/src/language-code-handler/language-code-mapping.d.ts +1 -0
  214. package/dist/src/language-code-handler/language-code-mapping.js +563 -0
  215. package/dist/src/language-code-handler/language-code-mapping.js.map +1 -0
  216. package/dist/src/manage/manage-bar.d.ts +58 -0
  217. package/dist/src/manage/manage-bar.js +237 -0
  218. package/dist/src/manage/manage-bar.js.map +1 -0
  219. package/dist/src/manage/remove-items-modal-content.d.ts +9 -0
  220. package/dist/src/manage/remove-items-modal-content.js +104 -0
  221. package/dist/src/manage/remove-items-modal-content.js.map +1 -0
  222. package/dist/src/mediatype/mediatype-config.d.ts +11 -0
  223. package/dist/src/mediatype/mediatype-config.js +116 -0
  224. package/dist/src/mediatype/mediatype-config.js.map +1 -0
  225. package/dist/src/models.d.ts +298 -0
  226. package/dist/src/models.js +507 -0
  227. package/dist/src/models.js.map +1 -0
  228. package/dist/src/restoration-state-handler.d.ts +74 -0
  229. package/dist/src/restoration-state-handler.js +397 -0
  230. package/dist/src/restoration-state-handler.js.map +1 -0
  231. package/dist/src/sort-filter-bar/alpha-bar-tooltip.d.ts +6 -0
  232. package/dist/src/sort-filter-bar/alpha-bar-tooltip.js +60 -0
  233. package/dist/src/sort-filter-bar/alpha-bar-tooltip.js.map +1 -0
  234. package/dist/src/sort-filter-bar/alpha-bar.d.ts +21 -0
  235. package/dist/src/sort-filter-bar/alpha-bar.js +241 -0
  236. package/dist/src/sort-filter-bar/alpha-bar.js.map +1 -0
  237. package/dist/src/sort-filter-bar/img/compact.d.ts +1 -0
  238. package/dist/src/sort-filter-bar/img/compact.js +5 -0
  239. package/dist/src/sort-filter-bar/img/compact.js.map +1 -0
  240. package/dist/src/sort-filter-bar/img/list.d.ts +1 -0
  241. package/dist/src/sort-filter-bar/img/list.js +5 -0
  242. package/dist/src/sort-filter-bar/img/list.js.map +1 -0
  243. package/dist/src/sort-filter-bar/img/sort-toggle-disabled.d.ts +1 -0
  244. package/dist/src/sort-filter-bar/img/sort-toggle-disabled.js +15 -0
  245. package/dist/src/sort-filter-bar/img/sort-toggle-disabled.js.map +1 -0
  246. package/dist/src/sort-filter-bar/img/sort-toggle-down.d.ts +1 -0
  247. package/dist/src/sort-filter-bar/img/sort-toggle-down.js +17 -0
  248. package/dist/src/sort-filter-bar/img/sort-toggle-down.js.map +1 -0
  249. package/dist/src/sort-filter-bar/img/sort-toggle-up.d.ts +1 -0
  250. package/dist/src/sort-filter-bar/img/sort-toggle-up.js +17 -0
  251. package/dist/src/sort-filter-bar/img/sort-toggle-up.js.map +1 -0
  252. package/dist/src/sort-filter-bar/img/sort-triangle.d.ts +1 -0
  253. package/dist/src/sort-filter-bar/img/sort-triangle.js +5 -0
  254. package/dist/src/sort-filter-bar/img/sort-triangle.js.map +1 -0
  255. package/dist/src/sort-filter-bar/img/tile.d.ts +1 -0
  256. package/dist/src/sort-filter-bar/img/tile.js +5 -0
  257. package/dist/src/sort-filter-bar/img/tile.js.map +1 -0
  258. package/dist/src/sort-filter-bar/sort-filter-bar.d.ts +278 -0
  259. package/dist/src/sort-filter-bar/sort-filter-bar.js +1161 -0
  260. package/dist/src/sort-filter-bar/sort-filter-bar.js.map +1 -0
  261. package/dist/src/styles/ia-button.d.ts +2 -0
  262. package/dist/src/styles/ia-button.js +134 -0
  263. package/dist/src/styles/ia-button.js.map +1 -0
  264. package/dist/src/styles/item-image-styles.d.ts +8 -0
  265. package/dist/src/styles/item-image-styles.js +123 -0
  266. package/dist/src/styles/item-image-styles.js.map +1 -0
  267. package/dist/src/styles/sr-only.d.ts +1 -0
  268. package/dist/src/styles/sr-only.js +18 -0
  269. package/dist/src/styles/sr-only.js.map +1 -0
  270. package/dist/src/tiles/base-tile-component.d.ts +27 -0
  271. package/dist/src/tiles/base-tile-component.js +82 -0
  272. package/dist/src/tiles/base-tile-component.js.map +1 -0
  273. package/dist/src/tiles/collection-browser-loading-tile.d.ts +5 -0
  274. package/dist/src/tiles/collection-browser-loading-tile.js +29 -0
  275. package/dist/src/tiles/collection-browser-loading-tile.js.map +1 -0
  276. package/dist/src/tiles/grid/account-tile.d.ts +18 -0
  277. package/dist/src/tiles/grid/account-tile.js +111 -0
  278. package/dist/src/tiles/grid/account-tile.js.map +1 -0
  279. package/dist/src/tiles/grid/collection-tile.d.ts +15 -0
  280. package/dist/src/tiles/grid/collection-tile.js +160 -0
  281. package/dist/src/tiles/grid/collection-tile.js.map +1 -0
  282. package/dist/src/tiles/grid/item-tile.d.ts +41 -0
  283. package/dist/src/tiles/grid/item-tile.js +321 -0
  284. package/dist/src/tiles/grid/item-tile.js.map +1 -0
  285. package/dist/src/tiles/grid/search-tile.d.ts +10 -0
  286. package/dist/src/tiles/grid/search-tile.js +95 -0
  287. package/dist/src/tiles/grid/search-tile.js.map +1 -0
  288. package/dist/src/tiles/grid/styles/tile-grid-shared-styles.d.ts +1 -0
  289. package/dist/src/tiles/grid/styles/tile-grid-shared-styles.js +128 -0
  290. package/dist/src/tiles/grid/styles/tile-grid-shared-styles.js.map +1 -0
  291. package/dist/src/tiles/grid/tile-stats.d.ts +58 -0
  292. package/dist/src/tiles/grid/tile-stats.js +204 -0
  293. package/dist/src/tiles/grid/tile-stats.js.map +1 -0
  294. package/dist/src/tiles/hover/hover-pane-controller.d.ts +228 -0
  295. package/dist/src/tiles/hover/hover-pane-controller.js +446 -0
  296. package/dist/src/tiles/hover/hover-pane-controller.js.map +1 -0
  297. package/dist/src/tiles/hover/tile-hover-pane.d.ts +20 -0
  298. package/dist/src/tiles/hover/tile-hover-pane.js +185 -0
  299. package/dist/src/tiles/hover/tile-hover-pane.js.map +1 -0
  300. package/dist/src/tiles/image-block.d.ts +19 -0
  301. package/dist/src/tiles/image-block.js +175 -0
  302. package/dist/src/tiles/image-block.js.map +1 -0
  303. package/dist/src/tiles/item-image.d.ts +40 -0
  304. package/dist/src/tiles/item-image.js +191 -0
  305. package/dist/src/tiles/item-image.js.map +1 -0
  306. package/dist/src/tiles/list/tile-list-compact-header.d.ts +6 -0
  307. package/dist/src/tiles/list/tile-list-compact-header.js +85 -0
  308. package/dist/src/tiles/list/tile-list-compact-header.js.map +1 -0
  309. package/dist/src/tiles/list/tile-list-compact.d.ts +19 -0
  310. package/dist/src/tiles/list/tile-list-compact.js +223 -0
  311. package/dist/src/tiles/list/tile-list-compact.js.map +1 -0
  312. package/dist/src/tiles/list/tile-list.d.ts +54 -0
  313. package/dist/src/tiles/list/tile-list.js +621 -0
  314. package/dist/src/tiles/list/tile-list.js.map +1 -0
  315. package/dist/src/tiles/models.d.ts +1 -0
  316. package/dist/src/tiles/models.js +2 -0
  317. package/dist/src/tiles/models.js.map +1 -0
  318. package/dist/src/tiles/overlay/icon-overlay.d.ts +8 -0
  319. package/dist/src/tiles/overlay/icon-overlay.js +55 -0
  320. package/dist/src/tiles/overlay/icon-overlay.js.map +1 -0
  321. package/dist/src/tiles/overlay/text-overlay.d.ts +9 -0
  322. package/dist/src/tiles/overlay/text-overlay.js +74 -0
  323. package/dist/src/tiles/overlay/text-overlay.js.map +1 -0
  324. package/dist/src/tiles/review-block.d.ts +12 -0
  325. package/dist/src/tiles/review-block.js +134 -0
  326. package/dist/src/tiles/review-block.js.map +1 -0
  327. package/dist/src/tiles/text-snippet-block.d.ts +27 -0
  328. package/dist/src/tiles/text-snippet-block.js +132 -0
  329. package/dist/src/tiles/text-snippet-block.js.map +1 -0
  330. package/dist/src/tiles/tile-dispatcher.d.ts +71 -0
  331. package/dist/src/tiles/tile-dispatcher.js +472 -0
  332. package/dist/src/tiles/tile-dispatcher.js.map +1 -0
  333. package/dist/src/tiles/tile-display-value-provider.d.ts +47 -0
  334. package/dist/src/tiles/tile-display-value-provider.js +95 -0
  335. package/dist/src/tiles/tile-display-value-provider.js.map +1 -0
  336. package/dist/src/tiles/tile-mediatype-icon.d.ts +27 -0
  337. package/dist/src/tiles/tile-mediatype-icon.js +130 -0
  338. package/dist/src/tiles/tile-mediatype-icon.js.map +1 -0
  339. package/dist/src/utils/analytics-events.d.ts +28 -0
  340. package/dist/src/utils/analytics-events.js +31 -0
  341. package/dist/src/utils/analytics-events.js.map +1 -0
  342. package/dist/src/utils/array-equals.d.ts +4 -0
  343. package/dist/src/utils/array-equals.js +11 -0
  344. package/dist/src/utils/array-equals.js.map +1 -0
  345. package/dist/src/utils/collapse-repeated-quotes.d.ts +11 -0
  346. package/dist/src/utils/collapse-repeated-quotes.js +14 -0
  347. package/dist/src/utils/collapse-repeated-quotes.js.map +1 -0
  348. package/dist/src/utils/facet-utils.d.ts +83 -0
  349. package/dist/src/utils/facet-utils.js +152 -0
  350. package/dist/src/utils/facet-utils.js.map +1 -0
  351. package/dist/src/utils/format-count.d.ts +7 -0
  352. package/dist/src/utils/format-count.js +76 -0
  353. package/dist/src/utils/format-count.js.map +1 -0
  354. package/dist/src/utils/format-date.d.ts +16 -0
  355. package/dist/src/utils/format-date.js +33 -0
  356. package/dist/src/utils/format-date.js.map +1 -0
  357. package/dist/src/utils/format-unit-size.d.ts +2 -0
  358. package/dist/src/utils/format-unit-size.js +34 -0
  359. package/dist/src/utils/format-unit-size.js.map +1 -0
  360. package/dist/src/utils/local-date-from-utc.d.ts +9 -0
  361. package/dist/src/utils/local-date-from-utc.js +16 -0
  362. package/dist/src/utils/local-date-from-utc.js.map +1 -0
  363. package/dist/src/utils/log.d.ts +7 -0
  364. package/dist/src/utils/log.js +14 -0
  365. package/dist/src/utils/log.js.map +1 -0
  366. package/dist/src/utils/resolve-mediatype.d.ts +8 -0
  367. package/dist/src/utils/resolve-mediatype.js +24 -0
  368. package/dist/src/utils/resolve-mediatype.js.map +1 -0
  369. package/dist/src/utils/sha1.d.ts +2 -0
  370. package/dist/src/utils/sha1.js +9 -0
  371. package/dist/src/utils/sha1.js.map +1 -0
  372. package/dist/test/collection-browser.test.d.ts +1 -0
  373. package/dist/test/collection-browser.test.js +1721 -0
  374. package/dist/test/collection-browser.test.js.map +1 -0
  375. package/dist/test/collection-facets/facet-row.test.d.ts +1 -0
  376. package/dist/test/collection-facets/facet-row.test.js +274 -0
  377. package/dist/test/collection-facets/facet-row.test.js.map +1 -0
  378. package/dist/test/collection-facets/facets-template.test.d.ts +1 -0
  379. package/dist/test/collection-facets/facets-template.test.js +101 -0
  380. package/dist/test/collection-facets/facets-template.test.js.map +1 -0
  381. package/dist/test/collection-facets/more-facets-content.test.d.ts +1 -0
  382. package/dist/test/collection-facets/more-facets-content.test.js +169 -0
  383. package/dist/test/collection-facets/more-facets-content.test.js.map +1 -0
  384. package/dist/test/collection-facets/more-facets-pagination.test.d.ts +1 -0
  385. package/dist/test/collection-facets/more-facets-pagination.test.js +132 -0
  386. package/dist/test/collection-facets/more-facets-pagination.test.js.map +1 -0
  387. package/dist/test/collection-facets/toggle-switch.test.d.ts +1 -0
  388. package/dist/test/collection-facets/toggle-switch.test.js +96 -0
  389. package/dist/test/collection-facets/toggle-switch.test.js.map +1 -0
  390. package/dist/test/collection-facets.test.d.ts +2 -0
  391. package/dist/test/collection-facets.test.js +745 -0
  392. package/dist/test/collection-facets.test.js.map +1 -0
  393. package/dist/test/data-source/collection-browser-data-source.test.d.ts +1 -0
  394. package/dist/test/data-source/collection-browser-data-source.test.js +102 -0
  395. package/dist/test/data-source/collection-browser-data-source.test.js.map +1 -0
  396. package/dist/test/empty-placeholder.test.d.ts +1 -0
  397. package/dist/test/empty-placeholder.test.js +63 -0
  398. package/dist/test/empty-placeholder.test.js.map +1 -0
  399. package/dist/test/expanded-date-picker.test.d.ts +1 -0
  400. package/dist/test/expanded-date-picker.test.js +130 -0
  401. package/dist/test/expanded-date-picker.test.js.map +1 -0
  402. package/dist/test/icon-overlay.test.d.ts +1 -0
  403. package/dist/test/icon-overlay.test.js +30 -0
  404. package/dist/test/icon-overlay.test.js.map +1 -0
  405. package/dist/test/image-block.test.d.ts +1 -0
  406. package/dist/test/image-block.test.js +218 -0
  407. package/dist/test/image-block.test.js.map +1 -0
  408. package/dist/test/item-image.test.d.ts +1 -0
  409. package/dist/test/item-image.test.js +196 -0
  410. package/dist/test/item-image.test.js.map +1 -0
  411. package/dist/test/manage/manage-bar.test.d.ts +2 -0
  412. package/dist/test/manage/manage-bar.test.js +106 -0
  413. package/dist/test/manage/manage-bar.test.js.map +1 -0
  414. package/dist/test/manage/remove-items-modal-content.test.d.ts +1 -0
  415. package/dist/test/manage/remove-items-modal-content.test.js +65 -0
  416. package/dist/test/manage/remove-items-modal-content.test.js.map +1 -0
  417. package/dist/test/mediatype-config.test.d.ts +1 -0
  418. package/dist/test/mediatype-config.test.js +11 -0
  419. package/dist/test/mediatype-config.test.js.map +1 -0
  420. package/dist/test/mocks/mock-analytics-handler.d.ts +10 -0
  421. package/dist/test/mocks/mock-analytics-handler.js +16 -0
  422. package/dist/test/mocks/mock-analytics-handler.js.map +1 -0
  423. package/dist/test/mocks/mock-search-responses.d.ts +31 -0
  424. package/dist/test/mocks/mock-search-responses.js +1241 -0
  425. package/dist/test/mocks/mock-search-responses.js.map +1 -0
  426. package/dist/test/mocks/mock-search-service.d.ts +16 -0
  427. package/dist/test/mocks/mock-search-service.js +65 -0
  428. package/dist/test/mocks/mock-search-service.js.map +1 -0
  429. package/dist/test/restoration-state-handler.test.d.ts +1 -0
  430. package/dist/test/restoration-state-handler.test.js +343 -0
  431. package/dist/test/restoration-state-handler.test.js.map +1 -0
  432. package/dist/test/review-block.test.d.ts +1 -0
  433. package/dist/test/review-block.test.js +48 -0
  434. package/dist/test/review-block.test.js.map +1 -0
  435. package/dist/test/sort-filter-bar/alpha-bar-tooltip.test.d.ts +1 -0
  436. package/dist/test/sort-filter-bar/alpha-bar-tooltip.test.js +13 -0
  437. package/dist/test/sort-filter-bar/alpha-bar-tooltip.test.js.map +1 -0
  438. package/dist/test/sort-filter-bar/alpha-bar.test.d.ts +1 -0
  439. package/dist/test/sort-filter-bar/alpha-bar.test.js +74 -0
  440. package/dist/test/sort-filter-bar/alpha-bar.test.js.map +1 -0
  441. package/dist/test/sort-filter-bar/sort-filter-bar.test.d.ts +1 -0
  442. package/dist/test/sort-filter-bar/sort-filter-bar.test.js +609 -0
  443. package/dist/test/sort-filter-bar/sort-filter-bar.test.js.map +1 -0
  444. package/dist/test/text-overlay.test.d.ts +1 -0
  445. package/dist/test/text-overlay.test.js +42 -0
  446. package/dist/test/text-overlay.test.js.map +1 -0
  447. package/dist/test/text-snippet-block.test.d.ts +1 -0
  448. package/dist/test/text-snippet-block.test.js +63 -0
  449. package/dist/test/text-snippet-block.test.js.map +1 -0
  450. package/dist/test/tile-stats.test.d.ts +1 -0
  451. package/dist/test/tile-stats.test.js +128 -0
  452. package/dist/test/tile-stats.test.js.map +1 -0
  453. package/dist/test/tiles/grid/account-tile.test.d.ts +1 -0
  454. package/dist/test/tiles/grid/account-tile.test.js +96 -0
  455. package/dist/test/tiles/grid/account-tile.test.js.map +1 -0
  456. package/dist/test/tiles/grid/collection-tile.test.d.ts +1 -0
  457. package/dist/test/tiles/grid/collection-tile.test.js +96 -0
  458. package/dist/test/tiles/grid/collection-tile.test.js.map +1 -0
  459. package/dist/test/tiles/grid/item-tile.test.d.ts +1 -0
  460. package/dist/test/tiles/grid/item-tile.test.js +427 -0
  461. package/dist/test/tiles/grid/item-tile.test.js.map +1 -0
  462. package/dist/test/tiles/grid/search-tile.test.d.ts +1 -0
  463. package/dist/test/tiles/grid/search-tile.test.js +66 -0
  464. package/dist/test/tiles/grid/search-tile.test.js.map +1 -0
  465. package/dist/test/tiles/hover/hover-pane-controller.test.d.ts +1 -0
  466. package/dist/test/tiles/hover/hover-pane-controller.test.js +329 -0
  467. package/dist/test/tiles/hover/hover-pane-controller.test.js.map +1 -0
  468. package/dist/test/tiles/hover/tile-hover-pane.test.d.ts +1 -0
  469. package/dist/test/tiles/hover/tile-hover-pane.test.js +57 -0
  470. package/dist/test/tiles/hover/tile-hover-pane.test.js.map +1 -0
  471. package/dist/test/tiles/list/tile-list-compact.test.d.ts +1 -0
  472. package/dist/test/tiles/list/tile-list-compact.test.js +251 -0
  473. package/dist/test/tiles/list/tile-list-compact.test.js.map +1 -0
  474. package/dist/test/tiles/list/tile-list.test.d.ts +1 -0
  475. package/dist/test/tiles/list/tile-list.test.js +469 -0
  476. package/dist/test/tiles/list/tile-list.test.js.map +1 -0
  477. package/dist/test/tiles/tile-dispatcher.test.d.ts +1 -0
  478. package/dist/test/tiles/tile-dispatcher.test.js +231 -0
  479. package/dist/test/tiles/tile-dispatcher.test.js.map +1 -0
  480. package/dist/test/tiles/tile-display-value-provider.test.d.ts +1 -0
  481. package/dist/test/tiles/tile-display-value-provider.test.js +142 -0
  482. package/dist/test/tiles/tile-display-value-provider.test.js.map +1 -0
  483. package/dist/test/tiles/tile-mediatype-icon.test.d.ts +1 -0
  484. package/dist/test/tiles/tile-mediatype-icon.test.js +145 -0
  485. package/dist/test/tiles/tile-mediatype-icon.test.js.map +1 -0
  486. package/dist/test/utils/array-equals.test.d.ts +1 -0
  487. package/dist/test/utils/array-equals.test.js +27 -0
  488. package/dist/test/utils/array-equals.test.js.map +1 -0
  489. package/dist/test/utils/format-count.test.d.ts +1 -0
  490. package/dist/test/utils/format-count.test.js +24 -0
  491. package/dist/test/utils/format-count.test.js.map +1 -0
  492. package/dist/test/utils/format-date.test.d.ts +1 -0
  493. package/dist/test/utils/format-date.test.js +61 -0
  494. package/dist/test/utils/format-date.test.js.map +1 -0
  495. package/dist/test/utils/format-unit-size.test.d.ts +1 -0
  496. package/dist/test/utils/format-unit-size.test.js +18 -0
  497. package/dist/test/utils/format-unit-size.test.js.map +1 -0
  498. package/dist/test/utils/local-date-from-utc.test.d.ts +1 -0
  499. package/dist/test/utils/local-date-from-utc.test.js +27 -0
  500. package/dist/test/utils/local-date-from-utc.test.js.map +1 -0
  501. package/eslint.config.mjs +53 -53
  502. package/index.html +24 -24
  503. package/local.archive.org.cert +86 -86
  504. package/local.archive.org.key +27 -27
  505. package/package.json +118 -117
  506. package/renovate.json +6 -6
  507. package/src/collection-browser.ts +2954 -2829
  508. package/src/collection-facets/facet-row.ts +299 -296
  509. package/src/collection-facets/models.ts +10 -10
  510. package/src/collection-facets/more-facets-content.ts +639 -639
  511. package/src/collection-facets.ts +1005 -995
  512. package/src/data-source/collection-browser-data-source-interface.ts +345 -333
  513. package/src/data-source/collection-browser-data-source.ts +1441 -1401
  514. package/src/data-source/collection-browser-query-state.ts +59 -65
  515. package/src/data-source/models.ts +56 -43
  516. package/src/manage/manage-bar.ts +247 -247
  517. package/src/models.ts +866 -870
  518. package/src/restoration-state-handler.ts +542 -544
  519. package/src/tiles/base-tile-component.ts +65 -65
  520. package/src/tiles/grid/account-tile.ts +113 -113
  521. package/src/tiles/grid/collection-tile.ts +163 -163
  522. package/src/tiles/grid/item-tile.ts +340 -340
  523. package/src/tiles/grid/search-tile.ts +90 -90
  524. package/src/tiles/grid/styles/tile-grid-shared-styles.ts +130 -130
  525. package/src/tiles/hover/hover-pane-controller.ts +613 -517
  526. package/src/tiles/hover/tile-hover-pane.ts +184 -180
  527. package/src/tiles/list/tile-list-compact.ts +239 -239
  528. package/src/tiles/list/tile-list.ts +700 -700
  529. package/src/tiles/tile-dispatcher.ts +517 -490
  530. package/src/utils/format-date.ts +62 -62
  531. package/test/collection-browser.test.ts +2413 -2403
  532. package/test/collection-facets/facet-row.test.ts +375 -375
  533. package/test/collection-facets.test.ts +928 -928
  534. package/test/restoration-state-handler.test.ts +480 -510
  535. package/test/tiles/grid/item-tile.test.ts +520 -520
  536. package/test/tiles/hover/hover-pane-controller.test.ts +418 -353
  537. package/test/tiles/list/tile-list-compact.test.ts +282 -282
  538. package/test/tiles/list/tile-list.test.ts +552 -552
  539. package/test/tiles/tile-dispatcher.test.ts +283 -187
  540. package/test/utils/format-date.test.ts +89 -89
  541. package/tsconfig.json +20 -20
  542. package/web-dev-server.config.mjs +30 -30
  543. package/web-test-runner.config.mjs +41 -41
@@ -1,1401 +1,1441 @@
1
- import type { ReactiveControllerHost } from 'lit';
2
- import {
3
- AccountExtraInfo,
4
- Aggregation,
5
- Bucket,
6
- CollectionExtraInfo,
7
- FilterConstraint,
8
- FilterMap,
9
- FilterMapBuilder,
10
- PageElementMap,
11
- SearchParams,
12
- SearchResponseSessionContext,
13
- SearchResult,
14
- SearchType,
15
- } from '@internetarchive/search-service';
16
- import {
17
- prefixFilterAggregationKeys,
18
- type FacetBucket,
19
- type PrefixFilterType,
20
- TileModel,
21
- PrefixFilterCounts,
22
- RequestKind,
23
- SortField,
24
- SORT_OPTIONS,
25
- HitRequestSource,
26
- } from '../models';
27
- import { FACETLESS_PAGE_ELEMENTS, type PageSpecifierParams } from './models';
28
- import type { CollectionBrowserDataSourceInterface } from './collection-browser-data-source-interface';
29
- import type { CollectionBrowserSearchInterface } from './collection-browser-query-state';
30
- import { sha1 } from '../utils/sha1';
31
- import { log } from '../utils/log';
32
- import { mergeSelectedFacets } from '../utils/facet-utils';
33
-
34
- export class CollectionBrowserDataSource
35
- implements CollectionBrowserDataSourceInterface
36
- {
37
- /**
38
- * All pages of tile models that have been fetched so far, indexed by their page
39
- * number (with the first being page 1).
40
- */
41
- private pages: Record<string, TileModel[]> = {};
42
-
43
- /**
44
- * Tile offset to apply when looking up tiles in the pages, in order to maintain
45
- * page alignment after tiles are removed.
46
- */
47
- private offset = 0;
48
-
49
- /**
50
- * Total number of tile models stored in this data source's pages
51
- */
52
- private numTileModels = 0;
53
-
54
- /**
55
- * How many consecutive pages should be batched together on the initial page fetch.
56
- * Defaults to 2 pages.
57
- */
58
- private numInitialPages = 2;
59
-
60
- /**
61
- * A set of fetch IDs that are valid for the current query state
62
- */
63
- private fetchesInProgress = new Set<string>();
64
-
65
- /**
66
- * A record of the query key used for the last search.
67
- * If this changes, we need to load new results.
68
- */
69
- private previousQueryKey: string = '';
70
-
71
- /**
72
- * Whether the initial page of search results for the current query state
73
- * is presently being fetched.
74
- */
75
- private searchResultsLoading = false;
76
-
77
- /**
78
- * Whether the facets (aggregations) for the current query state are
79
- * presently being fetched.
80
- */
81
- private facetsLoading = false;
82
-
83
- /**
84
- * Whether the facets are actually visible -- if not, then we can delay any facet
85
- * fetches until they become visible.
86
- */
87
- private facetsReadyToLoad = false;
88
-
89
- /**
90
- * Whether further query changes should be ignored and not trigger fetches
91
- */
92
- private suppressFetches = false;
93
-
94
- /**
95
- * @inheritdoc
96
- */
97
- totalResults = 0;
98
-
99
- /**
100
- * @inheritdoc
101
- */
102
- endOfDataReached = false;
103
-
104
- /**
105
- * @inheritdoc
106
- */
107
- queryInitialized = false;
108
-
109
- /**
110
- * @inheritdoc
111
- */
112
- aggregations?: Record<string, Aggregation>;
113
-
114
- /**
115
- * @inheritdoc
116
- */
117
- histogramAggregation?: Aggregation;
118
-
119
- /**
120
- * @inheritdoc
121
- */
122
- collectionTitles = new Map<string, string>();
123
-
124
- /**
125
- * @inheritdoc
126
- */
127
- tvChannelAliases = new Map<string, string>();
128
-
129
- /**
130
- * @inheritdoc
131
- */
132
- collectionExtraInfo?: CollectionExtraInfo;
133
-
134
- /**
135
- * @inheritdoc
136
- */
137
- accountExtraInfo?: AccountExtraInfo;
138
-
139
- /**
140
- * @inheritdoc
141
- */
142
- sessionContext?: SearchResponseSessionContext;
143
-
144
- /**
145
- * @inheritdoc
146
- */
147
- pageElements?: PageElementMap;
148
-
149
- /**
150
- * @inheritdoc
151
- */
152
- parentCollections?: string[] = [];
153
-
154
- /**
155
- * @inheritdoc
156
- */
157
- prefixFilterCountMap: Partial<Record<PrefixFilterType, PrefixFilterCounts>> =
158
- {};
159
-
160
- /**
161
- * @inheritdoc
162
- */
163
- queryErrorMessage?: string;
164
-
165
- /**
166
- * Internal property to store the private value backing the `initialSearchComplete` getter.
167
- */
168
- private _initialSearchCompletePromise: Promise<boolean> =
169
- Promise.resolve(true);
170
-
171
- /**
172
- * @inheritdoc
173
- */
174
- get initialSearchComplete(): Promise<boolean> {
175
- return this._initialSearchCompletePromise;
176
- }
177
-
178
- constructor(
179
- /** The host element to which this controller should attach listeners */
180
- private readonly host: ReactiveControllerHost &
181
- CollectionBrowserSearchInterface,
182
- /** Default size of result pages */
183
- private pageSize: number = 50,
184
- ) {
185
- // Just setting some property values
186
- }
187
-
188
- hostConnected(): void {
189
- this.setSearchResultsLoading(this.searchResultsLoading);
190
- this.setFacetsLoading(this.facetsLoading);
191
- }
192
-
193
- hostUpdate(): void {
194
- // This reactive controller hook is run whenever the host component (collection-browser) performs an update.
195
- // We check whether the host's state has changed in a way which should trigger a reset & new results fetch.
196
-
197
- // Only the currently-installed data source should react to the update
198
- if (!this.activeOnHost) return;
199
-
200
- // Copy loading states onto the host
201
- this.setSearchResultsLoading(this.searchResultsLoading);
202
- this.setFacetsLoading(this.facetsLoading);
203
-
204
- // Can't perform searches without a search service
205
- if (!this.host.searchService) return;
206
-
207
- // We should only reset if part of the full query state has changed
208
- const queryKeyChanged = this.pageFetchQueryKey !== this.previousQueryKey;
209
- if (!queryKeyChanged) return;
210
-
211
- // We should only reset if either:
212
- // (a) our state permits a valid search, or
213
- // (b) we have a blank query that we're showing a placeholder/message for
214
- const queryIsEmpty = !this.host.baseQuery;
215
- if (!(this.canPerformSearch || queryIsEmpty)) return;
216
-
217
- if (this.activeOnHost) this.host.emitQueryStateChanged();
218
- this.handleQueryChange();
219
- }
220
-
221
- /**
222
- * Returns whether this data source is the one currently installed on the host component.
223
- */
224
- private get activeOnHost(): boolean {
225
- return this.host.dataSource === this;
226
- }
227
-
228
- /**
229
- * @inheritdoc
230
- */
231
- get size(): number {
232
- return this.numTileModels;
233
- }
234
-
235
- /**
236
- * @inheritdoc
237
- */
238
- reset(): void {
239
- log('Resetting CB data source');
240
- this.pages = {};
241
- this.aggregations = {};
242
- this.histogramAggregation = undefined;
243
- this.pageElements = undefined;
244
- this.parentCollections = [];
245
- this.previousQueryKey = '';
246
- this.queryErrorMessage = undefined;
247
-
248
- this.offset = 0;
249
- this.numTileModels = 0;
250
- this.endOfDataReached = false;
251
- this.queryInitialized = false;
252
- this.facetsLoading = false;
253
-
254
- // Invalidate any fetches in progress
255
- this.fetchesInProgress.clear();
256
-
257
- this.setTotalResultCount(0);
258
- this.requestHostUpdate();
259
- }
260
-
261
- /**
262
- * @inheritdoc
263
- */
264
- resetPages(): void {
265
- if (Object.keys(this.pages).length < this.host.maxPagesToManage) {
266
- this.pages = {};
267
-
268
- // Invalidate any page fetches in progress (keep facet fetches)
269
- this.fetchesInProgress.forEach(key => {
270
- if (!key.startsWith('facets-')) this.fetchesInProgress.delete(key);
271
- });
272
- this.requestHostUpdate();
273
- }
274
- }
275
-
276
- /**
277
- * @inheritdoc
278
- */
279
- addPage(pageNum: number, pageTiles: TileModel[]): void {
280
- this.pages[pageNum] = pageTiles;
281
- this.numTileModels += pageTiles.length;
282
- this.requestHostUpdate();
283
- }
284
-
285
- /**
286
- * @inheritdoc
287
- */
288
- addMultiplePages(firstPageNum: number, tiles: TileModel[]): void {
289
- const numPages = Math.ceil(tiles.length / this.pageSize);
290
- for (let i = 0; i < numPages; i += 1) {
291
- const pageStartIndex = this.pageSize * i;
292
- this.addPage(
293
- firstPageNum + i,
294
- tiles.slice(pageStartIndex, pageStartIndex + this.pageSize),
295
- );
296
- }
297
-
298
- const visiblePages = this.host.currentVisiblePageNumbers;
299
- const needsReload = visiblePages.some(
300
- page => page >= firstPageNum && page <= firstPageNum + numPages,
301
- );
302
- if (needsReload) {
303
- this.refreshVisibleResults();
304
- }
305
- }
306
-
307
- /**
308
- * @inheritdoc
309
- */
310
- getPage(pageNum: number): TileModel[] {
311
- return this.pages[pageNum];
312
- }
313
-
314
- /**
315
- * @inheritdoc
316
- */
317
- getAllPages(): Record<string, TileModel[]> {
318
- return this.pages;
319
- }
320
-
321
- /**
322
- * @inheritdoc
323
- */
324
- hasPage(pageNum: number): boolean {
325
- return !!this.pages[pageNum];
326
- }
327
-
328
- /**
329
- * @inheritdoc
330
- */
331
- getTileModelAt(index: number): TileModel | undefined {
332
- const offsetIndex = index + this.offset;
333
- const expectedPageNum = Math.floor(offsetIndex / this.pageSize) + 1;
334
- const expectedIndexOnPage = offsetIndex % this.pageSize;
335
-
336
- let page = 1;
337
- let tilesSeen = 0;
338
- while (tilesSeen <= offsetIndex) {
339
- if (!this.pages[page]) {
340
- // If we encounter a missing page, either we're past all the results or the page data is sparse.
341
- // So just return the tile at the expected position if it exists.
342
- return this.pages[expectedPageNum]?.[expectedIndexOnPage];
343
- }
344
-
345
- if (tilesSeen + this.pages[page].length > offsetIndex) {
346
- return this.pages[page][offsetIndex - tilesSeen];
347
- }
348
-
349
- tilesSeen += this.pages[page].length;
350
- page += 1;
351
- }
352
-
353
- return this.pages[expectedPageNum]?.[expectedIndexOnPage];
354
- }
355
-
356
- /**
357
- * @inheritdoc
358
- */
359
- indexOf(tile: TileModel): number {
360
- return Object.values(this.pages).flat().indexOf(tile) - this.offset;
361
- }
362
-
363
- /**
364
- * @inheritdoc
365
- */
366
- getPageSize(): number {
367
- return this.pageSize;
368
- }
369
-
370
- /**
371
- * @inheritdoc
372
- */
373
- setPageSize(pageSize: number): void {
374
- this.reset();
375
- this.pageSize = pageSize;
376
- }
377
-
378
- /**
379
- * @inheritdoc
380
- */
381
- setNumInitialPages(numPages: number): void {
382
- this.numInitialPages = numPages;
383
- }
384
-
385
- /**
386
- * @inheritdoc
387
- */
388
- setTotalResultCount(count: number): void {
389
- this.totalResults = count;
390
- if (this.activeOnHost) {
391
- this.host.setTotalResultCount(count);
392
- }
393
- }
394
-
395
- /**
396
- * @inheritdoc
397
- */
398
- setFetchesSuppressed(suppressed: boolean): void {
399
- this.suppressFetches = suppressed;
400
- }
401
-
402
- /**
403
- * @inheritdoc
404
- */
405
- setEndOfDataReached(reached: boolean): void {
406
- this.endOfDataReached = reached;
407
- }
408
-
409
- /**
410
- * @inheritdoc
411
- */
412
- async handleQueryChange(): Promise<void> {
413
- // Don't react to the change if fetches are suppressed for this data source
414
- if (this.suppressFetches) return;
415
-
416
- this.reset();
417
-
418
- // Reset the `initialSearchComplete` promise with a new value for the imminent search
419
- let initialSearchCompleteResolver: (value: boolean) => void;
420
- this._initialSearchCompletePromise = new Promise(res => {
421
- initialSearchCompleteResolver = res;
422
- });
423
-
424
- // Fire the initial page & facet requests
425
- this.queryInitialized = true;
426
- await Promise.all([
427
- this.doInitialPageFetch(),
428
- this.canFetchFacets ? this.fetchFacets() : null,
429
- ]);
430
-
431
- // Resolve the `initialSearchComplete` promise for this search
432
- initialSearchCompleteResolver!(true);
433
- }
434
-
435
- /**
436
- * @inheritdoc
437
- */
438
- async handleFacetReadinessChange(ready: boolean): Promise<void> {
439
- const facetsBecameReady = !this.facetsReadyToLoad && ready;
440
- this.facetsReadyToLoad = ready;
441
-
442
- const needsFetch = facetsBecameReady && this.canFetchFacets;
443
- if (needsFetch) {
444
- this.fetchFacets();
445
- }
446
- }
447
-
448
- /**
449
- * Whether the data source & its host are in a state where a facet request should be fired.
450
- * (i.e., they aren't suppressed or already loading, etc.)
451
- */
452
- private get canFetchFacets(): boolean {
453
- // Don't fetch facets if they are suppressed entirely or not required for the current profile page element
454
- if (this.host.facetLoadStrategy === 'off') return false;
455
- if (FACETLESS_PAGE_ELEMENTS.includes(this.host.profileElement!))
456
- return false;
457
-
458
- // If facets are to be lazy-loaded, don't fetch them if they are not going to be visible anyway
459
- // (wait until they become visible instead)
460
- if (this.host.facetLoadStrategy !== 'eager' && !this.facetsReadyToLoad)
461
- return false;
462
-
463
- // Don't fetch facets again if they are already fetched or pending
464
- const facetsAlreadyFetched =
465
- Object.keys(this.aggregations ?? {}).length > 0;
466
- if (this.facetsLoading || facetsAlreadyFetched) return false;
467
-
468
- return true;
469
- }
470
-
471
- /**
472
- * @inheritdoc
473
- */
474
- map(
475
- callback: (
476
- model: TileModel,
477
- index: number,
478
- array: TileModel[],
479
- ) => TileModel,
480
- ): void {
481
- if (!Object.keys(this.pages).length) return;
482
- this.pages = Object.fromEntries(
483
- Object.entries(this.pages).map(([page, tileModels]) => [
484
- page,
485
- tileModels.map((model, index, array) =>
486
- model ? callback(model, index, array) : model,
487
- ),
488
- ]),
489
- );
490
- this.requestHostUpdate();
491
- this.refreshVisibleResults();
492
- }
493
-
494
- /**
495
- * @inheritdoc
496
- */
497
- checkAllTiles = (): void => {
498
- this.map(model => {
499
- const cloned = model.clone();
500
- cloned.checked = true;
501
- return cloned;
502
- });
503
- };
504
-
505
- /**
506
- * @inheritdoc
507
- */
508
- uncheckAllTiles = (): void => {
509
- this.map(model => {
510
- const cloned = model.clone();
511
- cloned.checked = false;
512
- return cloned;
513
- });
514
- };
515
-
516
- /**
517
- * @inheritdoc
518
- */
519
- removeCheckedTiles = (): void => {
520
- // To make sure our data source remains page-aligned, we will offset our data source by
521
- // the number of removed tiles, so that we can just add the offset when the infinite
522
- // scroller queries for cell contents.
523
- // This only matters while we're still viewing the same set of results. If the user changes
524
- // their query/filters/sort, then the data source is overwritten and the offset cleared.
525
- const { checkedTileModels, uncheckedTileModels } = this;
526
- const numChecked = checkedTileModels.length;
527
- if (numChecked === 0) return;
528
- this.offset += numChecked;
529
- const newPages: typeof this.pages = {};
530
-
531
- // Which page the remaining tile models start on, post-offset
532
- let offsetPageNumber = Math.floor(this.offset / this.pageSize) + 1;
533
- let indexOnPage = this.offset % this.pageSize;
534
-
535
- // Fill the pages up to that point with empty models
536
- for (let page = 1; page <= offsetPageNumber; page += 1) {
537
- const remainingHidden = this.offset - this.pageSize * (page - 1);
538
- const offsetCellsOnPage = Math.min(this.pageSize, remainingHidden);
539
- newPages[page] = Array(offsetCellsOnPage).fill(undefined);
540
- }
541
-
542
- // Shift all the remaining tiles into their new positions in the data source
543
- for (const model of uncheckedTileModels) {
544
- if (!newPages[offsetPageNumber]) newPages[offsetPageNumber] = [];
545
- newPages[offsetPageNumber].push(model);
546
- indexOnPage += 1;
547
- if (indexOnPage >= this.pageSize) {
548
- offsetPageNumber += 1;
549
- indexOnPage = 0;
550
- }
551
- }
552
-
553
- // Swap in the new pages
554
- this.pages = newPages;
555
- this.numTileModels -= numChecked;
556
- this.totalResults -= numChecked;
557
- this.host.setTileCount(this.size);
558
- this.host.setTotalResultCount(this.totalResults);
559
- this.requestHostUpdate();
560
- this.refreshVisibleResults();
561
- };
562
-
563
- /**
564
- * @inheritdoc
565
- */
566
- get checkedTileModels(): TileModel[] {
567
- return this.getFilteredTileModels(model => model.checked);
568
- }
569
-
570
- /**
571
- * @inheritdoc
572
- */
573
- get uncheckedTileModels(): TileModel[] {
574
- return this.getFilteredTileModels(model => !model.checked);
575
- }
576
-
577
- /**
578
- * Returns a flattened, filtered array of all the tile models in the data source
579
- * for which the given predicate returns a truthy value.
580
- *
581
- * @param predicate A callback function to apply on each tile model, as with Array.filter
582
- * @returns A filtered array of tile models satisfying the predicate
583
- */
584
- private getFilteredTileModels(
585
- predicate: (model: TileModel, index: number, array: TileModel[]) => unknown,
586
- ): TileModel[] {
587
- return Object.values(this.pages)
588
- .flat()
589
- .filter((model, index, array) =>
590
- model ? predicate(model, index, array) : false,
591
- );
592
- }
593
-
594
- /**
595
- * @inheritdoc
596
- */
597
- get canPerformSearch(): boolean {
598
- if (!this.host.searchService) return false;
599
-
600
- const trimmedQuery = this.host.baseQuery?.trim();
601
- const hasNonEmptyQuery = !!trimmedQuery;
602
- const hasIdentifiers = !!this.host.identifiers?.length;
603
- const isCollectionSearch = !!this.host.withinCollection;
604
- const isProfileSearch = !!this.host.withinProfile;
605
- const hasProfileElement = !!this.host.profileElement;
606
- const isDefaultedSearch = this.host.searchType === SearchType.DEFAULT;
607
- const isMetadataSearch = this.host.searchType === SearchType.METADATA;
608
- const isTvSearch = this.host.searchType === SearchType.TV;
609
-
610
- // Metadata/tv searches within a collection are allowed to have no query.
611
- const isValidForCollectionSearch =
612
- isDefaultedSearch || isMetadataSearch || isTvSearch;
613
-
614
- // Searches within a profile page may also be performed without a query, provided the profile element is set.
615
- const isValidForProfileSearch =
616
- hasProfileElement && (isDefaultedSearch || isMetadataSearch);
617
-
618
- // Otherwise, a non-empty query must be set.
619
- return (
620
- hasNonEmptyQuery ||
621
- hasIdentifiers ||
622
- (isCollectionSearch && isValidForCollectionSearch) ||
623
- (isProfileSearch && isValidForProfileSearch)
624
- );
625
- }
626
-
627
- /**
628
- * Sets the state for whether the initial set of search results for the
629
- * current query is loading
630
- */
631
- private setSearchResultsLoading(loading: boolean): void {
632
- this.searchResultsLoading = loading;
633
- if (this.activeOnHost) {
634
- this.host.setSearchResultsLoading(loading);
635
- }
636
- }
637
-
638
- /**
639
- * Sets the state for whether the facets for a query is loading
640
- */
641
- private setFacetsLoading(loading: boolean): void {
642
- this.facetsLoading = loading;
643
- if (this.activeOnHost) {
644
- this.host.setFacetsLoading(loading);
645
- }
646
- }
647
-
648
- /**
649
- * Requests that the host perform an update, provided this data
650
- * source is actively installed on it.
651
- */
652
- private requestHostUpdate(): void {
653
- if (this.activeOnHost) {
654
- this.host.requestUpdate();
655
- }
656
- }
657
-
658
- /**
659
- * Requests that the host refresh its visible tiles, provided this
660
- * data source is actively installed on it.
661
- */
662
- private refreshVisibleResults(): void {
663
- if (this.activeOnHost) {
664
- this.host.refreshVisibleResults();
665
- }
666
- }
667
-
668
- /**
669
- * The query key is a string that uniquely identifies the current search.
670
- * It consists of:
671
- * - The current base query
672
- * - The current collection/profile target & page element
673
- * - The current search type
674
- * - Any currently-applied facets
675
- * - Any currently-applied date range
676
- * - Any currently-applied prefix filters
677
- * - The current sort options
678
- *
679
- * This lets us internally keep track of queries so we don't persist data that's
680
- * no longer relevant. Not meant to be human-readable.
681
- */
682
- get pageFetchQueryKey(): string {
683
- const profileKey = `pf;${this.host.withinProfile}--pe;${this.host.profileElement}`;
684
- const pageTarget = this.host.withinCollection ?? profileKey;
685
- const sortField = this.host.selectedSort ?? 'none';
686
- const sortDirection = this.host.sortDirection ?? 'none';
687
- return `fq:${this.fullQuery}-pt:${pageTarget}-st:${this.host.searchType}-sf:${sortField}-sd:${sortDirection}`;
688
- }
689
-
690
- /**
691
- * Similar to `pageFetchQueryKey` above, but excludes sort fields since they
692
- * are not relevant in determining aggregation queries.
693
- */
694
- get facetFetchQueryKey(): string {
695
- const profileKey = `pf;${this.host.withinProfile}--pe;${this.host.profileElement}`;
696
- const pageTarget = this.host.withinCollection ?? profileKey;
697
- return `facets-fq:${this.fullQuery}-pt:${pageTarget}-st:${this.host.searchType}`;
698
- }
699
-
700
- /**
701
- * Constructs a search service FilterMap object from the combination of
702
- * all the currently-applied filters. This includes any facets, letter
703
- * filters, and date range.
704
- */
705
- get filterMap(): FilterMap {
706
- const builder = new FilterMapBuilder();
707
-
708
- const {
709
- minSelectedDate,
710
- maxSelectedDate,
711
- selectedFacets,
712
- internalFilters,
713
- selectedTitleFilter,
714
- selectedCreatorFilter,
715
- } = this.host;
716
-
717
- const dateField = this.host.searchType === SearchType.TV ? 'date' : 'year';
718
-
719
- if (minSelectedDate) {
720
- builder.addFilter(
721
- dateField,
722
- minSelectedDate,
723
- FilterConstraint.GREATER_OR_EQUAL,
724
- );
725
- }
726
- if (maxSelectedDate) {
727
- builder.addFilter(
728
- dateField,
729
- maxSelectedDate,
730
- FilterConstraint.LESS_OR_EQUAL,
731
- );
732
- }
733
-
734
- // Add any selected facets and internal filters
735
- const combinedFilters = mergeSelectedFacets(
736
- internalFilters,
737
- selectedFacets,
738
- );
739
- if (combinedFilters) {
740
- for (const [facetName, facetValues] of Object.entries(combinedFilters)) {
741
- const { name, values } = this.prepareFacetForFetch(
742
- facetName,
743
- facetValues,
744
- );
745
- for (const [value, bucket] of Object.entries(values)) {
746
- let constraint;
747
- if (bucket.state === 'selected') {
748
- constraint = FilterConstraint.INCLUDE;
749
- } else if (bucket.state === 'hidden') {
750
- constraint = FilterConstraint.EXCLUDE;
751
- }
752
-
753
- if (constraint) {
754
- builder.addFilter(name, value, constraint);
755
- }
756
- }
757
- }
758
- }
759
-
760
- // Add any letter filters
761
- if (selectedTitleFilter) {
762
- builder.addFilter(
763
- 'firstTitle',
764
- selectedTitleFilter,
765
- FilterConstraint.INCLUDE,
766
- );
767
- }
768
- if (selectedCreatorFilter) {
769
- builder.addFilter(
770
- 'firstCreator',
771
- selectedCreatorFilter,
772
- FilterConstraint.INCLUDE,
773
- );
774
- }
775
-
776
- // Add any TV clip type filter, if applicable
777
- if (this.host.searchType === SearchType.TV) {
778
- switch (this.host.tvClipFilter) {
779
- case 'commercials':
780
- builder.addFilter('ad_id', '*', FilterConstraint.INCLUDE);
781
- break;
782
- case 'factchecks':
783
- builder.addFilter('factcheck', '*', FilterConstraint.INCLUDE);
784
- break;
785
- case 'quotes':
786
- builder.addFilter('clip', '1', FilterConstraint.INCLUDE);
787
- break;
788
- case 'all':
789
- default:
790
- break;
791
- }
792
- }
793
-
794
- const filterMap = builder.build();
795
- return filterMap;
796
- }
797
-
798
- /**
799
- * Produces a compact unique ID for a search request that can help with debugging
800
- * on the backend by making related requests easier to trace through different services.
801
- * (e.g., tying the hits/aggregations requests for the same page back to a single hash).
802
- *
803
- * @param params The search service parameters for the request
804
- * @param kind The kind of request (hits-only, aggregations-only, or both)
805
- * @returns A Promise resolving to the uid to apply to the request
806
- */
807
- async requestUID(params: SearchParams, kind: RequestKind): Promise<string> {
808
- const paramsToHash = JSON.stringify({
809
- pageType: params.pageType,
810
- pageTarget: params.pageTarget,
811
- query: params.query,
812
- fields: params.fields,
813
- filters: params.filters,
814
- sort: params.sort,
815
- searchType: this.host.searchType,
816
- });
817
-
818
- const fullQueryHash = (await sha1(paramsToHash)).slice(0, 20); // First 80 bits of SHA-1 are plenty for this
819
- const sessionId = (await this.host.getSessionId()).slice(0, 20); // Likewise
820
- const page = params.page ?? 0;
821
- const kindPrefix = kind.charAt(0); // f = full, h = hits, a = aggregations
822
- const currentTime = Date.now();
823
-
824
- return `R:${fullQueryHash}-S:${sessionId}-P:${page}-K:${kindPrefix}-T:${currentTime}`;
825
- }
826
-
827
- /**
828
- * @inheritdoc
829
- */
830
- get pageSpecifierParams(): PageSpecifierParams | null {
831
- if (this.host.identifiers?.length) {
832
- return {
833
- pageType: 'client_document_fetch',
834
- };
835
- }
836
- if (this.host.withinCollection) {
837
- return {
838
- pageType: 'collection_details',
839
- pageTarget: this.host.withinCollection,
840
- };
841
- }
842
- if (this.host.withinProfile) {
843
- return {
844
- pageType: 'account_details',
845
- pageTarget: this.host.withinProfile,
846
- pageElements: this.host.profileElement
847
- ? [this.host.profileElement]
848
- : [],
849
- };
850
- }
851
- return null;
852
- }
853
-
854
- /**
855
- * The full query, including year facets and date range clauses
856
- */
857
- private get fullQuery(): string | undefined {
858
- const parts = [];
859
- const trimmedQuery = this.host.baseQuery?.trim();
860
- if (trimmedQuery) parts.push(trimmedQuery);
861
-
862
- if (this.host.identifiers) {
863
- parts.push(`identifier:(${this.host.identifiers.join(' OR ')})`);
864
- }
865
-
866
- const { facetQuery, dateRangeQueryClause, sortFilterQueries } = this;
867
- if (facetQuery) parts.push(facetQuery);
868
- if (dateRangeQueryClause) parts.push(dateRangeQueryClause);
869
- if (sortFilterQueries) parts.push(sortFilterQueries);
870
-
871
- return parts.join(' AND ').trim();
872
- }
873
-
874
- /**
875
- * Generates a query string representing the current set of applied facets
876
- *
877
- * Example: `mediatype:("collection" OR "audio" OR -"etree") AND year:("2000" OR "2001")`
878
- */
879
- private get facetQuery(): string | undefined {
880
- if (!this.host.selectedFacets) return undefined;
881
- const facetClauses = [];
882
- for (const [facetName, facetValues] of Object.entries(
883
- this.host.selectedFacets,
884
- )) {
885
- facetClauses.push(this.buildFacetClause(facetName, facetValues));
886
- }
887
- return this.joinFacetClauses(facetClauses)?.trim();
888
- }
889
-
890
- private get dateRangeQueryClause(): string | undefined {
891
- if (!this.host.minSelectedDate || !this.host.maxSelectedDate) {
892
- return undefined;
893
- }
894
-
895
- return `year:[${this.host.minSelectedDate} TO ${this.host.maxSelectedDate}]`;
896
- }
897
-
898
- private get sortFilterQueries(): string {
899
- const queries = [this.titleQuery, this.creatorQuery];
900
- return queries.filter(q => q).join(' AND ');
901
- }
902
-
903
- /**
904
- * Returns a query clause identifying the currently selected title filter,
905
- * e.g., `firstTitle:X`.
906
- */
907
- private get titleQuery(): string | undefined {
908
- return this.host.selectedTitleFilter
909
- ? `firstTitle:${this.host.selectedTitleFilter}`
910
- : undefined;
911
- }
912
-
913
- /**
914
- * Returns a query clause identifying the currently selected creator filter,
915
- * e.g., `firstCreator:X`.
916
- */
917
- private get creatorQuery(): string | undefined {
918
- return this.host.selectedCreatorFilter
919
- ? `firstCreator:${this.host.selectedCreatorFilter}`
920
- : undefined;
921
- }
922
-
923
- /**
924
- * Builds an OR-joined facet clause for the given facet name and values.
925
- *
926
- * E.g., for name `subject` and values
927
- * `{ foo: { state: 'selected' }, bar: { state: 'hidden' } }`
928
- * this will produce the clause
929
- * `subject:("foo" OR -"bar")`.
930
- *
931
- * @param facetName The facet type (e.g., 'collection')
932
- * @param facetValues The facet buckets, mapped by their keys
933
- */
934
- private buildFacetClause(
935
- facetName: string,
936
- facetValues: Record<string, FacetBucket>,
937
- ): string {
938
- const { name: facetQueryName, values } = this.prepareFacetForFetch(
939
- facetName,
940
- facetValues,
941
- );
942
- const facetEntries = Object.entries(values);
943
- if (facetEntries.length === 0) return '';
944
-
945
- const facetValuesArray: string[] = [];
946
- for (const [key, facetData] of facetEntries) {
947
- const plusMinusPrefix = facetData.state === 'hidden' ? '-' : '';
948
- facetValuesArray.push(`${plusMinusPrefix}"${key}"`);
949
- }
950
-
951
- const valueQuery = facetValuesArray.join(` OR `);
952
- return `${facetQueryName}:(${valueQuery})`;
953
- }
954
-
955
- /**
956
- * Handles some special pre-request normalization steps for certain facet types
957
- * that require them.
958
- *
959
- * @param facetName The name of the facet type (e.g., 'language')
960
- * @param facetValues An array of values for that facet type
961
- */
962
- private prepareFacetForFetch(
963
- facetName: string,
964
- facetValues: Record<string, FacetBucket>,
965
- ): { name: string; values: Record<string, FacetBucket> } {
966
- // eslint-disable-next-line prefer-const
967
- let [normalizedName, normalizedValues] = [facetName, facetValues];
968
-
969
- // The full "search engine" name of the lending field is "lending___status"
970
- if (facetName === 'lending') {
971
- normalizedName = 'lending___status';
972
- }
973
-
974
- return {
975
- name: normalizedName,
976
- values: normalizedValues,
977
- };
978
- }
979
-
980
- /**
981
- * Takes an array of facet clauses, and combines them into a
982
- * full AND-joined facet query string. Empty clauses are ignored.
983
- */
984
- private joinFacetClauses(facetClauses: string[]): string | undefined {
985
- const nonEmptyFacetClauses = facetClauses.filter(
986
- clause => clause.length > 0,
987
- );
988
- return nonEmptyFacetClauses.length > 0
989
- ? `(${nonEmptyFacetClauses.join(' AND ')})`
990
- : undefined;
991
- }
992
-
993
- /**
994
- * Fires a backend request to fetch a set of aggregations (representing UI facets) for
995
- * the current search state.
996
- */
997
- private async fetchFacets(): Promise<void> {
998
- const trimmedQuery = this.host.baseQuery?.trim();
999
- if (!this.canPerformSearch) return;
1000
-
1001
- const { facetFetchQueryKey } = this;
1002
- if (this.fetchesInProgress.has(facetFetchQueryKey)) return;
1003
- this.fetchesInProgress.add(facetFetchQueryKey);
1004
-
1005
- this.setFacetsLoading(true);
1006
-
1007
- const sortParams = this.host.sortParam ? [this.host.sortParam] : [];
1008
- const params: SearchParams = {
1009
- ...this.pageSpecifierParams,
1010
- query: trimmedQuery || '',
1011
- identifiers: this.host.identifiers,
1012
- rows: 0,
1013
- filters: this.filterMap,
1014
- // Fetch a few extra buckets beyond the 6 we show, in case some get suppressed
1015
- aggregationsSize: 10,
1016
- // Note: we don't need an aggregations param to fetch the default aggregations from the PPS.
1017
- // The default aggregations for the search_results page type should be what we need here.
1018
- };
1019
- params.uid = await this.requestUID(
1020
- { ...params, sort: sortParams },
1021
- 'aggregations',
1022
- );
1023
-
1024
- const searchResponse = await this.host.searchService?.search(
1025
- params,
1026
- this.host.searchType,
1027
- );
1028
- const success = searchResponse?.success;
1029
-
1030
- // This is checking to see if the query has changed since the data was fetched.
1031
- // If so, we just want to discard this set of aggregations because they are
1032
- // likely no longer valid for the newer query.
1033
- const queryChangedSinceFetch =
1034
- !this.fetchesInProgress.has(facetFetchQueryKey);
1035
- this.fetchesInProgress.delete(facetFetchQueryKey);
1036
- if (queryChangedSinceFetch) return;
1037
-
1038
- if (!success) {
1039
- const errorMsg = searchResponse?.error?.message;
1040
- const detailMsg = searchResponse?.error?.details?.message;
1041
-
1042
- if (!errorMsg && !detailMsg) {
1043
- // @ts-expect-error: Property 'Sentry' does not exist on type 'Window & typeof globalThis'
1044
- window?.Sentry?.captureMessage?.(
1045
- 'Missing or malformed facet response from backend',
1046
- 'error',
1047
- );
1048
- }
1049
-
1050
- this.setFacetsLoading(false);
1051
- return;
1052
- }
1053
-
1054
- const { aggregations, collectionTitles, tvChannelAliases } =
1055
- success.response;
1056
- this.aggregations = aggregations;
1057
-
1058
- this.histogramAggregation =
1059
- this.host.searchType === SearchType.TV
1060
- ? aggregations?.date_histogram
1061
- : aggregations?.year_histogram;
1062
-
1063
- if (collectionTitles) {
1064
- for (const [id, title] of Object.entries(collectionTitles)) {
1065
- this.collectionTitles.set(id, title);
1066
- }
1067
- }
1068
- if (tvChannelAliases) {
1069
- for (const [channel, network] of Object.entries(tvChannelAliases)) {
1070
- this.tvChannelAliases.set(channel, network);
1071
- }
1072
- }
1073
-
1074
- this.setFacetsLoading(false);
1075
- this.requestHostUpdate();
1076
- }
1077
-
1078
- /**
1079
- * Performs the initial page fetch(es) for the current search state.
1080
- */
1081
- private async doInitialPageFetch(): Promise<void> {
1082
- this.setSearchResultsLoading(true);
1083
- // Try to batch 2 initial page requests when possible
1084
- await this.fetchPage(this.host.initialPageNumber, this.numInitialPages);
1085
- }
1086
-
1087
- /**
1088
- * Fetches one or more pages of results and updates the data source.
1089
- *
1090
- * @param pageNumber The page number to fetch
1091
- * @param numInitialPages If this is an initial page fetch (`pageNumber = 1`),
1092
- * specifies how many pages to batch together in one request. Ignored
1093
- * if `pageNumber != 1`, defaulting to a single page.
1094
- */
1095
- async fetchPage(pageNumber: number, numInitialPages = 1): Promise<void> {
1096
- const trimmedQuery = this.host.baseQuery?.trim();
1097
- // reset loading status
1098
- if (!this.canPerformSearch) {
1099
- this.setSearchResultsLoading(false);
1100
- return;
1101
- }
1102
-
1103
- // if we already have data, don't fetch again
1104
- if (this.hasPage(pageNumber)) return;
1105
-
1106
- if (this.endOfDataReached) return;
1107
-
1108
- // Batch multiple initial page requests together if needed (e.g., can request
1109
- // pages 1 and 2 together in a single request).
1110
- let numPages = pageNumber === 1 ? numInitialPages : 1;
1111
- const numRows = this.pageSize * numPages;
1112
-
1113
- // if a fetch is already in progress for this query and page, don't fetch again
1114
- const { pageFetchQueryKey } = this;
1115
- const currentPageKey = `${pageFetchQueryKey}-p:${pageNumber}`;
1116
- if (this.fetchesInProgress.has(currentPageKey)) return;
1117
-
1118
- for (let i = 0; i < numPages; i += 1) {
1119
- this.fetchesInProgress.add(`${pageFetchQueryKey}-p:${pageNumber + i}`);
1120
- }
1121
- this.previousQueryKey = pageFetchQueryKey;
1122
-
1123
- const { withinCollection, withinProfile } = this.host;
1124
-
1125
- let sortParams = this.host.sortParam ? [this.host.sortParam] : [];
1126
- // TODO eventually the PPS should handle these defaults natively
1127
- const isDefaultProfileSort =
1128
- withinProfile && this.host.selectedSort === SortField.default;
1129
- if (isDefaultProfileSort && this.host.defaultSortField) {
1130
- const sortOption = SORT_OPTIONS[this.host.defaultSortField];
1131
- if (sortOption.searchServiceKey) {
1132
- sortParams = [
1133
- {
1134
- field: sortOption.searchServiceKey,
1135
- direction: 'desc',
1136
- },
1137
- ];
1138
- }
1139
- }
1140
-
1141
- const params: SearchParams = {
1142
- ...this.pageSpecifierParams,
1143
- query: trimmedQuery || '',
1144
- identifiers: this.host.identifiers,
1145
- page: pageNumber,
1146
- rows: numRows,
1147
- sort: sortParams,
1148
- filters: this.filterMap,
1149
- aggregations: { omit: true },
1150
- };
1151
- params.uid = await this.requestUID(params, 'hits');
1152
-
1153
- // log('=== FIRING PAGE REQUEST ===', params);
1154
- const searchResponse = await this.host.searchService?.search(
1155
- params,
1156
- this.host.searchType,
1157
- );
1158
- // log('=== RECEIVED PAGE RESPONSE IN CB ===', searchResponse);
1159
- const success = searchResponse?.success;
1160
-
1161
- // This is checking to see if the fetch has been invalidated since it was fired off.
1162
- // If so, we just want to discard the response since it is for an obsolete query state.
1163
- if (!this.fetchesInProgress.has(currentPageKey)) return;
1164
- for (let i = 0; i < numPages; i += 1) {
1165
- this.fetchesInProgress.delete(`${pageFetchQueryKey}-p:${pageNumber + i}`);
1166
- }
1167
-
1168
- if (!success) {
1169
- const errorMsg = searchResponse?.error?.message;
1170
- const detailMsg = searchResponse?.error?.details?.message;
1171
-
1172
- this.queryErrorMessage = `${errorMsg ?? ''}${
1173
- detailMsg ? `; ${detailMsg}` : ''
1174
- }`;
1175
-
1176
- if (!this.queryErrorMessage) {
1177
- this.queryErrorMessage = 'Missing or malformed response from backend';
1178
- // @ts-expect-error: Property 'Sentry' does not exist on type 'Window & typeof globalThis'
1179
- window?.Sentry?.captureMessage?.(this.queryErrorMessage, 'error');
1180
- }
1181
-
1182
- this.setSearchResultsLoading(false);
1183
- this.requestHostUpdate();
1184
- this.host.emitSearchError();
1185
- return;
1186
- }
1187
-
1188
- this.setTotalResultCount(success.response.totalResults - this.offset);
1189
- if (this.activeOnHost && this.totalResults === 0) {
1190
- // display event to offshoot when result count is zero.
1191
- this.host.emitEmptyResults();
1192
- }
1193
-
1194
- this.sessionContext = success.sessionContext;
1195
- if (withinCollection) {
1196
- this.collectionExtraInfo = success.response.collectionExtraInfo;
1197
-
1198
- // For collections, we want the UI to respect the default sort option
1199
- // which can be specified in metadata, or otherwise assumed to be week:desc
1200
- if (this.activeOnHost) {
1201
- this.host.applyDefaultCollectionSort(this.collectionExtraInfo);
1202
- }
1203
-
1204
- if (this.collectionExtraInfo) {
1205
- this.parentCollections = [].concat(
1206
- this.collectionExtraInfo.public_metadata?.collection ?? [],
1207
- );
1208
-
1209
- // Update the TV collection status now that we know the parent collections
1210
- this.host.isTVCollection =
1211
- this.host.withinCollection?.startsWith('TV-') ||
1212
- this.host.withinCollection === 'tvnews' ||
1213
- this.host.withinCollection === 'tvarchive' ||
1214
- this.parentCollections.includes('tvnews') ||
1215
- this.parentCollections.includes('tvarchive');
1216
- }
1217
- } else if (withinProfile) {
1218
- this.accountExtraInfo = success.response.accountExtraInfo;
1219
- this.pageElements = success.response.pageElements;
1220
- }
1221
-
1222
- const { results, collectionTitles, tvChannelAliases } = success.response;
1223
- if (results && results.length > 0) {
1224
- // Load any collection titles present on the response into the cache,
1225
- // or queue up preload fetches for them if none were present.
1226
- if (collectionTitles) {
1227
- for (const [id, title] of Object.entries(collectionTitles)) {
1228
- this.collectionTitles.set(id, title);
1229
- }
1230
-
1231
- // Also add the target collection's title if available
1232
- const targetTitle = this.collectionExtraInfo?.public_metadata?.title;
1233
- if (withinCollection && targetTitle) {
1234
- this.collectionTitles.set(withinCollection, targetTitle);
1235
- }
1236
- }
1237
-
1238
- if (tvChannelAliases) {
1239
- for (const [channel, network] of Object.entries(tvChannelAliases)) {
1240
- this.tvChannelAliases.set(channel, network);
1241
- }
1242
- }
1243
-
1244
- // Update the data source for each returned page.
1245
- // For loans and web archives, we must account for receiving more pages than we asked for.
1246
- const isUnpagedElement = ['lending', 'web_archives'].includes(
1247
- this.host.profileElement!,
1248
- );
1249
- if (isUnpagedElement) {
1250
- numPages = Math.ceil(results.length / this.pageSize);
1251
- this.endOfDataReached = true;
1252
- if (this.activeOnHost) this.host.setTileCount(this.totalResults);
1253
- }
1254
-
1255
- for (let i = 0; i < numPages; i += 1) {
1256
- const pageStartIndex = this.pageSize * i;
1257
- this.addFetchedResultsToDataSource(
1258
- pageNumber + i,
1259
- results.slice(pageStartIndex, pageStartIndex + this.pageSize),
1260
- !isUnpagedElement || i === numPages - 1,
1261
- );
1262
- }
1263
- }
1264
-
1265
- // When we reach the end of the data, we can set the infinite scroller's
1266
- // item count to the real total number of results (rather than the
1267
- // temporary estimates based on pages rendered so far).
1268
- if (this.size >= this.totalResults || results.length === 0) {
1269
- this.endOfDataReached = true;
1270
- if (this.activeOnHost) this.host.setTileCount(this.size);
1271
- }
1272
-
1273
- this.setSearchResultsLoading(false);
1274
- this.requestHostUpdate();
1275
- }
1276
-
1277
- /**
1278
- * Returns the type of request that produced the current set of hits,
1279
- * based on the presence of a search query or profile/collection target
1280
- * on the host.
1281
- */
1282
- private get hitRequestSource(): HitRequestSource {
1283
- const { host } = this;
1284
- if (host.baseQuery) return 'search_query';
1285
- if (host.withinProfile) return 'profile_tab';
1286
- if (host.withinCollection) return 'collection_members';
1287
- return 'unknown';
1288
- }
1289
-
1290
- /**
1291
- * Update the datasource from the fetch response
1292
- *
1293
- * @param pageNumber
1294
- * @param results
1295
- */
1296
- private addFetchedResultsToDataSource(
1297
- pageNumber: number,
1298
- results: SearchResult[],
1299
- needsReload = true,
1300
- ): void {
1301
- const tiles: TileModel[] = [];
1302
- const requestSource = this.hitRequestSource;
1303
- results?.forEach(result => {
1304
- if (!result.identifier) return;
1305
- tiles.push(new TileModel(result, requestSource));
1306
- });
1307
-
1308
- this.addPage(pageNumber, tiles);
1309
-
1310
- if (needsReload) {
1311
- this.refreshVisibleResults();
1312
- }
1313
- }
1314
-
1315
- /**
1316
- * Fetches the aggregation buckets for the given prefix filter type.
1317
- */
1318
- private async fetchPrefixFilterBuckets(
1319
- filterType: PrefixFilterType,
1320
- ): Promise<Bucket[]> {
1321
- const trimmedQuery = this.host.baseQuery?.trim();
1322
- if (!this.canPerformSearch) return [];
1323
-
1324
- const filterAggregationKey = prefixFilterAggregationKeys[filterType];
1325
- const sortParams = this.host.sortParam ? [this.host.sortParam] : [];
1326
-
1327
- const params: SearchParams = {
1328
- ...this.pageSpecifierParams,
1329
- query: trimmedQuery || '',
1330
- identifiers: this.host.identifiers,
1331
- rows: 0,
1332
- filters: this.filterMap,
1333
- // Only fetch the firstTitle or firstCreator aggregation
1334
- aggregations: { simpleParams: [filterAggregationKey] },
1335
- // Fetch all 26 letter buckets
1336
- aggregationsSize: 26,
1337
- };
1338
- params.uid = await this.requestUID(
1339
- { ...params, sort: sortParams },
1340
- 'aggregations',
1341
- );
1342
-
1343
- const searchResponse = await this.host.searchService?.search(
1344
- params,
1345
- this.host.searchType,
1346
- );
1347
-
1348
- return (searchResponse?.success?.response?.aggregations?.[
1349
- filterAggregationKey
1350
- ]?.buckets ?? []) as Bucket[];
1351
- }
1352
-
1353
- /**
1354
- * Fetches and caches the prefix filter counts for the given filter type.
1355
- */
1356
- async updatePrefixFilterCounts(filterType: PrefixFilterType): Promise<void> {
1357
- const { facetFetchQueryKey } = this;
1358
- const buckets = await this.fetchPrefixFilterBuckets(filterType);
1359
-
1360
- // Don't update the filter counts for an outdated query (if it has been changed
1361
- // since we sent the request)
1362
- const queryChangedSinceFetch =
1363
- facetFetchQueryKey !== this.facetFetchQueryKey;
1364
- if (queryChangedSinceFetch) return;
1365
-
1366
- // Unpack the aggregation buckets into a simple map like { 'A': 50, 'B': 25, ... }
1367
- this.prefixFilterCountMap = { ...this.prefixFilterCountMap }; // Clone the object to trigger an update
1368
- this.prefixFilterCountMap[filterType] = buckets.reduce(
1369
- (acc: Record<string, number>, bucket: Bucket) => {
1370
- acc[(bucket.key as string).toUpperCase()] = bucket.doc_count;
1371
- return acc;
1372
- },
1373
- {},
1374
- );
1375
-
1376
- this.requestHostUpdate();
1377
- }
1378
-
1379
- /**
1380
- * @inheritdoc
1381
- */
1382
- async updatePrefixFiltersForCurrentSort(): Promise<void> {
1383
- if (['title', 'creator'].includes(this.host.selectedSort as SortField)) {
1384
- const filterType = this.host.selectedSort as PrefixFilterType;
1385
- if (!this.prefixFilterCountMap[filterType]) {
1386
- this.updatePrefixFilterCounts(filterType);
1387
- }
1388
- }
1389
- }
1390
-
1391
- /**
1392
- * @inheritdoc
1393
- */
1394
- refreshLetterCounts(): void {
1395
- if (Object.keys(this.prefixFilterCountMap).length > 0) {
1396
- this.prefixFilterCountMap = {};
1397
- }
1398
- this.updatePrefixFiltersForCurrentSort();
1399
- this.requestHostUpdate();
1400
- }
1401
- }
1
+ import type { ReactiveControllerHost } from 'lit';
2
+ import {
3
+ AccountExtraInfo,
4
+ Aggregation,
5
+ Bucket,
6
+ CollectionExtraInfo,
7
+ FilterConstraint,
8
+ FilterMap,
9
+ FilterMapBuilder,
10
+ PageElementMap,
11
+ SearchParams,
12
+ SearchResponseSessionContext,
13
+ SearchResult,
14
+ SearchType,
15
+ } from '@internetarchive/search-service';
16
+ import {
17
+ prefixFilterAggregationKeys,
18
+ type FacetBucket,
19
+ type PrefixFilterType,
20
+ TileModel,
21
+ PrefixFilterCounts,
22
+ RequestKind,
23
+ SortField,
24
+ SORT_OPTIONS,
25
+ HitRequestSource,
26
+ } from '../models';
27
+ import {
28
+ FACETLESS_PAGE_ELEMENTS,
29
+ TVChannelMaps,
30
+ type PageSpecifierParams,
31
+ } from './models';
32
+ import type { CollectionBrowserDataSourceInterface } from './collection-browser-data-source-interface';
33
+ import type { CollectionBrowserSearchInterface } from './collection-browser-query-state';
34
+ import { sha1 } from '../utils/sha1';
35
+ import { log } from '../utils/log';
36
+ import { mergeSelectedFacets } from '../utils/facet-utils';
37
+
38
+ export class CollectionBrowserDataSource
39
+ implements CollectionBrowserDataSourceInterface
40
+ {
41
+ /**
42
+ * All pages of tile models that have been fetched so far, indexed by their page
43
+ * number (with the first being page 1).
44
+ */
45
+ private pages: Record<string, TileModel[]> = {};
46
+
47
+ /**
48
+ * Tile offset to apply when looking up tiles in the pages, in order to maintain
49
+ * page alignment after tiles are removed.
50
+ */
51
+ private offset = 0;
52
+
53
+ /**
54
+ * Total number of tile models stored in this data source's pages
55
+ */
56
+ private numTileModels = 0;
57
+
58
+ /**
59
+ * How many consecutive pages should be batched together on the initial page fetch.
60
+ * Defaults to 2 pages.
61
+ */
62
+ private numInitialPages = 2;
63
+
64
+ /**
65
+ * A set of fetch IDs that are valid for the current query state
66
+ */
67
+ private fetchesInProgress = new Set<string>();
68
+
69
+ /**
70
+ * A record of the query key used for the last search.
71
+ * If this changes, we need to load new results.
72
+ */
73
+ private previousQueryKey: string = '';
74
+
75
+ /**
76
+ * Whether the initial page of search results for the current query state
77
+ * is presently being fetched.
78
+ */
79
+ private searchResultsLoading = false;
80
+
81
+ /**
82
+ * Whether the facets (aggregations) for the current query state are
83
+ * presently being fetched.
84
+ */
85
+ private facetsLoading = false;
86
+
87
+ /**
88
+ * Whether the facets are actually visible -- if not, then we can delay any facet
89
+ * fetches until they become visible.
90
+ */
91
+ private facetsReadyToLoad = false;
92
+
93
+ /**
94
+ * Whether further query changes should be ignored and not trigger fetches
95
+ */
96
+ private suppressFetches = false;
97
+
98
+ /**
99
+ * @inheritdoc
100
+ */
101
+ totalResults = 0;
102
+
103
+ /**
104
+ * @inheritdoc
105
+ */
106
+ endOfDataReached = false;
107
+
108
+ /**
109
+ * @inheritdoc
110
+ */
111
+ queryInitialized = false;
112
+
113
+ /**
114
+ * @inheritdoc
115
+ */
116
+ aggregations?: Record<string, Aggregation>;
117
+
118
+ /**
119
+ * @inheritdoc
120
+ */
121
+ histogramAggregation?: Aggregation;
122
+
123
+ /**
124
+ * @inheritdoc
125
+ */
126
+ collectionTitles = new Map<string, string>();
127
+
128
+ /**
129
+ * @inheritdoc
130
+ */
131
+ tvChannelMaps: TVChannelMaps = {};
132
+
133
+ /**
134
+ * @inheritdoc
135
+ */
136
+ tvChannelAliases = new Map<string, string>();
137
+
138
+ /**
139
+ * @inheritdoc
140
+ */
141
+ collectionExtraInfo?: CollectionExtraInfo;
142
+
143
+ /**
144
+ * @inheritdoc
145
+ */
146
+ accountExtraInfo?: AccountExtraInfo;
147
+
148
+ /**
149
+ * @inheritdoc
150
+ */
151
+ sessionContext?: SearchResponseSessionContext;
152
+
153
+ /**
154
+ * @inheritdoc
155
+ */
156
+ pageElements?: PageElementMap;
157
+
158
+ /**
159
+ * @inheritdoc
160
+ */
161
+ parentCollections?: string[] = [];
162
+
163
+ /**
164
+ * @inheritdoc
165
+ */
166
+ prefixFilterCountMap: Partial<Record<PrefixFilterType, PrefixFilterCounts>> =
167
+ {};
168
+
169
+ /**
170
+ * @inheritdoc
171
+ */
172
+ queryErrorMessage?: string;
173
+
174
+ /**
175
+ * Internal property to store the promise for any current TV channel maps fetch.
176
+ */
177
+ private _tvMapsPromise?: Promise<TVChannelMaps>;
178
+
179
+ /**
180
+ * Internal property to store the private value backing the `initialSearchComplete` getter.
181
+ */
182
+ private _initialSearchCompletePromise: Promise<boolean> =
183
+ Promise.resolve(true);
184
+
185
+ /**
186
+ * @inheritdoc
187
+ */
188
+ get initialSearchComplete(): Promise<boolean> {
189
+ return this._initialSearchCompletePromise;
190
+ }
191
+
192
+ constructor(
193
+ /** The host element to which this controller should attach listeners */
194
+ private readonly host: ReactiveControllerHost &
195
+ CollectionBrowserSearchInterface,
196
+ /** Default size of result pages */
197
+ private pageSize: number = 50,
198
+ ) {
199
+ // Just setting some property values
200
+ }
201
+
202
+ hostConnected(): void {
203
+ this.setSearchResultsLoading(this.searchResultsLoading);
204
+ this.setFacetsLoading(this.facetsLoading);
205
+ }
206
+
207
+ hostUpdate(): void {
208
+ // This reactive controller hook is run whenever the host component (collection-browser) performs an update.
209
+ // We check whether the host's state has changed in a way which should trigger a reset & new results fetch.
210
+
211
+ // Only the currently-installed data source should react to the update
212
+ if (!this.activeOnHost) return;
213
+
214
+ // Copy loading states onto the host
215
+ this.setSearchResultsLoading(this.searchResultsLoading);
216
+ this.setFacetsLoading(this.facetsLoading);
217
+
218
+ // Can't perform searches without a search service
219
+ if (!this.host.searchService) return;
220
+
221
+ // We should only reset if part of the full query state has changed
222
+ const queryKeyChanged = this.pageFetchQueryKey !== this.previousQueryKey;
223
+ if (!queryKeyChanged) return;
224
+
225
+ // We should only reset if either:
226
+ // (a) our state permits a valid search, or
227
+ // (b) we have a blank query that we're showing a placeholder/message for
228
+ const queryIsEmpty = !this.host.baseQuery;
229
+ if (!(this.canPerformSearch || queryIsEmpty)) return;
230
+
231
+ if (this.activeOnHost) this.host.emitQueryStateChanged();
232
+ this.handleQueryChange();
233
+ }
234
+
235
+ /**
236
+ * Returns whether this data source is the one currently installed on the host component.
237
+ */
238
+ private get activeOnHost(): boolean {
239
+ return this.host.dataSource === this;
240
+ }
241
+
242
+ /**
243
+ * @inheritdoc
244
+ */
245
+ get size(): number {
246
+ return this.numTileModels;
247
+ }
248
+
249
+ /**
250
+ * @inheritdoc
251
+ */
252
+ reset(): void {
253
+ log('Resetting CB data source');
254
+ this.pages = {};
255
+ this.aggregations = {};
256
+ this.histogramAggregation = undefined;
257
+ this.pageElements = undefined;
258
+ this.parentCollections = [];
259
+ this.previousQueryKey = '';
260
+ this.queryErrorMessage = undefined;
261
+
262
+ this.offset = 0;
263
+ this.numTileModels = 0;
264
+ this.endOfDataReached = false;
265
+ this.queryInitialized = false;
266
+ this.facetsLoading = false;
267
+
268
+ // Invalidate any fetches in progress
269
+ this.fetchesInProgress.clear();
270
+
271
+ this.setTotalResultCount(0);
272
+ this.requestHostUpdate();
273
+ }
274
+
275
+ /**
276
+ * @inheritdoc
277
+ */
278
+ resetPages(): void {
279
+ if (Object.keys(this.pages).length < this.host.maxPagesToManage) {
280
+ this.pages = {};
281
+
282
+ // Invalidate any page fetches in progress (keep facet fetches)
283
+ this.fetchesInProgress.forEach(key => {
284
+ if (!key.startsWith('facets-')) this.fetchesInProgress.delete(key);
285
+ });
286
+ this.requestHostUpdate();
287
+ }
288
+ }
289
+
290
+ /**
291
+ * @inheritdoc
292
+ */
293
+ addPage(pageNum: number, pageTiles: TileModel[]): void {
294
+ this.pages[pageNum] = pageTiles;
295
+ this.numTileModels += pageTiles.length;
296
+ this.requestHostUpdate();
297
+ }
298
+
299
+ /**
300
+ * @inheritdoc
301
+ */
302
+ addMultiplePages(firstPageNum: number, tiles: TileModel[]): void {
303
+ const numPages = Math.ceil(tiles.length / this.pageSize);
304
+ for (let i = 0; i < numPages; i += 1) {
305
+ const pageStartIndex = this.pageSize * i;
306
+ this.addPage(
307
+ firstPageNum + i,
308
+ tiles.slice(pageStartIndex, pageStartIndex + this.pageSize),
309
+ );
310
+ }
311
+
312
+ const visiblePages = this.host.currentVisiblePageNumbers;
313
+ const needsReload = visiblePages.some(
314
+ page => page >= firstPageNum && page <= firstPageNum + numPages,
315
+ );
316
+ if (needsReload) {
317
+ this.refreshVisibleResults();
318
+ }
319
+ }
320
+
321
+ /**
322
+ * @inheritdoc
323
+ */
324
+ getPage(pageNum: number): TileModel[] {
325
+ return this.pages[pageNum];
326
+ }
327
+
328
+ /**
329
+ * @inheritdoc
330
+ */
331
+ getAllPages(): Record<string, TileModel[]> {
332
+ return this.pages;
333
+ }
334
+
335
+ /**
336
+ * @inheritdoc
337
+ */
338
+ hasPage(pageNum: number): boolean {
339
+ return !!this.pages[pageNum];
340
+ }
341
+
342
+ /**
343
+ * @inheritdoc
344
+ */
345
+ getTileModelAt(index: number): TileModel | undefined {
346
+ const offsetIndex = index + this.offset;
347
+ const expectedPageNum = Math.floor(offsetIndex / this.pageSize) + 1;
348
+ const expectedIndexOnPage = offsetIndex % this.pageSize;
349
+
350
+ let page = 1;
351
+ let tilesSeen = 0;
352
+ while (tilesSeen <= offsetIndex) {
353
+ if (!this.pages[page]) {
354
+ // If we encounter a missing page, either we're past all the results or the page data is sparse.
355
+ // So just return the tile at the expected position if it exists.
356
+ return this.pages[expectedPageNum]?.[expectedIndexOnPage];
357
+ }
358
+
359
+ if (tilesSeen + this.pages[page].length > offsetIndex) {
360
+ return this.pages[page][offsetIndex - tilesSeen];
361
+ }
362
+
363
+ tilesSeen += this.pages[page].length;
364
+ page += 1;
365
+ }
366
+
367
+ return this.pages[expectedPageNum]?.[expectedIndexOnPage];
368
+ }
369
+
370
+ /**
371
+ * @inheritdoc
372
+ */
373
+ indexOf(tile: TileModel): number {
374
+ return Object.values(this.pages).flat().indexOf(tile) - this.offset;
375
+ }
376
+
377
+ /**
378
+ * @inheritdoc
379
+ */
380
+ getPageSize(): number {
381
+ return this.pageSize;
382
+ }
383
+
384
+ /**
385
+ * @inheritdoc
386
+ */
387
+ setPageSize(pageSize: number): void {
388
+ this.reset();
389
+ this.pageSize = pageSize;
390
+ }
391
+
392
+ /**
393
+ * @inheritdoc
394
+ */
395
+ setNumInitialPages(numPages: number): void {
396
+ this.numInitialPages = numPages;
397
+ }
398
+
399
+ /**
400
+ * @inheritdoc
401
+ */
402
+ setTotalResultCount(count: number): void {
403
+ this.totalResults = count;
404
+ if (this.activeOnHost) {
405
+ this.host.setTotalResultCount(count);
406
+ }
407
+ }
408
+
409
+ /**
410
+ * @inheritdoc
411
+ */
412
+ setFetchesSuppressed(suppressed: boolean): void {
413
+ this.suppressFetches = suppressed;
414
+ }
415
+
416
+ /**
417
+ * @inheritdoc
418
+ */
419
+ setEndOfDataReached(reached: boolean): void {
420
+ this.endOfDataReached = reached;
421
+ }
422
+
423
+ /**
424
+ * @inheritdoc
425
+ */
426
+ async handleQueryChange(): Promise<void> {
427
+ // Don't react to the change if fetches are suppressed for this data source
428
+ if (this.suppressFetches) return;
429
+
430
+ this.reset();
431
+
432
+ // Reset the `initialSearchComplete` promise with a new value for the imminent search
433
+ let initialSearchCompleteResolver: (value: boolean) => void;
434
+ this._initialSearchCompletePromise = new Promise(res => {
435
+ initialSearchCompleteResolver = res;
436
+ });
437
+
438
+ // Fire the initial page & facet requests
439
+ this.queryInitialized = true;
440
+ await Promise.all([
441
+ this.doInitialPageFetch(),
442
+ this.canFetchFacets ? this.fetchFacets() : null,
443
+ ]);
444
+
445
+ // Resolve the `initialSearchComplete` promise for this search
446
+ initialSearchCompleteResolver!(true);
447
+ }
448
+
449
+ /**
450
+ * @inheritdoc
451
+ */
452
+ async handleFacetReadinessChange(ready: boolean): Promise<void> {
453
+ const facetsBecameReady = !this.facetsReadyToLoad && ready;
454
+ this.facetsReadyToLoad = ready;
455
+
456
+ const needsFetch = facetsBecameReady && this.canFetchFacets;
457
+ if (needsFetch) {
458
+ this.fetchFacets();
459
+ }
460
+ }
461
+
462
+ /**
463
+ * Whether the data source & its host are in a state where a facet request should be fired.
464
+ * (i.e., they aren't suppressed or already loading, etc.)
465
+ */
466
+ private get canFetchFacets(): boolean {
467
+ // Don't fetch facets if they are suppressed entirely or not required for the current profile page element
468
+ if (this.host.facetLoadStrategy === 'off') return false;
469
+ if (FACETLESS_PAGE_ELEMENTS.includes(this.host.profileElement!))
470
+ return false;
471
+
472
+ // If facets are to be lazy-loaded, don't fetch them if they are not going to be visible anyway
473
+ // (wait until they become visible instead)
474
+ if (this.host.facetLoadStrategy !== 'eager' && !this.facetsReadyToLoad)
475
+ return false;
476
+
477
+ // Don't fetch facets again if they are already fetched or pending
478
+ const facetsAlreadyFetched =
479
+ Object.keys(this.aggregations ?? {}).length > 0;
480
+ if (this.facetsLoading || facetsAlreadyFetched) return false;
481
+
482
+ return true;
483
+ }
484
+
485
+ /**
486
+ * @inheritdoc
487
+ */
488
+ map(
489
+ callback: (
490
+ model: TileModel,
491
+ index: number,
492
+ array: TileModel[],
493
+ ) => TileModel,
494
+ ): void {
495
+ if (!Object.keys(this.pages).length) return;
496
+ this.pages = Object.fromEntries(
497
+ Object.entries(this.pages).map(([page, tileModels]) => [
498
+ page,
499
+ tileModels.map((model, index, array) =>
500
+ model ? callback(model, index, array) : model,
501
+ ),
502
+ ]),
503
+ );
504
+ this.requestHostUpdate();
505
+ this.refreshVisibleResults();
506
+ }
507
+
508
+ /**
509
+ * @inheritdoc
510
+ */
511
+ checkAllTiles = (): void => {
512
+ this.map(model => {
513
+ const cloned = model.clone();
514
+ cloned.checked = true;
515
+ return cloned;
516
+ });
517
+ };
518
+
519
+ /**
520
+ * @inheritdoc
521
+ */
522
+ uncheckAllTiles = (): void => {
523
+ this.map(model => {
524
+ const cloned = model.clone();
525
+ cloned.checked = false;
526
+ return cloned;
527
+ });
528
+ };
529
+
530
+ /**
531
+ * @inheritdoc
532
+ */
533
+ removeCheckedTiles = (): void => {
534
+ // To make sure our data source remains page-aligned, we will offset our data source by
535
+ // the number of removed tiles, so that we can just add the offset when the infinite
536
+ // scroller queries for cell contents.
537
+ // This only matters while we're still viewing the same set of results. If the user changes
538
+ // their query/filters/sort, then the data source is overwritten and the offset cleared.
539
+ const { checkedTileModels, uncheckedTileModels } = this;
540
+ const numChecked = checkedTileModels.length;
541
+ if (numChecked === 0) return;
542
+ this.offset += numChecked;
543
+ const newPages: typeof this.pages = {};
544
+
545
+ // Which page the remaining tile models start on, post-offset
546
+ let offsetPageNumber = Math.floor(this.offset / this.pageSize) + 1;
547
+ let indexOnPage = this.offset % this.pageSize;
548
+
549
+ // Fill the pages up to that point with empty models
550
+ for (let page = 1; page <= offsetPageNumber; page += 1) {
551
+ const remainingHidden = this.offset - this.pageSize * (page - 1);
552
+ const offsetCellsOnPage = Math.min(this.pageSize, remainingHidden);
553
+ newPages[page] = Array(offsetCellsOnPage).fill(undefined);
554
+ }
555
+
556
+ // Shift all the remaining tiles into their new positions in the data source
557
+ for (const model of uncheckedTileModels) {
558
+ if (!newPages[offsetPageNumber]) newPages[offsetPageNumber] = [];
559
+ newPages[offsetPageNumber].push(model);
560
+ indexOnPage += 1;
561
+ if (indexOnPage >= this.pageSize) {
562
+ offsetPageNumber += 1;
563
+ indexOnPage = 0;
564
+ }
565
+ }
566
+
567
+ // Swap in the new pages
568
+ this.pages = newPages;
569
+ this.numTileModels -= numChecked;
570
+ this.totalResults -= numChecked;
571
+ this.host.setTileCount(this.size);
572
+ this.host.setTotalResultCount(this.totalResults);
573
+ this.requestHostUpdate();
574
+ this.refreshVisibleResults();
575
+ };
576
+
577
+ /**
578
+ * @inheritdoc
579
+ */
580
+ get checkedTileModels(): TileModel[] {
581
+ return this.getFilteredTileModels(model => model.checked);
582
+ }
583
+
584
+ /**
585
+ * @inheritdoc
586
+ */
587
+ get uncheckedTileModels(): TileModel[] {
588
+ return this.getFilteredTileModels(model => !model.checked);
589
+ }
590
+
591
+ /**
592
+ * Returns a flattened, filtered array of all the tile models in the data source
593
+ * for which the given predicate returns a truthy value.
594
+ *
595
+ * @param predicate A callback function to apply on each tile model, as with Array.filter
596
+ * @returns A filtered array of tile models satisfying the predicate
597
+ */
598
+ private getFilteredTileModels(
599
+ predicate: (model: TileModel, index: number, array: TileModel[]) => unknown,
600
+ ): TileModel[] {
601
+ return Object.values(this.pages)
602
+ .flat()
603
+ .filter((model, index, array) =>
604
+ model ? predicate(model, index, array) : false,
605
+ );
606
+ }
607
+
608
+ /**
609
+ * @inheritdoc
610
+ */
611
+ get canPerformSearch(): boolean {
612
+ if (!this.host.searchService) return false;
613
+
614
+ const trimmedQuery = this.host.baseQuery?.trim();
615
+ const hasNonEmptyQuery = !!trimmedQuery;
616
+ const hasIdentifiers = !!this.host.identifiers?.length;
617
+ const isCollectionSearch = !!this.host.withinCollection;
618
+ const isProfileSearch = !!this.host.withinProfile;
619
+ const hasProfileElement = !!this.host.profileElement;
620
+ const isDefaultedSearch = this.host.searchType === SearchType.DEFAULT;
621
+ const isMetadataSearch = this.host.searchType === SearchType.METADATA;
622
+ const isTvSearch = this.host.searchType === SearchType.TV;
623
+
624
+ // Metadata/tv searches within a collection are allowed to have no query.
625
+ const isValidForCollectionSearch =
626
+ isDefaultedSearch || isMetadataSearch || isTvSearch;
627
+
628
+ // Searches within a profile page may also be performed without a query, provided the profile element is set.
629
+ const isValidForProfileSearch =
630
+ hasProfileElement && (isDefaultedSearch || isMetadataSearch);
631
+
632
+ // Otherwise, a non-empty query must be set.
633
+ return (
634
+ hasNonEmptyQuery ||
635
+ hasIdentifiers ||
636
+ (isCollectionSearch && isValidForCollectionSearch) ||
637
+ (isProfileSearch && isValidForProfileSearch)
638
+ );
639
+ }
640
+
641
+ /**
642
+ * Sets the state for whether the initial set of search results for the
643
+ * current query is loading
644
+ */
645
+ private setSearchResultsLoading(loading: boolean): void {
646
+ this.searchResultsLoading = loading;
647
+ if (this.activeOnHost) {
648
+ this.host.setSearchResultsLoading(loading);
649
+ }
650
+ }
651
+
652
+ /**
653
+ * Sets the state for whether the facets for a query is loading
654
+ */
655
+ private setFacetsLoading(loading: boolean): void {
656
+ this.facetsLoading = loading;
657
+ if (this.activeOnHost) {
658
+ this.host.setFacetsLoading(loading);
659
+ }
660
+ }
661
+
662
+ /**
663
+ * Requests that the host perform an update, provided this data
664
+ * source is actively installed on it.
665
+ */
666
+ private requestHostUpdate(): void {
667
+ if (this.activeOnHost) {
668
+ this.host.requestUpdate();
669
+ }
670
+ }
671
+
672
+ /**
673
+ * Requests that the host refresh its visible tiles, provided this
674
+ * data source is actively installed on it.
675
+ */
676
+ private refreshVisibleResults(): void {
677
+ if (this.activeOnHost) {
678
+ this.host.refreshVisibleResults();
679
+ }
680
+ }
681
+
682
+ /**
683
+ * The query key is a string that uniquely identifies the current search.
684
+ * It consists of:
685
+ * - The current base query
686
+ * - The current collection/profile target & page element
687
+ * - The current search type
688
+ * - Any currently-applied facets
689
+ * - Any currently-applied date range
690
+ * - Any currently-applied prefix filters
691
+ * - The current sort options
692
+ *
693
+ * This lets us internally keep track of queries so we don't persist data that's
694
+ * no longer relevant. Not meant to be human-readable.
695
+ */
696
+ get pageFetchQueryKey(): string {
697
+ const profileKey = `pf;${this.host.withinProfile}--pe;${this.host.profileElement}`;
698
+ const pageTarget = this.host.withinCollection ?? profileKey;
699
+ const sortField = this.host.selectedSort ?? 'none';
700
+ const sortDirection = this.host.sortDirection ?? 'none';
701
+ return `fq:${this.fullQuery}-pt:${pageTarget}-st:${this.host.searchType}-sf:${sortField}-sd:${sortDirection}`;
702
+ }
703
+
704
+ /**
705
+ * Similar to `pageFetchQueryKey` above, but excludes sort fields since they
706
+ * are not relevant in determining aggregation queries.
707
+ */
708
+ get facetFetchQueryKey(): string {
709
+ const profileKey = `pf;${this.host.withinProfile}--pe;${this.host.profileElement}`;
710
+ const pageTarget = this.host.withinCollection ?? profileKey;
711
+ return `facets-fq:${this.fullQuery}-pt:${pageTarget}-st:${this.host.searchType}`;
712
+ }
713
+
714
+ /**
715
+ * Constructs a search service FilterMap object from the combination of
716
+ * all the currently-applied filters. This includes any facets, letter
717
+ * filters, and date range.
718
+ */
719
+ get filterMap(): FilterMap {
720
+ const builder = new FilterMapBuilder();
721
+
722
+ const {
723
+ minSelectedDate,
724
+ maxSelectedDate,
725
+ selectedFacets,
726
+ internalFilters,
727
+ selectedTitleFilter,
728
+ selectedCreatorFilter,
729
+ } = this.host;
730
+
731
+ const dateField = this.host.searchType === SearchType.TV ? 'date' : 'year';
732
+
733
+ if (minSelectedDate) {
734
+ builder.addFilter(
735
+ dateField,
736
+ minSelectedDate,
737
+ FilterConstraint.GREATER_OR_EQUAL,
738
+ );
739
+ }
740
+ if (maxSelectedDate) {
741
+ builder.addFilter(
742
+ dateField,
743
+ maxSelectedDate,
744
+ FilterConstraint.LESS_OR_EQUAL,
745
+ );
746
+ }
747
+
748
+ // Add any selected facets and internal filters
749
+ const combinedFilters = mergeSelectedFacets(
750
+ internalFilters,
751
+ selectedFacets,
752
+ );
753
+ if (combinedFilters) {
754
+ for (const [facetName, facetValues] of Object.entries(combinedFilters)) {
755
+ const { name, values } = this.prepareFacetForFetch(
756
+ facetName,
757
+ facetValues,
758
+ );
759
+ for (const [value, bucket] of Object.entries(values)) {
760
+ let constraint;
761
+ if (bucket.state === 'selected') {
762
+ constraint = FilterConstraint.INCLUDE;
763
+ } else if (bucket.state === 'hidden') {
764
+ constraint = FilterConstraint.EXCLUDE;
765
+ }
766
+
767
+ if (constraint) {
768
+ builder.addFilter(name, value, constraint);
769
+ }
770
+ }
771
+ }
772
+ }
773
+
774
+ // Add any letter filters
775
+ if (selectedTitleFilter) {
776
+ builder.addFilter(
777
+ 'firstTitle',
778
+ selectedTitleFilter,
779
+ FilterConstraint.INCLUDE,
780
+ );
781
+ }
782
+ if (selectedCreatorFilter) {
783
+ builder.addFilter(
784
+ 'firstCreator',
785
+ selectedCreatorFilter,
786
+ FilterConstraint.INCLUDE,
787
+ );
788
+ }
789
+
790
+ const filterMap = builder.build();
791
+ return filterMap;
792
+ }
793
+
794
+ /**
795
+ * Produces a compact unique ID for a search request that can help with debugging
796
+ * on the backend by making related requests easier to trace through different services.
797
+ * (e.g., tying the hits/aggregations requests for the same page back to a single hash).
798
+ *
799
+ * @param params The search service parameters for the request
800
+ * @param kind The kind of request (hits-only, aggregations-only, or both)
801
+ * @returns A Promise resolving to the uid to apply to the request
802
+ */
803
+ async requestUID(params: SearchParams, kind: RequestKind): Promise<string> {
804
+ const paramsToHash = JSON.stringify({
805
+ pageType: params.pageType,
806
+ pageTarget: params.pageTarget,
807
+ query: params.query,
808
+ fields: params.fields,
809
+ filters: params.filters,
810
+ sort: params.sort,
811
+ searchType: this.host.searchType,
812
+ });
813
+
814
+ const fullQueryHash = (await sha1(paramsToHash)).slice(0, 20); // First 80 bits of SHA-1 are plenty for this
815
+ const sessionId = (await this.host.getSessionId()).slice(0, 20); // Likewise
816
+ const page = params.page ?? 0;
817
+ const kindPrefix = kind.charAt(0); // f = full, h = hits, a = aggregations
818
+ const currentTime = Date.now();
819
+
820
+ return `R:${fullQueryHash}-S:${sessionId}-P:${page}-K:${kindPrefix}-T:${currentTime}`;
821
+ }
822
+
823
+ /**
824
+ * @inheritdoc
825
+ */
826
+ get pageSpecifierParams(): PageSpecifierParams | null {
827
+ if (this.host.identifiers?.length) {
828
+ return {
829
+ pageType: 'client_document_fetch',
830
+ };
831
+ }
832
+ if (this.host.withinCollection) {
833
+ return {
834
+ pageType: 'collection_details',
835
+ pageTarget: this.host.withinCollection,
836
+ };
837
+ }
838
+ if (this.host.withinProfile) {
839
+ return {
840
+ pageType: 'account_details',
841
+ pageTarget: this.host.withinProfile,
842
+ pageElements: this.host.profileElement
843
+ ? [this.host.profileElement]
844
+ : [],
845
+ };
846
+ }
847
+ return null;
848
+ }
849
+
850
+ /**
851
+ * The full query, including year facets and date range clauses
852
+ */
853
+ private get fullQuery(): string | undefined {
854
+ const parts = [];
855
+ const trimmedQuery = this.host.baseQuery?.trim();
856
+ if (trimmedQuery) parts.push(trimmedQuery);
857
+
858
+ if (this.host.identifiers) {
859
+ parts.push(`identifier:(${this.host.identifiers.join(' OR ')})`);
860
+ }
861
+
862
+ const { facetQuery, dateRangeQueryClause, sortFilterQueries } = this;
863
+ if (facetQuery) parts.push(facetQuery);
864
+ if (dateRangeQueryClause) parts.push(dateRangeQueryClause);
865
+ if (sortFilterQueries) parts.push(sortFilterQueries);
866
+
867
+ return parts.join(' AND ').trim();
868
+ }
869
+
870
+ /**
871
+ * Generates a query string representing the current set of applied facets
872
+ *
873
+ * Example: `mediatype:("collection" OR "audio" OR -"etree") AND year:("2000" OR "2001")`
874
+ */
875
+ private get facetQuery(): string | undefined {
876
+ if (!this.host.selectedFacets) return undefined;
877
+ const facetClauses = [];
878
+ for (const [facetName, facetValues] of Object.entries(
879
+ this.host.selectedFacets,
880
+ )) {
881
+ facetClauses.push(this.buildFacetClause(facetName, facetValues));
882
+ }
883
+ return this.joinFacetClauses(facetClauses)?.trim();
884
+ }
885
+
886
+ private get dateRangeQueryClause(): string | undefined {
887
+ if (!this.host.minSelectedDate || !this.host.maxSelectedDate) {
888
+ return undefined;
889
+ }
890
+
891
+ return `year:[${this.host.minSelectedDate} TO ${this.host.maxSelectedDate}]`;
892
+ }
893
+
894
+ private get sortFilterQueries(): string {
895
+ const queries = [this.titleQuery, this.creatorQuery];
896
+ return queries.filter(q => q).join(' AND ');
897
+ }
898
+
899
+ /**
900
+ * Returns a query clause identifying the currently selected title filter,
901
+ * e.g., `firstTitle:X`.
902
+ */
903
+ private get titleQuery(): string | undefined {
904
+ return this.host.selectedTitleFilter
905
+ ? `firstTitle:${this.host.selectedTitleFilter}`
906
+ : undefined;
907
+ }
908
+
909
+ /**
910
+ * Returns a query clause identifying the currently selected creator filter,
911
+ * e.g., `firstCreator:X`.
912
+ */
913
+ private get creatorQuery(): string | undefined {
914
+ return this.host.selectedCreatorFilter
915
+ ? `firstCreator:${this.host.selectedCreatorFilter}`
916
+ : undefined;
917
+ }
918
+
919
+ /**
920
+ * Builds an OR-joined facet clause for the given facet name and values.
921
+ *
922
+ * E.g., for name `subject` and values
923
+ * `{ foo: { state: 'selected' }, bar: { state: 'hidden' } }`
924
+ * this will produce the clause
925
+ * `subject:("foo" OR -"bar")`.
926
+ *
927
+ * @param facetName The facet type (e.g., 'collection')
928
+ * @param facetValues The facet buckets, mapped by their keys
929
+ */
930
+ private buildFacetClause(
931
+ facetName: string,
932
+ facetValues: Record<string, FacetBucket>,
933
+ ): string {
934
+ const { name: facetQueryName, values } = this.prepareFacetForFetch(
935
+ facetName,
936
+ facetValues,
937
+ );
938
+ const facetEntries = Object.entries(values);
939
+ if (facetEntries.length === 0) return '';
940
+
941
+ const facetValuesArray: string[] = [];
942
+ for (const [key, facetData] of facetEntries) {
943
+ const plusMinusPrefix = facetData.state === 'hidden' ? '-' : '';
944
+ facetValuesArray.push(`${plusMinusPrefix}"${key}"`);
945
+ }
946
+
947
+ const valueQuery = facetValuesArray.join(` OR `);
948
+ return `${facetQueryName}:(${valueQuery})`;
949
+ }
950
+
951
+ /**
952
+ * Handles some special pre-request normalization steps for certain facet types
953
+ * that require them.
954
+ *
955
+ * @param facetName The name of the facet type (e.g., 'language')
956
+ * @param facetValues An array of values for that facet type
957
+ */
958
+ private prepareFacetForFetch(
959
+ facetName: string,
960
+ facetValues: Record<string, FacetBucket>,
961
+ ): { name: string; values: Record<string, FacetBucket> } {
962
+ // eslint-disable-next-line prefer-const
963
+ let [normalizedName, normalizedValues] = [facetName, facetValues];
964
+
965
+ // The full "search engine" name of the lending field is "lending___status"
966
+ if (facetName === 'lending') {
967
+ normalizedName = 'lending___status';
968
+ }
969
+
970
+ return {
971
+ name: normalizedName,
972
+ values: normalizedValues,
973
+ };
974
+ }
975
+
976
+ /**
977
+ * Takes an array of facet clauses, and combines them into a
978
+ * full AND-joined facet query string. Empty clauses are ignored.
979
+ */
980
+ private joinFacetClauses(facetClauses: string[]): string | undefined {
981
+ const nonEmptyFacetClauses = facetClauses.filter(
982
+ clause => clause.length > 0,
983
+ );
984
+ return nonEmptyFacetClauses.length > 0
985
+ ? `(${nonEmptyFacetClauses.join(' AND ')})`
986
+ : undefined;
987
+ }
988
+
989
+ /**
990
+ * Fires a backend request to fetch a set of aggregations (representing UI facets) for
991
+ * the current search state.
992
+ */
993
+ private async fetchFacets(): Promise<void> {
994
+ const trimmedQuery = this.host.baseQuery?.trim();
995
+ if (!this.canPerformSearch) return;
996
+
997
+ const { facetFetchQueryKey } = this;
998
+ if (this.fetchesInProgress.has(facetFetchQueryKey)) return;
999
+ this.fetchesInProgress.add(facetFetchQueryKey);
1000
+
1001
+ this.setFacetsLoading(true);
1002
+
1003
+ const sortParams = this.host.sortParam ? [this.host.sortParam] : [];
1004
+ const params: SearchParams = {
1005
+ ...this.pageSpecifierParams,
1006
+ query: trimmedQuery || '',
1007
+ identifiers: this.host.identifiers,
1008
+ rows: 0,
1009
+ filters: this.filterMap,
1010
+ // Fetch a few extra buckets beyond the 6 we show, in case some get suppressed
1011
+ aggregationsSize: 10,
1012
+ // Note: we don't need an aggregations param to fetch the default aggregations from the PPS.
1013
+ // The default aggregations for the search_results page type should be what we need here.
1014
+ };
1015
+ params.uid = await this.requestUID(
1016
+ { ...params, sort: sortParams },
1017
+ 'aggregations',
1018
+ );
1019
+
1020
+ const searchResponse = await this.host.searchService?.search(
1021
+ params,
1022
+ this.host.searchType,
1023
+ );
1024
+ const success = searchResponse?.success;
1025
+
1026
+ // This is checking to see if the query has changed since the data was fetched.
1027
+ // If so, we just want to discard this set of aggregations because they are
1028
+ // likely no longer valid for the newer query.
1029
+ const queryChangedSinceFetch =
1030
+ !this.fetchesInProgress.has(facetFetchQueryKey);
1031
+ this.fetchesInProgress.delete(facetFetchQueryKey);
1032
+ if (queryChangedSinceFetch) return;
1033
+
1034
+ if (!success) {
1035
+ const errorMsg = searchResponse?.error?.message;
1036
+ const detailMsg = searchResponse?.error?.details?.message;
1037
+
1038
+ if (!errorMsg && !detailMsg) {
1039
+ // @ts-expect-error: Property 'Sentry' does not exist on type 'Window & typeof globalThis'
1040
+ window?.Sentry?.captureMessage?.(
1041
+ 'Missing or malformed facet response from backend',
1042
+ 'error',
1043
+ );
1044
+ }
1045
+
1046
+ this.setFacetsLoading(false);
1047
+ return;
1048
+ }
1049
+
1050
+ const { aggregations, collectionTitles, tvChannelAliases } =
1051
+ success.response;
1052
+ this.aggregations = aggregations;
1053
+
1054
+ this.histogramAggregation =
1055
+ this.host.searchType === SearchType.TV
1056
+ ? aggregations?.date_histogram
1057
+ : aggregations?.year_histogram;
1058
+
1059
+ if (collectionTitles) {
1060
+ for (const [id, title] of Object.entries(collectionTitles)) {
1061
+ this.collectionTitles.set(id, title);
1062
+ }
1063
+ }
1064
+ if (tvChannelAliases) {
1065
+ for (const [channel, network] of Object.entries(tvChannelAliases)) {
1066
+ this.tvChannelAliases.set(channel, network);
1067
+ }
1068
+ }
1069
+
1070
+ this.setFacetsLoading(false);
1071
+ this.requestHostUpdate();
1072
+ }
1073
+
1074
+ /**
1075
+ * Performs the initial page fetch(es) for the current search state.
1076
+ */
1077
+ private async doInitialPageFetch(): Promise<void> {
1078
+ this.setSearchResultsLoading(true);
1079
+ // Try to batch 2 initial page requests when possible
1080
+ await this.fetchPage(this.host.initialPageNumber, this.numInitialPages);
1081
+ }
1082
+
1083
+ /**
1084
+ * Fetches one or more pages of results and updates the data source.
1085
+ *
1086
+ * @param pageNumber The page number to fetch
1087
+ * @param numInitialPages If this is an initial page fetch (`pageNumber = 1`),
1088
+ * specifies how many pages to batch together in one request. Ignored
1089
+ * if `pageNumber != 1`, defaulting to a single page.
1090
+ */
1091
+ async fetchPage(pageNumber: number, numInitialPages = 1): Promise<void> {
1092
+ const trimmedQuery = this.host.baseQuery?.trim();
1093
+ // reset loading status
1094
+ if (!this.canPerformSearch) {
1095
+ this.setSearchResultsLoading(false);
1096
+ return;
1097
+ }
1098
+
1099
+ // if we already have data, don't fetch again
1100
+ if (this.hasPage(pageNumber)) return;
1101
+
1102
+ if (this.endOfDataReached) return;
1103
+
1104
+ // Batch multiple initial page requests together if needed (e.g., can request
1105
+ // pages 1 and 2 together in a single request).
1106
+ let numPages = pageNumber === 1 ? numInitialPages : 1;
1107
+ const numRows = this.pageSize * numPages;
1108
+
1109
+ // if a fetch is already in progress for this query and page, don't fetch again
1110
+ const { pageFetchQueryKey } = this;
1111
+ const currentPageKey = `${pageFetchQueryKey}-p:${pageNumber}`;
1112
+ if (this.fetchesInProgress.has(currentPageKey)) return;
1113
+
1114
+ for (let i = 0; i < numPages; i += 1) {
1115
+ this.fetchesInProgress.add(`${pageFetchQueryKey}-p:${pageNumber + i}`);
1116
+ }
1117
+ this.previousQueryKey = pageFetchQueryKey;
1118
+
1119
+ const { withinCollection, withinProfile } = this.host;
1120
+
1121
+ let sortParams = this.host.sortParam ? [this.host.sortParam] : [];
1122
+ // TODO eventually the PPS should handle these defaults natively
1123
+ const isDefaultProfileSort =
1124
+ withinProfile && this.host.selectedSort === SortField.default;
1125
+ if (isDefaultProfileSort && this.host.defaultSortField) {
1126
+ const sortOption = SORT_OPTIONS[this.host.defaultSortField];
1127
+ if (sortOption.searchServiceKey) {
1128
+ sortParams = [
1129
+ {
1130
+ field: sortOption.searchServiceKey,
1131
+ direction: 'desc',
1132
+ },
1133
+ ];
1134
+ }
1135
+ }
1136
+
1137
+ const params: SearchParams = {
1138
+ ...this.pageSpecifierParams,
1139
+ query: trimmedQuery || '',
1140
+ identifiers: this.host.identifiers,
1141
+ page: pageNumber,
1142
+ rows: numRows,
1143
+ sort: sortParams,
1144
+ filters: this.filterMap,
1145
+ aggregations: { omit: true },
1146
+ };
1147
+ params.uid = await this.requestUID(params, 'hits');
1148
+
1149
+ // log('=== FIRING PAGE REQUEST ===', params);
1150
+ const searchResponse = await this.host.searchService?.search(
1151
+ params,
1152
+ this.host.searchType,
1153
+ );
1154
+ // log('=== RECEIVED PAGE RESPONSE IN CB ===', searchResponse);
1155
+ const success = searchResponse?.success;
1156
+
1157
+ // This is checking to see if the fetch has been invalidated since it was fired off.
1158
+ // If so, we just want to discard the response since it is for an obsolete query state.
1159
+ if (!this.fetchesInProgress.has(currentPageKey)) return;
1160
+ for (let i = 0; i < numPages; i += 1) {
1161
+ this.fetchesInProgress.delete(`${pageFetchQueryKey}-p:${pageNumber + i}`);
1162
+ }
1163
+
1164
+ if (!success) {
1165
+ const errorMsg = searchResponse?.error?.message;
1166
+ const detailMsg = searchResponse?.error?.details?.message;
1167
+
1168
+ this.queryErrorMessage = `${errorMsg ?? ''}${
1169
+ detailMsg ? `; ${detailMsg}` : ''
1170
+ }`;
1171
+
1172
+ if (!this.queryErrorMessage) {
1173
+ this.queryErrorMessage = 'Missing or malformed response from backend';
1174
+ // @ts-expect-error: Property 'Sentry' does not exist on type 'Window & typeof globalThis'
1175
+ window?.Sentry?.captureMessage?.(this.queryErrorMessage, 'error');
1176
+ }
1177
+
1178
+ this.setSearchResultsLoading(false);
1179
+ this.requestHostUpdate();
1180
+ this.host.emitSearchError();
1181
+ return;
1182
+ }
1183
+
1184
+ this.setTotalResultCount(success.response.totalResults - this.offset);
1185
+ if (this.activeOnHost && this.totalResults === 0) {
1186
+ // display event to offshoot when result count is zero.
1187
+ this.host.emitEmptyResults();
1188
+ }
1189
+
1190
+ this.sessionContext = success.sessionContext;
1191
+ if (withinCollection) {
1192
+ this.collectionExtraInfo = success.response.collectionExtraInfo;
1193
+
1194
+ // For collections, we want the UI to respect the default sort option
1195
+ // which can be specified in metadata, or otherwise assumed to be week:desc
1196
+ if (this.activeOnHost) {
1197
+ this.host.applyDefaultCollectionSort(this.collectionExtraInfo);
1198
+ }
1199
+
1200
+ if (this.collectionExtraInfo) {
1201
+ this.parentCollections = [].concat(
1202
+ this.collectionExtraInfo.public_metadata?.collection ?? [],
1203
+ );
1204
+
1205
+ // Update the TV collection status now that we know the parent collections
1206
+ this.host.isTVCollection =
1207
+ this.host.withinCollection?.startsWith('TV-') ||
1208
+ this.host.withinCollection === 'tvnews' ||
1209
+ this.host.withinCollection === 'tvarchive' ||
1210
+ this.parentCollections.includes('tvnews') ||
1211
+ this.parentCollections.includes('tvarchive');
1212
+ }
1213
+ } else if (withinProfile) {
1214
+ this.accountExtraInfo = success.response.accountExtraInfo;
1215
+ this.pageElements = success.response.pageElements;
1216
+ }
1217
+
1218
+ const { results, collectionTitles, tvChannelAliases } = success.response;
1219
+ if (results && results.length > 0) {
1220
+ // Load any collection titles present on the response into the cache,
1221
+ // or queue up preload fetches for them if none were present.
1222
+ if (collectionTitles) {
1223
+ for (const [id, title] of Object.entries(collectionTitles)) {
1224
+ this.collectionTitles.set(id, title);
1225
+ }
1226
+
1227
+ // Also add the target collection's title if available
1228
+ const targetTitle = this.collectionExtraInfo?.public_metadata?.title;
1229
+ if (withinCollection && targetTitle) {
1230
+ this.collectionTitles.set(withinCollection, targetTitle);
1231
+ }
1232
+ }
1233
+
1234
+ if (tvChannelAliases) {
1235
+ for (const [channel, network] of Object.entries(tvChannelAliases)) {
1236
+ this.tvChannelAliases.set(channel, network);
1237
+ }
1238
+ }
1239
+
1240
+ // Update the data source for each returned page.
1241
+ // For loans and web archives, we must account for receiving more pages than we asked for.
1242
+ const isUnpagedElement = ['lending', 'web_archives'].includes(
1243
+ this.host.profileElement!,
1244
+ );
1245
+ if (isUnpagedElement) {
1246
+ numPages = Math.ceil(results.length / this.pageSize);
1247
+ this.endOfDataReached = true;
1248
+ if (this.activeOnHost) this.host.setTileCount(this.totalResults);
1249
+ }
1250
+
1251
+ for (let i = 0; i < numPages; i += 1) {
1252
+ const pageStartIndex = this.pageSize * i;
1253
+ this.addFetchedResultsToDataSource(
1254
+ pageNumber + i,
1255
+ results.slice(pageStartIndex, pageStartIndex + this.pageSize),
1256
+ !isUnpagedElement || i === numPages - 1,
1257
+ );
1258
+ }
1259
+ }
1260
+
1261
+ // When we reach the end of the data, we can set the infinite scroller's
1262
+ // item count to the real total number of results (rather than the
1263
+ // temporary estimates based on pages rendered so far).
1264
+ if (this.size >= this.totalResults || results.length === 0) {
1265
+ this.endOfDataReached = true;
1266
+ if (this.activeOnHost) this.host.setTileCount(this.size);
1267
+ }
1268
+
1269
+ this.setSearchResultsLoading(false);
1270
+ this.requestHostUpdate();
1271
+ }
1272
+
1273
+ /**
1274
+ * Returns the type of request that produced the current set of hits,
1275
+ * based on the presence of a search query or profile/collection target
1276
+ * on the host.
1277
+ */
1278
+ private get hitRequestSource(): HitRequestSource {
1279
+ const { host } = this;
1280
+ if (host.baseQuery) return 'search_query';
1281
+ if (host.withinProfile) return 'profile_tab';
1282
+ if (host.withinCollection) return 'collection_members';
1283
+ return 'unknown';
1284
+ }
1285
+
1286
+ /**
1287
+ * Update the datasource from the fetch response
1288
+ *
1289
+ * @param pageNumber
1290
+ * @param results
1291
+ */
1292
+ private addFetchedResultsToDataSource(
1293
+ pageNumber: number,
1294
+ results: SearchResult[],
1295
+ needsReload = true,
1296
+ ): void {
1297
+ const tiles: TileModel[] = [];
1298
+ const requestSource = this.hitRequestSource;
1299
+ results?.forEach(result => {
1300
+ if (!result.identifier) return;
1301
+ tiles.push(new TileModel(result, requestSource));
1302
+ });
1303
+
1304
+ this.addPage(pageNumber, tiles);
1305
+
1306
+ if (needsReload) {
1307
+ this.refreshVisibleResults();
1308
+ }
1309
+ }
1310
+
1311
+ /**
1312
+ * Fetches the aggregation buckets for the given prefix filter type.
1313
+ */
1314
+ private async fetchPrefixFilterBuckets(
1315
+ filterType: PrefixFilterType,
1316
+ ): Promise<Bucket[]> {
1317
+ const trimmedQuery = this.host.baseQuery?.trim();
1318
+ if (!this.canPerformSearch) return [];
1319
+
1320
+ const filterAggregationKey = prefixFilterAggregationKeys[filterType];
1321
+ const sortParams = this.host.sortParam ? [this.host.sortParam] : [];
1322
+
1323
+ const params: SearchParams = {
1324
+ ...this.pageSpecifierParams,
1325
+ query: trimmedQuery || '',
1326
+ identifiers: this.host.identifiers,
1327
+ rows: 0,
1328
+ filters: this.filterMap,
1329
+ // Only fetch the firstTitle or firstCreator aggregation
1330
+ aggregations: { simpleParams: [filterAggregationKey] },
1331
+ // Fetch all 26 letter buckets
1332
+ aggregationsSize: 26,
1333
+ };
1334
+ params.uid = await this.requestUID(
1335
+ { ...params, sort: sortParams },
1336
+ 'aggregations',
1337
+ );
1338
+
1339
+ const searchResponse = await this.host.searchService?.search(
1340
+ params,
1341
+ this.host.searchType,
1342
+ );
1343
+
1344
+ return (searchResponse?.success?.response?.aggregations?.[
1345
+ filterAggregationKey
1346
+ ]?.buckets ?? []) as Bucket[];
1347
+ }
1348
+
1349
+ /**
1350
+ * Fetches and caches the prefix filter counts for the given filter type.
1351
+ */
1352
+ async updatePrefixFilterCounts(filterType: PrefixFilterType): Promise<void> {
1353
+ const { facetFetchQueryKey } = this;
1354
+ const buckets = await this.fetchPrefixFilterBuckets(filterType);
1355
+
1356
+ // Don't update the filter counts for an outdated query (if it has been changed
1357
+ // since we sent the request)
1358
+ const queryChangedSinceFetch =
1359
+ facetFetchQueryKey !== this.facetFetchQueryKey;
1360
+ if (queryChangedSinceFetch) return;
1361
+
1362
+ // Unpack the aggregation buckets into a simple map like { 'A': 50, 'B': 25, ... }
1363
+ this.prefixFilterCountMap = { ...this.prefixFilterCountMap }; // Clone the object to trigger an update
1364
+ this.prefixFilterCountMap[filterType] = buckets.reduce(
1365
+ (acc: Record<string, number>, bucket: Bucket) => {
1366
+ acc[(bucket.key as string).toUpperCase()] = bucket.doc_count;
1367
+ return acc;
1368
+ },
1369
+ {},
1370
+ );
1371
+
1372
+ this.requestHostUpdate();
1373
+ }
1374
+
1375
+ /**
1376
+ * @inheritdoc
1377
+ */
1378
+ async updatePrefixFiltersForCurrentSort(): Promise<void> {
1379
+ if (['title', 'creator'].includes(this.host.selectedSort as SortField)) {
1380
+ const filterType = this.host.selectedSort as PrefixFilterType;
1381
+ if (!this.prefixFilterCountMap[filterType]) {
1382
+ this.updatePrefixFilterCounts(filterType);
1383
+ }
1384
+ }
1385
+ }
1386
+
1387
+ /**
1388
+ * @inheritdoc
1389
+ */
1390
+ refreshLetterCounts(): void {
1391
+ if (Object.keys(this.prefixFilterCountMap).length > 0) {
1392
+ this.prefixFilterCountMap = {};
1393
+ }
1394
+ this.updatePrefixFiltersForCurrentSort();
1395
+ this.requestHostUpdate();
1396
+ }
1397
+
1398
+ /**
1399
+ * @inheritdoc
1400
+ */
1401
+ populateTVChannelMaps(): Promise<TVChannelMaps> {
1402
+ // To ensure that we only make these requests once, cache the Promise returned by the
1403
+ // first call, and return the same Promise on repeated calls.
1404
+ // Resolves once both maps have been retrieved and saved in the data source.
1405
+ if (!this._tvMapsPromise) {
1406
+ this._tvMapsPromise = this._fetchTVChannelMaps();
1407
+ }
1408
+
1409
+ return this._tvMapsPromise;
1410
+ }
1411
+
1412
+ /**
1413
+ * Internal function implementing the actual fetches for TV channel mappings.
1414
+ * This should only called by the public populateTVChannelMaps method, which is guarded so
1415
+ * that we do not make extra requests for these rather large mappings.
1416
+ */
1417
+ private async _fetchTVChannelMaps(): Promise<TVChannelMaps> {
1418
+ const baseURL = 'https://av.archive.org/etc';
1419
+ const dateStr = new Date().toISOString().slice(0, 10); // YYYY-MM-DD
1420
+ const chan2networkPromise = fetch(
1421
+ `${baseURL}/chan2network.json?date=${dateStr}`,
1422
+ );
1423
+ const program2chansPromise = fetch(
1424
+ `${baseURL}/program2chans.json?date=${dateStr}`,
1425
+ );
1426
+
1427
+ const [chan2networkResponse, program2chansResponse] = await Promise.all([
1428
+ chan2networkPromise,
1429
+ program2chansPromise,
1430
+ ]);
1431
+ this.tvChannelMaps.channelToNetwork = new Map(
1432
+ Object.entries(await chan2networkResponse.json()),
1433
+ );
1434
+ this.tvChannelMaps.programToChannels = new Map(
1435
+ Object.entries(await program2chansResponse.json()),
1436
+ );
1437
+
1438
+ this.requestHostUpdate();
1439
+ return this.tvChannelMaps;
1440
+ }
1441
+ }