svelte-common 6.0.0 → 6.1.0
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 +36 -9
package/package.json
CHANGED
package/src/pagination.mjs
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { filter } from "./filter.mjs";
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Pagination support store.
|
|
3
5
|
* Pages go from 1 ... numberOfPages
|
|
@@ -9,15 +11,26 @@ export class Pagination {
|
|
|
9
11
|
#subscriptions = new Set();
|
|
10
12
|
#data;
|
|
11
13
|
#unsubscribeData;
|
|
12
|
-
|
|
14
|
+
#filter;
|
|
13
15
|
#itemsPerPage = 20;
|
|
14
16
|
#page = 1;
|
|
15
17
|
|
|
16
18
|
constructor(data, options) {
|
|
17
19
|
this.data = data;
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
20
|
+
|
|
21
|
+
Object.assign(this, options);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
set filter(filter)
|
|
25
|
+
{
|
|
26
|
+
this.#filter = filter;
|
|
27
|
+
|
|
28
|
+
this.#subscriptions.forEach(subscription => subscription(this));
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
get filter()
|
|
32
|
+
{
|
|
33
|
+
return this.#filter;
|
|
21
34
|
}
|
|
22
35
|
|
|
23
36
|
set data(data) {
|
|
@@ -73,10 +86,20 @@ export class Pagination {
|
|
|
73
86
|
}
|
|
74
87
|
|
|
75
88
|
get numberOfPages() {
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
89
|
+
let n;
|
|
90
|
+
|
|
91
|
+
if(this.filter) {
|
|
92
|
+
let data = Array.isArray(this.data)
|
|
93
|
+
? this.#data
|
|
94
|
+
: [...this.#data.values()];
|
|
95
|
+
data = data.filter(this.filter);
|
|
96
|
+
n = data.length;
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
n = Array.isArray(this.#data) ? this.#data.length : this.#data.size;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return Math.ceil(n / this.itemsPerPage);
|
|
80
103
|
}
|
|
81
104
|
|
|
82
105
|
get length() {
|
|
@@ -86,10 +109,14 @@ export class Pagination {
|
|
|
86
109
|
*[Symbol.iterator]() {
|
|
87
110
|
const n = this.page - 1;
|
|
88
111
|
|
|
89
|
-
|
|
112
|
+
let data = Array.isArray(this.data)
|
|
90
113
|
? this.#data
|
|
91
114
|
: [...this.#data.values()];
|
|
92
115
|
|
|
116
|
+
if(this.filter) {
|
|
117
|
+
data = data.filter(this.filter);
|
|
118
|
+
}
|
|
119
|
+
|
|
93
120
|
for (const item of data.slice(
|
|
94
121
|
n * this.itemsPerPage,
|
|
95
122
|
(n + 1) * this.itemsPerPage
|