@rokkit/data 1.0.0-next.94
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/LICENSE +21 -0
- package/README.md +50 -0
- package/package.json +56 -0
- package/src/aggregators.js +24 -0
- package/src/constants.js +72 -0
- package/src/convert.js +36 -0
- package/src/dataset.js +442 -0
- package/src/filter.js +57 -0
- package/src/formatter.js +56 -0
- package/src/hierarchy.js +136 -0
- package/src/index.js +6 -0
- package/src/infer.js +263 -0
- package/src/list.js +232 -0
- package/src/metadata.js +106 -0
- package/src/model.js +100 -0
- package/src/parser.js +110 -0
- package/src/renamer.js +98 -0
- package/src/rollup.js +138 -0
- package/src/types.js +208 -0
- package/src/utils.js +31 -0
- package/src/view.js +187 -0
package/src/view.js
ADDED
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import { omit } from 'ramda'
|
|
2
|
+
import { get, writable } from 'svelte/store'
|
|
3
|
+
import { deriveMetadata, deriveSortableColumn } from './infer'
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
flattenNestedChildren,
|
|
7
|
+
removeChildren,
|
|
8
|
+
deriveHierarchy,
|
|
9
|
+
toggleExpansion
|
|
10
|
+
} from './hierarchy'
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Creates a view object for managing data presentation with metadata and hierarchy structures,
|
|
14
|
+
* as well as providing methods for sorting, selection, and toggling of data expansion.
|
|
15
|
+
*
|
|
16
|
+
* @param {Array<Object>} data - The source data array to be presented by the view.
|
|
17
|
+
* @param {Object} options - The configuration options used for deriving metadata and hierarchy from the data.
|
|
18
|
+
* @returns {import('svelte/store').Writable} An object representing the view, providing methods to manipulate and query the presentation state.
|
|
19
|
+
*/
|
|
20
|
+
export function dataview(data, options) {
|
|
21
|
+
const store = writable({
|
|
22
|
+
columns: deriveMetadata(data, options),
|
|
23
|
+
hierarchy: deriveHierarchy(data, options)
|
|
24
|
+
})
|
|
25
|
+
let sortGroup = []
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Sorts the hierarchy based on a specific field name and order.
|
|
29
|
+
*
|
|
30
|
+
* @param {string} name - The field name to sort by.
|
|
31
|
+
* @param {boolean} [ascending=true] - Whether the sort should be in ascending order.
|
|
32
|
+
*/
|
|
33
|
+
const sortBy = (name, ascending = true) => {
|
|
34
|
+
sortGroup = [...sortGroup, [name, ascending]]
|
|
35
|
+
// change the sort order in the metadata
|
|
36
|
+
const { columns, hierarchy } = get(store)
|
|
37
|
+
columns.forEach((column) => {
|
|
38
|
+
if (column.name === name) {
|
|
39
|
+
column.order = ascending ? 'ascending' : 'descending'
|
|
40
|
+
}
|
|
41
|
+
})
|
|
42
|
+
groupSort(hierarchy, sortGroup)
|
|
43
|
+
store.set({ columns, hierarchy })
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return {
|
|
47
|
+
...omit(['set', 'update'], store),
|
|
48
|
+
// filter: () => {},
|
|
49
|
+
/**
|
|
50
|
+
* Clears the applied sort order from the hierarchy.
|
|
51
|
+
* TODO: clearing sort should clear the sort orders in columns
|
|
52
|
+
*/
|
|
53
|
+
clearSort: () => {
|
|
54
|
+
sortGroup = []
|
|
55
|
+
const { columns } = get(store)
|
|
56
|
+
columns.forEach((column) => {
|
|
57
|
+
column.order = 'none'
|
|
58
|
+
})
|
|
59
|
+
// clearSortOrder(metadata)
|
|
60
|
+
},
|
|
61
|
+
sortBy,
|
|
62
|
+
/**
|
|
63
|
+
* Toggles the selection state of a data element at the specified index.
|
|
64
|
+
*
|
|
65
|
+
* @param {number} index - The index of the element in the hierarchy to select or deselect.
|
|
66
|
+
*/
|
|
67
|
+
select: (index) => {
|
|
68
|
+
const { hierarchy } = get(store)
|
|
69
|
+
toggleSelection(hierarchy[index])
|
|
70
|
+
store.update((state) => ({ ...state, hierarchy }))
|
|
71
|
+
},
|
|
72
|
+
/**
|
|
73
|
+
* Toggles the expansion state of a data element at the specified index.
|
|
74
|
+
*
|
|
75
|
+
* @param {number} index - The index of the element in the hierarchy to expand or collapse.
|
|
76
|
+
*/
|
|
77
|
+
toggle: (index) => {
|
|
78
|
+
const { hierarchy } = get(store)
|
|
79
|
+
toggleExpansion(hierarchy[index])
|
|
80
|
+
store.update((state) => ({ ...state, hierarchy }))
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Sorts a hierarchy based on a group of sorters.
|
|
87
|
+
*
|
|
88
|
+
* @param {Array<Object>} hierarchy - The hierarchy to sort.
|
|
89
|
+
* @param {Array<Array<string, boolean>>} sortGroup - The group of sorters to apply.
|
|
90
|
+
*/
|
|
91
|
+
export function groupSort(hierarchy, sortGroup) {
|
|
92
|
+
const group = sortGroup.map(deriveSortableColumn).map(({ name, sorter }) => ({
|
|
93
|
+
name,
|
|
94
|
+
sorter: (a, b) => sorter(a.row[name], b.row[name])
|
|
95
|
+
}))
|
|
96
|
+
|
|
97
|
+
removeChildren(hierarchy)
|
|
98
|
+
sortNested(hierarchy, group)
|
|
99
|
+
flattenNestedChildren(hierarchy)
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Sorts a nested array of objects based on a group of sorters.
|
|
104
|
+
*
|
|
105
|
+
* @param {Array<Object>} elements - The array of objects to sort.
|
|
106
|
+
*/
|
|
107
|
+
function sortNested(elements, group) {
|
|
108
|
+
elements
|
|
109
|
+
.sort((a, b) => {
|
|
110
|
+
let result = 0
|
|
111
|
+
for (let i = 0; i < group.length && result === 0; i++) {
|
|
112
|
+
result = group[i].sorter(a, b)
|
|
113
|
+
}
|
|
114
|
+
return result
|
|
115
|
+
})
|
|
116
|
+
.forEach((x) => {
|
|
117
|
+
if (Array.isArray(x.children) && x.children.length > 0) {
|
|
118
|
+
sortNested(x.children, group)
|
|
119
|
+
}
|
|
120
|
+
})
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Toggles the selection state of a node in the hierarchy.
|
|
125
|
+
* Depending on whether the node to toggle is a parent or child,
|
|
126
|
+
* it will either select or deselect all children, or update the parent's selection state based on its children's states.
|
|
127
|
+
*
|
|
128
|
+
* @param {import('./types').Hierarchy} item - The node to toggle.
|
|
129
|
+
*
|
|
130
|
+
*/
|
|
131
|
+
export function toggleSelection(item) {
|
|
132
|
+
item.selected = item.selected === 'checked' ? 'unchecked' : 'checked'
|
|
133
|
+
|
|
134
|
+
updateParents(item)
|
|
135
|
+
updateChildren(item)
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Updates the selection state for all children of a node in the hierarchy.
|
|
140
|
+
*
|
|
141
|
+
* @param {Array<import('./types').Hierarchy>} hierarchy - The hierarchy to update.
|
|
142
|
+
* @param {number} index - The index of the node to update.
|
|
143
|
+
*/
|
|
144
|
+
function updateChildren(item) {
|
|
145
|
+
if (!item.children) return
|
|
146
|
+
|
|
147
|
+
item.children.forEach((child) => {
|
|
148
|
+
child.selected = item.selected
|
|
149
|
+
updateChildren(child)
|
|
150
|
+
})
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Determines the selected state of a group of children in the hierarchy.
|
|
155
|
+
* If all children are checked, returns 'checked'.
|
|
156
|
+
* If all children are unchecked, returns 'unchecked'.
|
|
157
|
+
* Otherwise, returns 'indeterminate'.
|
|
158
|
+
*
|
|
159
|
+
* @param {Array<Object>} children - The array of child objects with a 'selected' property.
|
|
160
|
+
* @returns {string} The determined selected state: 'checked', 'unchecked', or 'indeterminate'.
|
|
161
|
+
*/
|
|
162
|
+
export function determineSelectedState(children) {
|
|
163
|
+
const allChecked = children.every((child) => child.selected === 'checked')
|
|
164
|
+
const allUnchecked =
|
|
165
|
+
!allChecked && children.every((child) => !child.selected || child.selected === 'unchecked')
|
|
166
|
+
|
|
167
|
+
return allChecked ? 'checked' : allUnchecked ? 'unchecked' : 'indeterminate'
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Updates the selection state of parent nodes in the hierarchy based on their children's states.
|
|
172
|
+
* It traverses up from the provided `index` position in the hierarchy array, ensuring each
|
|
173
|
+
* parent's selected state reflects whether all, none, or some of its children are selected.
|
|
174
|
+
*
|
|
175
|
+
* @param {Array<Object>} hierarchy - The hierarchy structure containing nodes with
|
|
176
|
+
* 'parent' and 'children' references.
|
|
177
|
+
* @param {number} index - The index of the node in the hierarchy array from where
|
|
178
|
+
* to start updating parent nodes.
|
|
179
|
+
*/
|
|
180
|
+
function updateParents(item) {
|
|
181
|
+
let parent = item.parent
|
|
182
|
+
|
|
183
|
+
while (parent) {
|
|
184
|
+
parent.selected = determineSelectedState(parent.children)
|
|
185
|
+
parent = parent.parent
|
|
186
|
+
}
|
|
187
|
+
}
|