@internetarchive/collection-browser 0.2.21 → 0.2.22

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 (70) hide show
  1. package/dist/src/app-root.d.ts +1 -0
  2. package/dist/src/app-root.js +34 -4
  3. package/dist/src/app-root.js.map +1 -1
  4. package/dist/src/assets/img/icons/arrow-left.d.ts +2 -0
  5. package/dist/src/assets/img/icons/arrow-left.js +10 -0
  6. package/dist/src/assets/img/icons/arrow-left.js.map +1 -0
  7. package/dist/src/assets/img/icons/arrow-right.d.ts +2 -0
  8. package/dist/src/assets/img/icons/arrow-right.js +10 -0
  9. package/dist/src/assets/img/icons/arrow-right.js.map +1 -0
  10. package/dist/src/collection-browser.d.ts +2 -0
  11. package/dist/src/collection-browser.js +10 -2
  12. package/dist/src/collection-browser.js.map +1 -1
  13. package/dist/src/collection-facets/facets-template.d.ts +16 -0
  14. package/dist/src/collection-facets/facets-template.js +266 -0
  15. package/dist/src/collection-facets/facets-template.js.map +1 -0
  16. package/dist/src/collection-facets/facets-util.d.ts +10 -0
  17. package/dist/src/collection-facets/facets-util.js +20 -0
  18. package/dist/src/collection-facets/facets-util.js.map +1 -0
  19. package/dist/src/collection-facets/more-facets-content.d.ts +83 -0
  20. package/dist/src/collection-facets/more-facets-content.js +475 -0
  21. package/dist/src/collection-facets/more-facets-content.js.map +1 -0
  22. package/dist/src/collection-facets/more-facets-pagination.d.ts +36 -0
  23. package/dist/src/collection-facets/more-facets-pagination.js +267 -0
  24. package/dist/src/collection-facets/more-facets-pagination.js.map +1 -0
  25. package/dist/src/collection-facets.d.ts +19 -5
  26. package/dist/src/collection-facets.js +138 -239
  27. package/dist/src/collection-facets.js.map +1 -1
  28. package/dist/src/models.d.ts +4 -1
  29. package/dist/src/models.js +24 -0
  30. package/dist/src/models.js.map +1 -1
  31. package/dist/src/restoration-state-handler.js +5 -6
  32. package/dist/src/restoration-state-handler.js.map +1 -1
  33. package/dist/src/tiles/collection-browser-loading-tile.js +2 -5
  34. package/dist/src/tiles/collection-browser-loading-tile.js.map +1 -1
  35. package/dist/test/collection-browser.test.js +5 -3
  36. package/dist/test/collection-browser.test.js.map +1 -1
  37. package/dist/test/collection-facets/facets-template.test.d.ts +1 -0
  38. package/dist/test/collection-facets/facets-template.test.js +75 -0
  39. package/dist/test/collection-facets/facets-template.test.js.map +1 -0
  40. package/dist/test/collection-facets/facets-util.test.d.ts +1 -0
  41. package/dist/test/collection-facets/facets-util.test.js +13 -0
  42. package/dist/test/collection-facets/facets-util.test.js.map +1 -0
  43. package/dist/test/collection-facets/more-facets-content.test.d.ts +1 -0
  44. package/dist/test/collection-facets/more-facets-content.test.js +104 -0
  45. package/dist/test/collection-facets/more-facets-content.test.js.map +1 -0
  46. package/dist/test/collection-facets/more-facets-pagination.test.d.ts +1 -0
  47. package/dist/test/collection-facets/more-facets-pagination.test.js +133 -0
  48. package/dist/test/collection-facets/more-facets-pagination.test.js.map +1 -0
  49. package/dist/test/collection-facets.test.d.ts +1 -0
  50. package/dist/test/collection-facets.test.js +98 -33
  51. package/dist/test/collection-facets.test.js.map +1 -1
  52. package/package.json +11 -4
  53. package/src/app-root.ts +34 -4
  54. package/src/assets/img/icons/arrow-left.ts +10 -0
  55. package/src/assets/img/icons/arrow-right.ts +10 -0
  56. package/src/collection-browser.ts +9 -2
  57. package/src/collection-facets/facets-template.ts +284 -0
  58. package/src/collection-facets/facets-util.ts +22 -0
  59. package/src/collection-facets/more-facets-content.ts +529 -0
  60. package/src/collection-facets/more-facets-pagination.ts +297 -0
  61. package/src/collection-facets.ts +175 -261
  62. package/src/models.ts +28 -1
  63. package/src/restoration-state-handler.ts +7 -6
  64. package/src/tiles/collection-browser-loading-tile.ts +2 -5
  65. package/test/collection-browser.test.ts +6 -3
  66. package/test/collection-facets/facets-template.test.ts +103 -0
  67. package/test/collection-facets/facets-util.test.ts +18 -0
  68. package/test/collection-facets/more-facets-content.test.ts +146 -0
  69. package/test/collection-facets/more-facets-pagination.test.ts +202 -0
  70. package/test/collection-facets.test.ts +127 -44
@@ -0,0 +1,297 @@
1
+ import {
2
+ css,
3
+ CSSResultGroup,
4
+ html,
5
+ LitElement,
6
+ nothing,
7
+ TemplateResult,
8
+ } from 'lit';
9
+ import { customElement, property, state } from 'lit/decorators.js';
10
+ import arrowLeftIcon from '../assets/img/icons/arrow-left';
11
+ import arrowRightIcon from '../assets/img/icons/arrow-right';
12
+
13
+ @customElement('more-facets-pagination')
14
+ export class MoreFacetsPagination extends LitElement {
15
+ /**
16
+ * Total number of pages
17
+ */
18
+ @property({ type: Number }) size!: number;
19
+
20
+ /**
21
+ * Number of pages can be moved in back/forward
22
+ */
23
+ @property({ type: Number }) step = 2;
24
+
25
+ @property({ type: Number }) currentPage: number = 1;
26
+
27
+ @state() pages?: number[] = [];
28
+
29
+ firstUpdated() {
30
+ this.observePageCount();
31
+ }
32
+
33
+ override updated(changed: Map<string, any>) {
34
+ if (changed.has('size')) {
35
+ this.observePageCount();
36
+ }
37
+ if (changed.has('currentPage')) {
38
+ this.observePageCount();
39
+ this.emitPageClick();
40
+ }
41
+ }
42
+
43
+ /** creates `this.pages` array that notes which pages to draw
44
+ * - `0` is elipses marker
45
+ * - rule: selected page is centered between -2/+2 pages
46
+ * - outlier: first page selected, show _1_ 2 3 ... N
47
+ * - outlier: second page selected, show 1 _2_ 3 4 ... N
48
+ * - outlier: last page selected, show 1 ... N-2 N-1 _N_
49
+ * - outlier: if page count = 7, & selected is either [2, 3, 4, 5, 6], show all pages
50
+ */
51
+ observePageCount() {
52
+ this.pages = []; /* `0` is elipses marker */
53
+
54
+ const paginatorMaxPagesToShow = 7;
55
+ const atMinThreshold = this.size <= paginatorMaxPagesToShow;
56
+
57
+ /** Display outliers */
58
+ if (this.size <= 5) {
59
+ // display all pages
60
+ this.pages = [...Array(this.size).keys()].map(i => i + 1);
61
+ return;
62
+ }
63
+
64
+ if (this.size === paginatorMaxPagesToShow) {
65
+ // edge: 7 pages
66
+ if (this.currentPage === 2) {
67
+ this.pages = [1, 2, 3, 4, 0, this.size];
68
+ return;
69
+ }
70
+
71
+ if (this.currentPage === this.size - 1) {
72
+ this.pages = [1, 0, 4, 5, this.size - 1, this.size];
73
+ return;
74
+ }
75
+ }
76
+
77
+ if (this.currentPage === 1) {
78
+ // first page
79
+ this.pages = [1, 2, 3, 0, this.size];
80
+ return;
81
+ }
82
+
83
+ if (this.currentPage === this.size) {
84
+ // last page
85
+ this.pages = [1, 0, this.size - 2, this.size - 1, this.size];
86
+ return;
87
+ }
88
+
89
+ if (this.currentPage === this.size - 1) {
90
+ // second last page
91
+ this.pages = [
92
+ 1,
93
+ 0,
94
+ this.size - 3,
95
+ this.size - 2,
96
+ this.size - 1,
97
+ this.size,
98
+ ];
99
+ return;
100
+ }
101
+
102
+ if (
103
+ atMinThreshold &&
104
+ this.currentPage > 1 &&
105
+ this.currentPage < paginatorMaxPagesToShow
106
+ ) {
107
+ this.pages = [...Array(this.size).keys()].map(i => i + 1);
108
+ return;
109
+ }
110
+
111
+ /* The rest here calculates the range to display in "page window" */
112
+ let startPage = this.currentPage - this.step;
113
+ let endPage = this.currentPage + this.step;
114
+
115
+ if (startPage <= 0) {
116
+ endPage += -startPage + 1;
117
+ startPage = 1;
118
+ }
119
+
120
+ if (endPage >= this.size) {
121
+ startPage = Math.max(startPage - (endPage - this.size), 1);
122
+ endPage = this.size;
123
+ }
124
+
125
+ if (startPage === 2) {
126
+ endPage -= 1;
127
+ }
128
+
129
+ if (endPage === this.size - 1) {
130
+ startPage += 1;
131
+ }
132
+
133
+ // create first page node
134
+ this.createFirstNode(startPage);
135
+
136
+ // create middle pages node
137
+ this.createMiddelNode(startPage, endPage);
138
+
139
+ // create last page node
140
+ this.createLastNode(endPage);
141
+ }
142
+
143
+ private createFirstNode(startPage: number) {
144
+ if (startPage > 1) {
145
+ this.pages?.push(1);
146
+ }
147
+ if (startPage > 2) {
148
+ this.pages?.push(0); // let's asssume 0 is for ellipsis template
149
+ }
150
+ }
151
+
152
+ private createMiddelNode(startPage: number, endPage: number) {
153
+ for (let page = startPage; page <= endPage; page += 1) {
154
+ this.pages?.push(page);
155
+ }
156
+ }
157
+
158
+ private createLastNode(endPage: number) {
159
+ if (endPage < this.size - 1) {
160
+ this.pages?.push(0); // let's asssume 0 is for ellipsis template
161
+ }
162
+ if (endPage < this.size) {
163
+ this.pages?.push(this.size);
164
+ }
165
+ }
166
+
167
+ private get getEllipsisTemplate() {
168
+ return html`<i class="ellipses">...</i>`;
169
+ }
170
+
171
+ emitPageClick() {
172
+ this.dispatchEvent(
173
+ new CustomEvent('pageNumberClicked', {
174
+ detail: { page: this.currentPage },
175
+ bubbles: true,
176
+ composed: true,
177
+ })
178
+ );
179
+ }
180
+
181
+ private onRewind() {
182
+ this.currentPage -= 1;
183
+ if (this.currentPage < 1) {
184
+ this.currentPage = 1;
185
+ }
186
+ }
187
+
188
+ private onForward() {
189
+ this.currentPage += 1;
190
+ if (this.currentPage > this.size) {
191
+ this.currentPage = this.size;
192
+ }
193
+ }
194
+
195
+ private onChange(page: number) {
196
+ this.currentPage = page;
197
+ }
198
+
199
+ private getPageTemplate(page: number) {
200
+ return html`
201
+ <button
202
+ @click=${() => this.onChange(page)}
203
+ class=${this.currentPage === page ? 'current' : ''}
204
+ data-page=${page}
205
+ >
206
+ ${page}
207
+ </button>
208
+ `;
209
+ }
210
+
211
+ private get getPagesTemplate(): TemplateResult | typeof nothing {
212
+ if (!this.pages || !this.pages.length) {
213
+ return nothing;
214
+ }
215
+
216
+ return html`
217
+ ${this.pages?.map(
218
+ page =>
219
+ html`${page !== 0
220
+ ? this.getPageTemplate(page)
221
+ : this.getEllipsisTemplate}`
222
+ )}
223
+ `;
224
+ }
225
+
226
+ render() {
227
+ return html`
228
+ <div class="facets-pagination">
229
+ <button class="arrow-icon rewind" @click=${this.onRewind}>
230
+ <span class="sr-only">Rewind pagination:</span>
231
+ ${arrowLeftIcon}
232
+ </button>
233
+ <div class="page-numbers">${this.getPagesTemplate}</div>
234
+ <button class="arrow-icon forward" @click=${this.onForward}>
235
+ <span class="sr-only">Forward pagination:</span>
236
+ ${arrowRightIcon}
237
+ </button>
238
+ </div>
239
+ `;
240
+ }
241
+
242
+ static get styles(): CSSResultGroup {
243
+ return css`
244
+ .facets-pagination {
245
+ user-select: none;
246
+ margin-top: 10px;
247
+ background-color: #eee;
248
+ text-align: center;
249
+ }
250
+ .facets-pagination button {
251
+ border: none;
252
+ background: none;
253
+ }
254
+ .facets-pagination .arrow-icon {
255
+ width: 2.5rem;
256
+ vertical-align: middle;
257
+ }
258
+ .facets-pagination .arrow-icon svg {
259
+ height: 14px;
260
+ }
261
+ .facets-pagination button,
262
+ .facets-pagination i {
263
+ background: none;
264
+ border: 0;
265
+ cursor: pointer;
266
+ border-radius: 100%;
267
+ margin: 10px 5px;
268
+ padding: 5px;
269
+ font-size: 1.4rem;
270
+ vertical-align: middle;
271
+ display: inline-block;
272
+ min-width: 2.5rem;
273
+ }
274
+ .facets-pagination i {
275
+ cursor: auto;
276
+ display: inline;
277
+ }
278
+ .facets-pagination button.current {
279
+ background: black;
280
+ color: white;
281
+ }
282
+ .page-numbers {
283
+ display: inline-block;
284
+ }
285
+ .sr-only {
286
+ position: absolute;
287
+ width: 1px;
288
+ height: 1px;
289
+ padding: 0;
290
+ margin: -1px;
291
+ overflow: hidden;
292
+ clip: rect(0, 0, 0, 0);
293
+ border: 0;
294
+ }
295
+ `;
296
+ }
297
+ }