@keenmate/svelte-treeview 1.0.0-beta.5 → 1.0.0-beta.7

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.
@@ -6,6 +6,7 @@ import { TreeHelper } from './index.js';
6
6
  import Branch from './Branch.svelte';
7
7
  import { SelectionProvider } from './providers/selection-provider.js';
8
8
  import { DragDropProvider } from './providers/drag-drop-provider.js';
9
+ import uniq from 'lodash.uniq';
9
10
  const dispatch = createEventDispatcher();
10
11
  export let treeId;
11
12
  /**
@@ -73,6 +74,11 @@ export let showContextMenu = false;
73
74
  * any user made expansion will override this.
74
75
  */
75
76
  export let expandTo = 0;
77
+ /**
78
+ * Treshold for automatic expansion. If tree has less or equal nodes than this value,
79
+ * all nodes will be expanded. Default is 0, which means no automatic expansion
80
+ */
81
+ export let expansionTreshold = 0;
76
82
  /**
77
83
  * Classes used in tree. You can override default classes with this prop.
78
84
  * It is recommended to use default classes and add aditinal styles in your css
@@ -127,13 +133,35 @@ export function changeAllExpansion(changeTo) {
127
133
  expandedIds = [];
128
134
  }
129
135
  }
136
+ export function expandToNode(nodePath) {
137
+ const targetNode = helper.findNode(computedTree, nodePath);
138
+ if (!targetNode) {
139
+ console.error('Node with path', nodePath, 'not found');
140
+ return;
141
+ }
142
+ const parents = helper.getParents(computedTree, targetNode);
143
+ debugLog("expanding to node '" + nodePath + "'" + ' parents', parents);
144
+ console.log('parents', parents);
145
+ expandedIds = uniq([...expandedIds, ...parents.map((node) => node.id)]);
146
+ }
147
+ export function setExpansions(expansions) {
148
+ if (!Array.isArray(expansions)) {
149
+ console.error('expansions must be an array');
150
+ return;
151
+ }
152
+ expandedIds = expansions;
153
+ }
130
154
  function computeTree(helper, selectionProvider, userProvidedTree, filter, props, expandedIds, value) {
131
155
  if (!Array.isArray(userProvidedTree) || !Array.isArray(value)) {
132
156
  console.error('value and tree must be arrays!!');
133
157
  return [];
134
158
  }
135
159
  const mappedTree = helper.mapTree(userProvidedTree, { ...defaultPropNames, ...props });
136
- const filteredTree = helper.searchTree(mappedTree, filter);
160
+ const { tree: filteredTree, count: filteredCount } = helper.searchTree(mappedTree, filter);
161
+ // treshold applies to nodes that match the filter, not all their parents
162
+ if (filteredCount <= expansionTreshold) {
163
+ expandedIds = uniq([...expandedIds, ...filteredTree.map((node) => node.id)]);
164
+ }
137
165
  helper.markExpanded(filteredTree, expandedIds);
138
166
  // TODO here we could save last value and only recompute visual state if value changed
139
167
  // or use diff to only update affected nodes
@@ -55,6 +55,10 @@ declare const __propDef: {
55
55
  * Automaticaly expand nodes to this level,
56
56
  * any user made expansion will override this.
57
57
  */ expandTo?: number | undefined;
58
+ /**
59
+ * Treshold for automatic expansion. If tree has less or equal nodes than this value,
60
+ * all nodes will be expanded. Default is 0, which means no automatic expansion
61
+ */ expansionTreshold?: number | undefined;
58
62
  /**
59
63
  * Classes used in tree. You can override default classes with this prop.
60
64
  * It is recommended to use default classes and add aditinal styles in your css
@@ -76,6 +80,8 @@ declare const __propDef: {
76
80
  * It should return true, if drop is disabled on that node.
77
81
  */ dropDisabledCallback?: DragEnterCallback | null | undefined;
78
82
  changeAllExpansion?: ((changeTo: boolean) => void) | undefined;
83
+ expandToNode?: ((nodePath: string) => void) | undefined;
84
+ setExpansions?: ((expansions: NodeId[]) => void) | undefined;
79
85
  };
80
86
  events: {
81
87
  expansion: CustomEvent<any>;
@@ -104,5 +110,7 @@ export type TreeViewEvents = typeof __propDef.events;
104
110
  export type TreeViewSlots = typeof __propDef.slots;
105
111
  export default class TreeView extends SvelteComponent<TreeViewProps, TreeViewEvents, TreeViewSlots> {
106
112
  get changeAllExpansion(): (changeTo: boolean) => void;
113
+ get expandToNode(): (nodePath: string) => void;
114
+ get setExpansions(): (expansions: NodeId[]) => void;
107
115
  }
108
116
  export {};
@@ -21,7 +21,10 @@ export declare class TreeHelper {
21
21
  */
22
22
  expandToLevel(tree: Tree, level: number): NodeId[];
23
23
  getDepthLevel(nodePath: string): number;
24
- searchTree(tree: Tree, filter: FilterFunction | null): Tree;
24
+ searchTree(tree: Tree, filter: FilterFunction | null): {
25
+ count: number;
26
+ tree: Tree;
27
+ };
25
28
  getParents(tree: Tree, targetNode: Node): Node[];
26
29
  /** orders nodes by priorityProp
27
30
  */
@@ -21,7 +21,7 @@ export class TreeHelper {
21
21
  dragDisabled: node[properties.dragDisabled] === true,
22
22
  insertDisabled: node[properties.insertDisabled] === true,
23
23
  nestAllowed: node[properties.nestAllowed] === true,
24
- checkbox: node[properties.checkbox] === true,
24
+ checkbox: node[properties.checkbox] ?? null,
25
25
  expanded: false,
26
26
  selected: false,
27
27
  visualState: VisualState.notSelected,
@@ -116,7 +116,7 @@ export class TreeHelper {
116
116
  //#endregion
117
117
  searchTree(tree, filter) {
118
118
  if (!filter)
119
- return tree;
119
+ return { count: tree.length, tree };
120
120
  const filteredNodes = tree.filter(filter);
121
121
  const resultNodes = [];
122
122
  // add all parents from each node
@@ -127,12 +127,12 @@ export class TreeHelper {
127
127
  resultNodes.push(...parentNodes);
128
128
  });
129
129
  const uniqueNodes = uniqueBy(resultNodes, (node) => node.path);
130
- return uniqueNodes;
130
+ return { count: filteredNodes.length, tree: uniqueNodes };
131
131
  }
132
132
  getParents(tree, targetNode) {
133
133
  const parentsPaths = [];
134
134
  // TODO refactor
135
- let nodePath = this.getParentNodePath(targetNode.path);
135
+ let nodePath = targetNode.path;
136
136
  // get all parents
137
137
  while (nodePath !== null && nodePath !== '') {
138
138
  nodePath = this.getParentNodePath(nodePath);
package/dist/types.d.ts CHANGED
@@ -30,7 +30,7 @@ export type Node = {
30
30
  dragDisabled: boolean;
31
31
  insertDisabled: boolean;
32
32
  nestAllowed: boolean;
33
- checkbox: boolean;
33
+ checkbox: boolean | null;
34
34
  visualState: VisualState;
35
35
  expanded: boolean;
36
36
  selected: boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@keenmate/svelte-treeview",
3
- "version": "1.0.0-beta.5",
3
+ "version": "1.0.0-beta.7",
4
4
  "scripts": {
5
5
  "dev": "vite dev",
6
6
  "build": "vite build && npm run package",