svelte-common 6.1.2 → 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 +2 -2
- package/src/pagination.mjs +37 -23
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "svelte-common",
|
|
3
|
-
"version": "6.1
|
|
3
|
+
"version": "6.2.1",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
@@ -48,7 +48,7 @@
|
|
|
48
48
|
"ava": "^5.3.1",
|
|
49
49
|
"c8": "^8.0.0",
|
|
50
50
|
"documentation": "^14.0.2",
|
|
51
|
-
"mf-styling": "^2.0.
|
|
51
|
+
"mf-styling": "^2.0.3",
|
|
52
52
|
"npm-pkgbuild": "^11.8.14",
|
|
53
53
|
"semantic-release": "^21.0.7",
|
|
54
54
|
"stylelint": "^15.10.1",
|
package/src/pagination.mjs
CHANGED
|
@@ -1,16 +1,18 @@
|
|
|
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();
|
|
11
12
|
#data;
|
|
12
13
|
#unsubscribeData;
|
|
13
14
|
#filter;
|
|
15
|
+
#sorter;
|
|
14
16
|
#itemsPerPage = 20;
|
|
15
17
|
#page = 1;
|
|
16
18
|
|
|
@@ -20,18 +22,26 @@ export class Pagination {
|
|
|
20
22
|
Object.assign(this, options);
|
|
21
23
|
}
|
|
22
24
|
|
|
23
|
-
set filter(filter)
|
|
24
|
-
{
|
|
25
|
+
set filter(filter) {
|
|
25
26
|
this.#filter = filter;
|
|
26
27
|
|
|
27
28
|
this.#subscriptions.forEach(subscription => subscription(this));
|
|
28
29
|
}
|
|
29
30
|
|
|
30
|
-
get filter()
|
|
31
|
-
{
|
|
31
|
+
get filter() {
|
|
32
32
|
return this.#filter;
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
+
set sorter(sorter) {
|
|
36
|
+
this.#sorter = sorter;
|
|
37
|
+
|
|
38
|
+
this.#subscriptions.forEach(subscription => subscription(this));
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
get sorter() {
|
|
42
|
+
return this.#sorter;
|
|
43
|
+
}
|
|
44
|
+
|
|
35
45
|
set data(data) {
|
|
36
46
|
if (this.#unsubscribeData) {
|
|
37
47
|
this.#unsubscribeData();
|
|
@@ -87,14 +97,13 @@ export class Pagination {
|
|
|
87
97
|
get numberOfPages() {
|
|
88
98
|
let n;
|
|
89
99
|
|
|
90
|
-
if(this.filter) {
|
|
100
|
+
if (this.filter) {
|
|
91
101
|
let data = Array.isArray(this.data)
|
|
92
|
-
|
|
93
|
-
|
|
102
|
+
? this.#data
|
|
103
|
+
: [...this.#data.values()];
|
|
94
104
|
data = data.filter(this.filter);
|
|
95
105
|
n = data.length;
|
|
96
|
-
}
|
|
97
|
-
else {
|
|
106
|
+
} else {
|
|
98
107
|
n = Array.isArray(this.#data) ? this.#data.length : this.#data.size;
|
|
99
108
|
}
|
|
100
109
|
|
|
@@ -106,16 +115,18 @@ export class Pagination {
|
|
|
106
115
|
}
|
|
107
116
|
|
|
108
117
|
*[Symbol.iterator]() {
|
|
109
|
-
|
|
118
|
+
let data = Array.isArray(this.data) ? this.#data : [...this.#data.values()];
|
|
110
119
|
|
|
111
|
-
|
|
112
|
-
? this.#data
|
|
113
|
-
: [...this.#data.values()];
|
|
114
|
-
|
|
115
|
-
if(this.filter) {
|
|
120
|
+
if (this.filter) {
|
|
116
121
|
data = data.filter(this.filter);
|
|
117
122
|
}
|
|
118
123
|
|
|
124
|
+
if (this.sorter) {
|
|
125
|
+
data = data.sort(this.sorter);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const n = this.page - 1;
|
|
129
|
+
|
|
119
130
|
for (const item of data.slice(
|
|
120
131
|
n * this.itemsPerPage,
|
|
121
132
|
(n + 1) * this.itemsPerPage
|
|
@@ -152,7 +163,10 @@ export class Pagination {
|
|
|
152
163
|
a.classList.add("active");
|
|
153
164
|
a.setAttribute("aria-current", "page");
|
|
154
165
|
} else {
|
|
155
|
-
a.onclick =
|
|
166
|
+
a.onclick = e => {
|
|
167
|
+
e.preventDefault();
|
|
168
|
+
this.page = targetPage;
|
|
169
|
+
};
|
|
156
170
|
}
|
|
157
171
|
}
|
|
158
172
|
items.push(a);
|
|
@@ -189,12 +203,12 @@ export function pageNavigation(elem, pg) {
|
|
|
189
203
|
*/
|
|
190
204
|
export function* navigationItems(nunmberOfPages, currentPage) {
|
|
191
205
|
const pageJumps = [
|
|
192
|
-
{ maxPages: 10,
|
|
193
|
-
{ maxPages: 100,
|
|
194
|
-
{ maxPages: 1000,
|
|
195
|
-
{ maxPages: 10000,
|
|
196
|
-
{ maxPages: 100000,
|
|
197
|
-
{ 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 },
|
|
198
212
|
{ maxPages: 10000000, side: 1, edge: 2, step: 1000000 }
|
|
199
213
|
];
|
|
200
214
|
|