|
| 1 | +<script lang="ts"> |
| 2 | + import TreeViewItem from './TreeViewItem.svelte'; |
| 3 | + import RecursiveTreeViewItem from './RecursiveTreeViewItem.svelte'; |
| 4 | + import type { TreeViewNode } from './types.js'; |
| 5 | + import { getContext, onMount, tick } from 'svelte'; |
| 6 | +
|
| 7 | + // this can't be passed using context, since we have to pass it to recursive children. |
| 8 | + /** Provide data-driven nodes. */ |
| 9 | + export let nodes: TreeViewNode[] = []; |
| 10 | +
|
| 11 | + /** |
| 12 | + * provides id's of expanded nodes |
| 13 | + * @type {string[]} |
| 14 | + */ |
| 15 | + export let expandedNodes: string[] = []; |
| 16 | + /** |
| 17 | + * provides id's of disabled nodes |
| 18 | + * @type {string[]} |
| 19 | + */ |
| 20 | + export let disabledNodes: string[] = []; |
| 21 | + /** |
| 22 | + * provides id's of checked nodes |
| 23 | + * @type {string[]} |
| 24 | + */ |
| 25 | + export let checkedNodes: string[] = []; |
| 26 | + /** |
| 27 | + * provides id's of indeterminate nodes |
| 28 | + * @type {string[]} |
| 29 | + */ |
| 30 | + export let indeterminateNodes: string[] = []; |
| 31 | +
|
| 32 | + // Context API |
| 33 | + let selection: boolean = getContext('selection'); |
| 34 | + let multiple: boolean = getContext('multiple'); |
| 35 | + let relational: boolean = getContext('relational'); |
| 36 | +
|
| 37 | + let tempCheckedNodes: string[] = []; |
| 38 | +
|
| 39 | + // Locals |
| 40 | + let group: unknown; |
| 41 | + let name = ''; |
| 42 | +
|
| 43 | + function toggleNode(node: TreeViewNode, open: boolean) { |
| 44 | + // toggle only nodes with children |
| 45 | + if (!node.children?.length) return; |
| 46 | + if (open) { |
| 47 | + // node is not registered as opened |
| 48 | + if (!expandedNodes.includes(node.id)) { |
| 49 | + expandedNodes.push(node.id); |
| 50 | + expandedNodes = expandedNodes; |
| 51 | + } |
| 52 | + } else { |
| 53 | + // node is registered as open |
| 54 | + if (expandedNodes.includes(node.id)) { |
| 55 | + expandedNodes.splice(expandedNodes.indexOf(node.id), 1); |
| 56 | + expandedNodes = expandedNodes; |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | +
|
| 61 | + function checkNode(node: TreeViewNode, checked: boolean, indeterminate: boolean) { |
| 62 | + if (checked) { |
| 63 | + // node is not registered as checked |
| 64 | + if (!checkedNodes.includes(node.id)) { |
| 65 | + checkedNodes.push(node.id); |
| 66 | + checkedNodes = checkedNodes; |
| 67 | + } |
| 68 | +
|
| 69 | + // node is not indeterminate but registered as indeterminate |
| 70 | + if (!indeterminate && indeterminateNodes.includes(node.id)) { |
| 71 | + indeterminateNodes.splice(indeterminateNodes.indexOf(node.id), 1); |
| 72 | + indeterminateNodes = indeterminateNodes; |
| 73 | + } |
| 74 | + } else { |
| 75 | + // node is registered as checked |
| 76 | + if (checkedNodes.includes(node.id)) { |
| 77 | + checkedNodes.splice(checkedNodes.indexOf(node.id), 1); |
| 78 | + checkedNodes = checkedNodes; |
| 79 | + } |
| 80 | +
|
| 81 | + // node is indeterminate but not registered as indeterminate |
| 82 | + if (indeterminate && !indeterminateNodes.includes(node.id)) { |
| 83 | + indeterminateNodes.push(node.id); |
| 84 | + indeterminateNodes = indeterminateNodes; |
| 85 | + // node is not indeterminate but registered as indeterminate |
| 86 | + } else if (!indeterminate && indeterminateNodes.includes(node.id)) { |
| 87 | + indeterminateNodes.splice(indeterminateNodes.indexOf(node.id), 1); |
| 88 | + indeterminateNodes = indeterminateNodes; |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | +
|
| 93 | + // init check flow will messup the checked nodes, so we save it to reassign it onMount. |
| 94 | + tempCheckedNodes = [...checkedNodes]; |
| 95 | + onMount(async () => { |
| 96 | + if (selection) { |
| 97 | + // random number as name |
| 98 | + name = String(Math.random()); |
| 99 | +
|
| 100 | + // init groups if not initialized yet |
| 101 | + if (group === undefined) { |
| 102 | + if (multiple) { |
| 103 | + group = []; |
| 104 | + nodes.forEach((node) => { |
| 105 | + if (checkedNodes.includes(node.id) && Array.isArray(group)) group.push(node.id); |
| 106 | + }); |
| 107 | + group = group; |
| 108 | + } else if (!nodes.some((node) => checkedNodes.includes(node.id))) { |
| 109 | + group = ''; |
| 110 | + } |
| 111 | + } |
| 112 | +
|
| 113 | + // remove relational links |
| 114 | + if (!relational) treeItems = []; |
| 115 | +
|
| 116 | + // reassign checkNodes to ensure component starting with the correct check values. |
| 117 | + checkedNodes = []; |
| 118 | + await tick(); |
| 119 | + checkedNodes = [...tempCheckedNodes]; |
| 120 | + } |
| 121 | + }); |
| 122 | +
|
| 123 | + // important to pass children up to items (recursively) |
| 124 | + export let treeItems: TreeViewItem[] = []; |
| 125 | + let children: TreeViewItem[][] = []; |
| 126 | +</script> |
| 127 | + |
| 128 | +{#if nodes && nodes.length > 0} |
| 129 | + {#each nodes as node, i} |
| 130 | + <TreeViewItem |
| 131 | + bind:this={treeItems[i]} |
| 132 | + bind:children={children[i]} |
| 133 | + bind:group |
| 134 | + bind:name |
| 135 | + bind:value={node.id} |
| 136 | + hideLead={!node.lead} |
| 137 | + hideChildren={!node.children || node.children.length === 0} |
| 138 | + open={expandedNodes.includes(node.id)} |
| 139 | + disabled={disabledNodes.includes(node.id)} |
| 140 | + checked={checkedNodes.includes(node.id)} |
| 141 | + indeterminate={indeterminateNodes.includes(node.id)} |
| 142 | + on:toggle={(e) => toggleNode(node, e.detail.open)} |
| 143 | + on:groupChange={(e) => checkNode(node, e.detail.checked, e.detail.indeterminate)} |
| 144 | + > |
| 145 | + {@html node.content} |
| 146 | + <svelte:fragment slot="lead"> |
| 147 | + {@html node.lead} |
| 148 | + </svelte:fragment> |
| 149 | + <svelte:fragment slot="children"> |
| 150 | + <RecursiveTreeViewItem |
| 151 | + nodes={node.children} |
| 152 | + bind:expandedNodes |
| 153 | + bind:disabledNodes |
| 154 | + bind:checkedNodes |
| 155 | + bind:indeterminateNodes |
| 156 | + bind:treeItems={children[i]} |
| 157 | + /> |
| 158 | + </svelte:fragment> |
| 159 | + </TreeViewItem> |
| 160 | + {/each} |
| 161 | +{/if} |
0 commit comments