svelte-common 6.2.0 → 6.2.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.
- package/package.json +1 -1
- package/src/pagination.mjs +25 -28
package/package.json
CHANGED
package/src/pagination.mjs
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
|
|
2
1
|
/**
|
|
3
2
|
* Pagination support store.
|
|
4
3
|
* Pages go from 1 ... numberOfPages
|
|
5
4
|
* @param {Map|Array|Store} data
|
|
6
5
|
* @param {Object} options
|
|
7
6
|
* @param {number} [options.itemsPerPage]
|
|
7
|
+
* @param {Function} [options.sorter]
|
|
8
|
+
* @param {Function} [options.filter]
|
|
8
9
|
*/
|
|
9
10
|
export class Pagination {
|
|
10
11
|
#subscriptions = new Set();
|
|
@@ -21,27 +22,23 @@ export class Pagination {
|
|
|
21
22
|
Object.assign(this, options);
|
|
22
23
|
}
|
|
23
24
|
|
|
24
|
-
set filter(filter)
|
|
25
|
-
{
|
|
25
|
+
set filter(filter) {
|
|
26
26
|
this.#filter = filter;
|
|
27
27
|
|
|
28
28
|
this.#subscriptions.forEach(subscription => subscription(this));
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
get filter()
|
|
32
|
-
{
|
|
31
|
+
get filter() {
|
|
33
32
|
return this.#filter;
|
|
34
33
|
}
|
|
35
34
|
|
|
36
|
-
|
|
37
|
-
{
|
|
35
|
+
set sorter(sorter) {
|
|
38
36
|
this.#sorter = sorter;
|
|
39
37
|
|
|
40
38
|
this.#subscriptions.forEach(subscription => subscription(this));
|
|
41
39
|
}
|
|
42
40
|
|
|
43
|
-
get sorter()
|
|
44
|
-
{
|
|
41
|
+
get sorter() {
|
|
45
42
|
return this.#sorter;
|
|
46
43
|
}
|
|
47
44
|
|
|
@@ -100,14 +97,13 @@ export class Pagination {
|
|
|
100
97
|
get numberOfPages() {
|
|
101
98
|
let n;
|
|
102
99
|
|
|
103
|
-
if(this.filter) {
|
|
100
|
+
if (this.filter) {
|
|
104
101
|
let data = Array.isArray(this.data)
|
|
105
|
-
|
|
106
|
-
|
|
102
|
+
? this.#data
|
|
103
|
+
: [...this.#data.values()];
|
|
107
104
|
data = data.filter(this.filter);
|
|
108
105
|
n = data.length;
|
|
109
|
-
}
|
|
110
|
-
else {
|
|
106
|
+
} else {
|
|
111
107
|
n = Array.isArray(this.#data) ? this.#data.length : this.#data.size;
|
|
112
108
|
}
|
|
113
109
|
|
|
@@ -119,20 +115,18 @@ export class Pagination {
|
|
|
119
115
|
}
|
|
120
116
|
|
|
121
117
|
*[Symbol.iterator]() {
|
|
122
|
-
|
|
118
|
+
let data = Array.isArray(this.data) ? this.#data : [...this.#data.values()];
|
|
123
119
|
|
|
124
|
-
|
|
125
|
-
? this.#data
|
|
126
|
-
: [...this.#data.values()];
|
|
127
|
-
|
|
128
|
-
if(this.filter) {
|
|
120
|
+
if (this.filter) {
|
|
129
121
|
data = data.filter(this.filter);
|
|
130
122
|
}
|
|
131
123
|
|
|
132
|
-
if(this.sorter) {
|
|
124
|
+
if (this.sorter) {
|
|
133
125
|
data = data.sort(this.sorter);
|
|
134
126
|
}
|
|
135
127
|
|
|
128
|
+
const n = this.page - 1;
|
|
129
|
+
|
|
136
130
|
for (const item of data.slice(
|
|
137
131
|
n * this.itemsPerPage,
|
|
138
132
|
(n + 1) * this.itemsPerPage
|
|
@@ -169,7 +163,10 @@ export class Pagination {
|
|
|
169
163
|
a.classList.add("active");
|
|
170
164
|
a.setAttribute("aria-current", "page");
|
|
171
165
|
} else {
|
|
172
|
-
a.onclick =
|
|
166
|
+
a.onclick = e => {
|
|
167
|
+
e.preventDefault();
|
|
168
|
+
this.page = targetPage;
|
|
169
|
+
};
|
|
173
170
|
}
|
|
174
171
|
}
|
|
175
172
|
items.push(a);
|
|
@@ -206,12 +203,12 @@ export function pageNavigation(elem, pg) {
|
|
|
206
203
|
*/
|
|
207
204
|
export function* navigationItems(nunmberOfPages, currentPage) {
|
|
208
205
|
const pageJumps = [
|
|
209
|
-
{ maxPages: 10,
|
|
210
|
-
{ maxPages: 100,
|
|
211
|
-
{ maxPages: 1000,
|
|
212
|
-
{ maxPages: 10000,
|
|
213
|
-
{ maxPages: 100000,
|
|
214
|
-
{ maxPages: 1000000,
|
|
206
|
+
{ maxPages: 10, side: 1, edge: 2 },
|
|
207
|
+
{ maxPages: 100, side: 1, edge: 2, step: 10 },
|
|
208
|
+
{ maxPages: 1000, side: 1, edge: 2, step: 100 },
|
|
209
|
+
{ maxPages: 10000, side: 1, edge: 2, step: 1000 },
|
|
210
|
+
{ maxPages: 100000, side: 1, edge: 2, step: 10000 },
|
|
211
|
+
{ maxPages: 1000000, side: 1, edge: 2, step: 100000 },
|
|
215
212
|
{ maxPages: 10000000, side: 1, edge: 2, step: 1000000 }
|
|
216
213
|
];
|
|
217
214
|
|