|
1 | 1 | import { equals, has } from 'ramda'
|
| 2 | + |
| 3 | +/** |
| 4 | + * Add an event to the event stack |
| 5 | + * @param {Object} state |
| 6 | + * @param {Object} event |
| 7 | + * @param {Number} index |
| 8 | + * @returns {Object} The updated state. |
| 9 | + */ |
| 10 | +export function addEvent(state, event, index = -1) { |
| 11 | + // state.events = state.events ?? [] |
| 12 | + |
| 13 | + if (event === 'select') { |
| 14 | + const detail = state.selectedItems.map((i) => state.data[i].value) |
| 15 | + state.events.push({ type: event, detail }) |
| 16 | + } else if (event === 'move' || index > -1) { |
| 17 | + const { value = null, indexPath = null } = index >= 0 ? state.data[index] : {} |
| 18 | + state.events.push({ type: event, detail: { value, path: indexPath } }) |
| 19 | + } |
| 20 | +} |
| 21 | + |
2 | 22 | /**
|
3 | 23 | * Creates a list view data store using the given items.
|
4 | 24 | *
|
@@ -191,6 +211,47 @@ export function selectRange(state, offset) {
|
191 | 211 | return state
|
192 | 212 | }
|
193 | 213 |
|
| 214 | +/** |
| 215 | + * Recursively update visibility for all children of the given parent |
| 216 | + * based on the parent's expanded state. Stops recursion if a child's expanded |
| 217 | + * state does not match the parent's state. |
| 218 | + * |
| 219 | + * @param {Array} children - The children of the parent to update visibility for. |
| 220 | + * @param {Boolean} isVisible - Indicates whether children should be visible or hidden. |
| 221 | + */ |
| 222 | +function updateChildVisibility(children, isVisible) { |
| 223 | + children.forEach((child) => { |
| 224 | + // Update visibility only if child's expanded state mismatches with the desired visibility |
| 225 | + if (child.isExpanded === isVisible) { |
| 226 | + child.isHidden = !isVisible // Update based on parent's state |
| 227 | + if (child.children) { |
| 228 | + updateChildVisibility(child.children, isVisible) // Recursive call for further children |
| 229 | + } |
| 230 | + } |
| 231 | + }) |
| 232 | +} |
| 233 | + |
| 234 | +/** |
| 235 | + * Toggles the expansion of the item at the given index. |
| 236 | + * |
| 237 | + * @param {Object} state |
| 238 | + * @param {Number} index |
| 239 | + * @param {Boolean} expand - the state |
| 240 | + * @param {Boolean} events - Whether to emit event |
| 241 | + * @returns {Object} The updated state. |
| 242 | + */ |
| 243 | +function toggle(state, index, events = true) { |
| 244 | + const item = state.data[index] |
| 245 | + item.isExpanded = !item.isExpanded |
| 246 | + |
| 247 | + updateChildVisibility(item.children, item.isExpanded) |
| 248 | + if (events) { |
| 249 | + const event = item.isExpanded ? 'expand' : 'collapse' |
| 250 | + addEvent(state, event, index) |
| 251 | + } |
| 252 | + return state |
| 253 | +} |
| 254 | + |
194 | 255 | /**
|
195 | 256 | * Expands the item at the given index.
|
196 | 257 | *
|
@@ -230,28 +291,6 @@ export function collapse(state, pathIndex, emit = true) {
|
230 | 291 |
|
231 | 292 | return state
|
232 | 293 | }
|
233 |
| - |
234 |
| -/** |
235 |
| - * Toggles the expansion of the item at the given index. |
236 |
| - * |
237 |
| - * @param {Object} state |
238 |
| - * @param {Number} index |
239 |
| - * @param {Boolean} expand - the state |
240 |
| - * @param {Boolean} events - Whether to emit event |
241 |
| - * @returns {Object} The updated state. |
242 |
| - */ |
243 |
| -function toggle(state, index, events = true) { |
244 |
| - const item = state.data[index] |
245 |
| - item.isExpanded = !item.isExpanded |
246 |
| - |
247 |
| - updateChildVisibility(item.children, item.isExpanded) |
248 |
| - if (events) { |
249 |
| - const event = item.isExpanded ? 'expand' : 'collapse' |
250 |
| - addEvent(state, event, index) |
251 |
| - } |
252 |
| - return state |
253 |
| -} |
254 |
| - |
255 | 294 | /**
|
256 | 295 | * Toggles the expansion of the item at the given index.
|
257 | 296 | *
|
@@ -294,42 +333,3 @@ export function collapseAll(state) {
|
294 | 333 | addEvent(state, 'collapse')
|
295 | 334 | return state
|
296 | 335 | }
|
297 |
| - |
298 |
| -/** |
299 |
| - * Recursively update visibility for all children of the given parent |
300 |
| - * based on the parent's expanded state. Stops recursion if a child's expanded |
301 |
| - * state does not match the parent's state. |
302 |
| - * |
303 |
| - * @param {Array} children - The children of the parent to update visibility for. |
304 |
| - * @param {Boolean} isVisible - Indicates whether children should be visible or hidden. |
305 |
| - */ |
306 |
| -function updateChildVisibility(children, isVisible) { |
307 |
| - children.forEach((child) => { |
308 |
| - // Update visibility only if child's expanded state mismatches with the desired visibility |
309 |
| - if (child.isExpanded === isVisible) { |
310 |
| - child.isHidden = !isVisible // Update based on parent's state |
311 |
| - if (child.children) { |
312 |
| - updateChildVisibility(child.children, isVisible) // Recursive call for further children |
313 |
| - } |
314 |
| - } |
315 |
| - }) |
316 |
| -} |
317 |
| - |
318 |
| -/** |
319 |
| - * Add an event to the event stack |
320 |
| - * @param {Object} state |
321 |
| - * @param {Object} event |
322 |
| - * @param {Number} index |
323 |
| - * @returns {Object} The updated state. |
324 |
| - */ |
325 |
| -export function addEvent(state, event, index = -1) { |
326 |
| - // state.events = state.events ?? [] |
327 |
| - |
328 |
| - if (event === 'select') { |
329 |
| - const detail = state.selectedItems.map((i) => state.data[i].value) |
330 |
| - state.events.push({ type: event, detail }) |
331 |
| - } else if (event === 'move' || index > -1) { |
332 |
| - const { value = null, indexPath = null } = index >= 0 ? state.data[index] : {} |
333 |
| - state.events.push({ type: event, detail: { value, path: indexPath } }) |
334 |
| - } |
335 |
| -} |
0 commit comments