svelte-common 4.13.4 → 4.15.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/README.md +15 -2
- package/package.json +2 -2
- package/src/sorting.mjs +46 -23
package/README.md
CHANGED
|
@@ -32,6 +32,8 @@ or the [live example](https://arlac77.github.io/components/svelte-common/example
|
|
|
32
32
|
* [Parameters](#parameters-1)
|
|
33
33
|
* [sortable](#sortable)
|
|
34
34
|
* [Parameters](#parameters-2)
|
|
35
|
+
* [sorter](#sorter)
|
|
36
|
+
* [Parameters](#parameters-3)
|
|
35
37
|
|
|
36
38
|
## initializeServiceWorker
|
|
37
39
|
|
|
@@ -58,13 +60,24 @@ Returns **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/G
|
|
|
58
60
|
## sortable
|
|
59
61
|
|
|
60
62
|
Add sortable toggle to a node.
|
|
61
|
-
Synchronizes store value with node
|
|
63
|
+
Synchronizes store value with node "aria-sort" attribute.
|
|
62
64
|
|
|
63
65
|
### Parameters
|
|
64
66
|
|
|
65
67
|
* `node` **[Node](https://developer.mozilla.org/docs/Web/API/Node/nextSibling)** 
|
|
66
68
|
* `store`  
|
|
67
|
-
* `
|
|
69
|
+
* `to` **WritableStore** keep in sync with sorting properties
|
|
70
|
+
|
|
71
|
+
## sorter
|
|
72
|
+
|
|
73
|
+
Generate a sort function for a given sort by set.
|
|
74
|
+
|
|
75
|
+
### Parameters
|
|
76
|
+
|
|
77
|
+
* `sortBy` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** 
|
|
78
|
+
* `getters` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** (optional, default `{}`)
|
|
79
|
+
|
|
80
|
+
Returns **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)** sorter
|
|
68
81
|
|
|
69
82
|
# install
|
|
70
83
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "svelte-common",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.15.1",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
@@ -42,7 +42,7 @@
|
|
|
42
42
|
"@sveltejs/vite-plugin-svelte": "^1.0.2",
|
|
43
43
|
"ava": "^4.3.1",
|
|
44
44
|
"documentation": "^14.0.0",
|
|
45
|
-
"mf-styling": "^1.5.
|
|
45
|
+
"mf-styling": "^1.5.5",
|
|
46
46
|
"npm-pkgbuild": "^10.14.8",
|
|
47
47
|
"semantic-release": "^19.0.5",
|
|
48
48
|
"stylelint": "^14.11.0",
|
package/src/sorting.mjs
CHANGED
|
@@ -6,7 +6,7 @@ export const SORT_OTHER = "other";
|
|
|
6
6
|
export const orderByCycle = {
|
|
7
7
|
[SORT_NONE]: SORT_ASCENDING,
|
|
8
8
|
[SORT_ASCENDING]: SORT_DESCENDING,
|
|
9
|
-
[SORT_DESCENDING]:
|
|
9
|
+
[SORT_DESCENDING]: SORT_ASCENDING
|
|
10
10
|
};
|
|
11
11
|
|
|
12
12
|
/**
|
|
@@ -20,35 +20,30 @@ export function toggleOrderBy(orderBy) {
|
|
|
20
20
|
}
|
|
21
21
|
|
|
22
22
|
/**
|
|
23
|
-
* Add sortable toggle to a node.
|
|
23
|
+
* Add sortable toggle button to a node.
|
|
24
24
|
* Synchronizes store value with node "aria-sort" attribute.
|
|
25
|
-
* @param {Node} node
|
|
25
|
+
* @param {Node} th header node
|
|
26
26
|
* @param {WritableStore} to keep in sync with sorting properties
|
|
27
27
|
*/
|
|
28
|
-
export function sortable(
|
|
29
|
-
|
|
28
|
+
export function sortable(th, store) {
|
|
29
|
+
const button = document.createElement("button");
|
|
30
|
+
const img = document.createElement("img");
|
|
31
|
+
button.appendChild(img);
|
|
30
32
|
|
|
31
|
-
store.subscribe(orderBy =>
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
peer.setAttribute("aria-sort", orderBy[peer.id] || SORT_NONE);
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
});
|
|
33
|
+
store.subscribe(orderBy =>
|
|
34
|
+
th.setAttribute("aria-sort", orderBy[th.id] || SORT_NONE)
|
|
35
|
+
);
|
|
38
36
|
|
|
39
|
-
|
|
37
|
+
button.onclick = () => {
|
|
40
38
|
const orderBy = {};
|
|
41
39
|
|
|
42
|
-
|
|
43
|
-
"aria-sort",
|
|
44
|
-
toggleOrderBy(node.getAttribute("aria-sort"))
|
|
45
|
-
);
|
|
40
|
+
th.setAttribute("aria-sort", toggleOrderBy(th.getAttribute("aria-sort")));
|
|
46
41
|
|
|
47
|
-
for (const peer of
|
|
42
|
+
for (const peer of th.parentElement.children) {
|
|
48
43
|
let sort = peer.getAttribute("aria-sort");
|
|
49
44
|
|
|
50
45
|
if (sort) {
|
|
51
|
-
if (peer !==
|
|
46
|
+
if (peer !== th) {
|
|
52
47
|
if (sort !== SORT_NONE) {
|
|
53
48
|
sort = SORT_NONE;
|
|
54
49
|
peer.setAttribute("aria-sort", sort);
|
|
@@ -60,6 +55,8 @@ export function sortable(node, store) {
|
|
|
60
55
|
}
|
|
61
56
|
store.set(orderBy);
|
|
62
57
|
};
|
|
58
|
+
|
|
59
|
+
th.appendChild(button);
|
|
63
60
|
}
|
|
64
61
|
|
|
65
62
|
/**
|
|
@@ -77,21 +74,47 @@ export function sorter(sortBy, getters = {}) {
|
|
|
77
74
|
case SORT_ASCENDING:
|
|
78
75
|
return (a, b) => {
|
|
79
76
|
const av = getter(a);
|
|
77
|
+
const bv = getter(b);
|
|
78
|
+
if (av === undefined) {
|
|
79
|
+
return -1;
|
|
80
|
+
}
|
|
81
|
+
if (bv === undefined) {
|
|
82
|
+
return 1;
|
|
83
|
+
}
|
|
84
|
+
|
|
80
85
|
if (typeof av === "string") {
|
|
81
|
-
const bv = getter(b);
|
|
82
86
|
return typeof bv === "string" ? av.localeCompare(bv) : 1;
|
|
83
87
|
}
|
|
84
|
-
|
|
88
|
+
if (av instanceof Date) {
|
|
89
|
+
const avt = av.getTime();
|
|
90
|
+
const bvt = bv.getTime();
|
|
91
|
+
return avt > bvt ? 1 : avt === bvt ? 0 : -1;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return av > bv ? 1 : av == bv ? 0 : -1;
|
|
85
95
|
};
|
|
86
96
|
|
|
87
97
|
case SORT_DESCENDING:
|
|
88
98
|
return (b, a) => {
|
|
89
99
|
const av = getter(a);
|
|
100
|
+
const bv = getter(b);
|
|
101
|
+
if (av === undefined) {
|
|
102
|
+
return -1;
|
|
103
|
+
}
|
|
104
|
+
if (bv === undefined) {
|
|
105
|
+
return 1;
|
|
106
|
+
}
|
|
107
|
+
|
|
90
108
|
if (typeof av === "string") {
|
|
91
|
-
const bv = getter(b);
|
|
92
109
|
return typeof bv === "string" ? av.localeCompare(bv) : 1;
|
|
93
110
|
}
|
|
94
|
-
|
|
111
|
+
if (av instanceof Date) {
|
|
112
|
+
const avt = av.getTime();
|
|
113
|
+
const bvt = bv.getTime();
|
|
114
|
+
return avt > bvt ? 1 : avt === bvt ? 0 : -1;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return av > bv ? 1 : av == bv ? 0 : -1;
|
|
95
118
|
};
|
|
96
119
|
}
|
|
97
120
|
}
|