@internetarchive/collection-browser 0.0.1-alpha.1

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 (203) hide show
  1. package/.editorconfig +29 -0
  2. package/.github/workflows/ci.yml +26 -0
  3. package/.husky/pre-commit +4 -0
  4. package/LICENSE +661 -0
  5. package/README.md +68 -0
  6. package/demo/app-root.ts +414 -0
  7. package/demo/index.html +26 -0
  8. package/dist/demo/app-root.d.ts +43 -0
  9. package/dist/demo/app-root.js +385 -0
  10. package/dist/demo/app-root.js.map +1 -0
  11. package/dist/index.d.ts +3 -0
  12. package/dist/index.js +3 -0
  13. package/dist/index.js.map +1 -0
  14. package/dist/src/assets/img/icons/audio.d.ts +1 -0
  15. package/dist/src/assets/img/icons/audio.js +9 -0
  16. package/dist/src/assets/img/icons/audio.js.map +1 -0
  17. package/dist/src/assets/img/icons/collection.d.ts +1 -0
  18. package/dist/src/assets/img/icons/collection.js +9 -0
  19. package/dist/src/assets/img/icons/collection.js.map +1 -0
  20. package/dist/src/assets/img/icons/etree.d.ts +1 -0
  21. package/dist/src/assets/img/icons/etree.js +9 -0
  22. package/dist/src/assets/img/icons/etree.js.map +1 -0
  23. package/dist/src/assets/img/icons/images.d.ts +1 -0
  24. package/dist/src/assets/img/icons/images.js +10 -0
  25. package/dist/src/assets/img/icons/images.js.map +1 -0
  26. package/dist/src/assets/img/icons/mediatype/account.d.ts +2 -0
  27. package/dist/src/assets/img/icons/mediatype/account.js +12 -0
  28. package/dist/src/assets/img/icons/mediatype/account.js.map +1 -0
  29. package/dist/src/assets/img/icons/mediatype/audio.d.ts +1 -0
  30. package/dist/src/assets/img/icons/mediatype/audio.js +11 -0
  31. package/dist/src/assets/img/icons/mediatype/audio.js.map +1 -0
  32. package/dist/src/assets/img/icons/mediatype/collection.d.ts +1 -0
  33. package/dist/src/assets/img/icons/mediatype/collection.js +9 -0
  34. package/dist/src/assets/img/icons/mediatype/collection.js.map +1 -0
  35. package/dist/src/assets/img/icons/mediatype/etree copy.d.ts +1 -0
  36. package/dist/src/assets/img/icons/mediatype/etree copy.js +9 -0
  37. package/dist/src/assets/img/icons/mediatype/etree copy.js.map +1 -0
  38. package/dist/src/assets/img/icons/mediatype/etree.d.ts +1 -0
  39. package/dist/src/assets/img/icons/mediatype/etree.js +9 -0
  40. package/dist/src/assets/img/icons/mediatype/etree.js.map +1 -0
  41. package/dist/src/assets/img/icons/mediatype/film.d.ts +1 -0
  42. package/dist/src/assets/img/icons/mediatype/film.js +13 -0
  43. package/dist/src/assets/img/icons/mediatype/film.js.map +1 -0
  44. package/dist/src/assets/img/icons/mediatype/images.d.ts +1 -0
  45. package/dist/src/assets/img/icons/mediatype/images.js +10 -0
  46. package/dist/src/assets/img/icons/mediatype/images.js.map +1 -0
  47. package/dist/src/assets/img/icons/mediatype/livemusic.d.ts +1 -0
  48. package/dist/src/assets/img/icons/mediatype/livemusic.js +7 -0
  49. package/dist/src/assets/img/icons/mediatype/livemusic.js.map +1 -0
  50. package/dist/src/assets/img/icons/mediatype/photos.d.ts +1 -0
  51. package/dist/src/assets/img/icons/mediatype/photos.js +7 -0
  52. package/dist/src/assets/img/icons/mediatype/photos.js.map +1 -0
  53. package/dist/src/assets/img/icons/mediatype/software.d.ts +1 -0
  54. package/dist/src/assets/img/icons/mediatype/software.js +10 -0
  55. package/dist/src/assets/img/icons/mediatype/software.js.map +1 -0
  56. package/dist/src/assets/img/icons/mediatype/texts.d.ts +1 -0
  57. package/dist/src/assets/img/icons/mediatype/texts.js +10 -0
  58. package/dist/src/assets/img/icons/mediatype/texts.js.map +1 -0
  59. package/dist/src/assets/img/icons/mediatype/tv.d.ts +1 -0
  60. package/dist/src/assets/img/icons/mediatype/tv.js +9 -0
  61. package/dist/src/assets/img/icons/mediatype/tv.js.map +1 -0
  62. package/dist/src/assets/img/icons/mediatype/video.d.ts +1 -0
  63. package/dist/src/assets/img/icons/mediatype/video.js +10 -0
  64. package/dist/src/assets/img/icons/mediatype/video.js.map +1 -0
  65. package/dist/src/assets/img/icons/mediatype/web.d.ts +1 -0
  66. package/dist/src/assets/img/icons/mediatype/web.js +10 -0
  67. package/dist/src/assets/img/icons/mediatype/web.js.map +1 -0
  68. package/dist/src/assets/img/icons/software.d.ts +1 -0
  69. package/dist/src/assets/img/icons/software.js +10 -0
  70. package/dist/src/assets/img/icons/software.js.map +1 -0
  71. package/dist/src/assets/img/icons/texts.d.ts +1 -0
  72. package/dist/src/assets/img/icons/texts.js +10 -0
  73. package/dist/src/assets/img/icons/texts.js.map +1 -0
  74. package/dist/src/assets/img/icons/tv.d.ts +1 -0
  75. package/dist/src/assets/img/icons/tv.js +9 -0
  76. package/dist/src/assets/img/icons/tv.js.map +1 -0
  77. package/dist/src/assets/img/icons/video.d.ts +1 -0
  78. package/dist/src/assets/img/icons/video.js +10 -0
  79. package/dist/src/assets/img/icons/video.js.map +1 -0
  80. package/dist/src/assets/img/icons/web.d.ts +1 -0
  81. package/dist/src/assets/img/icons/web.js +10 -0
  82. package/dist/src/assets/img/icons/web.js.map +1 -0
  83. package/dist/src/circular-activity-indicator.d.ts +5 -0
  84. package/dist/src/circular-activity-indicator.js +66 -0
  85. package/dist/src/circular-activity-indicator.js.map +1 -0
  86. package/dist/src/collection-browser.d.ts +151 -0
  87. package/dist/src/collection-browser.js +697 -0
  88. package/dist/src/collection-browser.js.map +1 -0
  89. package/dist/src/collection-facets.d.ts +34 -0
  90. package/dist/src/collection-facets.js +245 -0
  91. package/dist/src/collection-facets.js.map +1 -0
  92. package/dist/src/mediatype-icon.d.ts +9 -0
  93. package/dist/src/mediatype-icon.js +89 -0
  94. package/dist/src/mediatype-icon.js.map +1 -0
  95. package/dist/src/models.d.ts +23 -0
  96. package/dist/src/models.js +2 -0
  97. package/dist/src/models.js.map +1 -0
  98. package/dist/src/sort-filter-bar/alpha-bar.d.ts +10 -0
  99. package/dist/src/sort-filter-bar/alpha-bar.js +88 -0
  100. package/dist/src/sort-filter-bar/alpha-bar.js.map +1 -0
  101. package/dist/src/sort-filter-bar/sort-filter-bar.d.ts +24 -0
  102. package/dist/src/sort-filter-bar/sort-filter-bar.js +257 -0
  103. package/dist/src/sort-filter-bar/sort-filter-bar.js.map +1 -0
  104. package/dist/src/tiles/grid/account-tile.d.ts +7 -0
  105. package/dist/src/tiles/grid/account-tile.js +144 -0
  106. package/dist/src/tiles/grid/account-tile.js.map +1 -0
  107. package/dist/src/tiles/grid/collection-tile.d.ts +7 -0
  108. package/dist/src/tiles/grid/collection-tile.js +160 -0
  109. package/dist/src/tiles/grid/collection-tile.js.map +1 -0
  110. package/dist/src/tiles/grid/icons/account.d.ts +1 -0
  111. package/dist/src/tiles/grid/icons/account.js +12 -0
  112. package/dist/src/tiles/grid/icons/account.js.map +1 -0
  113. package/dist/src/tiles/grid/icons/favorite-filled.d.ts +1 -0
  114. package/dist/src/tiles/grid/icons/favorite-filled.js +11 -0
  115. package/dist/src/tiles/grid/icons/favorite-filled.js.map +1 -0
  116. package/dist/src/tiles/grid/icons/reviews.d.ts +1 -0
  117. package/dist/src/tiles/grid/icons/reviews.js +11 -0
  118. package/dist/src/tiles/grid/icons/reviews.js.map +1 -0
  119. package/dist/src/tiles/grid/icons/upload.d.ts +1 -0
  120. package/dist/src/tiles/grid/icons/upload.js +12 -0
  121. package/dist/src/tiles/grid/icons/upload.js.map +1 -0
  122. package/dist/src/tiles/grid/icons/views.d.ts +2 -0
  123. package/dist/src/tiles/grid/icons/views.js +11 -0
  124. package/dist/src/tiles/grid/icons/views.js.map +1 -0
  125. package/dist/src/tiles/grid/item-tile.d.ts +9 -0
  126. package/dist/src/tiles/grid/item-tile.js +240 -0
  127. package/dist/src/tiles/grid/item-tile.js.map +1 -0
  128. package/dist/src/tiles/list/tile-list-compact.d.ts +16 -0
  129. package/dist/src/tiles/list/tile-list-compact.js +125 -0
  130. package/dist/src/tiles/list/tile-list-compact.js.map +1 -0
  131. package/dist/src/tiles/list/tile-list-detail.d.ts +17 -0
  132. package/dist/src/tiles/list/tile-list-detail.js +181 -0
  133. package/dist/src/tiles/list/tile-list-detail.js.map +1 -0
  134. package/dist/src/tiles/list/tile-list.d.ts +21 -0
  135. package/dist/src/tiles/list/tile-list.js +229 -0
  136. package/dist/src/tiles/list/tile-list.js.map +1 -0
  137. package/dist/src/tiles/loading-tile.d.ts +5 -0
  138. package/dist/src/tiles/loading-tile.js +73 -0
  139. package/dist/src/tiles/loading-tile.js.map +1 -0
  140. package/dist/src/tiles/tile-dispatcher.d.ts +27 -0
  141. package/dist/src/tiles/tile-dispatcher.js +160 -0
  142. package/dist/src/tiles/tile-dispatcher.js.map +1 -0
  143. package/dist/src/utils/format-count.d.ts +7 -0
  144. package/dist/src/utils/format-count.js +76 -0
  145. package/dist/src/utils/format-count.js.map +1 -0
  146. package/dist/src/utils/format-date.d.ts +2 -0
  147. package/dist/src/utils/format-date.js +24 -0
  148. package/dist/src/utils/format-date.js.map +1 -0
  149. package/dist/src/utils/format-string.d.ts +2 -0
  150. package/dist/src/utils/format-string.js +7 -0
  151. package/dist/src/utils/format-string.js.map +1 -0
  152. package/dist/test/collection-browser.test.d.ts +0 -0
  153. package/dist/test/collection-browser.test.js +3 -0
  154. package/dist/test/collection-browser.test.js.map +1 -0
  155. package/dist/test/utils/format-count.test.d.ts +1 -0
  156. package/dist/test/utils/format-count.test.js +24 -0
  157. package/dist/test/utils/format-count.test.js.map +1 -0
  158. package/dist/test/utils/format-date.test.d.ts +1 -0
  159. package/dist/test/utils/format-date.test.js +18 -0
  160. package/dist/test/utils/format-date.test.js.map +1 -0
  161. package/dist/test/utils/format-string.test.d.ts +1 -0
  162. package/dist/test/utils/format-string.test.js +17 -0
  163. package/dist/test/utils/format-string.test.js.map +1 -0
  164. package/index.ts +3 -0
  165. package/package.json +95 -0
  166. package/src/assets/img/icons/mediatype/account.ts +12 -0
  167. package/src/assets/img/icons/mediatype/audio.ts +11 -0
  168. package/src/assets/img/icons/mediatype/collection.ts +9 -0
  169. package/src/assets/img/icons/mediatype/etree.ts +9 -0
  170. package/src/assets/img/icons/mediatype/film.ts +13 -0
  171. package/src/assets/img/icons/mediatype/foo.svg +5 -0
  172. package/src/assets/img/icons/mediatype/images.ts +10 -0
  173. package/src/assets/img/icons/mediatype/software.ts +10 -0
  174. package/src/assets/img/icons/mediatype/texts.ts +10 -0
  175. package/src/assets/img/icons/mediatype/tv.ts +9 -0
  176. package/src/assets/img/icons/mediatype/video.ts +10 -0
  177. package/src/assets/img/icons/mediatype/web.ts +10 -0
  178. package/src/circular-activity-indicator.ts +64 -0
  179. package/src/collection-browser.ts +756 -0
  180. package/src/collection-facets.ts +285 -0
  181. package/src/mediatype-icon.ts +83 -0
  182. package/src/models.ts +25 -0
  183. package/src/sort-filter-bar/alpha-bar.ts +86 -0
  184. package/src/sort-filter-bar/sort-filter-bar.ts +256 -0
  185. package/src/tiles/grid/account-tile.ts +141 -0
  186. package/src/tiles/grid/collection-tile.ts +157 -0
  187. package/src/tiles/grid/icons/account.ts +12 -0
  188. package/src/tiles/grid/icons/favorite-filled.ts +11 -0
  189. package/src/tiles/grid/icons/reviews.ts +11 -0
  190. package/src/tiles/grid/icons/upload.ts +12 -0
  191. package/src/tiles/grid/icons/views.ts +11 -0
  192. package/src/tiles/grid/item-tile.ts +254 -0
  193. package/src/tiles/list/tile-list.ts +227 -0
  194. package/src/tiles/loading-tile.ts +70 -0
  195. package/src/tiles/tile-dispatcher.ts +160 -0
  196. package/src/utils/format-count.ts +95 -0
  197. package/src/utils/format-date.ts +36 -0
  198. package/test/collection-browser.test.ts +1 -0
  199. package/test/utils/format-count.test.ts +32 -0
  200. package/test/utils/format-date.test.ts +22 -0
  201. package/tsconfig.json +20 -0
  202. package/web-dev-server.config.mjs +28 -0
  203. package/web-test-runner.config.mjs +41 -0
@@ -0,0 +1,697 @@
1
+ import { __decorate } from "tslib";
2
+ /* eslint-disable import/no-duplicates */
3
+ import { html, css, LitElement, nothing, } from 'lit';
4
+ import { customElement, property, query, state } from 'lit/decorators.js';
5
+ import { SearchParams, } from '@internetarchive/search-service';
6
+ import { AggregateSearchParams, } from '@internetarchive/search-service/dist/src/search-params';
7
+ import '@internetarchive/infinite-scroller';
8
+ import '@internetarchive/histogram-date-range';
9
+ import './tiles/tile-dispatcher';
10
+ import './tiles/loading-tile';
11
+ import './sort-filter-bar/sort-filter-bar';
12
+ import './collection-facets';
13
+ import './circular-activity-indicator';
14
+ let CollectionBrowser = class CollectionBrowser extends LitElement {
15
+ constructor() {
16
+ super(...arguments);
17
+ this.showDeleteButtons = false;
18
+ this.displayMode = 'grid';
19
+ this.pageSize = 50;
20
+ /**
21
+ * The page that the consumer wants to load.
22
+ */
23
+ this.initialPageNumber = 1;
24
+ /**
25
+ * This the the number of pages that we want to show.
26
+ *
27
+ * The data isn't necessarily loaded for all of the pages, but this lets us
28
+ * know how many cells we should render.
29
+ */
30
+ this.pagesToRender = this.initialPageNumber;
31
+ this.searchResultsLoading = false;
32
+ this.selectedFacets = {};
33
+ this.facetsLoading = false;
34
+ this.fullYearAggregationLoading = false;
35
+ /**
36
+ * When we're animated scrolling to the page, we don't want to fetch
37
+ * all of the pages as it scrolls so this lets us know if we're scrolling
38
+ */
39
+ this.isScrollingToCell = false;
40
+ /**
41
+ * When we've reached the end of the data, stop trying to fetch more
42
+ */
43
+ this.endOfDataReached = false;
44
+ this.placeholderCellTemplate = html `<loading-tile></loading-tile>`;
45
+ /**
46
+ * The results per page so we can paginate.
47
+ *
48
+ * This allows us to start in the middle of the search results and
49
+ * fetch data before or after the current page. If we don't have a key
50
+ * for the previous/next page, we'll fetch the next/previous page to populate it
51
+ */
52
+ this.dataSource = {};
53
+ // we only want to scroll on the very first query change
54
+ // so this keeps track of whether we've already set the initial query
55
+ this.initialQueryChangeHappened = false;
56
+ // this maps the query to the pages being fetched for that query
57
+ this.pageFetchesInProgress = {};
58
+ }
59
+ tileModelAtCellIndex(index) {
60
+ var _a;
61
+ const pageNumber = Math.floor(index / this.pageSize) + 1;
62
+ const itemIndex = index % this.pageSize;
63
+ const model = (_a = this.dataSource[pageNumber]) === null || _a === void 0 ? void 0 : _a[itemIndex];
64
+ /**
65
+ * If we encounter a model we don't have yet and we're not in the middle of an
66
+ * automated scroll, fetch the page and just return undefined.
67
+ * The datasource will be updated once the page is loaded and the cell will be rendered.
68
+ *
69
+ * We disable it during the automated scroll since we may fetch pages for cells the
70
+ * user may never see.
71
+ */
72
+ if (!model && !this.isScrollingToCell) {
73
+ this.fetchPage(pageNumber);
74
+ }
75
+ return model;
76
+ }
77
+ // this is the total number of tiles we expect if
78
+ // the data returned is a full page worth
79
+ // this is useful for putting in placeholders for the expected number of tiles
80
+ get estimatedTileCount() {
81
+ return this.pagesToRender * this.pageSize;
82
+ }
83
+ // this is the actual number of tiles in the datasource,
84
+ // which is useful for removing excess placeholder tiles
85
+ // once we reached the end of the data
86
+ get actualTileCount() {
87
+ return Object.keys(this.dataSource).reduce((acc, page) => acc + this.dataSource[page].length, 0);
88
+ }
89
+ /**
90
+ *
91
+ * @param pageNumber
92
+ * @param scroll
93
+ */
94
+ goToPage(pageNumber) {
95
+ this.initialPageNumber = pageNumber;
96
+ this.pagesToRender = pageNumber;
97
+ this.scrollToPage(pageNumber);
98
+ }
99
+ render() {
100
+ var _a, _b;
101
+ return html `
102
+ <h1>Collection Browser</h1>
103
+
104
+ <div id="query">
105
+ <ul>
106
+ <li>Base Query: ${this.baseQuery}</li>
107
+ <li>Facet Query: ${this.facetQuery}</li>
108
+ <li>Additional Query: ${this.additionalQueryClause}</li>
109
+ <li>Date Range Query: ${this.dateRangeQueryClause}</li>
110
+ <li>Sort: ${(_a = this.sortParam) === null || _a === void 0 ? void 0 : _a.field} ${(_b = this.sortParam) === null || _b === void 0 ? void 0 : _b.direction}</li>
111
+ <li>Full Query: ${this.fullQuery}</li>
112
+ </ul>
113
+ </div>
114
+
115
+ <div id="content-container">
116
+ <div id="left-column">
117
+ <div id="histogram-container">${this.histogramTemplate}</div>
118
+ <div id="facets-container">
119
+ ${this.facetsLoading ? this.loadingTemplate : nothing}
120
+ <collection-facets
121
+ @facetsChanged=${this.facetsChanged}
122
+ .aggregations=${this.aggregations}
123
+ ></collection-facets>
124
+ </div>
125
+ </div>
126
+ <div id="right-column">
127
+ ${this.searchResultsLoading ? this.loadingTemplate : nothing}
128
+ <infinite-scroller
129
+ class="${this.displayMode}"
130
+ .cellProvider=${this}
131
+ .placeholderCellTemplate=${this.placeholderCellTemplate}
132
+ @scrollThresholdReached=${this.scrollThresholdReached}
133
+ @visibleCellsChanged=${this.visibleCellsChanged}
134
+ >
135
+ </infinite-scroller>
136
+ </div>
137
+ </div>
138
+ `;
139
+ }
140
+ get loadingTemplate() {
141
+ return html `
142
+ <div class="loading-cover">
143
+ <circular-activity-indicator></circular-activity-indicator>
144
+ </div>
145
+ `;
146
+ }
147
+ get currentYearsHistogramAggregation() {
148
+ var _a;
149
+ return (_a = this.aggregations) === null || _a === void 0 ? void 0 : _a.year_histogram;
150
+ }
151
+ get histogramDataLoading() {
152
+ return this.facetsLoading || this.fullYearAggregationLoading;
153
+ }
154
+ get histogramTemplate() {
155
+ const { currentYearsHistogramAggregation, fullYearsHistogramAggregation } = this;
156
+ return html `
157
+ <histogram-date-range
158
+ .minDate=${fullYearsHistogramAggregation === null || fullYearsHistogramAggregation === void 0 ? void 0 : fullYearsHistogramAggregation.first_bucket_key}
159
+ .maxDate=${fullYearsHistogramAggregation === null || fullYearsHistogramAggregation === void 0 ? void 0 : fullYearsHistogramAggregation.last_bucket_key}
160
+ .minSelectedDate=${currentYearsHistogramAggregation === null || currentYearsHistogramAggregation === void 0 ? void 0 : currentYearsHistogramAggregation.first_bucket_key}
161
+ .maxSelectedDate=${currentYearsHistogramAggregation === null || currentYearsHistogramAggregation === void 0 ? void 0 : currentYearsHistogramAggregation.last_bucket_key}
162
+ .updateDelay=${100}
163
+ missingDataMessage="..."
164
+ ?loading=${this.histogramDataLoading}
165
+ .width=${150}
166
+ .bins=${fullYearsHistogramAggregation === null || fullYearsHistogramAggregation === void 0 ? void 0 : fullYearsHistogramAggregation.buckets}
167
+ @histogramDateRangeUpdated=${this.histogramDateRangeUpdated}
168
+ ></histogram-date-range>
169
+ `;
170
+ }
171
+ histogramDateRangeUpdated(e) {
172
+ const { minDate, maxDate } = e.detail;
173
+ this.dateRangeQueryClause = `year:[${minDate} TO ${maxDate}]`;
174
+ }
175
+ updated(changed) {
176
+ if (changed.has('displayMode') || changed.has('showDeleteButtons')) {
177
+ this.infiniteScroller.reload();
178
+ }
179
+ if (changed.has('baseQuery') ||
180
+ changed.has('additionalQueryClause') ||
181
+ changed.has('dateRangeQueryClause') ||
182
+ changed.has('sortParam') ||
183
+ changed.has('selectedFacets')) {
184
+ this.handleQueryChange();
185
+ }
186
+ if (changed.has('pagesToRender')) {
187
+ if (!this.endOfDataReached) {
188
+ this.infiniteScroller.itemCount = this.estimatedTileCount;
189
+ }
190
+ }
191
+ }
192
+ /**
193
+ * When the visible cells change from the infinite scroller, we want to emit
194
+ * which page is currently visible so the consumer can update its UI or the URL
195
+ *
196
+ * @param e
197
+ * @returns
198
+ */
199
+ visibleCellsChanged(e) {
200
+ if (this.isScrollingToCell)
201
+ return;
202
+ const { visibleCellIndices } = e.detail;
203
+ if (visibleCellIndices.length === 0)
204
+ return;
205
+ const lastVisibleCellIndex = visibleCellIndices[visibleCellIndices.length - 1];
206
+ const lastVisibleCellPage = Math.floor(lastVisibleCellIndex / this.pageSize) + 1;
207
+ const event = new CustomEvent('visiblePageChanged', {
208
+ detail: {
209
+ pageNumber: lastVisibleCellPage,
210
+ },
211
+ });
212
+ this.dispatchEvent(event);
213
+ }
214
+ async handleQueryChange() {
215
+ // only reset if the query has actually changed
216
+ if (this.pageFetchQueryKey === this.previousQueryKey)
217
+ return;
218
+ this.previousQueryKey = this.pageFetchQueryKey;
219
+ this.dataSource = {};
220
+ this.pageFetchesInProgress = {};
221
+ this.endOfDataReached = false;
222
+ this.pagesToRender = this.initialPageNumber;
223
+ if (!this.initialQueryChangeHappened && this.initialPageNumber > 1) {
224
+ this.scrollToPage(this.initialPageNumber);
225
+ }
226
+ this.initialQueryChangeHappened = true;
227
+ await Promise.all([
228
+ this.doInitialPageFetch(),
229
+ this.fetchFacets(),
230
+ this.fetchFullYearHistogram(),
231
+ ]);
232
+ }
233
+ async doInitialPageFetch() {
234
+ this.searchResultsLoading = true;
235
+ await this.fetchPage(this.initialPageNumber);
236
+ this.searchResultsLoading = false;
237
+ }
238
+ get fullQuery() {
239
+ let { fullQueryWithoutDate } = this;
240
+ const { dateRangeQueryClause } = this;
241
+ if (dateRangeQueryClause) {
242
+ fullQueryWithoutDate += ` AND ${dateRangeQueryClause}`;
243
+ }
244
+ return fullQueryWithoutDate;
245
+ }
246
+ get fullQueryWithoutDate() {
247
+ if (!this.baseQuery)
248
+ return undefined;
249
+ let fullQuery = this.baseQuery;
250
+ const { facetQuery, additionalQueryClause } = this;
251
+ if (facetQuery) {
252
+ fullQuery += ` AND ${facetQuery}`;
253
+ }
254
+ if (additionalQueryClause) {
255
+ fullQuery += ` AND ${additionalQueryClause}`;
256
+ }
257
+ return fullQuery;
258
+ }
259
+ get facetQuery() {
260
+ const facetQuery = [];
261
+ for (const [facetName, selectedValues] of Object.entries(this.selectedFacets)) {
262
+ const values = [];
263
+ for (const value of selectedValues) {
264
+ values.push(`${facetName}:"${value}"`);
265
+ }
266
+ const valueQuery = values.join(' OR ');
267
+ facetQuery.push(`(${valueQuery})`);
268
+ }
269
+ return facetQuery.length > 0 ? `(${facetQuery.join(' AND ')})` : undefined;
270
+ }
271
+ facetsChanged(e) {
272
+ this.selectedFacets = e.detail;
273
+ }
274
+ async fetchFacets() {
275
+ var _a, _b;
276
+ if (!this.fullQuery)
277
+ return;
278
+ const aggregations = new AggregateSearchParams({
279
+ advancedParams: [
280
+ {
281
+ field: 'subjectSorter',
282
+ size: 6,
283
+ },
284
+ {
285
+ field: 'mediatypeSorter',
286
+ size: 6,
287
+ },
288
+ {
289
+ field: 'languageSorter',
290
+ size: 6,
291
+ },
292
+ {
293
+ field: 'creatorSorter',
294
+ size: 6,
295
+ },
296
+ {
297
+ field: 'collection',
298
+ size: 12,
299
+ },
300
+ {
301
+ field: 'year',
302
+ size: 50,
303
+ },
304
+ ],
305
+ });
306
+ const params = new SearchParams({
307
+ query: this.fullQuery,
308
+ fields: ['identifier'],
309
+ aggregations,
310
+ rows: 1,
311
+ });
312
+ this.facetsLoading = true;
313
+ const results = await ((_a = this.searchService) === null || _a === void 0 ? void 0 : _a.search(params));
314
+ this.facetsLoading = false;
315
+ this.aggregations = (_b = results === null || results === void 0 ? void 0 : results.success) === null || _b === void 0 ? void 0 : _b.response.aggregations;
316
+ }
317
+ /**
318
+ * The query key is a string that uniquely identifies the current query
319
+ * without the date range.
320
+ *
321
+ * If this doesn't change, we don't need to re-fetch the histogram date range
322
+ */
323
+ get fullQueryNoDateKey() {
324
+ var _a;
325
+ return `${this.fullQueryWithoutDate}-${(_a = this.sortParam) === null || _a === void 0 ? void 0 : _a.asString}`;
326
+ }
327
+ /**
328
+ * This method is similar to fetching the facets above,
329
+ * but only fetching the year histogram. There is a subtle difference
330
+ * in how you have to fetch the year histogram where you can't use the
331
+ * advanced JSON syntax like the other aggregations. It's a special
332
+ * case that @ximm put it place.
333
+ */
334
+ async fetchFullYearHistogram() {
335
+ var _a, _b, _c, _d;
336
+ const { fullQueryNoDateKey } = this;
337
+ if (!this.fullQueryWithoutDate ||
338
+ fullQueryNoDateKey === this.previousFullQueryNoDate)
339
+ return;
340
+ this.previousFullQueryNoDate = fullQueryNoDateKey;
341
+ const aggregations = new AggregateSearchParams({
342
+ simpleParams: ['year'],
343
+ });
344
+ const params = new SearchParams({
345
+ query: this.fullQueryWithoutDate,
346
+ fields: ['identifier'],
347
+ aggregations,
348
+ rows: 1,
349
+ });
350
+ this.fullYearAggregationLoading = true;
351
+ const results = await ((_a = this.searchService) === null || _a === void 0 ? void 0 : _a.search(params));
352
+ this.fullYearAggregationLoading = false;
353
+ this.fullYearsHistogramAggregation =
354
+ (_d = (_c = (_b = results === null || results === void 0 ? void 0 : results.success) === null || _b === void 0 ? void 0 : _b.response) === null || _c === void 0 ? void 0 : _c.aggregations) === null || _d === void 0 ? void 0 : _d.year_histogram;
355
+ }
356
+ scrollToPage(pageNumber) {
357
+ const cellIndexToScrollTo = this.pageSize * (pageNumber - 1);
358
+ // without this setTimeout, Safari just pauses until the `fetchPage` is complete
359
+ // then scrolls to the cell
360
+ setTimeout(() => {
361
+ this.isScrollingToCell = true;
362
+ this.infiniteScroller.scrollToCell(cellIndexToScrollTo, true);
363
+ // This timeout is to give the scroll animation time to finish
364
+ // then updating the infinite scroller once we're done scrolling
365
+ // There's no scroll animation completion callback so we're
366
+ // giving it 0.5s to finish.
367
+ setTimeout(() => {
368
+ this.isScrollingToCell = false;
369
+ this.infiniteScroller.reload();
370
+ }, 500);
371
+ }, 0);
372
+ }
373
+ /**
374
+ * The query key is a string that uniquely identifies the current query
375
+ *
376
+ * This lets us keep track of queries so we don't persist data that's
377
+ * no longer relevant.
378
+ */
379
+ get pageFetchQueryKey() {
380
+ var _a;
381
+ return `${this.fullQuery}-${(_a = this.sortParam) === null || _a === void 0 ? void 0 : _a.asString}`;
382
+ }
383
+ async fetchPage(pageNumber) {
384
+ var _a, _b, _c, _d;
385
+ if (!this.fullQuery)
386
+ return;
387
+ // if we already have data, don't fetch again
388
+ if (this.dataSource[pageNumber])
389
+ return;
390
+ if (this.endOfDataReached)
391
+ return;
392
+ // if a fetch is already in progress for this query and page, don't fetch again
393
+ const { pageFetchQueryKey } = this;
394
+ const pageFetches = (_a = this.pageFetchesInProgress[pageFetchQueryKey]) !== null && _a !== void 0 ? _a : new Set();
395
+ if (pageFetches.has(pageNumber))
396
+ return;
397
+ pageFetches.add(pageNumber);
398
+ this.pageFetchesInProgress[pageFetchQueryKey] = pageFetches;
399
+ const sortParams = this.sortParam ? [this.sortParam] : [];
400
+ const params = new SearchParams({
401
+ query: this.fullQuery,
402
+ fields: [
403
+ 'identifier',
404
+ 'title',
405
+ 'mediatype',
406
+ 'downloads',
407
+ 'avg_rating',
408
+ 'num_favorites',
409
+ 'num_reviews',
410
+ 'item_count',
411
+ 'description',
412
+ 'date',
413
+ 'addeddate',
414
+ 'publicdate',
415
+ 'reviewdate',
416
+ 'creator',
417
+ 'subject',
418
+ 'source',
419
+ 'collection',
420
+ ],
421
+ page: pageNumber,
422
+ rows: this.pageSize,
423
+ sort: sortParams,
424
+ });
425
+ const results = await ((_b = this.searchService) === null || _b === void 0 ? void 0 : _b.search(params));
426
+ const success = results === null || results === void 0 ? void 0 : results.success;
427
+ if (!success)
428
+ return;
429
+ // this is checking to see if the query has changed since the data was fetched
430
+ // if so, we just want to discard the data since there should be a new query
431
+ // right behind it
432
+ const searchQuery = success.responseHeader.params.qin;
433
+ const searchSort = success.responseHeader.params.sort;
434
+ const queryChangedSinceFetch = searchQuery !== this.fullQuery || searchSort !== ((_c = this.sortParam) === null || _c === void 0 ? void 0 : _c.asString);
435
+ if (queryChangedSinceFetch)
436
+ return;
437
+ const { docs } = success.response;
438
+ if (docs && docs.length > 0) {
439
+ this.updateDataSource(pageNumber, docs);
440
+ }
441
+ if (docs.length < this.pageSize) {
442
+ this.endOfDataReached = true;
443
+ // this updates the infinite scroller to show the actual size
444
+ this.infiniteScroller.itemCount = this.actualTileCount;
445
+ }
446
+ (_d = this.pageFetchesInProgress[pageFetchQueryKey]) === null || _d === void 0 ? void 0 : _d.delete(pageNumber);
447
+ this.searchResultsLoading = false;
448
+ }
449
+ /**
450
+ * This is useful for determining whether we need to reload the scroller.
451
+ *
452
+ * When the fetch completes, we need to reload the scroller if the cells for that
453
+ * page are visible, but if the page is not currenlty visible, we don't need to reload
454
+ */
455
+ get currentVisiblePageNumbers() {
456
+ const visibleCells = this.infiniteScroller.getVisibleCellIndices();
457
+ const visiblePages = new Set();
458
+ visibleCells.forEach(cellIndex => {
459
+ const visiblePage = Math.floor(cellIndex / this.pageSize) + 1;
460
+ visiblePages.add(visiblePage);
461
+ });
462
+ return Array.from(visiblePages);
463
+ }
464
+ /**
465
+ * Update the datasource from the fetch response
466
+ *
467
+ * @param pageNumber
468
+ * @param docs
469
+ */
470
+ updateDataSource(pageNumber, docs) {
471
+ // copy our existing datasource so when we set it below, it gets set
472
+ // instead of modifying the existing dataSource since object changes
473
+ // don't trigger a re-render
474
+ const datasource = { ...this.dataSource };
475
+ const tiles = [];
476
+ docs === null || docs === void 0 ? void 0 : docs.forEach(doc => {
477
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y;
478
+ if (!doc.identifier)
479
+ return;
480
+ tiles.push({
481
+ identifier: doc.identifier,
482
+ title: this.etreeTitle((_a = doc.title) === null || _a === void 0 ? void 0 : _a.value, (_b = doc.mediatype) === null || _b === void 0 ? void 0 : _b.value),
483
+ mediatype: (_d = (_c = doc.mediatype) === null || _c === void 0 ? void 0 : _c.value) !== null && _d !== void 0 ? _d : 'data',
484
+ viewCount: (_f = (_e = doc.downloads) === null || _e === void 0 ? void 0 : _e.value) !== null && _f !== void 0 ? _f : 0,
485
+ favCount: (_h = (_g = doc.num_favorites) === null || _g === void 0 ? void 0 : _g.value) !== null && _h !== void 0 ? _h : 0,
486
+ commentCount: (_k = (_j = doc.num_reviews) === null || _j === void 0 ? void 0 : _j.value) !== null && _k !== void 0 ? _k : 0,
487
+ itemCount: (_m = (_l = doc.item_count) === null || _l === void 0 ? void 0 : _l.value) !== null && _m !== void 0 ? _m : 0,
488
+ description: (_o = doc.description) === null || _o === void 0 ? void 0 : _o.value,
489
+ dateAdded: (_p = doc.addeddate) === null || _p === void 0 ? void 0 : _p.value,
490
+ dateArchived: (_q = doc.publicdate) === null || _q === void 0 ? void 0 : _q.value,
491
+ dateReviewed: (_r = doc.reviewdate) === null || _r === void 0 ? void 0 : _r.value,
492
+ datePublished: (_s = doc.date) === null || _s === void 0 ? void 0 : _s.value,
493
+ creator: (_t = doc.creator) === null || _t === void 0 ? void 0 : _t.value,
494
+ averageRating: (_u = doc.avg_rating) === null || _u === void 0 ? void 0 : _u.value,
495
+ subject: (_v = doc.subject) === null || _v === void 0 ? void 0 : _v.value,
496
+ source: (_w = doc.source) === null || _w === void 0 ? void 0 : _w.value,
497
+ collection: (_y = (_x = doc.collection) === null || _x === void 0 ? void 0 : _x.values) !== null && _y !== void 0 ? _y : [],
498
+ });
499
+ });
500
+ datasource[pageNumber] = tiles;
501
+ this.dataSource = datasource;
502
+ const visiblePages = this.currentVisiblePageNumbers;
503
+ const needsReload = visiblePages.includes(pageNumber);
504
+ if (needsReload) {
505
+ this.infiniteScroller.reload();
506
+ }
507
+ }
508
+ /*
509
+ * Convert etree titles
510
+ * "[Creator] Live at [Place] on [Date]" => "[Date]: [Place]"
511
+ *
512
+ * Todo: Check collection(s) for etree, need to get as array.
513
+ * Current search-service only returns first collection as string.
514
+ */
515
+ etreeTitle(title, mediatype) {
516
+ if (mediatype === 'etree') {
517
+ // || collections.includes('etree')) {
518
+ const regex = /^(.*) Live at (.*) on (\d\d\d\d-\d\d-\d\d)$/;
519
+ const newTitle = title === null || title === void 0 ? void 0 : title.replace(regex, '$3: $2');
520
+ if (newTitle) {
521
+ return `${newTitle}`;
522
+ }
523
+ }
524
+ return title !== null && title !== void 0 ? title : '';
525
+ }
526
+ cellForIndex(index) {
527
+ const model = this.tileModelAtCellIndex(index);
528
+ if (!model)
529
+ return undefined;
530
+ return html ` <tile-dispatcher
531
+ .baseNavigationUrl=${this.baseNavigationUrl}
532
+ .model=${model}
533
+ .displayMode=${this.displayMode}
534
+ .resizeObserver=${this.resizeObserver}
535
+ .sortParam=${this.sortParam}
536
+ ?showDeleteButton=${this.showDeleteButtons}
537
+ ></tile-dispatcher>`;
538
+ }
539
+ /**
540
+ * When the user scrolls near to the bottom of the page, fetch the next page
541
+ * increase the number of pages to render and start fetching data for the new page
542
+ */
543
+ scrollThresholdReached() {
544
+ this.pagesToRender += 1;
545
+ this.fetchPage(this.pagesToRender);
546
+ }
547
+ };
548
+ CollectionBrowser.styles = css `
549
+ :host {
550
+ display: block;
551
+ }
552
+
553
+ #content-container {
554
+ display: flex;
555
+ }
556
+
557
+ #right-column {
558
+ flex: 1;
559
+ position: relative;
560
+ }
561
+
562
+ #left-column {
563
+ width: 15rem;
564
+ }
565
+
566
+ #facets-container {
567
+ position: relative;
568
+ }
569
+
570
+ .loading-cover {
571
+ position: absolute;
572
+ top: 0;
573
+ left: 0;
574
+ width: 100%;
575
+ height: 100%;
576
+ background-color: rgba(255, 255, 255, 0.75);
577
+ display: flex;
578
+ justify-content: center;
579
+ z-index: 1;
580
+ padding-top: 50px;
581
+ }
582
+
583
+ circular-activity-indicator {
584
+ width: 30px;
585
+ height: 30px;
586
+ }
587
+
588
+ infinite-scroller {
589
+ display: block;
590
+ --infiniteScrollerRowGap: var(--collectionBrowserRowGap, 1.7rem);
591
+ --infiniteScrollerColGap: var(--collectionBrowserColGap, 1.7rem);
592
+ }
593
+
594
+ infinite-scroller.list-compact {
595
+ --infiniteScrollerCellMinWidth: var(
596
+ --collectionBrowserCellMinWidth,
597
+ 100%
598
+ );
599
+ --infiniteScrollerCellMinHeight: var(
600
+ --collectionBrowserCellMinHeight,
601
+ 2rem
602
+ );
603
+ --infiniteScrollerCellMaxHeight: var(
604
+ --collectionBrowserCellMaxHeight,
605
+ 2rem
606
+ );
607
+ }
608
+
609
+ infinite-scroller.list-detail {
610
+ --infiniteScrollerCellMinWidth: var(
611
+ --collectionBrowserCellMinWidth,
612
+ 100%
613
+ );
614
+ --infiniteScrollerCellMinHeight: var(
615
+ --collectionBrowserCellMinHeight,
616
+ 5rem
617
+ );
618
+ }
619
+
620
+ infinite-scroller.grid {
621
+ --infiniteScrollerCellMinWidth: var(
622
+ --collectionBrowserCellMinWidth,
623
+ 18rem
624
+ );
625
+ --infiniteScrollerCellMaxWidth: var(--collectionBrowserCellMaxWidth, 1fr);
626
+ --infiniteScrollerCellMinHeight: var(
627
+ --collectionBrowserCellMinHeight,
628
+ 29rem
629
+ );
630
+ --infiniteScrollerCellMaxHeight: var(
631
+ --collectionBrowserCellMaxHeight,
632
+ 29rem
633
+ );
634
+ }
635
+ `;
636
+ __decorate([
637
+ property({ type: String })
638
+ ], CollectionBrowser.prototype, "baseNavigationUrl", void 0);
639
+ __decorate([
640
+ property({ type: Object })
641
+ ], CollectionBrowser.prototype, "searchService", void 0);
642
+ __decorate([
643
+ property({ type: String })
644
+ ], CollectionBrowser.prototype, "baseQuery", void 0);
645
+ __decorate([
646
+ property({ type: Boolean })
647
+ ], CollectionBrowser.prototype, "showDeleteButtons", void 0);
648
+ __decorate([
649
+ property({ type: String })
650
+ ], CollectionBrowser.prototype, "displayMode", void 0);
651
+ __decorate([
652
+ property({ type: Object })
653
+ ], CollectionBrowser.prototype, "sortParam", void 0);
654
+ __decorate([
655
+ property({ type: String })
656
+ ], CollectionBrowser.prototype, "additionalQueryClause", void 0);
657
+ __decorate([
658
+ property({ type: String })
659
+ ], CollectionBrowser.prototype, "dateRangeQueryClause", void 0);
660
+ __decorate([
661
+ property({ type: Number })
662
+ ], CollectionBrowser.prototype, "pageSize", void 0);
663
+ __decorate([
664
+ property({ type: Object })
665
+ ], CollectionBrowser.prototype, "resizeObserver", void 0);
666
+ __decorate([
667
+ query('collection-facets')
668
+ ], CollectionBrowser.prototype, "collectionFacets", void 0);
669
+ __decorate([
670
+ state()
671
+ ], CollectionBrowser.prototype, "pagesToRender", void 0);
672
+ __decorate([
673
+ state()
674
+ ], CollectionBrowser.prototype, "searchResultsLoading", void 0);
675
+ __decorate([
676
+ state()
677
+ ], CollectionBrowser.prototype, "selectedFacets", void 0);
678
+ __decorate([
679
+ state()
680
+ ], CollectionBrowser.prototype, "facetsLoading", void 0);
681
+ __decorate([
682
+ state()
683
+ ], CollectionBrowser.prototype, "fullYearAggregationLoading", void 0);
684
+ __decorate([
685
+ state()
686
+ ], CollectionBrowser.prototype, "aggregations", void 0);
687
+ __decorate([
688
+ state()
689
+ ], CollectionBrowser.prototype, "fullYearsHistogramAggregation", void 0);
690
+ __decorate([
691
+ query('infinite-scroller')
692
+ ], CollectionBrowser.prototype, "infiniteScroller", void 0);
693
+ CollectionBrowser = __decorate([
694
+ customElement('collection-browser')
695
+ ], CollectionBrowser);
696
+ export { CollectionBrowser };
697
+ //# sourceMappingURL=collection-browser.js.map