svelte-common 5.0.1 → 5.0.2
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.
- package/package.json +1 -1
- package/src/pagination.mjs +20 -14
package/package.json
CHANGED
package/src/pagination.mjs
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Pagination support store.
|
|
3
|
+
* Pages go from 1 ... numberOfPages
|
|
3
4
|
*/
|
|
4
5
|
export class Pagination {
|
|
5
6
|
subscriptions = new Set();
|
|
6
7
|
data;
|
|
7
8
|
itemsPerPage;
|
|
8
|
-
#page =
|
|
9
|
+
#page = 1;
|
|
9
10
|
|
|
10
11
|
constructor(source, itemsPerPage = 10) {
|
|
11
12
|
this.data = [...source];
|
|
@@ -17,7 +18,7 @@ export class Pagination {
|
|
|
17
18
|
* @param {number} n
|
|
18
19
|
*/
|
|
19
20
|
set page(n) {
|
|
20
|
-
if (this.#page !== n && n
|
|
21
|
+
if (this.#page !== n && n >= 1 && n <= this.numberOfPages) {
|
|
21
22
|
this.#page = n;
|
|
22
23
|
this.subscriptions.forEach(subscription => subscription(this));
|
|
23
24
|
}
|
|
@@ -43,7 +44,7 @@ export class Pagination {
|
|
|
43
44
|
}
|
|
44
45
|
|
|
45
46
|
*items() {
|
|
46
|
-
const n = this.page;
|
|
47
|
+
const n = this.page - 1;
|
|
47
48
|
|
|
48
49
|
for (const item of this.data.slice(
|
|
49
50
|
n * this.itemsPerPage,
|
|
@@ -64,17 +65,25 @@ export class Pagination {
|
|
|
64
65
|
this.subscribe(pg => {
|
|
65
66
|
const items = [];
|
|
66
67
|
|
|
67
|
-
|
|
68
|
+
const add = (innerText, targetPage, label) => {
|
|
68
69
|
const a = document.createElement("a");
|
|
69
70
|
a.setAttribute("href", "#");
|
|
71
|
+
if (label) {
|
|
72
|
+
a.setAttribute("aria-label", label);
|
|
73
|
+
}
|
|
70
74
|
a.innerText = innerText;
|
|
71
|
-
|
|
75
|
+
if (targetPage === this.page) {
|
|
76
|
+
a.disabled = true;
|
|
77
|
+
a.setAttribute("aria-current", "page");
|
|
78
|
+
} else {
|
|
79
|
+
a.onclick = () => (this.page = targetPage);
|
|
80
|
+
}
|
|
72
81
|
items.push(a);
|
|
73
82
|
return a;
|
|
74
|
-
}
|
|
83
|
+
};
|
|
75
84
|
|
|
76
|
-
add("<<",
|
|
77
|
-
add("<",
|
|
85
|
+
add("<<", 1, "First Page");
|
|
86
|
+
add("<", this.page - 1, "Previous Page");
|
|
78
87
|
|
|
79
88
|
for (let n = 1; n < this.numberOfPages; n++) {
|
|
80
89
|
if (
|
|
@@ -84,15 +93,12 @@ export class Pagination {
|
|
|
84
93
|
n % 10 === 0 ||
|
|
85
94
|
(n < this.page + 3 && n > this.page - 3))
|
|
86
95
|
) {
|
|
87
|
-
|
|
88
|
-
if (n === this.page) {
|
|
89
|
-
a.setAttribute("aria-current", "page");
|
|
90
|
-
}
|
|
96
|
+
add(String(n), n);
|
|
91
97
|
}
|
|
92
98
|
}
|
|
93
99
|
|
|
94
|
-
add(">",
|
|
95
|
-
add(">>",
|
|
100
|
+
add(">", this.page + 1, "Next Page");
|
|
101
|
+
add(">>", this.numberOfPages, "Last Page");
|
|
96
102
|
|
|
97
103
|
nav.replaceChildren(...items);
|
|
98
104
|
});
|