@internetarchive/collection-browser 3.3.2 → 3.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (532) 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 +684 -0
  127. package/dist/src/collection-browser.js +2567 -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 +265 -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 +872 -0
  191. package/dist/src/collection-facets.js.map +1 -0
  192. package/dist/src/data-source/collection-browser-data-source-interface.d.ts +261 -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 +400 -0
  196. package/dist/src/data-source/collection-browser-data-source.js +1103 -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 +49 -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 +32 -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 +302 -0
  226. package/dist/src/models.js +511 -0
  227. package/dist/src/models.js.map +1 -0
  228. package/dist/src/restoration-state-handler.d.ts +75 -0
  229. package/dist/src/restoration-state-handler.js +403 -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 +1711 -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 +375 -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 +2829 -2829
  508. package/src/collection-facets/more-facets-content.ts +639 -639
  509. package/src/collection-facets.ts +994 -995
  510. package/src/data-source/collection-browser-data-source.ts +1401 -1401
  511. package/src/data-source/collection-browser-query-state.ts +65 -65
  512. package/src/data-source/models.ts +43 -43
  513. package/src/tiles/base-tile-component.ts +65 -65
  514. package/src/tiles/grid/account-tile.ts +113 -113
  515. package/src/tiles/grid/collection-tile.ts +163 -163
  516. package/src/tiles/grid/item-tile.ts +340 -340
  517. package/src/tiles/hover/hover-pane-controller.ts +613 -517
  518. package/src/tiles/hover/tile-hover-pane.ts +184 -180
  519. package/src/tiles/list/tile-list-compact.ts +239 -239
  520. package/src/tiles/list/tile-list.ts +700 -700
  521. package/src/tiles/tile-dispatcher.ts +517 -490
  522. package/src/utils/format-date.ts +62 -62
  523. package/test/collection-browser.test.ts +2403 -2403
  524. package/test/tiles/grid/item-tile.test.ts +520 -520
  525. package/test/tiles/hover/hover-pane-controller.test.ts +418 -353
  526. package/test/tiles/list/tile-list-compact.test.ts +282 -282
  527. package/test/tiles/list/tile-list.test.ts +552 -552
  528. package/test/tiles/tile-dispatcher.test.ts +283 -187
  529. package/test/utils/format-date.test.ts +89 -89
  530. package/tsconfig.json +20 -20
  531. package/web-dev-server.config.mjs +30 -30
  532. package/web-test-runner.config.mjs +41 -41
@@ -1,1401 +1,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 { 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 { 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
+ }