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 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
- cycles "aria-sort" though orderByCycle.
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.9.0",
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.3.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
@@ -45,8 +45,8 @@
45
45
  formatSecondsSinceEpoch
46
46
  } from "./util.mjs";
47
47
  export {
48
+ sorter,
48
49
  sortable,
49
- sortableStore,
50
50
  toggleOrderBy,
51
51
  orderByCycle,
52
52
  SORT_NONE,
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
- * cycles "aria-sort" though orderByCycle.
24
+ * Synchronizes store value with node "aria-sort" attribute.
25
25
  * @param {Node} node
26
- * @param {Store} where to put sorting info into
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
- if (peer !== node) {
40
- if (peer.getAttribute("aria-sort") !== SORT_NONE) {
41
- peer.setAttribute("aria-sort", SORT_NONE);
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
- orderBy[peer.id] = peer.getAttribute("aria-sort");
57
+ orderBy[peer.id] = sort;
58
+ }
46
59
  }
47
60
  store.set(orderBy);
48
61
  };
49
62
  }
50
63
 
51
- export function sortableStore() {
52
- const subscriptions = new Set();
53
-
54
- let value = {};
55
-
56
- return {
57
- subscribe: cb => {
58
- subscriptions.add(cb);
59
- cb(value);
60
- return () => subscriptions.delete(cb);
61
- },
62
- set(v) {
63
- value = v;
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
  }