@internetarchive/collection-browser 2.7.12 → 2.7.13

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 (318) hide show
  1. package/.editorconfig +29 -29
  2. package/.husky/pre-commit +4 -4
  3. package/LICENSE +661 -661
  4. package/README.md +83 -83
  5. package/dist/index.d.ts +13 -13
  6. package/dist/index.js +13 -13
  7. package/dist/src/app-root.d.ts +107 -107
  8. package/dist/src/app-root.js +531 -531
  9. package/dist/src/assets/img/icons/arrow-left.d.ts +2 -2
  10. package/dist/src/assets/img/icons/arrow-left.js +2 -2
  11. package/dist/src/assets/img/icons/arrow-right.d.ts +2 -2
  12. package/dist/src/assets/img/icons/arrow-right.js +2 -2
  13. package/dist/src/assets/img/icons/chevron.d.ts +2 -2
  14. package/dist/src/assets/img/icons/chevron.js +2 -2
  15. package/dist/src/assets/img/icons/contract.d.ts +2 -2
  16. package/dist/src/assets/img/icons/contract.js +2 -2
  17. package/dist/src/assets/img/icons/empty-query.d.ts +2 -2
  18. package/dist/src/assets/img/icons/empty-query.js +2 -2
  19. package/dist/src/assets/img/icons/expand.d.ts +2 -2
  20. package/dist/src/assets/img/icons/expand.js +2 -2
  21. package/dist/src/assets/img/icons/eye-closed.d.ts +2 -2
  22. package/dist/src/assets/img/icons/eye-closed.js +2 -2
  23. package/dist/src/assets/img/icons/eye.d.ts +2 -2
  24. package/dist/src/assets/img/icons/eye.js +2 -2
  25. package/dist/src/assets/img/icons/favorite-filled.d.ts +1 -1
  26. package/dist/src/assets/img/icons/favorite-filled.js +2 -2
  27. package/dist/src/assets/img/icons/favorite-unfilled.d.ts +1 -1
  28. package/dist/src/assets/img/icons/favorite-unfilled.js +2 -2
  29. package/dist/src/assets/img/icons/filter.d.ts +2 -2
  30. package/dist/src/assets/img/icons/filter.js +2 -2
  31. package/dist/src/assets/img/icons/login-required.d.ts +1 -1
  32. package/dist/src/assets/img/icons/login-required.js +2 -2
  33. package/dist/src/assets/img/icons/mediatype/account.d.ts +1 -1
  34. package/dist/src/assets/img/icons/mediatype/account.js +2 -2
  35. package/dist/src/assets/img/icons/mediatype/audio.d.ts +1 -1
  36. package/dist/src/assets/img/icons/mediatype/audio.js +2 -2
  37. package/dist/src/assets/img/icons/mediatype/collection.d.ts +1 -1
  38. package/dist/src/assets/img/icons/mediatype/collection.js +2 -2
  39. package/dist/src/assets/img/icons/mediatype/data.d.ts +1 -1
  40. package/dist/src/assets/img/icons/mediatype/data.js +2 -2
  41. package/dist/src/assets/img/icons/mediatype/etree.d.ts +1 -1
  42. package/dist/src/assets/img/icons/mediatype/etree.js +2 -2
  43. package/dist/src/assets/img/icons/mediatype/film.d.ts +1 -1
  44. package/dist/src/assets/img/icons/mediatype/film.js +2 -2
  45. package/dist/src/assets/img/icons/mediatype/images.d.ts +1 -1
  46. package/dist/src/assets/img/icons/mediatype/images.js +2 -2
  47. package/dist/src/assets/img/icons/mediatype/radio.d.ts +1 -1
  48. package/dist/src/assets/img/icons/mediatype/radio.js +2 -2
  49. package/dist/src/assets/img/icons/mediatype/search.d.ts +1 -1
  50. package/dist/src/assets/img/icons/mediatype/search.js +2 -2
  51. package/dist/src/assets/img/icons/mediatype/software.d.ts +1 -1
  52. package/dist/src/assets/img/icons/mediatype/software.js +2 -2
  53. package/dist/src/assets/img/icons/mediatype/texts.d.ts +1 -1
  54. package/dist/src/assets/img/icons/mediatype/texts.js +2 -2
  55. package/dist/src/assets/img/icons/mediatype/tv.d.ts +1 -1
  56. package/dist/src/assets/img/icons/mediatype/tv.js +2 -2
  57. package/dist/src/assets/img/icons/mediatype/video.d.ts +1 -1
  58. package/dist/src/assets/img/icons/mediatype/video.js +2 -2
  59. package/dist/src/assets/img/icons/mediatype/web.d.ts +1 -1
  60. package/dist/src/assets/img/icons/mediatype/web.js +2 -2
  61. package/dist/src/assets/img/icons/null-result.d.ts +2 -2
  62. package/dist/src/assets/img/icons/null-result.js +2 -2
  63. package/dist/src/assets/img/icons/restricted.d.ts +1 -1
  64. package/dist/src/assets/img/icons/restricted.js +2 -2
  65. package/dist/src/assets/img/icons/reviews.d.ts +1 -1
  66. package/dist/src/assets/img/icons/reviews.js +2 -2
  67. package/dist/src/assets/img/icons/upload.d.ts +1 -1
  68. package/dist/src/assets/img/icons/upload.js +2 -2
  69. package/dist/src/assets/img/icons/views.d.ts +1 -1
  70. package/dist/src/assets/img/icons/views.js +2 -2
  71. package/dist/src/circular-activity-indicator.d.ts +5 -5
  72. package/dist/src/circular-activity-indicator.js +17 -17
  73. package/dist/src/collection-browser.d.ts +606 -606
  74. package/dist/src/collection-browser.js +1677 -1677
  75. package/dist/src/collection-facets/facet-row.d.ts +30 -30
  76. package/dist/src/collection-facets/facet-row.js +118 -118
  77. package/dist/src/collection-facets/facet-tombstone-row.d.ts +5 -5
  78. package/dist/src/collection-facets/facet-tombstone-row.js +15 -15
  79. package/dist/src/collection-facets/facets-template.d.ts +13 -13
  80. package/dist/src/collection-facets/facets-template.js +44 -44
  81. package/dist/src/collection-facets/more-facets-content.d.ts +106 -106
  82. package/dist/src/collection-facets/more-facets-content.js +407 -407
  83. package/dist/src/collection-facets/more-facets-pagination.d.ts +36 -36
  84. package/dist/src/collection-facets/more-facets-pagination.js +196 -196
  85. package/dist/src/collection-facets/smart-facets/dedupe.d.ts +10 -10
  86. package/dist/src/collection-facets/smart-facets/dedupe.js +33 -33
  87. package/dist/src/collection-facets/smart-facets/heuristics/browser-language-heuristic.d.ts +5 -5
  88. package/dist/src/collection-facets/smart-facets/heuristics/browser-language-heuristic.js +23 -23
  89. package/dist/src/collection-facets/smart-facets/heuristics/query-keywords-heuristic.d.ts +5 -5
  90. package/dist/src/collection-facets/smart-facets/heuristics/query-keywords-heuristic.js +44 -44
  91. package/dist/src/collection-facets/smart-facets/heuristics/wikidata-heuristic.d.ts +5 -5
  92. package/dist/src/collection-facets/smart-facets/heuristics/wikidata-heuristic.js +172 -172
  93. package/dist/src/collection-facets/smart-facets/models.d.ts +26 -26
  94. package/dist/src/collection-facets/smart-facets/models.js +1 -1
  95. package/dist/src/collection-facets/smart-facets/smart-facet-bar.d.ts +31 -31
  96. package/dist/src/collection-facets/smart-facets/smart-facet-bar.js +240 -240
  97. package/dist/src/collection-facets/smart-facets/smart-facet-button.d.ts +11 -11
  98. package/dist/src/collection-facets/smart-facets/smart-facet-button.js +86 -86
  99. package/dist/src/collection-facets/smart-facets/smart-facet-dropdown.d.ts +16 -16
  100. package/dist/src/collection-facets/smart-facets/smart-facet-dropdown.js +100 -100
  101. package/dist/src/collection-facets/smart-facets/smart-facet-equals.d.ts +2 -2
  102. package/dist/src/collection-facets/smart-facets/smart-facet-equals.js +12 -12
  103. package/dist/src/collection-facets/smart-facets/smart-facet-heuristics.d.ts +5 -5
  104. package/dist/src/collection-facets/smart-facets/smart-facet-heuristics.js +15 -15
  105. package/dist/src/collection-facets/toggle-switch.d.ts +41 -41
  106. package/dist/src/collection-facets/toggle-switch.js +94 -94
  107. package/dist/src/collection-facets.d.ts +103 -103
  108. package/dist/src/collection-facets.js +522 -522
  109. package/dist/src/data-source/collection-browser-data-source-interface.d.ts +245 -245
  110. package/dist/src/data-source/collection-browser-data-source-interface.js +1 -1
  111. package/dist/src/data-source/collection-browser-data-source.d.ts +377 -377
  112. package/dist/src/data-source/collection-browser-data-source.js +1004 -1001
  113. package/dist/src/data-source/collection-browser-data-source.js.map +1 -1
  114. package/dist/src/data-source/collection-browser-query-state.d.ts +44 -44
  115. package/dist/src/data-source/collection-browser-query-state.js +1 -1
  116. package/dist/src/data-source/models.d.ts +28 -28
  117. package/dist/src/data-source/models.js +8 -8
  118. package/dist/src/empty-placeholder.d.ts +23 -23
  119. package/dist/src/empty-placeholder.js +79 -79
  120. package/dist/src/expanded-date-picker.d.ts +43 -43
  121. package/dist/src/expanded-date-picker.js +109 -109
  122. package/dist/src/language-code-handler/language-code-handler.d.ts +37 -37
  123. package/dist/src/language-code-handler/language-code-handler.js +26 -26
  124. package/dist/src/language-code-handler/language-code-mapping.d.ts +1 -1
  125. package/dist/src/language-code-handler/language-code-mapping.js +562 -562
  126. package/dist/src/manage/manage-bar.d.ts +58 -58
  127. package/dist/src/manage/manage-bar.js +159 -159
  128. package/dist/src/manage/remove-items-modal-content.d.ts +9 -9
  129. package/dist/src/manage/remove-items-modal-content.js +34 -34
  130. package/dist/src/mediatype/mediatype-config.d.ts +3 -3
  131. package/dist/src/mediatype/mediatype-config.js +91 -91
  132. package/dist/src/models.d.ts +228 -228
  133. package/dist/src/models.js +401 -401
  134. package/dist/src/restoration-state-handler.d.ts +70 -70
  135. package/dist/src/restoration-state-handler.js +363 -363
  136. package/dist/src/sort-filter-bar/alpha-bar-tooltip.d.ts +6 -6
  137. package/dist/src/sort-filter-bar/alpha-bar-tooltip.js +24 -24
  138. package/dist/src/sort-filter-bar/alpha-bar.d.ts +21 -21
  139. package/dist/src/sort-filter-bar/alpha-bar.js +128 -128
  140. package/dist/src/sort-filter-bar/img/compact.d.ts +1 -1
  141. package/dist/src/sort-filter-bar/img/compact.js +2 -2
  142. package/dist/src/sort-filter-bar/img/list.d.ts +1 -1
  143. package/dist/src/sort-filter-bar/img/list.js +2 -2
  144. package/dist/src/sort-filter-bar/img/sort-toggle-disabled.d.ts +1 -1
  145. package/dist/src/sort-filter-bar/img/sort-toggle-disabled.js +2 -2
  146. package/dist/src/sort-filter-bar/img/sort-toggle-down.d.ts +1 -1
  147. package/dist/src/sort-filter-bar/img/sort-toggle-down.js +2 -2
  148. package/dist/src/sort-filter-bar/img/sort-toggle-up.d.ts +1 -1
  149. package/dist/src/sort-filter-bar/img/sort-toggle-up.js +2 -2
  150. package/dist/src/sort-filter-bar/img/sort-triangle.d.ts +1 -1
  151. package/dist/src/sort-filter-bar/img/sort-triangle.js +2 -2
  152. package/dist/src/sort-filter-bar/img/tile.d.ts +1 -1
  153. package/dist/src/sort-filter-bar/img/tile.js +2 -2
  154. package/dist/src/sort-filter-bar/sort-filter-bar.d.ts +223 -223
  155. package/dist/src/sort-filter-bar/sort-filter-bar.js +697 -697
  156. package/dist/src/styles/ia-button.d.ts +2 -2
  157. package/dist/src/styles/ia-button.js +17 -17
  158. package/dist/src/styles/item-image-styles.d.ts +8 -8
  159. package/dist/src/styles/item-image-styles.js +9 -9
  160. package/dist/src/styles/sr-only.d.ts +1 -1
  161. package/dist/src/styles/sr-only.js +2 -2
  162. package/dist/src/tiles/base-tile-component.d.ts +19 -19
  163. package/dist/src/tiles/base-tile-component.js +64 -64
  164. package/dist/src/tiles/collection-browser-loading-tile.d.ts +5 -5
  165. package/dist/src/tiles/collection-browser-loading-tile.js +15 -15
  166. package/dist/src/tiles/grid/account-tile.d.ts +18 -18
  167. package/dist/src/tiles/grid/account-tile.js +72 -72
  168. package/dist/src/tiles/grid/collection-tile.d.ts +15 -15
  169. package/dist/src/tiles/grid/collection-tile.js +80 -80
  170. package/dist/src/tiles/grid/item-tile.d.ts +34 -34
  171. package/dist/src/tiles/grid/item-tile.js +158 -158
  172. package/dist/src/tiles/grid/search-tile.d.ts +10 -10
  173. package/dist/src/tiles/grid/search-tile.js +51 -51
  174. package/dist/src/tiles/grid/styles/tile-grid-shared-styles.d.ts +1 -1
  175. package/dist/src/tiles/grid/styles/tile-grid-shared-styles.js +8 -8
  176. package/dist/src/tiles/grid/tile-stats.d.ts +11 -11
  177. package/dist/src/tiles/grid/tile-stats.js +53 -53
  178. package/dist/src/tiles/hover/hover-pane-controller.d.ts +219 -219
  179. package/dist/src/tiles/hover/hover-pane-controller.js +352 -352
  180. package/dist/src/tiles/hover/tile-hover-pane.d.ts +18 -18
  181. package/dist/src/tiles/hover/tile-hover-pane.js +69 -69
  182. package/dist/src/tiles/image-block.d.ts +18 -18
  183. package/dist/src/tiles/image-block.js +89 -89
  184. package/dist/src/tiles/item-image.d.ts +39 -39
  185. package/dist/src/tiles/item-image.js +154 -154
  186. package/dist/src/tiles/list/tile-list-compact-header.d.ts +6 -6
  187. package/dist/src/tiles/list/tile-list-compact-header.js +38 -38
  188. package/dist/src/tiles/list/tile-list-compact.d.ts +19 -19
  189. package/dist/src/tiles/list/tile-list-compact.js +122 -122
  190. package/dist/src/tiles/list/tile-list.d.ts +54 -54
  191. package/dist/src/tiles/list/tile-list.js +323 -323
  192. package/dist/src/tiles/mediatype-icon.d.ts +9 -9
  193. package/dist/src/tiles/mediatype-icon.js +47 -47
  194. package/dist/src/tiles/overlay/icon-overlay.d.ts +8 -8
  195. package/dist/src/tiles/overlay/icon-overlay.js +25 -25
  196. package/dist/src/tiles/overlay/text-overlay.d.ts +9 -9
  197. package/dist/src/tiles/overlay/text-overlay.js +31 -31
  198. package/dist/src/tiles/review-block.d.ts +12 -12
  199. package/dist/src/tiles/review-block.js +56 -56
  200. package/dist/src/tiles/text-snippet-block.d.ts +27 -27
  201. package/dist/src/tiles/text-snippet-block.js +73 -73
  202. package/dist/src/tiles/tile-dispatcher.d.ts +64 -64
  203. package/dist/src/tiles/tile-dispatcher.js +230 -230
  204. package/dist/src/tiles/tile-display-value-provider.d.ts +47 -47
  205. package/dist/src/tiles/tile-display-value-provider.js +94 -94
  206. package/dist/src/utils/analytics-events.d.ts +28 -28
  207. package/dist/src/utils/analytics-events.js +30 -30
  208. package/dist/src/utils/array-equals.d.ts +4 -4
  209. package/dist/src/utils/array-equals.js +10 -10
  210. package/dist/src/utils/collapse-repeated-quotes.d.ts +11 -11
  211. package/dist/src/utils/collapse-repeated-quotes.js +13 -13
  212. package/dist/src/utils/facet-utils.d.ts +83 -83
  213. package/dist/src/utils/facet-utils.js +145 -145
  214. package/dist/src/utils/format-count.d.ts +7 -7
  215. package/dist/src/utils/format-count.js +76 -76
  216. package/dist/src/utils/format-date.d.ts +2 -2
  217. package/dist/src/utils/format-date.js +27 -27
  218. package/dist/src/utils/format-unit-size.d.ts +2 -2
  219. package/dist/src/utils/format-unit-size.js +33 -33
  220. package/dist/src/utils/local-date-from-utc.d.ts +9 -9
  221. package/dist/src/utils/local-date-from-utc.js +15 -15
  222. package/dist/src/utils/log.d.ts +7 -7
  223. package/dist/src/utils/log.js +15 -15
  224. package/dist/src/utils/resolve-mediatype.d.ts +8 -8
  225. package/dist/src/utils/resolve-mediatype.js +23 -23
  226. package/dist/src/utils/sha1.d.ts +2 -2
  227. package/dist/src/utils/sha1.js +8 -8
  228. package/dist/test/collection-browser.test.d.ts +1 -1
  229. package/dist/test/collection-browser.test.js +1308 -1308
  230. package/dist/test/collection-facets/facet-row.test.d.ts +1 -1
  231. package/dist/test/collection-facets/facet-row.test.js +227 -227
  232. package/dist/test/collection-facets/facets-template.test.d.ts +1 -1
  233. package/dist/test/collection-facets/facets-template.test.js +91 -91
  234. package/dist/test/collection-facets/more-facets-content.test.d.ts +1 -1
  235. package/dist/test/collection-facets/more-facets-content.test.js +141 -141
  236. package/dist/test/collection-facets/more-facets-pagination.test.d.ts +1 -1
  237. package/dist/test/collection-facets/more-facets-pagination.test.js +117 -117
  238. package/dist/test/collection-facets/toggle-switch.test.d.ts +1 -1
  239. package/dist/test/collection-facets/toggle-switch.test.js +73 -73
  240. package/dist/test/collection-facets.test.d.ts +2 -2
  241. package/dist/test/collection-facets.test.js +690 -690
  242. package/dist/test/data-source/collection-browser-data-source.test.d.ts +1 -1
  243. package/dist/test/data-source/collection-browser-data-source.test.js +89 -89
  244. package/dist/test/empty-placeholder.test.d.ts +1 -1
  245. package/dist/test/empty-placeholder.test.js +63 -63
  246. package/dist/test/expanded-date-picker.test.d.ts +1 -1
  247. package/dist/test/expanded-date-picker.test.js +95 -95
  248. package/dist/test/icon-overlay.test.d.ts +1 -1
  249. package/dist/test/icon-overlay.test.js +24 -24
  250. package/dist/test/image-block.test.d.ts +1 -1
  251. package/dist/test/image-block.test.js +107 -107
  252. package/dist/test/item-image.test.d.ts +1 -1
  253. package/dist/test/item-image.test.js +85 -85
  254. package/dist/test/manage/manage-bar.test.d.ts +2 -2
  255. package/dist/test/manage/manage-bar.test.js +100 -100
  256. package/dist/test/manage/remove-items-modal-content.test.d.ts +1 -1
  257. package/dist/test/manage/remove-items-modal-content.test.js +45 -45
  258. package/dist/test/mediatype-config.test.d.ts +1 -1
  259. package/dist/test/mediatype-config.test.js +16 -16
  260. package/dist/test/mocks/mock-analytics-handler.d.ts +10 -10
  261. package/dist/test/mocks/mock-analytics-handler.js +15 -15
  262. package/dist/test/mocks/mock-search-responses.d.ts +25 -25
  263. package/dist/test/mocks/mock-search-responses.js +942 -942
  264. package/dist/test/mocks/mock-search-service.d.ts +15 -15
  265. package/dist/test/mocks/mock-search-service.js +54 -54
  266. package/dist/test/restoration-state-handler.test.d.ts +1 -1
  267. package/dist/test/restoration-state-handler.test.js +270 -270
  268. package/dist/test/review-block.test.d.ts +1 -1
  269. package/dist/test/review-block.test.js +44 -44
  270. package/dist/test/sort-filter-bar/alpha-bar-tooltip.test.d.ts +1 -1
  271. package/dist/test/sort-filter-bar/alpha-bar-tooltip.test.js +12 -12
  272. package/dist/test/sort-filter-bar/alpha-bar.test.d.ts +1 -1
  273. package/dist/test/sort-filter-bar/alpha-bar.test.js +73 -73
  274. package/dist/test/sort-filter-bar/sort-filter-bar.test.d.ts +1 -1
  275. package/dist/test/sort-filter-bar/sort-filter-bar.test.js +426 -426
  276. package/dist/test/text-overlay.test.d.ts +1 -1
  277. package/dist/test/text-overlay.test.js +38 -38
  278. package/dist/test/text-snippet-block.test.d.ts +1 -1
  279. package/dist/test/text-snippet-block.test.js +57 -57
  280. package/dist/test/tile-stats.test.d.ts +1 -1
  281. package/dist/test/tile-stats.test.js +81 -81
  282. package/dist/test/tiles/grid/account-tile.test.d.ts +1 -1
  283. package/dist/test/tiles/grid/account-tile.test.js +76 -76
  284. package/dist/test/tiles/grid/collection-tile.test.d.ts +1 -1
  285. package/dist/test/tiles/grid/collection-tile.test.js +73 -73
  286. package/dist/test/tiles/grid/item-tile.test.d.ts +1 -1
  287. package/dist/test/tiles/grid/item-tile.test.js +312 -312
  288. package/dist/test/tiles/grid/search-tile.test.d.ts +1 -1
  289. package/dist/test/tiles/grid/search-tile.test.js +51 -51
  290. package/dist/test/tiles/hover/hover-pane-controller.test.d.ts +1 -1
  291. package/dist/test/tiles/hover/hover-pane-controller.test.js +259 -259
  292. package/dist/test/tiles/hover/tile-hover-pane.test.d.ts +1 -1
  293. package/dist/test/tiles/hover/tile-hover-pane.test.js +56 -56
  294. package/dist/test/tiles/list/tile-list-compact.test.d.ts +1 -1
  295. package/dist/test/tiles/list/tile-list-compact.test.js +143 -143
  296. package/dist/test/tiles/list/tile-list.test.d.ts +1 -1
  297. package/dist/test/tiles/list/tile-list.test.js +297 -297
  298. package/dist/test/tiles/tile-dispatcher.test.d.ts +1 -1
  299. package/dist/test/tiles/tile-dispatcher.test.js +100 -100
  300. package/dist/test/tiles/tile-display-value-provider.test.d.ts +1 -1
  301. package/dist/test/tiles/tile-display-value-provider.test.js +141 -141
  302. package/dist/test/utils/array-equals.test.d.ts +1 -1
  303. package/dist/test/utils/array-equals.test.js +26 -26
  304. package/dist/test/utils/format-count.test.d.ts +1 -1
  305. package/dist/test/utils/format-count.test.js +23 -23
  306. package/dist/test/utils/format-date.test.d.ts +1 -1
  307. package/dist/test/utils/format-date.test.js +30 -30
  308. package/dist/test/utils/format-unit-size.test.d.ts +1 -1
  309. package/dist/test/utils/format-unit-size.test.js +17 -17
  310. package/dist/test/utils/local-date-from-utc.test.d.ts +1 -1
  311. package/dist/test/utils/local-date-from-utc.test.js +26 -26
  312. package/local.archive.org.cert +86 -86
  313. package/local.archive.org.key +27 -27
  314. package/package.json +1 -1
  315. package/renovate.json +6 -6
  316. package/src/data-source/collection-browser-data-source.ts +5 -1
  317. package/web-dev-server.config.mjs +30 -30
  318. package/web-test-runner.config.mjs +41 -41
@@ -1,1002 +1,1005 @@
1
- import { FilterConstraint, FilterMapBuilder, SearchType, } from '@internetarchive/search-service';
2
- import { prefixFilterAggregationKeys, TileModel, SortField, SORT_OPTIONS, } from '../models';
3
- import { FACETLESS_PAGE_ELEMENTS } from './models';
4
- import { sha1 } from '../utils/sha1';
5
- import { log } from '../utils/log';
6
- export class CollectionBrowserDataSource {
7
- // eslint-disable-next-line no-useless-constructor
8
- constructor(
9
- /** The host element to which this controller should attach listeners */
10
- host,
11
- /** Default size of result pages */
12
- pageSize = 50) {
13
- this.host = host;
14
- this.pageSize = pageSize;
15
- /**
16
- * All pages of tile models that have been fetched so far, indexed by their page
17
- * number (with the first being page 1).
18
- */
19
- this.pages = {};
20
- /**
21
- * Tile offset to apply when looking up tiles in the pages, in order to maintain
22
- * page alignment after tiles are removed.
23
- */
24
- this.offset = 0;
25
- /**
26
- * Total number of tile models stored in this data source's pages
27
- */
28
- this.numTileModels = 0;
29
- /**
30
- * A set of fetch IDs that are valid for the current query state
31
- */
32
- this.fetchesInProgress = new Set();
33
- /**
34
- * A record of the query key used for the last search.
35
- * If this changes, we need to load new results.
36
- */
37
- this.previousQueryKey = '';
38
- /**
39
- * Whether the initial page of search results for the current query state
40
- * is presently being fetched.
41
- */
42
- this.searchResultsLoading = false;
43
- /**
44
- * Whether the facets (aggregations) for the current query state are
45
- * presently being fetched.
46
- */
47
- this.facetsLoading = false;
48
- /**
49
- * Whether the facets are actually visible -- if not, then we can delay any facet
50
- * fetches until they become visible.
51
- */
52
- this.facetsReadyToLoad = false;
53
- /**
54
- * Whether further query changes should be ignored and not trigger fetches
55
- */
56
- this.suppressFetches = false;
57
- /**
58
- * @inheritdoc
59
- */
60
- this.totalResults = 0;
61
- /**
62
- * @inheritdoc
63
- */
64
- this.endOfDataReached = false;
65
- /**
66
- * @inheritdoc
67
- */
68
- this.queryInitialized = false;
69
- /**
70
- * @inheritdoc
71
- */
72
- this.collectionTitles = new Map();
73
- /**
74
- * @inheritdoc
75
- */
76
- this.parentCollections = [];
77
- /**
78
- * @inheritdoc
79
- */
80
- this.prefixFilterCountMap = {};
81
- /**
82
- * Internal property to store the private value backing the `initialSearchComplete` getter.
83
- */
84
- this._initialSearchCompletePromise = Promise.resolve(true);
85
- /**
86
- * @inheritdoc
87
- */
88
- this.checkAllTiles = () => {
89
- this.map(model => {
90
- const cloned = model.clone();
91
- cloned.checked = true;
92
- return cloned;
93
- });
94
- };
95
- /**
96
- * @inheritdoc
97
- */
98
- this.uncheckAllTiles = () => {
99
- this.map(model => {
100
- const cloned = model.clone();
101
- cloned.checked = false;
102
- return cloned;
103
- });
104
- };
105
- /**
106
- * @inheritdoc
107
- */
108
- this.removeCheckedTiles = () => {
109
- // To make sure our data source remains page-aligned, we will offset our data source by
110
- // the number of removed tiles, so that we can just add the offset when the infinite
111
- // scroller queries for cell contents.
112
- // This only matters while we're still viewing the same set of results. If the user changes
113
- // their query/filters/sort, then the data source is overwritten and the offset cleared.
114
- const { checkedTileModels, uncheckedTileModels } = this;
115
- const numChecked = checkedTileModels.length;
116
- if (numChecked === 0)
117
- return;
118
- this.offset += numChecked;
119
- const newPages = {};
120
- // Which page the remaining tile models start on, post-offset
121
- let offsetPageNumber = Math.floor(this.offset / this.pageSize) + 1;
122
- let indexOnPage = this.offset % this.pageSize;
123
- // Fill the pages up to that point with empty models
124
- for (let page = 1; page <= offsetPageNumber; page += 1) {
125
- const remainingHidden = this.offset - this.pageSize * (page - 1);
126
- const offsetCellsOnPage = Math.min(this.pageSize, remainingHidden);
127
- newPages[page] = Array(offsetCellsOnPage).fill(undefined);
128
- }
129
- // Shift all the remaining tiles into their new positions in the data source
130
- for (const model of uncheckedTileModels) {
131
- if (!newPages[offsetPageNumber])
132
- newPages[offsetPageNumber] = [];
133
- newPages[offsetPageNumber].push(model);
134
- indexOnPage += 1;
135
- if (indexOnPage >= this.pageSize) {
136
- offsetPageNumber += 1;
137
- indexOnPage = 0;
138
- }
139
- }
140
- // Swap in the new pages
141
- this.pages = newPages;
142
- this.numTileModels -= numChecked;
143
- this.totalResults -= numChecked;
144
- this.host.setTileCount(this.size);
145
- this.host.setTotalResultCount(this.totalResults);
146
- this.requestHostUpdate();
147
- this.refreshVisibleResults();
148
- };
149
- // Just setting some property values
150
- }
151
- /**
152
- * @inheritdoc
153
- */
154
- get initialSearchComplete() {
155
- return this._initialSearchCompletePromise;
156
- }
157
- hostConnected() {
158
- this.setSearchResultsLoading(this.searchResultsLoading);
159
- this.setFacetsLoading(this.facetsLoading);
160
- }
161
- hostUpdate() {
162
- // This reactive controller hook is run whenever the host component (collection-browser) performs an update.
163
- // We check whether the host's state has changed in a way which should trigger a reset & new results fetch.
164
- // Only the currently-installed data source should react to the update
165
- if (!this.activeOnHost)
166
- return;
167
- // Copy loading states onto the host
168
- this.setSearchResultsLoading(this.searchResultsLoading);
169
- this.setFacetsLoading(this.facetsLoading);
170
- // Can't perform searches without a search service
171
- if (!this.host.searchService)
172
- return;
173
- // We should only reset if part of the full query state has changed
174
- const queryKeyChanged = this.pageFetchQueryKey !== this.previousQueryKey;
175
- if (!queryKeyChanged)
176
- return;
177
- // We should only reset if either:
178
- // (a) our state permits a valid search, or
179
- // (b) we have a blank query that we're showing a placeholder/message for
180
- const queryIsEmpty = !this.host.baseQuery;
181
- if (!(this.canPerformSearch || queryIsEmpty))
182
- return;
183
- if (this.activeOnHost)
184
- this.host.emitQueryStateChanged();
185
- this.handleQueryChange();
186
- }
187
- /**
188
- * Returns whether this data source is the one currently installed on the host component.
189
- */
190
- get activeOnHost() {
191
- return this.host.dataSource === this;
192
- }
193
- /**
194
- * @inheritdoc
195
- */
196
- get size() {
197
- return this.numTileModels;
198
- }
199
- /**
200
- * @inheritdoc
201
- */
202
- reset() {
203
- log('Resetting CB data source');
204
- this.pages = {};
205
- this.aggregations = {};
206
- this.yearHistogramAggregation = undefined;
207
- this.pageElements = undefined;
208
- this.parentCollections = [];
209
- this.previousQueryKey = '';
210
- this.queryErrorMessage = undefined;
211
- this.offset = 0;
212
- this.numTileModels = 0;
213
- this.endOfDataReached = false;
214
- this.queryInitialized = false;
215
- this.facetsLoading = false;
216
- // Invalidate any fetches in progress
217
- this.fetchesInProgress.clear();
218
- this.setTotalResultCount(0);
219
- this.requestHostUpdate();
220
- }
221
- /**
222
- * @inheritdoc
223
- */
224
- resetPages() {
225
- if (Object.keys(this.pages).length < this.host.maxPagesToManage) {
226
- this.pages = {};
227
- // Invalidate any fetches in progress
228
- this.fetchesInProgress.clear();
229
- this.requestHostUpdate();
230
- }
231
- }
232
- /**
233
- * @inheritdoc
234
- */
235
- addPage(pageNum, pageTiles) {
236
- this.pages[pageNum] = pageTiles;
237
- this.numTileModels += pageTiles.length;
238
- this.requestHostUpdate();
239
- }
240
- /**
241
- * @inheritdoc
242
- */
243
- addMultiplePages(firstPageNum, tiles) {
244
- const numPages = Math.ceil(tiles.length / this.pageSize);
245
- for (let i = 0; i < numPages; i += 1) {
246
- const pageStartIndex = this.pageSize * i;
247
- this.addPage(firstPageNum + i, tiles.slice(pageStartIndex, pageStartIndex + this.pageSize));
248
- }
249
- const visiblePages = this.host.currentVisiblePageNumbers;
250
- const needsReload = visiblePages.some(page => page >= firstPageNum && page <= firstPageNum + numPages);
251
- if (needsReload) {
252
- this.refreshVisibleResults();
253
- }
254
- }
255
- /**
256
- * @inheritdoc
257
- */
258
- getPage(pageNum) {
259
- return this.pages[pageNum];
260
- }
261
- /**
262
- * @inheritdoc
263
- */
264
- getAllPages() {
265
- return this.pages;
266
- }
267
- /**
268
- * @inheritdoc
269
- */
270
- hasPage(pageNum) {
271
- return !!this.pages[pageNum];
272
- }
273
- /**
274
- * @inheritdoc
275
- */
276
- getTileModelAt(index) {
277
- var _a, _b;
278
- const offsetIndex = index + this.offset;
279
- const expectedPageNum = Math.floor(offsetIndex / this.pageSize) + 1;
280
- const expectedIndexOnPage = offsetIndex % this.pageSize;
281
- let page = 1;
282
- let tilesSeen = 0;
283
- while (tilesSeen <= offsetIndex) {
284
- if (!this.pages[page]) {
285
- // If we encounter a missing page, either we're past all the results or the page data is sparse.
286
- // So just return the tile at the expected position if it exists.
287
- return (_a = this.pages[expectedPageNum]) === null || _a === void 0 ? void 0 : _a[expectedIndexOnPage];
288
- }
289
- if (tilesSeen + this.pages[page].length > offsetIndex) {
290
- return this.pages[page][offsetIndex - tilesSeen];
291
- }
292
- tilesSeen += this.pages[page].length;
293
- page += 1;
294
- }
295
- return (_b = this.pages[expectedPageNum]) === null || _b === void 0 ? void 0 : _b[expectedIndexOnPage];
296
- }
297
- /**
298
- * @inheritdoc
299
- */
300
- indexOf(tile) {
301
- return Object.values(this.pages).flat().indexOf(tile) - this.offset;
302
- }
303
- /**
304
- * @inheritdoc
305
- */
306
- getPageSize() {
307
- return this.pageSize;
308
- }
309
- /**
310
- * @inheritdoc
311
- */
312
- setPageSize(pageSize) {
313
- this.reset();
314
- this.pageSize = pageSize;
315
- }
316
- /**
317
- * @inheritdoc
318
- */
319
- setTotalResultCount(count) {
320
- this.totalResults = count;
321
- if (this.activeOnHost) {
322
- this.host.setTotalResultCount(count);
323
- }
324
- }
325
- /**
326
- * @inheritdoc
327
- */
328
- setFetchesSuppressed(suppressed) {
329
- this.suppressFetches = suppressed;
330
- }
331
- /**
332
- * @inheritdoc
333
- */
334
- async handleQueryChange() {
335
- // Don't react to the change if fetches are suppressed for this data source
336
- if (this.suppressFetches)
337
- return;
338
- this.reset();
339
- // Reset the `initialSearchComplete` promise with a new value for the imminent search
340
- let initialSearchCompleteResolver;
341
- this._initialSearchCompletePromise = new Promise(res => {
342
- initialSearchCompleteResolver = res;
343
- });
344
- // Fire the initial page & facet requests
345
- this.queryInitialized = true;
346
- await Promise.all([
347
- this.doInitialPageFetch(),
348
- this.canFetchFacets ? this.fetchFacets() : null,
349
- ]);
350
- // Resolve the `initialSearchComplete` promise for this search
351
- initialSearchCompleteResolver(true);
352
- }
353
- /**
354
- * @inheritdoc
355
- */
356
- async handleFacetReadinessChange(ready) {
357
- const facetsBecameReady = !this.facetsReadyToLoad && ready;
358
- this.facetsReadyToLoad = ready;
359
- const needsFetch = facetsBecameReady && this.canFetchFacets;
360
- if (needsFetch) {
361
- this.fetchFacets();
362
- }
363
- }
364
- /**
365
- * Whether the data source & its host are in a state where a facet request should be fired.
366
- * (i.e., they aren't suppressed or already loading, etc.)
367
- */
368
- get canFetchFacets() {
369
- var _a;
370
- // Don't fetch facets if they are suppressed entirely or not required for the current profile page element
371
- if (this.host.facetLoadStrategy === 'off')
372
- return false;
373
- if (FACETLESS_PAGE_ELEMENTS.includes(this.host.profileElement))
374
- return false;
375
- // If facets are to be lazy-loaded, don't fetch them if they are not going to be visible anyway
376
- // (wait until they become visible instead)
377
- if (this.host.facetLoadStrategy !== 'eager' && !this.facetsReadyToLoad)
378
- return false;
379
- // Don't fetch facets again if they are already fetched or pending
380
- const facetsAlreadyFetched = Object.keys((_a = this.aggregations) !== null && _a !== void 0 ? _a : {}).length > 0;
381
- if (this.facetsLoading || facetsAlreadyFetched)
382
- return false;
383
- return true;
384
- }
385
- /**
386
- * @inheritdoc
387
- */
388
- map(callback) {
389
- if (!Object.keys(this.pages).length)
390
- return;
391
- this.pages = Object.fromEntries(Object.entries(this.pages).map(([page, tileModels]) => [
392
- page,
393
- tileModels.map((model, index, array) => model ? callback(model, index, array) : model),
394
- ]));
395
- this.requestHostUpdate();
396
- this.refreshVisibleResults();
397
- }
398
- /**
399
- * @inheritdoc
400
- */
401
- get checkedTileModels() {
402
- return this.getFilteredTileModels(model => model.checked);
403
- }
404
- /**
405
- * @inheritdoc
406
- */
407
- get uncheckedTileModels() {
408
- return this.getFilteredTileModels(model => !model.checked);
409
- }
410
- /**
411
- * Returns a flattened, filtered array of all the tile models in the data source
412
- * for which the given predicate returns a truthy value.
413
- *
414
- * @param predicate A callback function to apply on each tile model, as with Array.filter
415
- * @returns A filtered array of tile models satisfying the predicate
416
- */
417
- getFilteredTileModels(predicate) {
418
- return Object.values(this.pages)
419
- .flat()
420
- .filter((model, index, array) => model ? predicate(model, index, array) : false);
421
- }
422
- /**
423
- * @inheritdoc
424
- */
425
- get canPerformSearch() {
426
- var _a;
427
- if (!this.host.searchService)
428
- return false;
429
- const trimmedQuery = (_a = this.host.baseQuery) === null || _a === void 0 ? void 0 : _a.trim();
430
- const hasNonEmptyQuery = !!trimmedQuery;
431
- const isCollectionSearch = !!this.host.withinCollection;
432
- const isProfileSearch = !!this.host.withinProfile;
433
- const hasProfileElement = !!this.host.profileElement;
434
- const isMetadataSearch = this.host.searchType === SearchType.METADATA;
435
- // Metadata searches within a collection/profile are allowed to have no query.
436
- // Otherwise, a non-empty query must be set.
437
- return (hasNonEmptyQuery ||
438
- (isCollectionSearch && isMetadataSearch) ||
439
- (isProfileSearch && hasProfileElement && isMetadataSearch));
440
- }
441
- /**
442
- * Sets the state for whether the initial set of search results for the
443
- * current query is loading
444
- */
445
- setSearchResultsLoading(loading) {
446
- this.searchResultsLoading = loading;
447
- if (this.activeOnHost) {
448
- this.host.setSearchResultsLoading(loading);
449
- }
450
- }
451
- /**
452
- * Sets the state for whether the facets for a query is loading
453
- */
454
- setFacetsLoading(loading) {
455
- this.facetsLoading = loading;
456
- if (this.activeOnHost) {
457
- this.host.setFacetsLoading(loading);
458
- }
459
- }
460
- /**
461
- * Requests that the host perform an update, provided this data
462
- * source is actively installed on it.
463
- */
464
- requestHostUpdate() {
465
- if (this.activeOnHost) {
466
- this.host.requestUpdate();
467
- }
468
- }
469
- /**
470
- * Requests that the host refresh its visible tiles, provided this
471
- * data source is actively installed on it.
472
- */
473
- refreshVisibleResults() {
474
- if (this.activeOnHost) {
475
- this.host.refreshVisibleResults();
476
- }
477
- }
478
- /**
479
- * The query key is a string that uniquely identifies the current search.
480
- * It consists of:
481
- * - The current base query
482
- * - The current collection/profile target & page element
483
- * - The current search type
484
- * - Any currently-applied facets
485
- * - Any currently-applied date range
486
- * - Any currently-applied prefix filters
487
- * - The current sort options
488
- *
489
- * This lets us internally keep track of queries so we don't persist data that's
490
- * no longer relevant. Not meant to be human-readable.
491
- */
492
- get pageFetchQueryKey() {
493
- var _a, _b, _c;
494
- const profileKey = `pf;${this.host.withinProfile}--pe;${this.host.profileElement}`;
495
- const pageTarget = (_a = this.host.withinCollection) !== null && _a !== void 0 ? _a : profileKey;
496
- const sortField = (_b = this.host.selectedSort) !== null && _b !== void 0 ? _b : 'none';
497
- const sortDirection = (_c = this.host.sortDirection) !== null && _c !== void 0 ? _c : 'none';
498
- return `fq:${this.fullQuery}-pt:${pageTarget}-st:${this.host.searchType}-sf:${sortField}-sd:${sortDirection}`;
499
- }
500
- /**
501
- * Similar to `pageFetchQueryKey` above, but excludes sort fields since they
502
- * are not relevant in determining aggregation queries.
503
- */
504
- get facetFetchQueryKey() {
505
- var _a;
506
- const profileKey = `pf;${this.host.withinProfile}--pe;${this.host.profileElement}`;
507
- const pageTarget = (_a = this.host.withinCollection) !== null && _a !== void 0 ? _a : profileKey;
508
- return `fq:${this.fullQuery}-pt:${pageTarget}-st:${this.host.searchType}`;
509
- }
510
- /**
511
- * Constructs a search service FilterMap object from the combination of
512
- * all the currently-applied filters. This includes any facets, letter
513
- * filters, and date range.
514
- */
515
- get filterMap() {
516
- const builder = new FilterMapBuilder();
517
- // Add the date range, if applicable
518
- if (this.host.minSelectedDate) {
519
- builder.addFilter('year', this.host.minSelectedDate, FilterConstraint.GREATER_OR_EQUAL);
520
- }
521
- if (this.host.maxSelectedDate) {
522
- builder.addFilter('year', this.host.maxSelectedDate, FilterConstraint.LESS_OR_EQUAL);
523
- }
524
- // Add any selected facets
525
- if (this.host.selectedFacets) {
526
- for (const [facetName, facetValues] of Object.entries(this.host.selectedFacets)) {
527
- const { name, values } = this.prepareFacetForFetch(facetName, facetValues);
528
- for (const [value, bucket] of Object.entries(values)) {
529
- let constraint;
530
- if (bucket.state === 'selected') {
531
- constraint = FilterConstraint.INCLUDE;
532
- }
533
- else if (bucket.state === 'hidden') {
534
- constraint = FilterConstraint.EXCLUDE;
535
- }
536
- if (constraint) {
537
- builder.addFilter(name, value, constraint);
538
- }
539
- }
540
- }
541
- }
542
- // Add any letter filters
543
- if (this.host.selectedTitleFilter) {
544
- builder.addFilter('firstTitle', this.host.selectedTitleFilter, FilterConstraint.INCLUDE);
545
- }
546
- if (this.host.selectedCreatorFilter) {
547
- builder.addFilter('firstCreator', this.host.selectedCreatorFilter, FilterConstraint.INCLUDE);
548
- }
549
- const filterMap = builder.build();
550
- return filterMap;
551
- }
552
- /**
553
- * Produces a compact unique ID for a search request that can help with debugging
554
- * on the backend by making related requests easier to trace through different services.
555
- * (e.g., tying the hits/aggregations requests for the same page back to a single hash).
556
- *
557
- * @param params The search service parameters for the request
558
- * @param kind The kind of request (hits-only, aggregations-only, or both)
559
- * @returns A Promise resolving to the uid to apply to the request
560
- */
561
- async requestUID(params, kind) {
562
- var _a;
563
- const paramsToHash = JSON.stringify({
564
- pageType: params.pageType,
565
- pageTarget: params.pageTarget,
566
- query: params.query,
567
- fields: params.fields,
568
- filters: params.filters,
569
- sort: params.sort,
570
- searchType: this.host.searchType,
571
- });
572
- const fullQueryHash = (await sha1(paramsToHash)).slice(0, 20); // First 80 bits of SHA-1 are plenty for this
573
- const sessionId = (await this.host.getSessionId()).slice(0, 20); // Likewise
574
- const page = (_a = params.page) !== null && _a !== void 0 ? _a : 0;
575
- const kindPrefix = kind.charAt(0); // f = full, h = hits, a = aggregations
576
- const currentTime = Date.now();
577
- return `R:${fullQueryHash}-S:${sessionId}-P:${page}-K:${kindPrefix}-T:${currentTime}`;
578
- }
579
- /**
580
- * @inheritdoc
581
- */
582
- get pageSpecifierParams() {
583
- if (this.host.withinCollection) {
584
- return {
585
- pageType: 'collection_details',
586
- pageTarget: this.host.withinCollection,
587
- };
588
- }
589
- if (this.host.withinProfile) {
590
- return {
591
- pageType: 'account_details',
592
- pageTarget: this.host.withinProfile,
593
- pageElements: this.host.profileElement
594
- ? [this.host.profileElement]
595
- : [],
596
- };
597
- }
598
- return null;
599
- }
600
- /**
601
- * The full query, including year facets and date range clauses
602
- */
603
- get fullQuery() {
604
- var _a, _b;
605
- let fullQuery = (_b = (_a = this.host.baseQuery) === null || _a === void 0 ? void 0 : _a.trim()) !== null && _b !== void 0 ? _b : '';
606
- const { facetQuery, dateRangeQueryClause, sortFilterQueries } = this;
607
- if (facetQuery) {
608
- fullQuery += ` AND ${facetQuery}`;
609
- }
610
- if (dateRangeQueryClause) {
611
- fullQuery += ` AND ${dateRangeQueryClause}`;
612
- }
613
- if (sortFilterQueries) {
614
- fullQuery += ` AND ${sortFilterQueries}`;
615
- }
616
- return fullQuery.trim();
617
- }
618
- /**
619
- * Generates a query string representing the current set of applied facets
620
- *
621
- * Example: `mediatype:("collection" OR "audio" OR -"etree") AND year:("2000" OR "2001")`
622
- */
623
- get facetQuery() {
624
- var _a;
625
- if (!this.host.selectedFacets)
626
- return undefined;
627
- const facetClauses = [];
628
- for (const [facetName, facetValues] of Object.entries(this.host.selectedFacets)) {
629
- facetClauses.push(this.buildFacetClause(facetName, facetValues));
630
- }
631
- return (_a = this.joinFacetClauses(facetClauses)) === null || _a === void 0 ? void 0 : _a.trim();
632
- }
633
- get dateRangeQueryClause() {
634
- if (!this.host.minSelectedDate || !this.host.maxSelectedDate) {
635
- return undefined;
636
- }
637
- return `year:[${this.host.minSelectedDate} TO ${this.host.maxSelectedDate}]`;
638
- }
639
- get sortFilterQueries() {
640
- const queries = [this.titleQuery, this.creatorQuery];
641
- return queries.filter(q => q).join(' AND ');
642
- }
643
- /**
644
- * Returns a query clause identifying the currently selected title filter,
645
- * e.g., `firstTitle:X`.
646
- */
647
- get titleQuery() {
648
- return this.host.selectedTitleFilter
649
- ? `firstTitle:${this.host.selectedTitleFilter}`
650
- : undefined;
651
- }
652
- /**
653
- * Returns a query clause identifying the currently selected creator filter,
654
- * e.g., `firstCreator:X`.
655
- */
656
- get creatorQuery() {
657
- return this.host.selectedCreatorFilter
658
- ? `firstCreator:${this.host.selectedCreatorFilter}`
659
- : undefined;
660
- }
661
- /**
662
- * Builds an OR-joined facet clause for the given facet name and values.
663
- *
664
- * E.g., for name `subject` and values
665
- * `{ foo: { state: 'selected' }, bar: { state: 'hidden' } }`
666
- * this will produce the clause
667
- * `subject:("foo" OR -"bar")`.
668
- *
669
- * @param facetName The facet type (e.g., 'collection')
670
- * @param facetValues The facet buckets, mapped by their keys
671
- */
672
- buildFacetClause(facetName, facetValues) {
673
- const { name: facetQueryName, values } = this.prepareFacetForFetch(facetName, facetValues);
674
- const facetEntries = Object.entries(values);
675
- if (facetEntries.length === 0)
676
- return '';
677
- const facetValuesArray = [];
678
- for (const [key, facetData] of facetEntries) {
679
- const plusMinusPrefix = facetData.state === 'hidden' ? '-' : '';
680
- facetValuesArray.push(`${plusMinusPrefix}"${key}"`);
681
- }
682
- const valueQuery = facetValuesArray.join(` OR `);
683
- return `${facetQueryName}:(${valueQuery})`;
684
- }
685
- /**
686
- * Handles some special pre-request normalization steps for certain facet types
687
- * that require them.
688
- *
689
- * @param facetName The name of the facet type (e.g., 'language')
690
- * @param facetValues An array of values for that facet type
691
- */
692
- prepareFacetForFetch(facetName, facetValues) {
693
- // eslint-disable-next-line prefer-const
694
- let [normalizedName, normalizedValues] = [facetName, facetValues];
695
- // The full "search engine" name of the lending field is "lending___status"
696
- if (facetName === 'lending') {
697
- normalizedName = 'lending___status';
698
- }
699
- return {
700
- name: normalizedName,
701
- values: normalizedValues,
702
- };
703
- }
704
- /**
705
- * Takes an array of facet clauses, and combines them into a
706
- * full AND-joined facet query string. Empty clauses are ignored.
707
- */
708
- joinFacetClauses(facetClauses) {
709
- const nonEmptyFacetClauses = facetClauses.filter(clause => clause.length > 0);
710
- return nonEmptyFacetClauses.length > 0
711
- ? `(${nonEmptyFacetClauses.join(' AND ')})`
712
- : undefined;
713
- }
714
- /**
715
- * Fires a backend request to fetch a set of aggregations (representing UI facets) for
716
- * the current search state.
717
- */
718
- async fetchFacets() {
719
- var _a, _b, _c, _d, _e, _f, _g, _h, _j;
720
- const trimmedQuery = (_a = this.host.baseQuery) === null || _a === void 0 ? void 0 : _a.trim();
721
- if (!this.canPerformSearch)
722
- return;
723
- const { facetFetchQueryKey } = this;
724
- if (this.fetchesInProgress.has(facetFetchQueryKey))
725
- return;
726
- this.fetchesInProgress.add(facetFetchQueryKey);
727
- this.setFacetsLoading(true);
728
- const sortParams = this.host.sortParam ? [this.host.sortParam] : [];
729
- const params = {
730
- ...this.pageSpecifierParams,
731
- query: trimmedQuery || '',
732
- rows: 0,
733
- filters: this.filterMap,
734
- // Fetch a few extra buckets beyond the 6 we show, in case some get suppressed
735
- aggregationsSize: 10,
736
- // Note: we don't need an aggregations param to fetch the default aggregations from the PPS.
737
- // The default aggregations for the search_results page type should be what we need here.
738
- };
739
- params.uid = await this.requestUID({ ...params, sort: sortParams }, 'aggregations');
740
- const searchResponse = await ((_b = this.host.searchService) === null || _b === void 0 ? void 0 : _b.search(params, this.host.searchType));
741
- const success = searchResponse === null || searchResponse === void 0 ? void 0 : searchResponse.success;
742
- // This is checking to see if the query has changed since the data was fetched.
743
- // If so, we just want to discard this set of aggregations because they are
744
- // likely no longer valid for the newer query.
745
- const queryChangedSinceFetch = !this.fetchesInProgress.has(facetFetchQueryKey);
746
- this.fetchesInProgress.delete(facetFetchQueryKey);
747
- if (queryChangedSinceFetch)
748
- return;
749
- if (!success) {
750
- const errorMsg = (_c = searchResponse === null || searchResponse === void 0 ? void 0 : searchResponse.error) === null || _c === void 0 ? void 0 : _c.message;
751
- const detailMsg = (_e = (_d = searchResponse === null || searchResponse === void 0 ? void 0 : searchResponse.error) === null || _d === void 0 ? void 0 : _d.details) === null || _e === void 0 ? void 0 : _e.message;
752
- if (!errorMsg && !detailMsg) {
753
- // @ts-ignore: Property 'Sentry' does not exist on type 'Window & typeof globalThis'
754
- (_g = (_f = window === null || window === void 0 ? void 0 : window.Sentry) === null || _f === void 0 ? void 0 : _f.captureMessage) === null || _g === void 0 ? void 0 : _g.call(_f, 'Missing or malformed facet response from backend', 'error');
755
- }
756
- this.setFacetsLoading(false);
757
- return;
758
- }
759
- const { aggregations, collectionTitles } = success.response;
760
- this.aggregations = aggregations;
761
- if (collectionTitles) {
762
- for (const [id, title] of Object.entries(collectionTitles)) {
763
- this.collectionTitles.set(id, title);
764
- }
765
- }
766
- this.yearHistogramAggregation =
767
- (_j = (_h = success === null || success === void 0 ? void 0 : success.response) === null || _h === void 0 ? void 0 : _h.aggregations) === null || _j === void 0 ? void 0 : _j.year_histogram;
768
- this.setFacetsLoading(false);
769
- this.requestHostUpdate();
770
- }
771
- /**
772
- * Performs the initial page fetch(es) for the current search state.
773
- */
774
- async doInitialPageFetch() {
775
- this.setSearchResultsLoading(true);
776
- // Try to batch 2 initial page requests when possible
777
- await this.fetchPage(this.host.initialPageNumber, 2);
778
- }
779
- /**
780
- * Fetches one or more pages of results and updates the data source.
781
- *
782
- * @param pageNumber The page number to fetch
783
- * @param numInitialPages If this is an initial page fetch (`pageNumber = 1`),
784
- * specifies how many pages to batch together in one request. Ignored
785
- * if `pageNumber != 1`, defaulting to a single page.
786
- */
787
- async fetchPage(pageNumber, numInitialPages = 1) {
788
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
789
- const trimmedQuery = (_a = this.host.baseQuery) === null || _a === void 0 ? void 0 : _a.trim();
790
- if (!this.canPerformSearch)
791
- return;
792
- // if we already have data, don't fetch again
793
- if (this.hasPage(pageNumber))
794
- return;
795
- if (this.endOfDataReached)
796
- return;
797
- // Batch multiple initial page requests together if needed (e.g., can request
798
- // pages 1 and 2 together in a single request).
799
- let numPages = pageNumber === 1 ? numInitialPages : 1;
800
- const numRows = this.pageSize * numPages;
801
- // if a fetch is already in progress for this query and page, don't fetch again
802
- const { pageFetchQueryKey } = this;
803
- const currentPageKey = `${pageFetchQueryKey}-p:${pageNumber}`;
804
- if (this.fetchesInProgress.has(currentPageKey))
805
- return;
806
- for (let i = 0; i < numPages; i += 1) {
807
- this.fetchesInProgress.add(`${pageFetchQueryKey}-p:${pageNumber + i}`);
808
- }
809
- this.previousQueryKey = pageFetchQueryKey;
810
- const { withinCollection, withinProfile } = this.host;
811
- let sortParams = this.host.sortParam ? [this.host.sortParam] : [];
812
- // TODO eventually the PPS should handle these defaults natively
813
- const isDefaultProfileSort = withinProfile && this.host.selectedSort === SortField.default;
814
- if (isDefaultProfileSort && this.host.defaultSortField) {
815
- const sortOption = SORT_OPTIONS[this.host.defaultSortField];
816
- if (sortOption.searchServiceKey) {
817
- sortParams = [
818
- {
819
- field: sortOption.searchServiceKey,
820
- direction: 'desc',
821
- },
822
- ];
823
- }
824
- }
825
- const params = {
826
- ...this.pageSpecifierParams,
827
- query: trimmedQuery || '',
828
- page: pageNumber,
829
- rows: numRows,
830
- sort: sortParams,
831
- filters: this.filterMap,
832
- aggregations: { omit: true },
833
- };
834
- params.uid = await this.requestUID(params, 'hits');
835
- // log('=== FIRING PAGE REQUEST ===', params);
836
- const searchResponse = await ((_b = this.host.searchService) === null || _b === void 0 ? void 0 : _b.search(params, this.host.searchType));
837
- // log('=== RECEIVED PAGE RESPONSE IN CB ===', searchResponse);
838
- const success = searchResponse === null || searchResponse === void 0 ? void 0 : searchResponse.success;
839
- // This is checking to see if the fetch has been invalidated since it was fired off.
840
- // If so, we just want to discard the response since it is for an obsolete query state.
841
- if (!this.fetchesInProgress.has(currentPageKey))
842
- return;
843
- for (let i = 0; i < numPages; i += 1) {
844
- this.fetchesInProgress.delete(`${pageFetchQueryKey}-p:${pageNumber + i}`);
845
- }
846
- if (!success) {
847
- const errorMsg = (_c = searchResponse === null || searchResponse === void 0 ? void 0 : searchResponse.error) === null || _c === void 0 ? void 0 : _c.message;
848
- const detailMsg = (_e = (_d = searchResponse === null || searchResponse === void 0 ? void 0 : searchResponse.error) === null || _d === void 0 ? void 0 : _d.details) === null || _e === void 0 ? void 0 : _e.message;
849
- this.queryErrorMessage = `${errorMsg !== null && errorMsg !== void 0 ? errorMsg : ''}${detailMsg ? `; ${detailMsg}` : ''}`;
850
- if (!this.queryErrorMessage) {
851
- this.queryErrorMessage = 'Missing or malformed response from backend';
852
- // @ts-ignore: Property 'Sentry' does not exist on type 'Window & typeof globalThis'
853
- (_g = (_f = window === null || window === void 0 ? void 0 : window.Sentry) === null || _f === void 0 ? void 0 : _f.captureMessage) === null || _g === void 0 ? void 0 : _g.call(_f, this.queryErrorMessage, 'error');
854
- }
855
- this.setSearchResultsLoading(false);
856
- this.requestHostUpdate();
857
- return;
858
- }
859
- this.setTotalResultCount(success.response.totalResults - this.offset);
860
- if (this.activeOnHost && this.totalResults === 0) {
861
- // display event to offshoot when result count is zero.
862
- this.host.emitEmptyResults();
863
- }
864
- this.sessionContext = success.sessionContext;
865
- if (withinCollection) {
866
- this.collectionExtraInfo = success.response.collectionExtraInfo;
867
- // For collections, we want the UI to respect the default sort option
868
- // which can be specified in metadata, or otherwise assumed to be week:desc
869
- if (this.activeOnHost) {
870
- this.host.applyDefaultCollectionSort(this.collectionExtraInfo);
871
- }
872
- if (this.collectionExtraInfo) {
873
- this.parentCollections = [].concat((_j = (_h = this.collectionExtraInfo.public_metadata) === null || _h === void 0 ? void 0 : _h.collection) !== null && _j !== void 0 ? _j : []);
874
- }
875
- }
876
- else if (withinProfile) {
877
- this.accountExtraInfo = success.response.accountExtraInfo;
878
- this.pageElements = success.response.pageElements;
879
- }
880
- const { results, collectionTitles } = success.response;
881
- if (results && results.length > 0) {
882
- // Load any collection titles present on the response into the cache,
883
- // or queue up preload fetches for them if none were present.
884
- if (collectionTitles) {
885
- for (const [id, title] of Object.entries(collectionTitles)) {
886
- this.collectionTitles.set(id, title);
887
- }
888
- // Also add the target collection's title if available
889
- const targetTitle = (_l = (_k = this.collectionExtraInfo) === null || _k === void 0 ? void 0 : _k.public_metadata) === null || _l === void 0 ? void 0 : _l.title;
890
- if (withinCollection && targetTitle) {
891
- this.collectionTitles.set(withinCollection, targetTitle);
892
- }
893
- }
894
- // Update the data source for each returned page.
895
- // For loans and web archives, we must account for receiving more pages than we asked for.
896
- const isUnpagedElement = ['lending', 'web_archives'].includes(this.host.profileElement);
897
- if (isUnpagedElement) {
898
- numPages = Math.ceil(results.length / this.pageSize);
899
- this.endOfDataReached = true;
900
- if (this.activeOnHost)
901
- this.host.setTileCount(this.totalResults);
902
- }
903
- for (let i = 0; i < numPages; i += 1) {
904
- const pageStartIndex = this.pageSize * i;
905
- this.addFetchedResultsToDataSource(pageNumber + i, results.slice(pageStartIndex, pageStartIndex + this.pageSize), !isUnpagedElement || i === numPages - 1);
906
- }
907
- }
908
- // When we reach the end of the data, we can set the infinite scroller's
909
- // item count to the real total number of results (rather than the
910
- // temporary estimates based on pages rendered so far).
911
- if (this.size >= this.totalResults || results.length === 0) {
912
- this.endOfDataReached = true;
913
- if (this.activeOnHost)
914
- this.host.setTileCount(this.size);
915
- }
916
- this.setSearchResultsLoading(false);
917
- this.requestHostUpdate();
918
- }
919
- /**
920
- * Update the datasource from the fetch response
921
- *
922
- * @param pageNumber
923
- * @param results
924
- */
925
- addFetchedResultsToDataSource(pageNumber, results, needsReload = true) {
926
- const tiles = [];
927
- results === null || results === void 0 ? void 0 : results.forEach(result => {
928
- if (!result.identifier)
929
- return;
930
- tiles.push(new TileModel(result));
931
- });
932
- this.addPage(pageNumber, tiles);
933
- if (needsReload) {
934
- this.refreshVisibleResults();
935
- }
936
- }
937
- /**
938
- * Fetches the aggregation buckets for the given prefix filter type.
939
- */
940
- async fetchPrefixFilterBuckets(filterType) {
941
- var _a, _b, _c, _d, _e, _f, _g;
942
- const trimmedQuery = (_a = this.host.baseQuery) === null || _a === void 0 ? void 0 : _a.trim();
943
- if (!this.canPerformSearch)
944
- return [];
945
- const filterAggregationKey = prefixFilterAggregationKeys[filterType];
946
- const sortParams = this.host.sortParam ? [this.host.sortParam] : [];
947
- const params = {
948
- ...this.pageSpecifierParams,
949
- query: trimmedQuery || '',
950
- rows: 0,
951
- filters: this.filterMap,
952
- // Only fetch the firstTitle or firstCreator aggregation
953
- aggregations: { simpleParams: [filterAggregationKey] },
954
- // Fetch all 26 letter buckets
955
- aggregationsSize: 26,
956
- };
957
- params.uid = await this.requestUID({ ...params, sort: sortParams }, 'aggregations');
958
- const searchResponse = await ((_b = this.host.searchService) === null || _b === void 0 ? void 0 : _b.search(params, this.host.searchType));
959
- return ((_g = (_f = (_e = (_d = (_c = searchResponse === null || searchResponse === void 0 ? void 0 : searchResponse.success) === null || _c === void 0 ? void 0 : _c.response) === null || _d === void 0 ? void 0 : _d.aggregations) === null || _e === void 0 ? void 0 : _e[filterAggregationKey]) === null || _f === void 0 ? void 0 : _f.buckets) !== null && _g !== void 0 ? _g : []);
960
- }
961
- /**
962
- * Fetches and caches the prefix filter counts for the given filter type.
963
- */
964
- async updatePrefixFilterCounts(filterType) {
965
- const { facetFetchQueryKey } = this;
966
- const buckets = await this.fetchPrefixFilterBuckets(filterType);
967
- // Don't update the filter counts for an outdated query (if it has been changed
968
- // since we sent the request)
969
- const queryChangedSinceFetch = facetFetchQueryKey !== this.facetFetchQueryKey;
970
- if (queryChangedSinceFetch)
971
- return;
972
- // Unpack the aggregation buckets into a simple map like { 'A': 50, 'B': 25, ... }
973
- this.prefixFilterCountMap = { ...this.prefixFilterCountMap }; // Clone the object to trigger an update
974
- this.prefixFilterCountMap[filterType] = buckets.reduce((acc, bucket) => {
975
- acc[bucket.key.toUpperCase()] = bucket.doc_count;
976
- return acc;
977
- }, {});
978
- this.requestHostUpdate();
979
- }
980
- /**
981
- * @inheritdoc
982
- */
983
- async updatePrefixFiltersForCurrentSort() {
984
- if (['title', 'creator'].includes(this.host.selectedSort)) {
985
- const filterType = this.host.selectedSort;
986
- if (!this.prefixFilterCountMap[filterType]) {
987
- this.updatePrefixFilterCounts(filterType);
988
- }
989
- }
990
- }
991
- /**
992
- * @inheritdoc
993
- */
994
- refreshLetterCounts() {
995
- if (Object.keys(this.prefixFilterCountMap).length > 0) {
996
- this.prefixFilterCountMap = {};
997
- }
998
- this.updatePrefixFiltersForCurrentSort();
999
- this.requestHostUpdate();
1000
- }
1001
- }
1
+ import { FilterConstraint, FilterMapBuilder, SearchType, } from '@internetarchive/search-service';
2
+ import { prefixFilterAggregationKeys, TileModel, SortField, SORT_OPTIONS, } from '../models';
3
+ import { FACETLESS_PAGE_ELEMENTS } from './models';
4
+ import { sha1 } from '../utils/sha1';
5
+ import { log } from '../utils/log';
6
+ export class CollectionBrowserDataSource {
7
+ // eslint-disable-next-line no-useless-constructor
8
+ constructor(
9
+ /** The host element to which this controller should attach listeners */
10
+ host,
11
+ /** Default size of result pages */
12
+ pageSize = 50) {
13
+ this.host = host;
14
+ this.pageSize = pageSize;
15
+ /**
16
+ * All pages of tile models that have been fetched so far, indexed by their page
17
+ * number (with the first being page 1).
18
+ */
19
+ this.pages = {};
20
+ /**
21
+ * Tile offset to apply when looking up tiles in the pages, in order to maintain
22
+ * page alignment after tiles are removed.
23
+ */
24
+ this.offset = 0;
25
+ /**
26
+ * Total number of tile models stored in this data source's pages
27
+ */
28
+ this.numTileModels = 0;
29
+ /**
30
+ * A set of fetch IDs that are valid for the current query state
31
+ */
32
+ this.fetchesInProgress = new Set();
33
+ /**
34
+ * A record of the query key used for the last search.
35
+ * If this changes, we need to load new results.
36
+ */
37
+ this.previousQueryKey = '';
38
+ /**
39
+ * Whether the initial page of search results for the current query state
40
+ * is presently being fetched.
41
+ */
42
+ this.searchResultsLoading = false;
43
+ /**
44
+ * Whether the facets (aggregations) for the current query state are
45
+ * presently being fetched.
46
+ */
47
+ this.facetsLoading = false;
48
+ /**
49
+ * Whether the facets are actually visible -- if not, then we can delay any facet
50
+ * fetches until they become visible.
51
+ */
52
+ this.facetsReadyToLoad = false;
53
+ /**
54
+ * Whether further query changes should be ignored and not trigger fetches
55
+ */
56
+ this.suppressFetches = false;
57
+ /**
58
+ * @inheritdoc
59
+ */
60
+ this.totalResults = 0;
61
+ /**
62
+ * @inheritdoc
63
+ */
64
+ this.endOfDataReached = false;
65
+ /**
66
+ * @inheritdoc
67
+ */
68
+ this.queryInitialized = false;
69
+ /**
70
+ * @inheritdoc
71
+ */
72
+ this.collectionTitles = new Map();
73
+ /**
74
+ * @inheritdoc
75
+ */
76
+ this.parentCollections = [];
77
+ /**
78
+ * @inheritdoc
79
+ */
80
+ this.prefixFilterCountMap = {};
81
+ /**
82
+ * Internal property to store the private value backing the `initialSearchComplete` getter.
83
+ */
84
+ this._initialSearchCompletePromise = Promise.resolve(true);
85
+ /**
86
+ * @inheritdoc
87
+ */
88
+ this.checkAllTiles = () => {
89
+ this.map(model => {
90
+ const cloned = model.clone();
91
+ cloned.checked = true;
92
+ return cloned;
93
+ });
94
+ };
95
+ /**
96
+ * @inheritdoc
97
+ */
98
+ this.uncheckAllTiles = () => {
99
+ this.map(model => {
100
+ const cloned = model.clone();
101
+ cloned.checked = false;
102
+ return cloned;
103
+ });
104
+ };
105
+ /**
106
+ * @inheritdoc
107
+ */
108
+ this.removeCheckedTiles = () => {
109
+ // To make sure our data source remains page-aligned, we will offset our data source by
110
+ // the number of removed tiles, so that we can just add the offset when the infinite
111
+ // scroller queries for cell contents.
112
+ // This only matters while we're still viewing the same set of results. If the user changes
113
+ // their query/filters/sort, then the data source is overwritten and the offset cleared.
114
+ const { checkedTileModels, uncheckedTileModels } = this;
115
+ const numChecked = checkedTileModels.length;
116
+ if (numChecked === 0)
117
+ return;
118
+ this.offset += numChecked;
119
+ const newPages = {};
120
+ // Which page the remaining tile models start on, post-offset
121
+ let offsetPageNumber = Math.floor(this.offset / this.pageSize) + 1;
122
+ let indexOnPage = this.offset % this.pageSize;
123
+ // Fill the pages up to that point with empty models
124
+ for (let page = 1; page <= offsetPageNumber; page += 1) {
125
+ const remainingHidden = this.offset - this.pageSize * (page - 1);
126
+ const offsetCellsOnPage = Math.min(this.pageSize, remainingHidden);
127
+ newPages[page] = Array(offsetCellsOnPage).fill(undefined);
128
+ }
129
+ // Shift all the remaining tiles into their new positions in the data source
130
+ for (const model of uncheckedTileModels) {
131
+ if (!newPages[offsetPageNumber])
132
+ newPages[offsetPageNumber] = [];
133
+ newPages[offsetPageNumber].push(model);
134
+ indexOnPage += 1;
135
+ if (indexOnPage >= this.pageSize) {
136
+ offsetPageNumber += 1;
137
+ indexOnPage = 0;
138
+ }
139
+ }
140
+ // Swap in the new pages
141
+ this.pages = newPages;
142
+ this.numTileModels -= numChecked;
143
+ this.totalResults -= numChecked;
144
+ this.host.setTileCount(this.size);
145
+ this.host.setTotalResultCount(this.totalResults);
146
+ this.requestHostUpdate();
147
+ this.refreshVisibleResults();
148
+ };
149
+ // Just setting some property values
150
+ }
151
+ /**
152
+ * @inheritdoc
153
+ */
154
+ get initialSearchComplete() {
155
+ return this._initialSearchCompletePromise;
156
+ }
157
+ hostConnected() {
158
+ this.setSearchResultsLoading(this.searchResultsLoading);
159
+ this.setFacetsLoading(this.facetsLoading);
160
+ }
161
+ hostUpdate() {
162
+ // This reactive controller hook is run whenever the host component (collection-browser) performs an update.
163
+ // We check whether the host's state has changed in a way which should trigger a reset & new results fetch.
164
+ // Only the currently-installed data source should react to the update
165
+ if (!this.activeOnHost)
166
+ return;
167
+ // Copy loading states onto the host
168
+ this.setSearchResultsLoading(this.searchResultsLoading);
169
+ this.setFacetsLoading(this.facetsLoading);
170
+ // Can't perform searches without a search service
171
+ if (!this.host.searchService)
172
+ return;
173
+ // We should only reset if part of the full query state has changed
174
+ const queryKeyChanged = this.pageFetchQueryKey !== this.previousQueryKey;
175
+ if (!queryKeyChanged)
176
+ return;
177
+ // We should only reset if either:
178
+ // (a) our state permits a valid search, or
179
+ // (b) we have a blank query that we're showing a placeholder/message for
180
+ const queryIsEmpty = !this.host.baseQuery;
181
+ if (!(this.canPerformSearch || queryIsEmpty))
182
+ return;
183
+ if (this.activeOnHost)
184
+ this.host.emitQueryStateChanged();
185
+ this.handleQueryChange();
186
+ }
187
+ /**
188
+ * Returns whether this data source is the one currently installed on the host component.
189
+ */
190
+ get activeOnHost() {
191
+ return this.host.dataSource === this;
192
+ }
193
+ /**
194
+ * @inheritdoc
195
+ */
196
+ get size() {
197
+ return this.numTileModels;
198
+ }
199
+ /**
200
+ * @inheritdoc
201
+ */
202
+ reset() {
203
+ log('Resetting CB data source');
204
+ this.pages = {};
205
+ this.aggregations = {};
206
+ this.yearHistogramAggregation = undefined;
207
+ this.pageElements = undefined;
208
+ this.parentCollections = [];
209
+ this.previousQueryKey = '';
210
+ this.queryErrorMessage = undefined;
211
+ this.offset = 0;
212
+ this.numTileModels = 0;
213
+ this.endOfDataReached = false;
214
+ this.queryInitialized = false;
215
+ this.facetsLoading = false;
216
+ // Invalidate any fetches in progress
217
+ this.fetchesInProgress.clear();
218
+ this.setTotalResultCount(0);
219
+ this.requestHostUpdate();
220
+ }
221
+ /**
222
+ * @inheritdoc
223
+ */
224
+ resetPages() {
225
+ if (Object.keys(this.pages).length < this.host.maxPagesToManage) {
226
+ this.pages = {};
227
+ // Invalidate any fetches in progress
228
+ this.fetchesInProgress.clear();
229
+ this.requestHostUpdate();
230
+ }
231
+ }
232
+ /**
233
+ * @inheritdoc
234
+ */
235
+ addPage(pageNum, pageTiles) {
236
+ this.pages[pageNum] = pageTiles;
237
+ this.numTileModels += pageTiles.length;
238
+ this.requestHostUpdate();
239
+ }
240
+ /**
241
+ * @inheritdoc
242
+ */
243
+ addMultiplePages(firstPageNum, tiles) {
244
+ const numPages = Math.ceil(tiles.length / this.pageSize);
245
+ for (let i = 0; i < numPages; i += 1) {
246
+ const pageStartIndex = this.pageSize * i;
247
+ this.addPage(firstPageNum + i, tiles.slice(pageStartIndex, pageStartIndex + this.pageSize));
248
+ }
249
+ const visiblePages = this.host.currentVisiblePageNumbers;
250
+ const needsReload = visiblePages.some(page => page >= firstPageNum && page <= firstPageNum + numPages);
251
+ if (needsReload) {
252
+ this.refreshVisibleResults();
253
+ }
254
+ }
255
+ /**
256
+ * @inheritdoc
257
+ */
258
+ getPage(pageNum) {
259
+ return this.pages[pageNum];
260
+ }
261
+ /**
262
+ * @inheritdoc
263
+ */
264
+ getAllPages() {
265
+ return this.pages;
266
+ }
267
+ /**
268
+ * @inheritdoc
269
+ */
270
+ hasPage(pageNum) {
271
+ return !!this.pages[pageNum];
272
+ }
273
+ /**
274
+ * @inheritdoc
275
+ */
276
+ getTileModelAt(index) {
277
+ var _a, _b;
278
+ const offsetIndex = index + this.offset;
279
+ const expectedPageNum = Math.floor(offsetIndex / this.pageSize) + 1;
280
+ const expectedIndexOnPage = offsetIndex % this.pageSize;
281
+ let page = 1;
282
+ let tilesSeen = 0;
283
+ while (tilesSeen <= offsetIndex) {
284
+ if (!this.pages[page]) {
285
+ // If we encounter a missing page, either we're past all the results or the page data is sparse.
286
+ // So just return the tile at the expected position if it exists.
287
+ return (_a = this.pages[expectedPageNum]) === null || _a === void 0 ? void 0 : _a[expectedIndexOnPage];
288
+ }
289
+ if (tilesSeen + this.pages[page].length > offsetIndex) {
290
+ return this.pages[page][offsetIndex - tilesSeen];
291
+ }
292
+ tilesSeen += this.pages[page].length;
293
+ page += 1;
294
+ }
295
+ return (_b = this.pages[expectedPageNum]) === null || _b === void 0 ? void 0 : _b[expectedIndexOnPage];
296
+ }
297
+ /**
298
+ * @inheritdoc
299
+ */
300
+ indexOf(tile) {
301
+ return Object.values(this.pages).flat().indexOf(tile) - this.offset;
302
+ }
303
+ /**
304
+ * @inheritdoc
305
+ */
306
+ getPageSize() {
307
+ return this.pageSize;
308
+ }
309
+ /**
310
+ * @inheritdoc
311
+ */
312
+ setPageSize(pageSize) {
313
+ this.reset();
314
+ this.pageSize = pageSize;
315
+ }
316
+ /**
317
+ * @inheritdoc
318
+ */
319
+ setTotalResultCount(count) {
320
+ this.totalResults = count;
321
+ if (this.activeOnHost) {
322
+ this.host.setTotalResultCount(count);
323
+ }
324
+ }
325
+ /**
326
+ * @inheritdoc
327
+ */
328
+ setFetchesSuppressed(suppressed) {
329
+ this.suppressFetches = suppressed;
330
+ }
331
+ /**
332
+ * @inheritdoc
333
+ */
334
+ async handleQueryChange() {
335
+ // Don't react to the change if fetches are suppressed for this data source
336
+ if (this.suppressFetches)
337
+ return;
338
+ this.reset();
339
+ // Reset the `initialSearchComplete` promise with a new value for the imminent search
340
+ let initialSearchCompleteResolver;
341
+ this._initialSearchCompletePromise = new Promise(res => {
342
+ initialSearchCompleteResolver = res;
343
+ });
344
+ // Fire the initial page & facet requests
345
+ this.queryInitialized = true;
346
+ await Promise.all([
347
+ this.doInitialPageFetch(),
348
+ this.canFetchFacets ? this.fetchFacets() : null,
349
+ ]);
350
+ // Resolve the `initialSearchComplete` promise for this search
351
+ initialSearchCompleteResolver(true);
352
+ }
353
+ /**
354
+ * @inheritdoc
355
+ */
356
+ async handleFacetReadinessChange(ready) {
357
+ const facetsBecameReady = !this.facetsReadyToLoad && ready;
358
+ this.facetsReadyToLoad = ready;
359
+ const needsFetch = facetsBecameReady && this.canFetchFacets;
360
+ if (needsFetch) {
361
+ this.fetchFacets();
362
+ }
363
+ }
364
+ /**
365
+ * Whether the data source & its host are in a state where a facet request should be fired.
366
+ * (i.e., they aren't suppressed or already loading, etc.)
367
+ */
368
+ get canFetchFacets() {
369
+ var _a;
370
+ // Don't fetch facets if they are suppressed entirely or not required for the current profile page element
371
+ if (this.host.facetLoadStrategy === 'off')
372
+ return false;
373
+ if (FACETLESS_PAGE_ELEMENTS.includes(this.host.profileElement))
374
+ return false;
375
+ // If facets are to be lazy-loaded, don't fetch them if they are not going to be visible anyway
376
+ // (wait until they become visible instead)
377
+ if (this.host.facetLoadStrategy !== 'eager' && !this.facetsReadyToLoad)
378
+ return false;
379
+ // Don't fetch facets again if they are already fetched or pending
380
+ const facetsAlreadyFetched = Object.keys((_a = this.aggregations) !== null && _a !== void 0 ? _a : {}).length > 0;
381
+ if (this.facetsLoading || facetsAlreadyFetched)
382
+ return false;
383
+ return true;
384
+ }
385
+ /**
386
+ * @inheritdoc
387
+ */
388
+ map(callback) {
389
+ if (!Object.keys(this.pages).length)
390
+ return;
391
+ this.pages = Object.fromEntries(Object.entries(this.pages).map(([page, tileModels]) => [
392
+ page,
393
+ tileModels.map((model, index, array) => model ? callback(model, index, array) : model),
394
+ ]));
395
+ this.requestHostUpdate();
396
+ this.refreshVisibleResults();
397
+ }
398
+ /**
399
+ * @inheritdoc
400
+ */
401
+ get checkedTileModels() {
402
+ return this.getFilteredTileModels(model => model.checked);
403
+ }
404
+ /**
405
+ * @inheritdoc
406
+ */
407
+ get uncheckedTileModels() {
408
+ return this.getFilteredTileModels(model => !model.checked);
409
+ }
410
+ /**
411
+ * Returns a flattened, filtered array of all the tile models in the data source
412
+ * for which the given predicate returns a truthy value.
413
+ *
414
+ * @param predicate A callback function to apply on each tile model, as with Array.filter
415
+ * @returns A filtered array of tile models satisfying the predicate
416
+ */
417
+ getFilteredTileModels(predicate) {
418
+ return Object.values(this.pages)
419
+ .flat()
420
+ .filter((model, index, array) => model ? predicate(model, index, array) : false);
421
+ }
422
+ /**
423
+ * @inheritdoc
424
+ */
425
+ get canPerformSearch() {
426
+ var _a;
427
+ if (!this.host.searchService)
428
+ return false;
429
+ const trimmedQuery = (_a = this.host.baseQuery) === null || _a === void 0 ? void 0 : _a.trim();
430
+ const hasNonEmptyQuery = !!trimmedQuery;
431
+ const isCollectionSearch = !!this.host.withinCollection;
432
+ const isProfileSearch = !!this.host.withinProfile;
433
+ const hasProfileElement = !!this.host.profileElement;
434
+ const isMetadataSearch = this.host.searchType === SearchType.METADATA;
435
+ // Metadata searches within a collection/profile are allowed to have no query.
436
+ // Otherwise, a non-empty query must be set.
437
+ return (hasNonEmptyQuery ||
438
+ (isCollectionSearch && isMetadataSearch) ||
439
+ (isProfileSearch && hasProfileElement && isMetadataSearch));
440
+ }
441
+ /**
442
+ * Sets the state for whether the initial set of search results for the
443
+ * current query is loading
444
+ */
445
+ setSearchResultsLoading(loading) {
446
+ this.searchResultsLoading = loading;
447
+ if (this.activeOnHost) {
448
+ this.host.setSearchResultsLoading(loading);
449
+ }
450
+ }
451
+ /**
452
+ * Sets the state for whether the facets for a query is loading
453
+ */
454
+ setFacetsLoading(loading) {
455
+ this.facetsLoading = loading;
456
+ if (this.activeOnHost) {
457
+ this.host.setFacetsLoading(loading);
458
+ }
459
+ }
460
+ /**
461
+ * Requests that the host perform an update, provided this data
462
+ * source is actively installed on it.
463
+ */
464
+ requestHostUpdate() {
465
+ if (this.activeOnHost) {
466
+ this.host.requestUpdate();
467
+ }
468
+ }
469
+ /**
470
+ * Requests that the host refresh its visible tiles, provided this
471
+ * data source is actively installed on it.
472
+ */
473
+ refreshVisibleResults() {
474
+ if (this.activeOnHost) {
475
+ this.host.refreshVisibleResults();
476
+ }
477
+ }
478
+ /**
479
+ * The query key is a string that uniquely identifies the current search.
480
+ * It consists of:
481
+ * - The current base query
482
+ * - The current collection/profile target & page element
483
+ * - The current search type
484
+ * - Any currently-applied facets
485
+ * - Any currently-applied date range
486
+ * - Any currently-applied prefix filters
487
+ * - The current sort options
488
+ *
489
+ * This lets us internally keep track of queries so we don't persist data that's
490
+ * no longer relevant. Not meant to be human-readable.
491
+ */
492
+ get pageFetchQueryKey() {
493
+ var _a, _b, _c;
494
+ const profileKey = `pf;${this.host.withinProfile}--pe;${this.host.profileElement}`;
495
+ const pageTarget = (_a = this.host.withinCollection) !== null && _a !== void 0 ? _a : profileKey;
496
+ const sortField = (_b = this.host.selectedSort) !== null && _b !== void 0 ? _b : 'none';
497
+ const sortDirection = (_c = this.host.sortDirection) !== null && _c !== void 0 ? _c : 'none';
498
+ return `fq:${this.fullQuery}-pt:${pageTarget}-st:${this.host.searchType}-sf:${sortField}-sd:${sortDirection}`;
499
+ }
500
+ /**
501
+ * Similar to `pageFetchQueryKey` above, but excludes sort fields since they
502
+ * are not relevant in determining aggregation queries.
503
+ */
504
+ get facetFetchQueryKey() {
505
+ var _a;
506
+ const profileKey = `pf;${this.host.withinProfile}--pe;${this.host.profileElement}`;
507
+ const pageTarget = (_a = this.host.withinCollection) !== null && _a !== void 0 ? _a : profileKey;
508
+ return `fq:${this.fullQuery}-pt:${pageTarget}-st:${this.host.searchType}`;
509
+ }
510
+ /**
511
+ * Constructs a search service FilterMap object from the combination of
512
+ * all the currently-applied filters. This includes any facets, letter
513
+ * filters, and date range.
514
+ */
515
+ get filterMap() {
516
+ const builder = new FilterMapBuilder();
517
+ // Add the date range, if applicable
518
+ if (this.host.minSelectedDate) {
519
+ builder.addFilter('year', this.host.minSelectedDate, FilterConstraint.GREATER_OR_EQUAL);
520
+ }
521
+ if (this.host.maxSelectedDate) {
522
+ builder.addFilter('year', this.host.maxSelectedDate, FilterConstraint.LESS_OR_EQUAL);
523
+ }
524
+ // Add any selected facets
525
+ if (this.host.selectedFacets) {
526
+ for (const [facetName, facetValues] of Object.entries(this.host.selectedFacets)) {
527
+ const { name, values } = this.prepareFacetForFetch(facetName, facetValues);
528
+ for (const [value, bucket] of Object.entries(values)) {
529
+ let constraint;
530
+ if (bucket.state === 'selected') {
531
+ constraint = FilterConstraint.INCLUDE;
532
+ }
533
+ else if (bucket.state === 'hidden') {
534
+ constraint = FilterConstraint.EXCLUDE;
535
+ }
536
+ if (constraint) {
537
+ builder.addFilter(name, value, constraint);
538
+ }
539
+ }
540
+ }
541
+ }
542
+ // Add any letter filters
543
+ if (this.host.selectedTitleFilter) {
544
+ builder.addFilter('firstTitle', this.host.selectedTitleFilter, FilterConstraint.INCLUDE);
545
+ }
546
+ if (this.host.selectedCreatorFilter) {
547
+ builder.addFilter('firstCreator', this.host.selectedCreatorFilter, FilterConstraint.INCLUDE);
548
+ }
549
+ const filterMap = builder.build();
550
+ return filterMap;
551
+ }
552
+ /**
553
+ * Produces a compact unique ID for a search request that can help with debugging
554
+ * on the backend by making related requests easier to trace through different services.
555
+ * (e.g., tying the hits/aggregations requests for the same page back to a single hash).
556
+ *
557
+ * @param params The search service parameters for the request
558
+ * @param kind The kind of request (hits-only, aggregations-only, or both)
559
+ * @returns A Promise resolving to the uid to apply to the request
560
+ */
561
+ async requestUID(params, kind) {
562
+ var _a;
563
+ const paramsToHash = JSON.stringify({
564
+ pageType: params.pageType,
565
+ pageTarget: params.pageTarget,
566
+ query: params.query,
567
+ fields: params.fields,
568
+ filters: params.filters,
569
+ sort: params.sort,
570
+ searchType: this.host.searchType,
571
+ });
572
+ const fullQueryHash = (await sha1(paramsToHash)).slice(0, 20); // First 80 bits of SHA-1 are plenty for this
573
+ const sessionId = (await this.host.getSessionId()).slice(0, 20); // Likewise
574
+ const page = (_a = params.page) !== null && _a !== void 0 ? _a : 0;
575
+ const kindPrefix = kind.charAt(0); // f = full, h = hits, a = aggregations
576
+ const currentTime = Date.now();
577
+ return `R:${fullQueryHash}-S:${sessionId}-P:${page}-K:${kindPrefix}-T:${currentTime}`;
578
+ }
579
+ /**
580
+ * @inheritdoc
581
+ */
582
+ get pageSpecifierParams() {
583
+ if (this.host.withinCollection) {
584
+ return {
585
+ pageType: 'collection_details',
586
+ pageTarget: this.host.withinCollection,
587
+ };
588
+ }
589
+ if (this.host.withinProfile) {
590
+ return {
591
+ pageType: 'account_details',
592
+ pageTarget: this.host.withinProfile,
593
+ pageElements: this.host.profileElement
594
+ ? [this.host.profileElement]
595
+ : [],
596
+ };
597
+ }
598
+ return null;
599
+ }
600
+ /**
601
+ * The full query, including year facets and date range clauses
602
+ */
603
+ get fullQuery() {
604
+ var _a, _b;
605
+ let fullQuery = (_b = (_a = this.host.baseQuery) === null || _a === void 0 ? void 0 : _a.trim()) !== null && _b !== void 0 ? _b : '';
606
+ const { facetQuery, dateRangeQueryClause, sortFilterQueries } = this;
607
+ if (facetQuery) {
608
+ fullQuery += ` AND ${facetQuery}`;
609
+ }
610
+ if (dateRangeQueryClause) {
611
+ fullQuery += ` AND ${dateRangeQueryClause}`;
612
+ }
613
+ if (sortFilterQueries) {
614
+ fullQuery += ` AND ${sortFilterQueries}`;
615
+ }
616
+ return fullQuery.trim();
617
+ }
618
+ /**
619
+ * Generates a query string representing the current set of applied facets
620
+ *
621
+ * Example: `mediatype:("collection" OR "audio" OR -"etree") AND year:("2000" OR "2001")`
622
+ */
623
+ get facetQuery() {
624
+ var _a;
625
+ if (!this.host.selectedFacets)
626
+ return undefined;
627
+ const facetClauses = [];
628
+ for (const [facetName, facetValues] of Object.entries(this.host.selectedFacets)) {
629
+ facetClauses.push(this.buildFacetClause(facetName, facetValues));
630
+ }
631
+ return (_a = this.joinFacetClauses(facetClauses)) === null || _a === void 0 ? void 0 : _a.trim();
632
+ }
633
+ get dateRangeQueryClause() {
634
+ if (!this.host.minSelectedDate || !this.host.maxSelectedDate) {
635
+ return undefined;
636
+ }
637
+ return `year:[${this.host.minSelectedDate} TO ${this.host.maxSelectedDate}]`;
638
+ }
639
+ get sortFilterQueries() {
640
+ const queries = [this.titleQuery, this.creatorQuery];
641
+ return queries.filter(q => q).join(' AND ');
642
+ }
643
+ /**
644
+ * Returns a query clause identifying the currently selected title filter,
645
+ * e.g., `firstTitle:X`.
646
+ */
647
+ get titleQuery() {
648
+ return this.host.selectedTitleFilter
649
+ ? `firstTitle:${this.host.selectedTitleFilter}`
650
+ : undefined;
651
+ }
652
+ /**
653
+ * Returns a query clause identifying the currently selected creator filter,
654
+ * e.g., `firstCreator:X`.
655
+ */
656
+ get creatorQuery() {
657
+ return this.host.selectedCreatorFilter
658
+ ? `firstCreator:${this.host.selectedCreatorFilter}`
659
+ : undefined;
660
+ }
661
+ /**
662
+ * Builds an OR-joined facet clause for the given facet name and values.
663
+ *
664
+ * E.g., for name `subject` and values
665
+ * `{ foo: { state: 'selected' }, bar: { state: 'hidden' } }`
666
+ * this will produce the clause
667
+ * `subject:("foo" OR -"bar")`.
668
+ *
669
+ * @param facetName The facet type (e.g., 'collection')
670
+ * @param facetValues The facet buckets, mapped by their keys
671
+ */
672
+ buildFacetClause(facetName, facetValues) {
673
+ const { name: facetQueryName, values } = this.prepareFacetForFetch(facetName, facetValues);
674
+ const facetEntries = Object.entries(values);
675
+ if (facetEntries.length === 0)
676
+ return '';
677
+ const facetValuesArray = [];
678
+ for (const [key, facetData] of facetEntries) {
679
+ const plusMinusPrefix = facetData.state === 'hidden' ? '-' : '';
680
+ facetValuesArray.push(`${plusMinusPrefix}"${key}"`);
681
+ }
682
+ const valueQuery = facetValuesArray.join(` OR `);
683
+ return `${facetQueryName}:(${valueQuery})`;
684
+ }
685
+ /**
686
+ * Handles some special pre-request normalization steps for certain facet types
687
+ * that require them.
688
+ *
689
+ * @param facetName The name of the facet type (e.g., 'language')
690
+ * @param facetValues An array of values for that facet type
691
+ */
692
+ prepareFacetForFetch(facetName, facetValues) {
693
+ // eslint-disable-next-line prefer-const
694
+ let [normalizedName, normalizedValues] = [facetName, facetValues];
695
+ // The full "search engine" name of the lending field is "lending___status"
696
+ if (facetName === 'lending') {
697
+ normalizedName = 'lending___status';
698
+ }
699
+ return {
700
+ name: normalizedName,
701
+ values: normalizedValues,
702
+ };
703
+ }
704
+ /**
705
+ * Takes an array of facet clauses, and combines them into a
706
+ * full AND-joined facet query string. Empty clauses are ignored.
707
+ */
708
+ joinFacetClauses(facetClauses) {
709
+ const nonEmptyFacetClauses = facetClauses.filter(clause => clause.length > 0);
710
+ return nonEmptyFacetClauses.length > 0
711
+ ? `(${nonEmptyFacetClauses.join(' AND ')})`
712
+ : undefined;
713
+ }
714
+ /**
715
+ * Fires a backend request to fetch a set of aggregations (representing UI facets) for
716
+ * the current search state.
717
+ */
718
+ async fetchFacets() {
719
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j;
720
+ const trimmedQuery = (_a = this.host.baseQuery) === null || _a === void 0 ? void 0 : _a.trim();
721
+ if (!this.canPerformSearch)
722
+ return;
723
+ const { facetFetchQueryKey } = this;
724
+ if (this.fetchesInProgress.has(facetFetchQueryKey))
725
+ return;
726
+ this.fetchesInProgress.add(facetFetchQueryKey);
727
+ this.setFacetsLoading(true);
728
+ const sortParams = this.host.sortParam ? [this.host.sortParam] : [];
729
+ const params = {
730
+ ...this.pageSpecifierParams,
731
+ query: trimmedQuery || '',
732
+ rows: 0,
733
+ filters: this.filterMap,
734
+ // Fetch a few extra buckets beyond the 6 we show, in case some get suppressed
735
+ aggregationsSize: 10,
736
+ // Note: we don't need an aggregations param to fetch the default aggregations from the PPS.
737
+ // The default aggregations for the search_results page type should be what we need here.
738
+ };
739
+ params.uid = await this.requestUID({ ...params, sort: sortParams }, 'aggregations');
740
+ const searchResponse = await ((_b = this.host.searchService) === null || _b === void 0 ? void 0 : _b.search(params, this.host.searchType));
741
+ const success = searchResponse === null || searchResponse === void 0 ? void 0 : searchResponse.success;
742
+ // This is checking to see if the query has changed since the data was fetched.
743
+ // If so, we just want to discard this set of aggregations because they are
744
+ // likely no longer valid for the newer query.
745
+ const queryChangedSinceFetch = !this.fetchesInProgress.has(facetFetchQueryKey);
746
+ this.fetchesInProgress.delete(facetFetchQueryKey);
747
+ if (queryChangedSinceFetch)
748
+ return;
749
+ if (!success) {
750
+ const errorMsg = (_c = searchResponse === null || searchResponse === void 0 ? void 0 : searchResponse.error) === null || _c === void 0 ? void 0 : _c.message;
751
+ const detailMsg = (_e = (_d = searchResponse === null || searchResponse === void 0 ? void 0 : searchResponse.error) === null || _d === void 0 ? void 0 : _d.details) === null || _e === void 0 ? void 0 : _e.message;
752
+ if (!errorMsg && !detailMsg) {
753
+ // @ts-ignore: Property 'Sentry' does not exist on type 'Window & typeof globalThis'
754
+ (_g = (_f = window === null || window === void 0 ? void 0 : window.Sentry) === null || _f === void 0 ? void 0 : _f.captureMessage) === null || _g === void 0 ? void 0 : _g.call(_f, 'Missing or malformed facet response from backend', 'error');
755
+ }
756
+ this.setFacetsLoading(false);
757
+ return;
758
+ }
759
+ const { aggregations, collectionTitles } = success.response;
760
+ this.aggregations = aggregations;
761
+ if (collectionTitles) {
762
+ for (const [id, title] of Object.entries(collectionTitles)) {
763
+ this.collectionTitles.set(id, title);
764
+ }
765
+ }
766
+ this.yearHistogramAggregation =
767
+ (_j = (_h = success === null || success === void 0 ? void 0 : success.response) === null || _h === void 0 ? void 0 : _h.aggregations) === null || _j === void 0 ? void 0 : _j.year_histogram;
768
+ this.setFacetsLoading(false);
769
+ this.requestHostUpdate();
770
+ }
771
+ /**
772
+ * Performs the initial page fetch(es) for the current search state.
773
+ */
774
+ async doInitialPageFetch() {
775
+ this.setSearchResultsLoading(true);
776
+ // Try to batch 2 initial page requests when possible
777
+ await this.fetchPage(this.host.initialPageNumber, 2);
778
+ }
779
+ /**
780
+ * Fetches one or more pages of results and updates the data source.
781
+ *
782
+ * @param pageNumber The page number to fetch
783
+ * @param numInitialPages If this is an initial page fetch (`pageNumber = 1`),
784
+ * specifies how many pages to batch together in one request. Ignored
785
+ * if `pageNumber != 1`, defaulting to a single page.
786
+ */
787
+ async fetchPage(pageNumber, numInitialPages = 1) {
788
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
789
+ const trimmedQuery = (_a = this.host.baseQuery) === null || _a === void 0 ? void 0 : _a.trim();
790
+ // reset loading status
791
+ if (!this.canPerformSearch) {
792
+ this.setSearchResultsLoading(false);
793
+ return;
794
+ }
795
+ // if we already have data, don't fetch again
796
+ if (this.hasPage(pageNumber))
797
+ return;
798
+ if (this.endOfDataReached)
799
+ return;
800
+ // Batch multiple initial page requests together if needed (e.g., can request
801
+ // pages 1 and 2 together in a single request).
802
+ let numPages = pageNumber === 1 ? numInitialPages : 1;
803
+ const numRows = this.pageSize * numPages;
804
+ // if a fetch is already in progress for this query and page, don't fetch again
805
+ const { pageFetchQueryKey } = this;
806
+ const currentPageKey = `${pageFetchQueryKey}-p:${pageNumber}`;
807
+ if (this.fetchesInProgress.has(currentPageKey))
808
+ return;
809
+ for (let i = 0; i < numPages; i += 1) {
810
+ this.fetchesInProgress.add(`${pageFetchQueryKey}-p:${pageNumber + i}`);
811
+ }
812
+ this.previousQueryKey = pageFetchQueryKey;
813
+ const { withinCollection, withinProfile } = this.host;
814
+ let sortParams = this.host.sortParam ? [this.host.sortParam] : [];
815
+ // TODO eventually the PPS should handle these defaults natively
816
+ const isDefaultProfileSort = withinProfile && this.host.selectedSort === SortField.default;
817
+ if (isDefaultProfileSort && this.host.defaultSortField) {
818
+ const sortOption = SORT_OPTIONS[this.host.defaultSortField];
819
+ if (sortOption.searchServiceKey) {
820
+ sortParams = [
821
+ {
822
+ field: sortOption.searchServiceKey,
823
+ direction: 'desc',
824
+ },
825
+ ];
826
+ }
827
+ }
828
+ const params = {
829
+ ...this.pageSpecifierParams,
830
+ query: trimmedQuery || '',
831
+ page: pageNumber,
832
+ rows: numRows,
833
+ sort: sortParams,
834
+ filters: this.filterMap,
835
+ aggregations: { omit: true },
836
+ };
837
+ params.uid = await this.requestUID(params, 'hits');
838
+ // log('=== FIRING PAGE REQUEST ===', params);
839
+ const searchResponse = await ((_b = this.host.searchService) === null || _b === void 0 ? void 0 : _b.search(params, this.host.searchType));
840
+ // log('=== RECEIVED PAGE RESPONSE IN CB ===', searchResponse);
841
+ const success = searchResponse === null || searchResponse === void 0 ? void 0 : searchResponse.success;
842
+ // This is checking to see if the fetch has been invalidated since it was fired off.
843
+ // If so, we just want to discard the response since it is for an obsolete query state.
844
+ if (!this.fetchesInProgress.has(currentPageKey))
845
+ return;
846
+ for (let i = 0; i < numPages; i += 1) {
847
+ this.fetchesInProgress.delete(`${pageFetchQueryKey}-p:${pageNumber + i}`);
848
+ }
849
+ if (!success) {
850
+ const errorMsg = (_c = searchResponse === null || searchResponse === void 0 ? void 0 : searchResponse.error) === null || _c === void 0 ? void 0 : _c.message;
851
+ const detailMsg = (_e = (_d = searchResponse === null || searchResponse === void 0 ? void 0 : searchResponse.error) === null || _d === void 0 ? void 0 : _d.details) === null || _e === void 0 ? void 0 : _e.message;
852
+ this.queryErrorMessage = `${errorMsg !== null && errorMsg !== void 0 ? errorMsg : ''}${detailMsg ? `; ${detailMsg}` : ''}`;
853
+ if (!this.queryErrorMessage) {
854
+ this.queryErrorMessage = 'Missing or malformed response from backend';
855
+ // @ts-ignore: Property 'Sentry' does not exist on type 'Window & typeof globalThis'
856
+ (_g = (_f = window === null || window === void 0 ? void 0 : window.Sentry) === null || _f === void 0 ? void 0 : _f.captureMessage) === null || _g === void 0 ? void 0 : _g.call(_f, this.queryErrorMessage, 'error');
857
+ }
858
+ this.setSearchResultsLoading(false);
859
+ this.requestHostUpdate();
860
+ return;
861
+ }
862
+ this.setTotalResultCount(success.response.totalResults - this.offset);
863
+ if (this.activeOnHost && this.totalResults === 0) {
864
+ // display event to offshoot when result count is zero.
865
+ this.host.emitEmptyResults();
866
+ }
867
+ this.sessionContext = success.sessionContext;
868
+ if (withinCollection) {
869
+ this.collectionExtraInfo = success.response.collectionExtraInfo;
870
+ // For collections, we want the UI to respect the default sort option
871
+ // which can be specified in metadata, or otherwise assumed to be week:desc
872
+ if (this.activeOnHost) {
873
+ this.host.applyDefaultCollectionSort(this.collectionExtraInfo);
874
+ }
875
+ if (this.collectionExtraInfo) {
876
+ this.parentCollections = [].concat((_j = (_h = this.collectionExtraInfo.public_metadata) === null || _h === void 0 ? void 0 : _h.collection) !== null && _j !== void 0 ? _j : []);
877
+ }
878
+ }
879
+ else if (withinProfile) {
880
+ this.accountExtraInfo = success.response.accountExtraInfo;
881
+ this.pageElements = success.response.pageElements;
882
+ }
883
+ const { results, collectionTitles } = success.response;
884
+ if (results && results.length > 0) {
885
+ // Load any collection titles present on the response into the cache,
886
+ // or queue up preload fetches for them if none were present.
887
+ if (collectionTitles) {
888
+ for (const [id, title] of Object.entries(collectionTitles)) {
889
+ this.collectionTitles.set(id, title);
890
+ }
891
+ // Also add the target collection's title if available
892
+ const targetTitle = (_l = (_k = this.collectionExtraInfo) === null || _k === void 0 ? void 0 : _k.public_metadata) === null || _l === void 0 ? void 0 : _l.title;
893
+ if (withinCollection && targetTitle) {
894
+ this.collectionTitles.set(withinCollection, targetTitle);
895
+ }
896
+ }
897
+ // Update the data source for each returned page.
898
+ // For loans and web archives, we must account for receiving more pages than we asked for.
899
+ const isUnpagedElement = ['lending', 'web_archives'].includes(this.host.profileElement);
900
+ if (isUnpagedElement) {
901
+ numPages = Math.ceil(results.length / this.pageSize);
902
+ this.endOfDataReached = true;
903
+ if (this.activeOnHost)
904
+ this.host.setTileCount(this.totalResults);
905
+ }
906
+ for (let i = 0; i < numPages; i += 1) {
907
+ const pageStartIndex = this.pageSize * i;
908
+ this.addFetchedResultsToDataSource(pageNumber + i, results.slice(pageStartIndex, pageStartIndex + this.pageSize), !isUnpagedElement || i === numPages - 1);
909
+ }
910
+ }
911
+ // When we reach the end of the data, we can set the infinite scroller's
912
+ // item count to the real total number of results (rather than the
913
+ // temporary estimates based on pages rendered so far).
914
+ if (this.size >= this.totalResults || results.length === 0) {
915
+ this.endOfDataReached = true;
916
+ if (this.activeOnHost)
917
+ this.host.setTileCount(this.size);
918
+ }
919
+ this.setSearchResultsLoading(false);
920
+ this.requestHostUpdate();
921
+ }
922
+ /**
923
+ * Update the datasource from the fetch response
924
+ *
925
+ * @param pageNumber
926
+ * @param results
927
+ */
928
+ addFetchedResultsToDataSource(pageNumber, results, needsReload = true) {
929
+ const tiles = [];
930
+ results === null || results === void 0 ? void 0 : results.forEach(result => {
931
+ if (!result.identifier)
932
+ return;
933
+ tiles.push(new TileModel(result));
934
+ });
935
+ this.addPage(pageNumber, tiles);
936
+ if (needsReload) {
937
+ this.refreshVisibleResults();
938
+ }
939
+ }
940
+ /**
941
+ * Fetches the aggregation buckets for the given prefix filter type.
942
+ */
943
+ async fetchPrefixFilterBuckets(filterType) {
944
+ var _a, _b, _c, _d, _e, _f, _g;
945
+ const trimmedQuery = (_a = this.host.baseQuery) === null || _a === void 0 ? void 0 : _a.trim();
946
+ if (!this.canPerformSearch)
947
+ return [];
948
+ const filterAggregationKey = prefixFilterAggregationKeys[filterType];
949
+ const sortParams = this.host.sortParam ? [this.host.sortParam] : [];
950
+ const params = {
951
+ ...this.pageSpecifierParams,
952
+ query: trimmedQuery || '',
953
+ rows: 0,
954
+ filters: this.filterMap,
955
+ // Only fetch the firstTitle or firstCreator aggregation
956
+ aggregations: { simpleParams: [filterAggregationKey] },
957
+ // Fetch all 26 letter buckets
958
+ aggregationsSize: 26,
959
+ };
960
+ params.uid = await this.requestUID({ ...params, sort: sortParams }, 'aggregations');
961
+ const searchResponse = await ((_b = this.host.searchService) === null || _b === void 0 ? void 0 : _b.search(params, this.host.searchType));
962
+ return ((_g = (_f = (_e = (_d = (_c = searchResponse === null || searchResponse === void 0 ? void 0 : searchResponse.success) === null || _c === void 0 ? void 0 : _c.response) === null || _d === void 0 ? void 0 : _d.aggregations) === null || _e === void 0 ? void 0 : _e[filterAggregationKey]) === null || _f === void 0 ? void 0 : _f.buckets) !== null && _g !== void 0 ? _g : []);
963
+ }
964
+ /**
965
+ * Fetches and caches the prefix filter counts for the given filter type.
966
+ */
967
+ async updatePrefixFilterCounts(filterType) {
968
+ const { facetFetchQueryKey } = this;
969
+ const buckets = await this.fetchPrefixFilterBuckets(filterType);
970
+ // Don't update the filter counts for an outdated query (if it has been changed
971
+ // since we sent the request)
972
+ const queryChangedSinceFetch = facetFetchQueryKey !== this.facetFetchQueryKey;
973
+ if (queryChangedSinceFetch)
974
+ return;
975
+ // Unpack the aggregation buckets into a simple map like { 'A': 50, 'B': 25, ... }
976
+ this.prefixFilterCountMap = { ...this.prefixFilterCountMap }; // Clone the object to trigger an update
977
+ this.prefixFilterCountMap[filterType] = buckets.reduce((acc, bucket) => {
978
+ acc[bucket.key.toUpperCase()] = bucket.doc_count;
979
+ return acc;
980
+ }, {});
981
+ this.requestHostUpdate();
982
+ }
983
+ /**
984
+ * @inheritdoc
985
+ */
986
+ async updatePrefixFiltersForCurrentSort() {
987
+ if (['title', 'creator'].includes(this.host.selectedSort)) {
988
+ const filterType = this.host.selectedSort;
989
+ if (!this.prefixFilterCountMap[filterType]) {
990
+ this.updatePrefixFilterCounts(filterType);
991
+ }
992
+ }
993
+ }
994
+ /**
995
+ * @inheritdoc
996
+ */
997
+ refreshLetterCounts() {
998
+ if (Object.keys(this.prefixFilterCountMap).length > 0) {
999
+ this.prefixFilterCountMap = {};
1000
+ }
1001
+ this.updatePrefixFiltersForCurrentSort();
1002
+ this.requestHostUpdate();
1003
+ }
1004
+ }
1002
1005
  //# sourceMappingURL=collection-browser-data-source.js.map