svelte-common 5.0.3 → 5.0.5
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 +2 -2
- package/src/pagination.mjs +29 -10
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "svelte-common",
|
|
3
|
-
"version": "5.0.
|
|
3
|
+
"version": "5.0.5",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
@@ -44,7 +44,7 @@
|
|
|
44
44
|
"ava": "^5.3.1",
|
|
45
45
|
"c8": "^8.0.0",
|
|
46
46
|
"documentation": "^14.0.2",
|
|
47
|
-
"mf-styling": "^2.0.
|
|
47
|
+
"mf-styling": "^2.0.1",
|
|
48
48
|
"npm-pkgbuild": "^11.8.9",
|
|
49
49
|
"semantic-release": "^21.0.5",
|
|
50
50
|
"stylelint": "^15.9.0",
|
package/src/pagination.mjs
CHANGED
|
@@ -3,16 +3,30 @@
|
|
|
3
3
|
* Pages go from 1 ... numberOfPages
|
|
4
4
|
*/
|
|
5
5
|
export class Pagination {
|
|
6
|
-
subscriptions = new Set();
|
|
7
|
-
data;
|
|
8
|
-
itemsPerPage;
|
|
6
|
+
#subscriptions = new Set();
|
|
7
|
+
#data;
|
|
8
|
+
#itemsPerPage = 1;
|
|
9
9
|
#page = 1;
|
|
10
10
|
|
|
11
|
-
constructor(
|
|
12
|
-
this
|
|
11
|
+
constructor(data, itemsPerPage = 10) {
|
|
12
|
+
this.#data = data;
|
|
13
13
|
this.itemsPerPage = itemsPerPage;
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
+
set data(data) {
|
|
17
|
+
this.#data = data;
|
|
18
|
+
this.#subscriptions.forEach(subscription => subscription(this));
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
get itemsPerPage() {
|
|
22
|
+
return this.#itemsPerPage;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
set itemsPerPage(n) {
|
|
26
|
+
this.#itemsPerPage = n;
|
|
27
|
+
this.#subscriptions.forEach(subscription => subscription(this));
|
|
28
|
+
}
|
|
29
|
+
|
|
16
30
|
/**
|
|
17
31
|
* Set current page
|
|
18
32
|
* @param {number} n
|
|
@@ -20,7 +34,7 @@ export class Pagination {
|
|
|
20
34
|
set page(n) {
|
|
21
35
|
if (this.#page !== n && n >= 1 && n <= this.numberOfPages) {
|
|
22
36
|
this.#page = n;
|
|
23
|
-
this
|
|
37
|
+
this.#subscriptions.forEach(subscription => subscription(this));
|
|
24
38
|
}
|
|
25
39
|
}
|
|
26
40
|
|
|
@@ -32,21 +46,26 @@ export class Pagination {
|
|
|
32
46
|
}
|
|
33
47
|
|
|
34
48
|
subscribe(s) {
|
|
35
|
-
this
|
|
49
|
+
this.#subscriptions.add(s);
|
|
36
50
|
|
|
37
51
|
s(this);
|
|
38
52
|
|
|
39
|
-
return () => this
|
|
53
|
+
return () => this.#subscriptions.delete(s);
|
|
40
54
|
}
|
|
41
55
|
|
|
42
56
|
get numberOfPages() {
|
|
43
|
-
return
|
|
57
|
+
return (
|
|
58
|
+
(Array.isArray(this.#data) ? this.#data.length : this.#data.size) /
|
|
59
|
+
this.itemsPerPage
|
|
60
|
+
);
|
|
44
61
|
}
|
|
45
62
|
|
|
46
63
|
*items() {
|
|
47
64
|
const n = this.page - 1;
|
|
48
65
|
|
|
49
|
-
|
|
66
|
+
const data = Array.isArray(this.data) ? this.#data : [...this.#data.values()];
|
|
67
|
+
|
|
68
|
+
for (const item of data.slice(
|
|
50
69
|
n * this.itemsPerPage,
|
|
51
70
|
(n + 1) * this.itemsPerPage
|
|
52
71
|
)) {
|