@keenmate/svelte-treeview 1.0.0-beta.0 → 1.0.0-beta.1

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
@@ -1,38 +1,38 @@
1
- # Svelte Treeview
2
-
3
- The most elaborate treeview for svelte on earth (or even in our galaxy).
4
-
5
- ## Features
6
-
7
- - load new nodes whne expanding
8
- - automatically expanding to given depth
9
- - customization of all object properties
10
- - checkboxes enabled on whole tree or based on property
11
- - recursive seletion mode, where leafes can be selected
12
- - build-in support for search
13
-
14
- ## Instalation
15
-
16
- install the package `@keenmate/svelte-treeview` using your favourite package manager.
17
-
18
- Font awesome is required for expand/collapse icons.
19
-
20
- ## Minimal usage
21
-
22
-
23
- ```svelte
24
- <script lang="ts">
25
- import { TreeView } from '$lib/index.js';
26
-
27
- let tree = [
28
- { nodePath: 'animals', title: 'Animals', hasChildren: true },
29
- //...
30
- { nodePath: 'animals.insects.butterflies', title: 'Butterflies' }
31
- ];
32
- </script>
33
-
34
- <TreeView {tree} treeId="my-tree" let:node>
35
- {node.title}
36
- </TreeView>
37
-
38
- ```
1
+ # Svelte Treeview
2
+
3
+ The most elaborate treeview for svelte on earth (or even in our galaxy).
4
+
5
+ ## Features
6
+
7
+ - load new nodes whne expanding
8
+ - automatically expanding to given depth
9
+ - customization of all object properties
10
+ - checkboxes enabled on whole tree or based on property
11
+ - recursive seletion mode, where leafes can be selected
12
+ - build-in support for search
13
+
14
+ ## Instalation
15
+
16
+ install the package `@keenmate/svelte-treeview` using your favourite package manager.
17
+
18
+ Font awesome is required for expand/collapse icons.
19
+
20
+ ## Minimal usage
21
+
22
+
23
+ ```svelte
24
+ <script lang="ts">
25
+ import { TreeView } from '$lib/index.js';
26
+
27
+ let tree = [
28
+ { nodePath: 'animals', title: 'Animals', hasChildren: true },
29
+ //...
30
+ { nodePath: 'animals.insects.butterflies', title: 'Butterflies' }
31
+ ];
32
+ </script>
33
+
34
+ <TreeView {tree} treeId="my-tree" let:node>
35
+ {node.title}
36
+ </TreeView>
37
+
38
+ ```
@@ -21,12 +21,12 @@ export let branchRootNode;
21
21
  export let canNest;
22
22
  export let validTarget;
23
23
  export let insPos;
24
- const getNodeId = (node) => `${treeId}-${helper.path(node)}`;
24
+ const getNodeId = (node) => `${treeId}-${node.path}`;
25
25
  function setExpansion(node, changeTo) {
26
26
  dispatch('internal-expand', { node: node, changeTo });
27
27
  }
28
28
  function isExpanded(node, depth, expandToDepth) {
29
- const nodeExpanded = helper.props.expanded(node);
29
+ const nodeExpanded = node.expanded;
30
30
  //if expanded prop is defined it has priority over expand to
31
31
  if (nodeExpanded === null) {
32
32
  return depth <= expandToDepth;
@@ -60,7 +60,7 @@ function handleDragLeave(e, node, el) {
60
60
  *check if this node is one being hovered over (highlited) and is valid target
61
61
  */
62
62
  function highlighThisNode(node, highlitedNode, validTarget) {
63
- return validTarget && helper.path(highlitedNode) == helper.path(node);
63
+ return validTarget && highlitedNode.path == node.path;
64
64
  }
65
65
  /**
66
66
  * returns true, it should highlight nesting on this node
@@ -72,9 +72,7 @@ function highlighThisNode(node, highlitedNode, validTarget) {
72
72
  function highlightNesting(node, highlitedNode, validTarget, canNest) {
73
73
  if (!highlitedNode)
74
74
  return false;
75
- return (canNest &&
76
- highlighThisNode(node, highlitedNode, validTarget) &&
77
- helper.props.nestDisabled(node) !== true);
75
+ return (canNest && highlighThisNode(node, highlitedNode, validTarget) && node.nestDisabled !== true);
78
76
  }
79
77
  /**
80
78
  * returns true, it should highlight nesting on this node
@@ -86,131 +84,127 @@ function highlightNesting(node, highlitedNode, validTarget, canNest) {
86
84
  function highlightInsert(node, highlitedNode, validTarget, canNest) {
87
85
  if (!highlitedNode)
88
86
  return false;
89
- return (!canNest &&
90
- highlighThisNode(node, highlitedNode, validTarget) &&
91
- helper.props.nestDisabled(node) !== true);
87
+ return (!canNest && highlighThisNode(node, highlitedNode, validTarget) && node.nestDisabled !== true);
92
88
  }
93
89
  let liElements = {};
94
- </script>
95
-
96
- <ul
97
- class:show-lines={childDepth === 0 && verticalLines}
98
- class:child-menu={childDepth > 0}
99
- class={childDepth === 0 ? classes.treeClass : ''}
100
- >
101
- {#each helper.getDirectChildren(tree, helper.path(branchRootNode)) as node (getNodeId(node))}
102
- {@const nesthighlighed = highlightNesting(node, highlightedNode, validTarget, canNest)}
103
- {@const insertHighlighted = highlightInsert(node, highlightedNode, validTarget, canNest)}
104
- {@const expanded = isExpanded(node, childDepth, expandTo)}
105
- {@const hasChildren = helper.props.hasChildren(node)}
106
- {@const draggable = !readonly && dragAndDrop && helper.props.isDraggable(node)}
107
- {@const isCurrentlyDragged =
108
- draggedPath == helper.path(node) ||
109
- (draggedPath && helper.path(node)?.startsWith(draggedPath))}
110
-
111
- <li
112
- class:is-child={helper.nodePathIsChild(helper.path(node))}
113
- class:has-children={hasChildren}
114
- on:contextmenu|stopPropagation={(e) => {
115
- dispatch('open-ctxmenu', { e: e, node: Node });
116
- }}
117
- on:drop|stopPropagation={(e) => handleDragDrop(e, node, liElements[getNodeId(node)])}
118
- on:dragover|stopPropagation={(e) => handleDragOver(e, node, liElements[getNodeId(node)])}
119
- on:dragenter|stopPropagation={(e) => handleDragEnter(e, node, liElements[getNodeId(node)])}
120
- on:dragleave|stopPropagation={(e) => handleDragLeave(e, node, liElements[getNodeId(node)])}
121
- bind:this={liElements[getNodeId(node)]}
122
- >
123
- {#if insPos == InsertionType.above && insertHighlighted}
124
- <div class="insert-line-wrapper">
125
- <div class="insert-line {classes.inserLineClass}" />
126
- </div>
127
- {/if}
128
-
129
- <!-- svelte-ignore a11y-no-static-element-interactions -->
130
- <div
131
- class="tree-item
132
- {nesthighlighed ? classes.expandClass : ''}
133
- {classes.nodeClass} {isCurrentlyDragged ? classes.currentlyDraggedClass : ''}"
134
- class:div-has-children={hasChildren}
135
- class:hover={insertHighlighted || nesthighlighed}
136
- {draggable}
137
- on:dragstart={(e) => handleDragStart(e, node)}
138
- on:dragend={(e) => handleDragEnd(e, node)}
139
- >
140
- {#if hasChildren}
141
- <!-- svelte-ignore a11y-click-events-have-key-events -->
142
- <span on:click={() => setExpansion(node, !expanded)}>
143
- <!-- use callback overrides expanded -->
144
- <i
145
- class="far {expanded ? classes.expandedToggleClass : classes.collapsedToggleClass}"
146
- class:fa-minus-square={expanded}
147
- class:fa-plus-square={!expanded || helper.props.useCallback(node)}
148
- />
149
- </span>
150
- {:else}
151
- <span />
152
- {/if}
153
-
154
- <Checkbox
155
- {checkboxes}
156
- {helper}
157
- {recursive}
158
- {node}
159
- {onlyLeafCheckboxes}
160
- {hideDisabledCheckboxes}
161
- {readonly}
162
- on:select={({ detail: { node } }) => selectionChanged(node)}
163
- />
164
- <span class:pointer-cursor={draggable}>
165
- <slot {node} />
166
- </span>
167
- </div>
168
-
169
- {#if nesthighlighed}
170
- <div class="insert-line-wrapper">
171
- <div
172
- class="insert-line insert-line-child {classes.inserLineClass} {classes.inserLineNestClass}"
173
- />
174
- </div>
175
- {/if}
176
- {#if expanded && hasChildren}
177
- <svelte:self
178
- branchRootNode={node}
179
- childDepth={childDepth + 1}
180
- {treeId}
181
- {checkboxes}
182
- {tree}
183
- {recursive}
184
- {helper}
185
- {classes}
186
- {readonly}
187
- {onlyLeafCheckboxes}
188
- {hideDisabledCheckboxes}
189
- {expandTo}
190
- {draggedPath}
191
- {dragAndDrop}
192
- {verticalLines}
193
- {canNest}
194
- {insPos}
195
- {validTarget}
196
- {highlightedNode}
197
- on:open-ctxmenu
198
- on:internal-expand
199
- on:internal-selectionChanged
200
- let:node={nodeNested}
201
- >
202
- <slot node={nodeNested} />
203
- </svelte:self>
204
- {/if}
205
- {#if !expanded && hasChildren}
206
- <ul class:child-menu={childDepth > 0} />
207
- {/if}
208
- <!-- Show line if insering -->
209
- {#if insPos === InsertionType.below && insertHighlighted}
210
- <div class="insert-line-wrapper">
211
- <div class="insert-line {classes.inserLineClass}" />
212
- </div>
213
- {/if}
214
- </li>
215
- {/each}
216
- </ul>
90
+ </script>
91
+
92
+ <ul
93
+ class:show-lines={childDepth === 0 && verticalLines}
94
+ class:child-menu={childDepth > 0}
95
+ class={childDepth === 0 ? classes.treeClass : ''}
96
+ >
97
+ {#each helper.getDirectChildren(tree, branchRootNode?.path ?? null) as node (getNodeId(node))}
98
+ {@const nesthighlighed = highlightNesting(node, highlightedNode, validTarget, canNest)}
99
+ {@const insertHighlighted = highlightInsert(node, highlightedNode, validTarget, canNest)}
100
+ {@const expanded = isExpanded(node, childDepth, expandTo)}
101
+ {@const hasChildren = node.hasChildren}
102
+ {@const draggable = !readonly && dragAndDrop && node.isDraggable}
103
+ {@const isCurrentlyDragged =
104
+ draggedPath == node.path || (draggedPath && node.path?.startsWith(draggedPath))}
105
+
106
+ <li
107
+ class:is-child={helper.nodePathIsChild(node.path)}
108
+ class:has-children={hasChildren}
109
+ on:contextmenu|stopPropagation={(e) => {
110
+ dispatch('open-ctxmenu', { e: e, node: Node });
111
+ }}
112
+ on:drop|stopPropagation={(e) => handleDragDrop(e, node, liElements[getNodeId(node)])}
113
+ on:dragover|stopPropagation={(e) => handleDragOver(e, node, liElements[getNodeId(node)])}
114
+ on:dragenter|stopPropagation={(e) => handleDragEnter(e, node, liElements[getNodeId(node)])}
115
+ on:dragleave|stopPropagation={(e) => handleDragLeave(e, node, liElements[getNodeId(node)])}
116
+ bind:this={liElements[getNodeId(node)]}
117
+ >
118
+ {#if insPos == InsertionType.above && insertHighlighted}
119
+ <div class="insert-line-wrapper">
120
+ <div class="insert-line {classes.inserLineClass}" />
121
+ </div>
122
+ {/if}
123
+
124
+ <!-- svelte-ignore a11y-no-static-element-interactions -->
125
+ <div
126
+ class="tree-item
127
+ {nesthighlighed ? classes.expandClass : ''}
128
+ {classes.nodeClass} {isCurrentlyDragged ? classes.currentlyDraggedClass : ''}"
129
+ class:div-has-children={hasChildren}
130
+ class:hover={insertHighlighted || nesthighlighed}
131
+ {draggable}
132
+ on:dragstart={(e) => handleDragStart(e, node)}
133
+ on:dragend={(e) => handleDragEnd(e, node)}
134
+ >
135
+ {#if hasChildren}
136
+ <!-- svelte-ignore a11y-click-events-have-key-events -->
137
+ <span on:click={() => setExpansion(node, !expanded)}>
138
+ <!-- use callback overrides expanded -->
139
+ <i
140
+ class="far {expanded ? classes.expandedToggleClass : classes.collapsedToggleClass}"
141
+ class:fa-minus-square={expanded}
142
+ class:fa-plus-square={!expanded || node.useCallback}
143
+ />
144
+ </span>
145
+ {:else}
146
+ <span />
147
+ {/if}
148
+
149
+ <Checkbox
150
+ {checkboxes}
151
+ {recursive}
152
+ {node}
153
+ {onlyLeafCheckboxes}
154
+ {hideDisabledCheckboxes}
155
+ {readonly}
156
+ on:select={({ detail: { node } }) => selectionChanged(node)}
157
+ />
158
+ <span class:pointer-cursor={draggable}>
159
+ <slot node={node.originalNode} />
160
+ </span>
161
+ </div>
162
+
163
+ {#if nesthighlighed}
164
+ <div class="insert-line-wrapper">
165
+ <div
166
+ class="insert-line insert-line-child {classes.inserLineClass} {classes.inserLineNestClass}"
167
+ />
168
+ </div>
169
+ {/if}
170
+ {#if expanded && hasChildren}
171
+ <svelte:self
172
+ branchRootNode={node}
173
+ childDepth={childDepth + 1}
174
+ {treeId}
175
+ {checkboxes}
176
+ {tree}
177
+ {recursive}
178
+ {helper}
179
+ {classes}
180
+ {readonly}
181
+ {onlyLeafCheckboxes}
182
+ {hideDisabledCheckboxes}
183
+ {expandTo}
184
+ {draggedPath}
185
+ {dragAndDrop}
186
+ {verticalLines}
187
+ {canNest}
188
+ {insPos}
189
+ {validTarget}
190
+ {highlightedNode}
191
+ on:open-ctxmenu
192
+ on:internal-expand
193
+ on:internal-selectionChanged
194
+ let:node={nodeNested}
195
+ >
196
+ <slot node={nodeNested} />
197
+ </svelte:self>
198
+ {/if}
199
+ {#if !expanded && hasChildren}
200
+ <ul class:child-menu={childDepth > 0} />
201
+ {/if}
202
+ <!-- Show line if insering -->
203
+ {#if insPos === InsertionType.below && insertHighlighted}
204
+ <div class="insert-line-wrapper">
205
+ <div class="insert-line {classes.inserLineClass}" />
206
+ </div>
207
+ {/if}
208
+ </li>
209
+ {/each}
210
+ </ul>
@@ -1,64 +1,42 @@
1
1
  <script>import { createEventDispatcher } from 'svelte';
2
2
  import { SelectionModes, VisualState } from './types.js';
3
- import { SelectionProvider } from './providers/selection-provider.js';
3
+ import { isSelectable } from './providers/selection-provider.js';
4
4
  export let checkboxes;
5
- export let helper;
6
5
  export let recursive;
7
6
  export let node;
8
7
  export let onlyLeafCheckboxes;
9
8
  export let hideDisabledCheckboxes;
10
9
  export let readonly = false;
11
- let indeterminate;
12
- $: {
13
- if (helper.props.visualState(node) == 'indeterminate') {
14
- indeterminate = true;
15
- }
16
- else {
17
- indeterminate = false;
18
- }
19
- }
20
- // TODO pass from root
21
- $: selectionProvider = new SelectionProvider(helper, recursive);
22
10
  const dispatch = createEventDispatcher();
23
11
  function onSelect(node) {
24
12
  dispatch('select', { node });
25
13
  }
26
- </script>
27
-
28
- {#if checkboxes == SelectionModes.perNode || checkboxes == SelectionModes.all}
29
- {#if selectionProvider.isSelectable(node, checkboxes)}
30
- <!-- select node -->
31
- {#if !recursive || (recursive && !helper.props.hasChildren(node))}
32
- <input
33
- type="checkbox"
34
- on:change={() => onSelect(node)}
35
- checked={helper.props.selected(node)}
36
- disabled={readonly}
37
- />
38
- <!-- select children-->
39
- {:else if !onlyLeafCheckboxes}
40
- <!-- @ts-ingore -->
41
- <input
42
- type="checkbox"
43
- on:click={() => onSelect(node)}
44
- checked={helper.props.visualState(node) === VisualState.selected}
45
- {indeterminate}
46
- disabled={readonly}
47
- />
48
- {:else}
49
- <input
50
- type="checkbox"
51
- on:click={null}
52
- disabled={true}
53
- class:invisible={hideDisabledCheckboxes}
54
- />
55
- {/if}
56
- {:else}
57
- <input
58
- type="checkbox"
59
- on:click|preventDefault|stopPropagation
60
- disabled={true}
61
- class:invisible={hideDisabledCheckboxes}
62
- />
63
- {/if}
64
- {/if}
14
+ </script>
15
+
16
+ {#if checkboxes == SelectionModes.perNode || checkboxes == SelectionModes.all}
17
+ {#if isSelectable(node, checkboxes)}
18
+ {#if !recursive || (recursive && !node.hasChildren) || !onlyLeafCheckboxes}
19
+ <input
20
+ type="checkbox"
21
+ on:change={() => onSelect(node)}
22
+ checked={node.visualState === VisualState.selected}
23
+ indeterminate={node.visualState === VisualState.indeterminate}
24
+ disabled={readonly}
25
+ />
26
+ {:else}
27
+ <input
28
+ type="checkbox"
29
+ on:click={null}
30
+ disabled={true}
31
+ class:invisible={hideDisabledCheckboxes}
32
+ />
33
+ {/if}
34
+ {:else}
35
+ <input
36
+ type="checkbox"
37
+ on:click|preventDefault|stopPropagation
38
+ disabled={true}
39
+ class:invisible={hideDisabledCheckboxes}
40
+ />
41
+ {/if}
42
+ {/if}
@@ -1,10 +1,8 @@
1
1
  import { SvelteComponent } from "svelte";
2
2
  import { SelectionModes, type Node } from './types.js';
3
- import type { TreeHelper } from './index.js';
4
3
  declare const __propDef: {
5
4
  props: {
6
5
  checkboxes: SelectionModes;
7
- helper: TreeHelper;
8
6
  recursive: boolean;
9
7
  node: Node;
10
8
  onlyLeafCheckboxes: boolean;