svelte-common 4.9.0 → 4.12.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/README.md +3 -1
- package/package.json +2 -2
- package/src/index.svelte +1 -1
- package/src/sorting.mjs +34 -22
package/README.md
CHANGED
|
@@ -58,11 +58,13 @@ Returns **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/G
|
|
|
58
58
|
## sortable
|
|
59
59
|
|
|
60
60
|
Add sortable toggle to a node.
|
|
61
|
-
|
|
61
|
+
Synchronizes store value with node sortable attribute.
|
|
62
62
|
|
|
63
63
|
### Parameters
|
|
64
64
|
|
|
65
65
|
* `node` **[Node](https://developer.mozilla.org/docs/Web/API/Node/nextSibling)** 
|
|
66
|
+
* `store`  
|
|
67
|
+
* `where` **Store** to keep in sync with sorting properties
|
|
66
68
|
|
|
67
69
|
# install
|
|
68
70
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "svelte-common",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.12.0",
|
|
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.
|
|
45
|
+
"mf-styling": "^1.4.0",
|
|
46
46
|
"npm-pkgbuild": "^10.14.8",
|
|
47
47
|
"semantic-release": "^19.0.4",
|
|
48
48
|
"stylelint": "^14.11.0",
|
package/src/index.svelte
CHANGED
package/src/sorting.mjs
CHANGED
|
@@ -21,47 +21,59 @@ export function toggleOrderBy(orderBy) {
|
|
|
21
21
|
|
|
22
22
|
/**
|
|
23
23
|
* Add sortable toggle to a node.
|
|
24
|
-
*
|
|
24
|
+
* Synchronizes store value with node "aria-sort" attribute.
|
|
25
25
|
* @param {Node} node
|
|
26
|
-
* @param {Store}
|
|
26
|
+
* @param {Store} to keep in sync with sorting properties
|
|
27
27
|
*/
|
|
28
28
|
export function sortable(node, store) {
|
|
29
29
|
node.setAttribute("aria-sort", SORT_NONE);
|
|
30
30
|
|
|
31
|
+
store.subscribe(orderBy => {
|
|
32
|
+
for (const peer of node.parentElement.children) {
|
|
33
|
+
if (peer.getAttribute("aria-sort")) {
|
|
34
|
+
peer.setAttribute("aria-sort", orderBy[peer.id] || SORT_NONE);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
|
|
31
39
|
node.onclick = () => {
|
|
32
40
|
const orderBy = {};
|
|
41
|
+
|
|
33
42
|
node.setAttribute(
|
|
34
43
|
"aria-sort",
|
|
35
44
|
toggleOrderBy(node.getAttribute("aria-sort"))
|
|
36
45
|
);
|
|
37
46
|
|
|
38
47
|
for (const peer of node.parentElement.children) {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
48
|
+
const sort = peer.getAttribute("aria-sort");
|
|
49
|
+
|
|
50
|
+
if (sort) {
|
|
51
|
+
if (peer !== node) {
|
|
52
|
+
if (sort !== SORT_NONE) {
|
|
53
|
+
peer.setAttribute("aria-sort", SORT_NONE);
|
|
54
|
+
}
|
|
42
55
|
}
|
|
43
|
-
}
|
|
44
56
|
|
|
45
|
-
|
|
57
|
+
orderBy[peer.id] = sort;
|
|
58
|
+
}
|
|
46
59
|
}
|
|
47
60
|
store.set(orderBy);
|
|
48
61
|
};
|
|
49
62
|
}
|
|
50
63
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
subscriptions.forEach(subscription => subscription(value));
|
|
64
|
+
/**
|
|
65
|
+
* Generate a sort function for a given sort by set.
|
|
66
|
+
* @param {Object} sortBy
|
|
67
|
+
* @return {Function} sorter
|
|
68
|
+
*/
|
|
69
|
+
export function sorter(sortBy)
|
|
70
|
+
{
|
|
71
|
+
for(const [key,value] of Object.entries(sortBy)) {
|
|
72
|
+
switch(value) {
|
|
73
|
+
case SORT_ASCENDING:
|
|
74
|
+
return (a,b) => a[key].localeCompare(b[key]);
|
|
75
|
+
case SORT_DESCENDING:
|
|
76
|
+
return (b,a) => a[key].localeCompare(b[key]);
|
|
65
77
|
}
|
|
66
|
-
}
|
|
78
|
+
}
|
|
67
79
|
}
|