@internetarchive/collection-browser 1.8.0 → 1.9.0-alpha2

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 (269) hide show
  1. package/.editorconfig +29 -29
  2. package/.github/workflows/ci.yml +26 -26
  3. package/.github/workflows/gh-pages-main.yml +39 -39
  4. package/.github/workflows/npm-publish.yml +39 -39
  5. package/.github/workflows/pr-preview.yml +38 -38
  6. package/.husky/pre-commit +4 -4
  7. package/LICENSE +661 -661
  8. package/README.md +83 -83
  9. package/dist/index.d.ts +9 -9
  10. package/dist/index.js +9 -9
  11. package/dist/src/app-root.d.ts +54 -54
  12. package/dist/src/app-root.js +293 -293
  13. package/dist/src/assets/img/icons/arrow-left.d.ts +2 -2
  14. package/dist/src/assets/img/icons/arrow-left.js +2 -2
  15. package/dist/src/assets/img/icons/arrow-right.d.ts +2 -2
  16. package/dist/src/assets/img/icons/arrow-right.js +2 -2
  17. package/dist/src/assets/img/icons/chevron.d.ts +2 -2
  18. package/dist/src/assets/img/icons/chevron.js +2 -2
  19. package/dist/src/assets/img/icons/contract.d.ts +2 -2
  20. package/dist/src/assets/img/icons/contract.js +2 -2
  21. package/dist/src/assets/img/icons/empty-query.d.ts +2 -2
  22. package/dist/src/assets/img/icons/empty-query.js +2 -2
  23. package/dist/src/assets/img/icons/expand.d.ts +2 -2
  24. package/dist/src/assets/img/icons/expand.js +2 -2
  25. package/dist/src/assets/img/icons/eye-closed.d.ts +2 -2
  26. package/dist/src/assets/img/icons/eye-closed.js +2 -2
  27. package/dist/src/assets/img/icons/eye.d.ts +2 -2
  28. package/dist/src/assets/img/icons/eye.js +2 -2
  29. package/dist/src/assets/img/icons/favorite-filled.d.ts +1 -1
  30. package/dist/src/assets/img/icons/favorite-filled.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/software.d.ts +1 -1
  50. package/dist/src/assets/img/icons/mediatype/software.js +2 -2
  51. package/dist/src/assets/img/icons/mediatype/texts.d.ts +1 -1
  52. package/dist/src/assets/img/icons/mediatype/texts.js +2 -2
  53. package/dist/src/assets/img/icons/mediatype/tv.d.ts +1 -1
  54. package/dist/src/assets/img/icons/mediatype/tv.js +2 -2
  55. package/dist/src/assets/img/icons/mediatype/video.d.ts +1 -1
  56. package/dist/src/assets/img/icons/mediatype/video.js +2 -2
  57. package/dist/src/assets/img/icons/mediatype/web.d.ts +1 -1
  58. package/dist/src/assets/img/icons/mediatype/web.js +2 -2
  59. package/dist/src/assets/img/icons/null-result.d.ts +2 -2
  60. package/dist/src/assets/img/icons/null-result.js +2 -2
  61. package/dist/src/assets/img/icons/restricted.d.ts +1 -1
  62. package/dist/src/assets/img/icons/restricted.js +2 -2
  63. package/dist/src/assets/img/icons/reviews.d.ts +1 -1
  64. package/dist/src/assets/img/icons/reviews.js +2 -2
  65. package/dist/src/assets/img/icons/upload.d.ts +1 -1
  66. package/dist/src/assets/img/icons/upload.js +2 -2
  67. package/dist/src/assets/img/icons/views.d.ts +1 -1
  68. package/dist/src/assets/img/icons/views.js +2 -2
  69. package/dist/src/circular-activity-indicator.d.ts +5 -5
  70. package/dist/src/circular-activity-indicator.js +17 -17
  71. package/dist/src/collection-browser.d.ts +469 -469
  72. package/dist/src/collection-browser.js +1677 -1653
  73. package/dist/src/collection-browser.js.map +1 -1
  74. package/dist/src/collection-facets/facet-tombstone-row.d.ts +5 -5
  75. package/dist/src/collection-facets/facet-tombstone-row.js +15 -15
  76. package/dist/src/collection-facets/facets-template.d.ts +20 -20
  77. package/dist/src/collection-facets/facets-template.js +152 -152
  78. package/dist/src/collection-facets/more-facets-content.d.ts +77 -77
  79. package/dist/src/collection-facets/more-facets-content.js +359 -359
  80. package/dist/src/collection-facets/more-facets-pagination.d.ts +36 -36
  81. package/dist/src/collection-facets/more-facets-pagination.js +196 -196
  82. package/dist/src/collection-facets/toggle-switch.d.ts +41 -41
  83. package/dist/src/collection-facets/toggle-switch.js +94 -94
  84. package/dist/src/collection-facets.d.ts +99 -99
  85. package/dist/src/collection-facets.js +471 -471
  86. package/dist/src/empty-placeholder.d.ts +21 -21
  87. package/dist/src/empty-placeholder.js +69 -69
  88. package/dist/src/expanded-date-picker.d.ts +43 -43
  89. package/dist/src/expanded-date-picker.js +109 -109
  90. package/dist/src/language-code-handler/language-code-handler.d.ts +37 -37
  91. package/dist/src/language-code-handler/language-code-handler.js +26 -26
  92. package/dist/src/language-code-handler/language-code-mapping.d.ts +1 -1
  93. package/dist/src/language-code-handler/language-code-mapping.js +562 -562
  94. package/dist/src/mediatype/mediatype-config.d.ts +3 -3
  95. package/dist/src/mediatype/mediatype-config.js +85 -85
  96. package/dist/src/models.d.ts +162 -149
  97. package/dist/src/models.js +256 -195
  98. package/dist/src/models.js.map +1 -1
  99. package/dist/src/restoration-state-handler.d.ts +70 -63
  100. package/dist/src/restoration-state-handler.js +355 -326
  101. package/dist/src/restoration-state-handler.js.map +1 -1
  102. package/dist/src/sort-filter-bar/alpha-bar-tooltip.d.ts +6 -6
  103. package/dist/src/sort-filter-bar/alpha-bar-tooltip.js +24 -24
  104. package/dist/src/sort-filter-bar/alpha-bar.d.ts +21 -21
  105. package/dist/src/sort-filter-bar/alpha-bar.js +128 -128
  106. package/dist/src/sort-filter-bar/img/compact.d.ts +1 -1
  107. package/dist/src/sort-filter-bar/img/compact.js +2 -2
  108. package/dist/src/sort-filter-bar/img/list.d.ts +1 -1
  109. package/dist/src/sort-filter-bar/img/list.js +2 -2
  110. package/dist/src/sort-filter-bar/img/sort-toggle-disabled.d.ts +1 -1
  111. package/dist/src/sort-filter-bar/img/sort-toggle-disabled.js +2 -2
  112. package/dist/src/sort-filter-bar/img/sort-toggle-down.d.ts +1 -1
  113. package/dist/src/sort-filter-bar/img/sort-toggle-down.js +2 -2
  114. package/dist/src/sort-filter-bar/img/sort-toggle-up.d.ts +1 -1
  115. package/dist/src/sort-filter-bar/img/sort-toggle-up.js +2 -2
  116. package/dist/src/sort-filter-bar/img/sort-triangle.d.ts +1 -1
  117. package/dist/src/sort-filter-bar/img/sort-triangle.js +2 -2
  118. package/dist/src/sort-filter-bar/img/tile.d.ts +1 -1
  119. package/dist/src/sort-filter-bar/img/tile.js +2 -2
  120. package/dist/src/sort-filter-bar/sort-filter-bar.d.ts +201 -199
  121. package/dist/src/sort-filter-bar/sort-filter-bar.js +622 -617
  122. package/dist/src/sort-filter-bar/sort-filter-bar.js.map +1 -1
  123. package/dist/src/styles/item-image-styles.d.ts +8 -8
  124. package/dist/src/styles/item-image-styles.js +9 -9
  125. package/dist/src/styles/sr-only.d.ts +1 -1
  126. package/dist/src/styles/sr-only.js +2 -2
  127. package/dist/src/tiles/base-tile-component.d.ts +18 -18
  128. package/dist/src/tiles/base-tile-component.js +59 -59
  129. package/dist/src/tiles/collection-browser-loading-tile.d.ts +5 -5
  130. package/dist/src/tiles/collection-browser-loading-tile.js +15 -15
  131. package/dist/src/tiles/grid/account-tile.d.ts +18 -18
  132. package/dist/src/tiles/grid/account-tile.js +72 -72
  133. package/dist/src/tiles/grid/collection-tile.d.ts +15 -15
  134. package/dist/src/tiles/grid/collection-tile.js +80 -80
  135. package/dist/src/tiles/grid/item-tile.d.ts +27 -27
  136. package/dist/src/tiles/grid/item-tile.js +134 -134
  137. package/dist/src/tiles/grid/styles/tile-grid-shared-styles.d.ts +1 -1
  138. package/dist/src/tiles/grid/styles/tile-grid-shared-styles.js +8 -8
  139. package/dist/src/tiles/grid/tile-stats.d.ts +11 -11
  140. package/dist/src/tiles/grid/tile-stats.js +48 -48
  141. package/dist/src/tiles/hover/hover-pane-controller.d.ts +219 -219
  142. package/dist/src/tiles/hover/hover-pane-controller.js +352 -352
  143. package/dist/src/tiles/hover/tile-hover-pane.d.ts +15 -15
  144. package/dist/src/tiles/hover/tile-hover-pane.js +38 -38
  145. package/dist/src/tiles/image-block.d.ts +17 -17
  146. package/dist/src/tiles/image-block.js +72 -72
  147. package/dist/src/tiles/item-image.d.ts +35 -35
  148. package/dist/src/tiles/item-image.js +117 -117
  149. package/dist/src/tiles/list/tile-list-compact-header.d.ts +6 -6
  150. package/dist/src/tiles/list/tile-list-compact-header.js +38 -38
  151. package/dist/src/tiles/list/tile-list-compact.d.ts +15 -15
  152. package/dist/src/tiles/list/tile-list-compact.js +114 -114
  153. package/dist/src/tiles/list/tile-list.d.ts +46 -46
  154. package/dist/src/tiles/list/tile-list.js +298 -298
  155. package/dist/src/tiles/mediatype-icon.d.ts +9 -9
  156. package/dist/src/tiles/mediatype-icon.js +47 -47
  157. package/dist/src/tiles/overlay/icon-overlay.d.ts +10 -10
  158. package/dist/src/tiles/overlay/icon-overlay.js +40 -40
  159. package/dist/src/tiles/overlay/icon-text-overlay.d.ts +9 -9
  160. package/dist/src/tiles/overlay/icon-text-overlay.js +38 -38
  161. package/dist/src/tiles/overlay/text-overlay.d.ts +10 -10
  162. package/dist/src/tiles/overlay/text-overlay.js +42 -42
  163. package/dist/src/tiles/text-snippet-block.d.ts +27 -27
  164. package/dist/src/tiles/text-snippet-block.js +73 -73
  165. package/dist/src/tiles/tile-dispatcher.d.ts +50 -50
  166. package/dist/src/tiles/tile-dispatcher.js +185 -185
  167. package/dist/src/tiles/tile-display-value-provider.d.ts +43 -43
  168. package/dist/src/tiles/tile-display-value-provider.js +80 -80
  169. package/dist/src/utils/analytics-events.d.ts +24 -24
  170. package/dist/src/utils/analytics-events.js +26 -26
  171. package/dist/src/utils/array-equals.d.ts +4 -4
  172. package/dist/src/utils/array-equals.js +10 -10
  173. package/dist/src/utils/format-count.d.ts +7 -7
  174. package/dist/src/utils/format-count.js +76 -76
  175. package/dist/src/utils/format-date.d.ts +2 -2
  176. package/dist/src/utils/format-date.js +25 -25
  177. package/dist/src/utils/format-unit-size.d.ts +2 -2
  178. package/dist/src/utils/format-unit-size.js +33 -33
  179. package/dist/src/utils/local-date-from-utc.d.ts +9 -9
  180. package/dist/src/utils/local-date-from-utc.js +15 -15
  181. package/dist/src/utils/sha1.d.ts +2 -2
  182. package/dist/src/utils/sha1.js +8 -8
  183. package/dist/test/collection-browser.test.d.ts +1 -1
  184. package/dist/test/collection-browser.test.js +808 -808
  185. package/dist/test/collection-facets/facets-template.test.d.ts +1 -1
  186. package/dist/test/collection-facets/facets-template.test.js +134 -134
  187. package/dist/test/collection-facets/more-facets-content.test.d.ts +1 -1
  188. package/dist/test/collection-facets/more-facets-content.test.js +133 -133
  189. package/dist/test/collection-facets/more-facets-pagination.test.d.ts +1 -1
  190. package/dist/test/collection-facets/more-facets-pagination.test.js +117 -117
  191. package/dist/test/collection-facets/toggle-switch.test.d.ts +1 -1
  192. package/dist/test/collection-facets/toggle-switch.test.js +73 -73
  193. package/dist/test/collection-facets.test.d.ts +2 -2
  194. package/dist/test/collection-facets.test.js +645 -645
  195. package/dist/test/empty-placeholder.test.d.ts +1 -1
  196. package/dist/test/empty-placeholder.test.js +56 -56
  197. package/dist/test/expanded-date-picker.test.d.ts +1 -1
  198. package/dist/test/expanded-date-picker.test.js +95 -95
  199. package/dist/test/icon-overlay.test.d.ts +1 -1
  200. package/dist/test/icon-overlay.test.js +24 -24
  201. package/dist/test/image-block.test.d.ts +1 -1
  202. package/dist/test/image-block.test.js +48 -48
  203. package/dist/test/item-image.test.d.ts +1 -1
  204. package/dist/test/item-image.test.js +84 -84
  205. package/dist/test/mediatype-config.test.d.ts +1 -1
  206. package/dist/test/mediatype-config.test.js +16 -16
  207. package/dist/test/mocks/mock-analytics-handler.d.ts +10 -10
  208. package/dist/test/mocks/mock-analytics-handler.js +15 -15
  209. package/dist/test/mocks/mock-collection-name-cache.d.ts +9 -9
  210. package/dist/test/mocks/mock-collection-name-cache.js +17 -17
  211. package/dist/test/mocks/mock-search-responses.d.ts +19 -19
  212. package/dist/test/mocks/mock-search-responses.js +623 -623
  213. package/dist/test/mocks/mock-search-service.d.ts +15 -15
  214. package/dist/test/mocks/mock-search-service.js +48 -48
  215. package/dist/test/restoration-state-handler.test.d.ts +1 -1
  216. package/dist/test/restoration-state-handler.test.js +270 -218
  217. package/dist/test/restoration-state-handler.test.js.map +1 -1
  218. package/dist/test/sort-filter-bar/alpha-bar-tooltip.test.d.ts +1 -1
  219. package/dist/test/sort-filter-bar/alpha-bar-tooltip.test.js +12 -12
  220. package/dist/test/sort-filter-bar/alpha-bar.test.d.ts +1 -1
  221. package/dist/test/sort-filter-bar/alpha-bar.test.js +73 -73
  222. package/dist/test/sort-filter-bar/sort-filter-bar.test.d.ts +1 -1
  223. package/dist/test/sort-filter-bar/sort-filter-bar.test.js +378 -378
  224. package/dist/test/text-overlay.test.d.ts +1 -1
  225. package/dist/test/text-overlay.test.js +48 -48
  226. package/dist/test/text-snippet-block.test.d.ts +1 -1
  227. package/dist/test/text-snippet-block.test.js +57 -57
  228. package/dist/test/tile-stats.test.d.ts +1 -1
  229. package/dist/test/tile-stats.test.js +33 -33
  230. package/dist/test/tiles/grid/account-tile.test.d.ts +1 -1
  231. package/dist/test/tiles/grid/account-tile.test.js +76 -76
  232. package/dist/test/tiles/grid/collection-tile.test.d.ts +1 -1
  233. package/dist/test/tiles/grid/collection-tile.test.js +73 -73
  234. package/dist/test/tiles/grid/item-tile.test.d.ts +1 -1
  235. package/dist/test/tiles/grid/item-tile.test.js +254 -254
  236. package/dist/test/tiles/hover/hover-pane-controller.test.d.ts +1 -1
  237. package/dist/test/tiles/hover/hover-pane-controller.test.js +257 -257
  238. package/dist/test/tiles/hover/tile-hover-pane.test.d.ts +1 -1
  239. package/dist/test/tiles/hover/tile-hover-pane.test.js +13 -13
  240. package/dist/test/tiles/list/tile-list-compact.test.d.ts +1 -1
  241. package/dist/test/tiles/list/tile-list-compact.test.js +143 -143
  242. package/dist/test/tiles/list/tile-list.test.d.ts +1 -1
  243. package/dist/test/tiles/list/tile-list.test.js +242 -242
  244. package/dist/test/tiles/tile-dispatcher.test.d.ts +1 -1
  245. package/dist/test/tiles/tile-dispatcher.test.js +67 -67
  246. package/dist/test/tiles/tile-display-value-provider.test.d.ts +1 -1
  247. package/dist/test/tiles/tile-display-value-provider.test.js +141 -141
  248. package/dist/test/utils/array-equals.test.d.ts +1 -1
  249. package/dist/test/utils/array-equals.test.js +26 -26
  250. package/dist/test/utils/format-count.test.d.ts +1 -1
  251. package/dist/test/utils/format-count.test.js +23 -23
  252. package/dist/test/utils/format-date.test.d.ts +1 -1
  253. package/dist/test/utils/format-date.test.js +17 -17
  254. package/dist/test/utils/format-unit-size.test.d.ts +1 -1
  255. package/dist/test/utils/format-unit-size.test.js +17 -17
  256. package/dist/test/utils/local-date-from-utc.test.d.ts +1 -1
  257. package/dist/test/utils/local-date-from-utc.test.js +26 -26
  258. package/local.archive.org.cert +86 -86
  259. package/local.archive.org.key +27 -27
  260. package/package.json +1 -1
  261. package/renovate.json +6 -6
  262. package/src/collection-browser.ts +42 -8
  263. package/src/models.ts +193 -109
  264. package/src/restoration-state-handler.ts +66 -40
  265. package/src/sort-filter-bar/sort-filter-bar.ts +34 -27
  266. package/test/restoration-state-handler.test.ts +68 -1
  267. package/tsconfig.json +21 -21
  268. package/web-dev-server.config.mjs +30 -30
  269. package/web-test-runner.config.mjs +41 -41
@@ -1,327 +1,356 @@
1
- import { SearchType, } from '@internetarchive/search-service';
2
- import { getCookie, setCookie } from 'typescript-cookie';
3
- import { URLFieldToSortField, getDefaultSelectedFacets, MetadataFieldToURLField, } from './models';
4
- import { arrayEquals } from './utils/array-equals';
5
- export class RestorationStateHandler {
6
- constructor(options) {
7
- this.cookieDomain = '.archive.org';
8
- this.cookieExpiration = 30;
9
- this.cookiePath = '/';
10
- this.context = options.context;
11
- }
12
- persistState(state) {
13
- if (state.displayMode)
14
- this.persistViewStateToCookies(state.displayMode);
15
- this.persistQueryStateToUrl(state);
16
- }
17
- getRestorationState() {
18
- const restorationState = this.loadQueryStateFromUrl();
19
- const displayMode = this.loadTileViewStateFromCookies();
20
- restorationState.displayMode = displayMode;
21
- return restorationState;
22
- }
23
- persistViewStateToCookies(displayMode) {
24
- const gridState = displayMode === 'grid' ? 'tiles' : 'lists';
25
- setCookie(`view-${this.context}`, gridState, {
26
- domain: this.cookieDomain,
27
- expires: this.cookieExpiration,
28
- path: this.cookiePath,
29
- });
30
- const detailsState = displayMode === 'list-detail' ? 'showdetails' : '';
31
- setCookie(`showdetails-${this.context}`, detailsState, {
32
- domain: this.cookieDomain,
33
- expires: this.cookieExpiration,
34
- path: this.cookiePath,
35
- });
36
- }
37
- loadTileViewStateFromCookies() {
38
- const viewState = getCookie(`view-${this.context}`);
39
- const detailsState = getCookie(`showdetails-${this.context}`);
40
- if (viewState === 'tiles' || viewState === undefined)
41
- return 'grid';
42
- if (detailsState === 'showdetails')
43
- return 'list-detail';
44
- return 'list-compact';
45
- }
46
- persistQueryStateToUrl(state) {
47
- var _a, _b;
48
- const url = new URL(window.location.href);
49
- const oldParams = new URLSearchParams(url.searchParams);
50
- const newParams = this.removeRecognizedParams(url.searchParams);
51
- let replaceEmptySin = false;
52
- if (state.baseQuery) {
53
- newParams.set('query', state.baseQuery);
54
- }
55
- if (state.searchType === SearchType.FULLTEXT) {
56
- newParams.set('sin', 'TXT');
57
- }
58
- if (oldParams.get('sin') === '') {
59
- // Treat empty sin the same as no sin at all
60
- oldParams.delete('sin');
61
- replaceEmptySin = true;
62
- }
63
- if (state.currentPage) {
64
- if (state.currentPage > 1) {
65
- newParams.set('page', state.currentPage.toString());
66
- }
67
- else {
68
- newParams.delete('page');
69
- }
70
- }
71
- if (state.sortParam) {
72
- const prefix = state.sortParam.direction === 'desc' ? '-' : '';
73
- const readableSortField = MetadataFieldToURLField[state.sortParam.field];
74
- newParams.set('sort', `${prefix}${readableSortField}`);
75
- }
76
- if (state.selectedFacets) {
77
- for (const [facetName, facetValues] of Object.entries(state.selectedFacets)) {
78
- const facetEntries = Object.entries(facetValues);
79
- // eslint-disable-next-line no-continue
80
- if (facetEntries.length === 0)
81
- continue;
82
- for (const [key, data] of facetEntries) {
83
- const notValue = data.state === 'hidden';
84
- const paramValue = `${facetName}:"${key}"`;
85
- if (notValue) {
86
- newParams.append('not[]', paramValue);
87
- }
88
- else {
89
- newParams.append('and[]', paramValue);
90
- }
91
- }
92
- }
93
- }
94
- if (state.minSelectedDate && state.maxSelectedDate) {
95
- newParams.append('and[]', `year:[${state.minSelectedDate} TO ${state.maxSelectedDate}]`);
96
- }
97
- if (state.titleQuery) {
98
- newParams.append('and[]', state.titleQuery);
99
- }
100
- if (state.creatorQuery) {
101
- newParams.append('and[]', state.creatorQuery);
102
- }
103
- // Ensure we aren't pushing consecutive identical states to the history stack.
104
- // - If the state has changed, we push a new history entry.
105
- // - If only the page number has changed, we replace the current history entry.
106
- // - If the state hasn't changed, then do nothing.
107
- let historyMethod = 'pushState';
108
- const nonQueryParamsMatch = this.paramsMatch(oldParams, newParams, [
109
- 'sin',
110
- 'sort',
111
- 'and[]',
112
- 'not[]',
113
- ]);
114
- if (nonQueryParamsMatch &&
115
- this.paramsMatch(oldParams, newParams, ['query'])) {
116
- if (replaceEmptySin) {
117
- // Get rid of any empty sin param
118
- newParams.delete('sin');
119
- }
120
- else if (this.paramsMatch(oldParams, newParams, ['page'])) {
121
- // For page number, we want to replace the page state when it changes,
122
- // not push a new history entry. If it hasn't changed, then we're done.
123
- return;
124
- }
125
- historyMethod = 'replaceState';
126
- }
127
- else if (nonQueryParamsMatch && this.hasLegacyParam(oldParams)) {
128
- // Similarly, if the only non-matching param was a legacy query param, then
129
- // we just want to overwrite it.
130
- historyMethod = 'replaceState';
131
- }
132
- (_b = (_a = window.history)[historyMethod]) === null || _b === void 0 ? void 0 : _b.call(_a, {
133
- query: state.baseQuery,
134
- searchType: state.searchType,
135
- page: state.currentPage,
136
- sort: state.sortParam,
137
- minDate: state.minSelectedDate,
138
- maxDate: state.maxSelectedDate,
139
- facets: state.selectedFacets,
140
- }, '', url);
141
- }
142
- loadQueryStateFromUrl() {
143
- var _a;
144
- const url = new URL(window.location.href);
145
- const searchInside = url.searchParams.get('sin');
146
- const pageNumber = url.searchParams.get('page');
147
- const searchQuery = url.searchParams.get('query');
148
- const sortQuery = url.searchParams.get('sort');
149
- const facetAnds = url.searchParams.getAll('and[]');
150
- const facetNots = url.searchParams.getAll('not[]');
151
- // We also need to check for the presence of params like 'and[0]', 'not[1]', etc.
152
- // since Facebook automatically converts URLs with [] into those forms.
153
- for (const [key, val] of url.searchParams.entries()) {
154
- if (/and\[\d+\]/.test(key)) {
155
- facetAnds.push(val);
156
- }
157
- else if (/not\[\d+\]/.test(key)) {
158
- facetNots.push(val);
159
- }
160
- }
161
- // Legacy search allowed `q` and `search` params for the query, so in the interest
162
- // of backwards-compatibility with old bookmarks, we recognize those here too.
163
- // (However, they still get upgraded to a `query` param when we persist our state
164
- // to the URL).
165
- const legacySearchQuery = (_a = url.searchParams.get('q')) !== null && _a !== void 0 ? _a : url.searchParams.get('search');
166
- const restorationState = {
167
- selectedFacets: getDefaultSelectedFacets(),
168
- };
169
- if (searchQuery) {
170
- restorationState.baseQuery = searchQuery;
171
- }
172
- else if (legacySearchQuery) {
173
- restorationState.baseQuery = legacySearchQuery;
174
- }
175
- switch (searchInside) {
176
- // Eventually there will be TV/Radio search types here too.
177
- case 'TXT':
178
- restorationState.searchType = SearchType.FULLTEXT;
179
- break;
180
- default:
181
- restorationState.searchType = SearchType.METADATA;
182
- break;
183
- }
184
- if (pageNumber) {
185
- const parsed = parseInt(pageNumber, 10);
186
- restorationState.currentPage = parsed;
187
- }
188
- else {
189
- restorationState.currentPage = 1;
190
- }
191
- if (sortQuery) {
192
- // check for two different sort formats: `date desc` and `-date`
193
- const hasSpace = sortQuery.indexOf(' ') > -1;
194
- if (hasSpace) {
195
- const [field, direction] = sortQuery.split(' ');
196
- const metadataField = URLFieldToSortField[field];
197
- if (metadataField) {
198
- restorationState.selectedSort = metadataField;
199
- }
200
- if (direction === 'desc' || direction === 'asc') {
201
- restorationState.sortDirection = direction;
202
- }
203
- }
204
- else {
205
- const direction = sortQuery.startsWith('-') ? 'desc' : 'asc';
206
- const field = sortQuery.startsWith('-')
207
- ? sortQuery.slice(1)
208
- : sortQuery;
209
- const metadataField = URLFieldToSortField[field];
210
- if (metadataField) {
211
- restorationState.selectedSort = metadataField;
212
- restorationState.sortDirection = direction;
213
- }
214
- }
215
- }
216
- if (facetAnds) {
217
- facetAnds.forEach(and => {
218
- // eslint-disable-next-line prefer-const
219
- let [field, value] = and.split(':');
220
- // Legacy search allowed and[] fields like 'creatorSorter', 'languageSorter', etc.
221
- // which we want to normalize to 'creator', 'language', etc. if redirected here.
222
- field = field.replace(/Sorter$/, '');
223
- switch (field) {
224
- case 'year': {
225
- const [minDate, maxDate] = value.split(' TO ');
226
- // we have two potential ways of filtering by date:
227
- // the range with "date TO date" or the single date with "date"
228
- // this is checking for the range case and if we don't have those, fall
229
- // back to the single date case
230
- if (minDate && maxDate) {
231
- restorationState.minSelectedDate = minDate.substring(1, minDate.length);
232
- restorationState.maxSelectedDate = maxDate.substring(0, maxDate.length - 1);
233
- }
234
- else {
235
- this.setSelectedFacetState(restorationState.selectedFacets, field, value, 'selected');
236
- }
237
- break;
238
- }
239
- case 'firstTitle':
240
- restorationState.selectedTitleFilter = value;
241
- break;
242
- case 'firstCreator':
243
- restorationState.selectedCreatorFilter = value;
244
- break;
245
- default:
246
- this.setSelectedFacetState(restorationState.selectedFacets, field, value, 'selected');
247
- }
248
- });
249
- }
250
- if (facetNots) {
251
- facetNots.forEach(not => {
252
- const [field, value] = not.split(':');
253
- this.setSelectedFacetState(restorationState.selectedFacets, field, value, 'hidden');
254
- });
255
- }
256
- return restorationState;
257
- }
258
- // remove optional opening and closing quotes from a string
259
- stripQuotes(value) {
260
- if (value.startsWith('"') && value.endsWith('"')) {
261
- return value.substring(1, value.length - 1);
262
- }
263
- return value;
264
- }
265
- /**
266
- * Returns whether the two given URLSearchParams objects have
267
- * identical values for all of the given param keys. If either
268
- * object contains more than one value for a given key, then
269
- * all of the values for that key must match (disregarding order).
270
- */
271
- paramsMatch(searchParams1, searchParams2, keys) {
272
- return keys.every(key => arrayEquals(searchParams1.getAll(key).sort(), searchParams2.getAll(key).sort()));
273
- }
274
- /**
275
- * Deletes any params from the given URLSearchParams object that are recognized
276
- * when loading state from the URL.
277
- */
278
- removeRecognizedParams(searchParams) {
279
- // Remove all of our standard params
280
- searchParams.delete('query');
281
- searchParams.delete('sin');
282
- searchParams.delete('page');
283
- searchParams.delete('sort');
284
- searchParams.delete('and[]');
285
- searchParams.delete('not[]');
286
- // Remove any and/not facet params that contain numbers in their square brackets
287
- for (const key of searchParams.keys()) {
288
- if (/(and|not)\[\d+\]/.test(key)) {
289
- searchParams.delete(key);
290
- }
291
- }
292
- // Also remove some legacy params that should have been upgraded to the ones above
293
- searchParams.delete('q');
294
- searchParams.delete('search');
295
- return searchParams;
296
- }
297
- /**
298
- * Returns whether the given URLSearchParams object contains a param that is
299
- * only recognized as a holdover from legacy search, and should not be
300
- * persisted to the URL.
301
- */
302
- hasLegacyParam(searchParams) {
303
- return searchParams.has('q') || searchParams.has('search');
304
- }
305
- /**
306
- * Sets the facet state for the given field & value to the given state,
307
- * creating any previously-undefined buckets as needed.
308
- */
309
- setSelectedFacetState(selectedFacets, field, value, state) {
310
- var _a;
311
- const facet = selectedFacets[field];
312
- if (!facet)
313
- return; // Unrecognized facet group, ignore it.
314
- const unQuotedValue = this.stripQuotes(value);
315
- (_a = facet[unQuotedValue]) !== null && _a !== void 0 ? _a : (facet[unQuotedValue] = this.getDefaultBucket(value));
316
- facet[unQuotedValue].state = state;
317
- }
318
- /** Returns a default bucket with the given key, count of 0, and state 'none'. */
319
- getDefaultBucket(key) {
320
- return {
321
- key,
322
- count: 0,
323
- state: 'none',
324
- };
325
- }
326
- }
1
+ import { SearchType } from '@internetarchive/search-service';
2
+ import { getCookie, setCookie } from 'typescript-cookie';
3
+ import { SortField, getDefaultSelectedFacets, sortOptionFromAPIString, SORT_OPTIONS, } from './models';
4
+ import { arrayEquals } from './utils/array-equals';
5
+ export class RestorationStateHandler {
6
+ constructor(options) {
7
+ this.cookieDomain = '.archive.org';
8
+ this.cookieExpiration = 30;
9
+ this.cookiePath = '/';
10
+ this.context = options.context;
11
+ }
12
+ persistState(state) {
13
+ if (state.displayMode)
14
+ this.persistViewStateToCookies(state.displayMode);
15
+ this.persistQueryStateToUrl(state);
16
+ }
17
+ getRestorationState() {
18
+ const restorationState = this.loadQueryStateFromUrl();
19
+ const displayMode = this.loadTileViewStateFromCookies();
20
+ restorationState.displayMode = displayMode;
21
+ return restorationState;
22
+ }
23
+ persistViewStateToCookies(displayMode) {
24
+ const gridState = displayMode === 'grid' ? 'tiles' : 'lists';
25
+ setCookie(`view-${this.context}`, gridState, {
26
+ domain: this.cookieDomain,
27
+ expires: this.cookieExpiration,
28
+ path: this.cookiePath,
29
+ });
30
+ const detailsState = displayMode === 'list-detail' ? 'showdetails' : '';
31
+ setCookie(`showdetails-${this.context}`, detailsState, {
32
+ domain: this.cookieDomain,
33
+ expires: this.cookieExpiration,
34
+ path: this.cookiePath,
35
+ });
36
+ }
37
+ loadTileViewStateFromCookies() {
38
+ const viewState = getCookie(`view-${this.context}`);
39
+ const detailsState = getCookie(`showdetails-${this.context}`);
40
+ if (viewState === 'tiles' || viewState === undefined)
41
+ return 'grid';
42
+ if (detailsState === 'showdetails')
43
+ return 'list-detail';
44
+ return 'list-compact';
45
+ }
46
+ persistQueryStateToUrl(state) {
47
+ var _a, _b, _c;
48
+ const url = new URL(window.location.href);
49
+ const oldParams = new URLSearchParams(url.searchParams);
50
+ const newParams = this.removeRecognizedParams(url.searchParams);
51
+ let replaceEmptySin = false;
52
+ if (state.baseQuery) {
53
+ newParams.set('query', state.baseQuery);
54
+ }
55
+ if (state.searchType === SearchType.FULLTEXT) {
56
+ newParams.set('sin', 'TXT');
57
+ }
58
+ if (oldParams.get('sin') === '') {
59
+ // Treat empty sin the same as no sin at all
60
+ oldParams.delete('sin');
61
+ replaceEmptySin = true;
62
+ }
63
+ if (state.currentPage) {
64
+ if (state.currentPage > 1) {
65
+ newParams.set('page', state.currentPage.toString());
66
+ }
67
+ else {
68
+ newParams.delete('page');
69
+ }
70
+ }
71
+ if (state.selectedSort) {
72
+ const sortOption = SORT_OPTIONS[state.selectedSort];
73
+ let prefix = this.sortDirectionPrefix(state.sortDirection);
74
+ if (sortOption.field === SortField.unrecognized) {
75
+ // For unrecognized sorts, use the existing param, possibly updating its direction
76
+ const oldSortParam = (_a = oldParams.get('sort')) !== null && _a !== void 0 ? _a : '';
77
+ const { field, direction } = this.getSortFieldAndDirection(oldSortParam);
78
+ // Use the state-specified direction if available, or extract one from the param if not
79
+ if (!state.sortDirection)
80
+ prefix = this.sortDirectionPrefix(direction);
81
+ if (field) {
82
+ newParams.set('sort', `${prefix}${field}`);
83
+ }
84
+ else {
85
+ newParams.set('sort', oldSortParam);
86
+ }
87
+ }
88
+ else if (sortOption.shownInURL) {
89
+ // Otherwise, use the canonical API form of the sort option
90
+ const canonicalApiSort = sortOption.urlNames[0];
91
+ newParams.set('sort', `${prefix}${canonicalApiSort}`);
92
+ }
93
+ }
94
+ if (state.selectedFacets) {
95
+ for (const [facetName, facetValues] of Object.entries(state.selectedFacets)) {
96
+ const facetEntries = Object.entries(facetValues);
97
+ // eslint-disable-next-line no-continue
98
+ if (facetEntries.length === 0)
99
+ continue;
100
+ for (const [key, data] of facetEntries) {
101
+ const notValue = data.state === 'hidden';
102
+ const paramValue = `${facetName}:"${key}"`;
103
+ if (notValue) {
104
+ newParams.append('not[]', paramValue);
105
+ }
106
+ else {
107
+ newParams.append('and[]', paramValue);
108
+ }
109
+ }
110
+ }
111
+ }
112
+ if (state.minSelectedDate && state.maxSelectedDate) {
113
+ newParams.append('and[]', `year:[${state.minSelectedDate} TO ${state.maxSelectedDate}]`);
114
+ }
115
+ if (state.titleQuery) {
116
+ newParams.append('and[]', state.titleQuery);
117
+ }
118
+ if (state.creatorQuery) {
119
+ newParams.append('and[]', state.creatorQuery);
120
+ }
121
+ // Ensure we aren't pushing consecutive identical states to the history stack.
122
+ // - If the state has changed, we push a new history entry.
123
+ // - If only the page number has changed, we replace the current history entry.
124
+ // - If the state hasn't changed, then do nothing.
125
+ let historyMethod = 'pushState';
126
+ const nonQueryParamsMatch = this.paramsMatch(oldParams, newParams, [
127
+ 'sin',
128
+ 'sort',
129
+ 'and[]',
130
+ 'not[]',
131
+ ]);
132
+ if (nonQueryParamsMatch &&
133
+ this.paramsMatch(oldParams, newParams, ['query'])) {
134
+ if (replaceEmptySin) {
135
+ // Get rid of any empty sin param
136
+ newParams.delete('sin');
137
+ }
138
+ else if (this.paramsMatch(oldParams, newParams, ['page'])) {
139
+ // For page number, we want to replace the page state when it changes,
140
+ // not push a new history entry. If it hasn't changed, then we're done.
141
+ return;
142
+ }
143
+ historyMethod = 'replaceState';
144
+ }
145
+ else if (nonQueryParamsMatch && this.hasLegacyParam(oldParams)) {
146
+ // Similarly, if the only non-matching param was a legacy query param, then
147
+ // we just want to overwrite it.
148
+ historyMethod = 'replaceState';
149
+ }
150
+ (_c = (_b = window.history)[historyMethod]) === null || _c === void 0 ? void 0 : _c.call(_b, {
151
+ query: state.baseQuery,
152
+ searchType: state.searchType,
153
+ page: state.currentPage,
154
+ sort: { field: state.selectedSort, direction: state.sortDirection },
155
+ minDate: state.minSelectedDate,
156
+ maxDate: state.maxSelectedDate,
157
+ facets: state.selectedFacets,
158
+ }, '', url);
159
+ }
160
+ loadQueryStateFromUrl() {
161
+ var _a;
162
+ const url = new URL(window.location.href);
163
+ const searchInside = url.searchParams.get('sin');
164
+ const pageNumber = url.searchParams.get('page');
165
+ const searchQuery = url.searchParams.get('query');
166
+ const sortQuery = url.searchParams.get('sort');
167
+ const facetAnds = url.searchParams.getAll('and[]');
168
+ const facetNots = url.searchParams.getAll('not[]');
169
+ // We also need to check for the presence of params like 'and[0]', 'not[1]', etc.
170
+ // since Facebook automatically converts URLs with [] into those forms.
171
+ for (const [key, val] of url.searchParams.entries()) {
172
+ if (/and\[\d+\]/.test(key)) {
173
+ facetAnds.push(val);
174
+ }
175
+ else if (/not\[\d+\]/.test(key)) {
176
+ facetNots.push(val);
177
+ }
178
+ }
179
+ // Legacy search allowed `q` and `search` params for the query, so in the interest
180
+ // of backwards-compatibility with old bookmarks, we recognize those here too.
181
+ // (However, they still get upgraded to a `query` param when we persist our state
182
+ // to the URL).
183
+ const legacySearchQuery = (_a = url.searchParams.get('q')) !== null && _a !== void 0 ? _a : url.searchParams.get('search');
184
+ const restorationState = {
185
+ selectedFacets: getDefaultSelectedFacets(),
186
+ };
187
+ if (searchQuery) {
188
+ restorationState.baseQuery = searchQuery;
189
+ }
190
+ else if (legacySearchQuery) {
191
+ restorationState.baseQuery = legacySearchQuery;
192
+ }
193
+ switch (searchInside) {
194
+ // Eventually there will be TV/Radio search types here too.
195
+ case 'TXT':
196
+ restorationState.searchType = SearchType.FULLTEXT;
197
+ break;
198
+ default:
199
+ restorationState.searchType = SearchType.METADATA;
200
+ break;
201
+ }
202
+ if (pageNumber) {
203
+ const parsed = parseInt(pageNumber, 10);
204
+ restorationState.currentPage = parsed;
205
+ }
206
+ else {
207
+ restorationState.currentPage = 1;
208
+ }
209
+ if (sortQuery) {
210
+ const { field, direction } = this.getSortFieldAndDirection(sortQuery);
211
+ const sortOption = sortOptionFromAPIString(field);
212
+ restorationState.selectedSort = sortOption.field;
213
+ if (['asc', 'desc'].includes(direction)) {
214
+ restorationState.sortDirection = direction;
215
+ }
216
+ }
217
+ if (facetAnds) {
218
+ facetAnds.forEach(and => {
219
+ // eslint-disable-next-line prefer-const
220
+ let [field, value] = and.split(':');
221
+ // Legacy search allowed and[] fields like 'creatorSorter', 'languageSorter', etc.
222
+ // which we want to normalize to 'creator', 'language', etc. if redirected here.
223
+ field = field.replace(/Sorter$/, '');
224
+ // Legacy search also allowed a form of negative faceting like `and[]=-collection:foo`
225
+ // which we want to normalize to a not[] param instead
226
+ if (field.startsWith('-')) {
227
+ facetNots.push(and.slice(1));
228
+ return;
229
+ }
230
+ switch (field) {
231
+ case 'year': {
232
+ const [minDate, maxDate] = value.split(' TO ');
233
+ // we have two potential ways of filtering by date:
234
+ // the range with "date TO date" or the single date with "date"
235
+ // this is checking for the range case and if we don't have those, fall
236
+ // back to the single date case
237
+ if (minDate && maxDate) {
238
+ restorationState.minSelectedDate = minDate.substring(1, minDate.length);
239
+ restorationState.maxSelectedDate = maxDate.substring(0, maxDate.length - 1);
240
+ }
241
+ else {
242
+ this.setSelectedFacetState(restorationState.selectedFacets, field, value, 'selected');
243
+ }
244
+ break;
245
+ }
246
+ case 'firstTitle':
247
+ restorationState.selectedTitleFilter = value;
248
+ break;
249
+ case 'firstCreator':
250
+ restorationState.selectedCreatorFilter = value;
251
+ break;
252
+ default:
253
+ this.setSelectedFacetState(restorationState.selectedFacets, field, value, 'selected');
254
+ }
255
+ });
256
+ }
257
+ if (facetNots) {
258
+ facetNots.forEach(not => {
259
+ const [field, value] = not.split(':');
260
+ this.setSelectedFacetState(restorationState.selectedFacets, field, value, 'hidden');
261
+ });
262
+ }
263
+ return restorationState;
264
+ }
265
+ /**
266
+ * Converts a URL sort param into a field/direction pair, if possible.
267
+ * Either or both may be undefined if the param is not in a recognized format.
268
+ */
269
+ getSortFieldAndDirection(sortParam) {
270
+ // check for two different sort formats: `date desc` and `-date`
271
+ const hasSpace = sortParam.indexOf(' ') > -1;
272
+ let field;
273
+ let direction;
274
+ if (hasSpace) {
275
+ [field, direction] = sortParam.split(' ');
276
+ }
277
+ else {
278
+ field = sortParam.startsWith('-') ? sortParam.slice(1) : sortParam;
279
+ direction = sortParam.startsWith('-') ? 'desc' : 'asc';
280
+ }
281
+ return { field, direction };
282
+ }
283
+ /** Returns the `-` prefix for `desc` sort, or the empty string otherwise. */
284
+ sortDirectionPrefix(sortDirection) {
285
+ return sortDirection === 'desc' ? '-' : '';
286
+ }
287
+ /** Remove optional opening and closing quotes from a string */
288
+ stripQuotes(value) {
289
+ if (value.startsWith('"') && value.endsWith('"')) {
290
+ return value.substring(1, value.length - 1);
291
+ }
292
+ return value;
293
+ }
294
+ /**
295
+ * Returns whether the two given URLSearchParams objects have
296
+ * identical values for all of the given param keys. If either
297
+ * object contains more than one value for a given key, then
298
+ * all of the values for that key must match (disregarding order).
299
+ */
300
+ paramsMatch(searchParams1, searchParams2, keys) {
301
+ return keys.every(key => arrayEquals(searchParams1.getAll(key).sort(), searchParams2.getAll(key).sort()));
302
+ }
303
+ /**
304
+ * Deletes any params from the given URLSearchParams object that are recognized
305
+ * when loading state from the URL.
306
+ */
307
+ removeRecognizedParams(searchParams) {
308
+ // Remove all of our standard params
309
+ searchParams.delete('query');
310
+ searchParams.delete('sin');
311
+ searchParams.delete('page');
312
+ searchParams.delete('sort');
313
+ searchParams.delete('and[]');
314
+ searchParams.delete('not[]');
315
+ // Remove any and/not facet params that contain numbers in their square brackets
316
+ for (const key of searchParams.keys()) {
317
+ if (/(and|not)\[\d+\]/.test(key)) {
318
+ searchParams.delete(key);
319
+ }
320
+ }
321
+ // Also remove some legacy params that should have been upgraded to the ones above
322
+ searchParams.delete('q');
323
+ searchParams.delete('search');
324
+ return searchParams;
325
+ }
326
+ /**
327
+ * Returns whether the given URLSearchParams object contains a param that is
328
+ * only recognized as a holdover from legacy search, and should not be
329
+ * persisted to the URL.
330
+ */
331
+ hasLegacyParam(searchParams) {
332
+ return searchParams.has('q') || searchParams.has('search');
333
+ }
334
+ /**
335
+ * Sets the facet state for the given field & value to the given state,
336
+ * creating any previously-undefined buckets as needed.
337
+ */
338
+ setSelectedFacetState(selectedFacets, field, value, state) {
339
+ var _a;
340
+ const facet = selectedFacets[field];
341
+ if (!facet)
342
+ return; // Unrecognized facet group, ignore it.
343
+ const unQuotedValue = this.stripQuotes(value);
344
+ (_a = facet[unQuotedValue]) !== null && _a !== void 0 ? _a : (facet[unQuotedValue] = this.getDefaultBucket(value));
345
+ facet[unQuotedValue].state = state;
346
+ }
347
+ /** Returns a default bucket with the given key, count of 0, and state 'none'. */
348
+ getDefaultBucket(key) {
349
+ return {
350
+ key,
351
+ count: 0,
352
+ state: 'none',
353
+ };
354
+ }
355
+ }
327
356
  //# sourceMappingURL=restoration-state-handler.js.map