tree-element 0.1.1 → 0.2.0

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.
Files changed (148) hide show
  1. package/README.md +33 -29
  2. package/lib/animation.js +5 -5
  3. package/lib/animation.js.map +1 -1
  4. package/lib/classNames.d.ts.map +1 -1
  5. package/lib/classNames.js +2 -2
  6. package/lib/classNames.js.map +1 -1
  7. package/lib/dataLoader.d.ts.map +1 -1
  8. package/lib/dataLoader.js +14 -20
  9. package/lib/dataLoader.js.map +1 -1
  10. package/lib/dragAndDropHandler/binarySearch.js +3 -3
  11. package/lib/dragAndDropHandler/binarySearch.js.map +1 -1
  12. package/lib/dragAndDropHandler/dragElement.d.ts.map +1 -1
  13. package/lib/dragAndDropHandler/dragElement.js +9 -12
  14. package/lib/dragAndDropHandler/dragElement.js.map +1 -1
  15. package/lib/dragAndDropHandler/generateHitAreas.d.ts.map +1 -1
  16. package/lib/dragAndDropHandler/generateHitAreas.js +25 -25
  17. package/lib/dragAndDropHandler/generateHitAreas.js.map +1 -1
  18. package/lib/dragAndDropHandler/index.d.ts.map +1 -1
  19. package/lib/dragAndDropHandler/index.js +90 -114
  20. package/lib/dragAndDropHandler/index.js.map +1 -1
  21. package/lib/dragAndDropHandler/iterateVisibleNodes.d.ts.map +1 -1
  22. package/lib/dragAndDropHandler/iterateVisibleNodes.js +10 -10
  23. package/lib/dragAndDropHandler/iterateVisibleNodes.js.map +1 -1
  24. package/lib/dragAndDropHandler/types.d.ts.map +1 -1
  25. package/lib/elementsRenderer.d.ts +15 -6
  26. package/lib/elementsRenderer.d.ts.map +1 -1
  27. package/lib/elementsRenderer.js +157 -121
  28. package/lib/elementsRenderer.js.map +1 -1
  29. package/lib/events.d.ts.map +1 -1
  30. package/lib/index.d.ts +5 -5
  31. package/lib/index.d.ts.map +1 -1
  32. package/lib/index.js +249 -252
  33. package/lib/index.js.map +1 -1
  34. package/lib/keyHandler.d.ts +2 -3
  35. package/lib/keyHandler.d.ts.map +1 -1
  36. package/lib/keyHandler.js +26 -25
  37. package/lib/keyHandler.js.map +1 -1
  38. package/lib/methodTypes.d.ts +1 -1
  39. package/lib/methodTypes.d.ts.map +1 -1
  40. package/lib/mouseHandler.d.ts.map +1 -1
  41. package/lib/mouseHandler.js +26 -42
  42. package/lib/mouseHandler.js.map +1 -1
  43. package/lib/mouseUtils.d.ts.map +1 -1
  44. package/lib/mouseUtils.js +2 -2
  45. package/lib/mouseUtils.js.map +1 -1
  46. package/lib/node.d.ts +2 -1
  47. package/lib/node.d.ts.map +1 -1
  48. package/lib/node.js +36 -35
  49. package/lib/node.js.map +1 -1
  50. package/lib/nodeElement/borderDropHint.d.ts.map +1 -1
  51. package/lib/nodeElement/borderDropHint.js +4 -5
  52. package/lib/nodeElement/borderDropHint.js.map +1 -1
  53. package/lib/nodeElement/folderElement.d.ts +7 -4
  54. package/lib/nodeElement/folderElement.d.ts.map +1 -1
  55. package/lib/nodeElement/folderElement.js +61 -42
  56. package/lib/nodeElement/folderElement.js.map +1 -1
  57. package/lib/nodeElement/ghostDropHint.d.ts.map +1 -1
  58. package/lib/nodeElement/ghostDropHint.js +5 -9
  59. package/lib/nodeElement/ghostDropHint.js.map +1 -1
  60. package/lib/nodeElement/index.d.ts +2 -1
  61. package/lib/nodeElement/index.d.ts.map +1 -1
  62. package/lib/nodeElement/index.js +34 -26
  63. package/lib/nodeElement/index.js.map +1 -1
  64. package/lib/nodeUtils.d.ts.map +1 -1
  65. package/lib/nodeUtils.js +1 -1
  66. package/lib/nodeUtils.js.map +1 -1
  67. package/lib/options.d.ts.map +1 -1
  68. package/lib/positionUtils.js +3 -3
  69. package/lib/positionUtils.js.map +1 -1
  70. package/lib/requestUrl.d.ts +1 -1
  71. package/lib/requestUrl.d.ts.map +1 -1
  72. package/lib/requestUrl.js +4 -6
  73. package/lib/requestUrl.js.map +1 -1
  74. package/lib/saveStateHandler.d.ts.map +1 -1
  75. package/lib/saveStateHandler.js +38 -48
  76. package/lib/saveStateHandler.js.map +1 -1
  77. package/lib/scrollHandler/containerScrollParent.d.ts.map +1 -1
  78. package/lib/scrollHandler/containerScrollParent.js +9 -11
  79. package/lib/scrollHandler/containerScrollParent.js.map +1 -1
  80. package/lib/scrollHandler/createScrollParent.d.ts.map +1 -1
  81. package/lib/scrollHandler/createScrollParent.js +10 -10
  82. package/lib/scrollHandler/createScrollParent.js.map +1 -1
  83. package/lib/scrollHandler/documentScrollParent.d.ts.map +1 -1
  84. package/lib/scrollHandler/documentScrollParent.js +16 -19
  85. package/lib/scrollHandler/documentScrollParent.js.map +1 -1
  86. package/lib/scrollHandler/scrollParent.d.ts.map +1 -1
  87. package/lib/scrollHandler/scrollParent.js +11 -17
  88. package/lib/scrollHandler/scrollParent.js.map +1 -1
  89. package/lib/scrollHandler.d.ts.map +1 -1
  90. package/lib/scrollHandler.js +11 -14
  91. package/lib/scrollHandler.js.map +1 -1
  92. package/lib/selectNodeHandler.d.ts +2 -2
  93. package/lib/selectNodeHandler.d.ts.map +1 -1
  94. package/lib/selectNodeHandler.js +45 -54
  95. package/lib/selectNodeHandler.js.map +1 -1
  96. package/lib/setDefaultOptions.d.ts.map +1 -1
  97. package/lib/setDefaultOptions.js +23 -28
  98. package/lib/setDefaultOptions.js.map +1 -1
  99. package/lib/triggerCustomEvent.d.ts.map +1 -1
  100. package/lib/triggerCustomEvent.js +2 -2
  101. package/lib/triggerCustomEvent.js.map +1 -1
  102. package/lib/util.d.ts.map +1 -1
  103. package/lib/util.js +2 -2
  104. package/lib/util.js.map +1 -1
  105. package/lib/version.d.ts +1 -1
  106. package/lib/version.js +1 -1
  107. package/lib/version.js.map +1 -1
  108. package/package.json +5 -3
  109. package/src/animation.ts +1 -1
  110. package/src/classNames.ts +52 -52
  111. package/src/dataLoader.ts +68 -71
  112. package/src/dragAndDropHandler/binarySearch.ts +17 -17
  113. package/src/dragAndDropHandler/dragElement.ts +50 -50
  114. package/src/dragAndDropHandler/generateHitAreas.ts +156 -161
  115. package/src/dragAndDropHandler/index.ts +342 -351
  116. package/src/dragAndDropHandler/iterateVisibleNodes.ts +59 -63
  117. package/src/dragAndDropHandler/types.ts +5 -5
  118. package/src/elementsRenderer.ts +351 -298
  119. package/src/events.ts +216 -216
  120. package/src/header.txt +1 -1
  121. package/src/index.ts +52 -82
  122. package/src/keyHandler.ts +125 -115
  123. package/src/methodTypes.ts +4 -7
  124. package/src/mouseHandler.ts +314 -317
  125. package/src/mouseUtils.ts +14 -14
  126. package/src/node.ts +761 -766
  127. package/src/nodeElement/borderDropHint.ts +25 -25
  128. package/src/nodeElement/folderElement.ts +135 -112
  129. package/src/nodeElement/ghostDropHint.ts +62 -62
  130. package/src/nodeElement/index.ts +88 -74
  131. package/src/nodeUtils.ts +5 -5
  132. package/src/options.ts +6 -2
  133. package/src/requestUrl.ts +3 -3
  134. package/src/saveStateHandler.ts +204 -206
  135. package/src/scrollHandler/containerScrollParent.ts +49 -52
  136. package/src/scrollHandler/createScrollParent.ts +27 -27
  137. package/src/scrollHandler/documentScrollParent.ts +70 -70
  138. package/src/scrollHandler/scrollParent.ts +98 -98
  139. package/src/scrollHandler.ts +45 -45
  140. package/src/selectNodeHandler.ts +176 -166
  141. package/src/setDefaultOptions.ts +25 -22
  142. package/src/triggerCustomEvent.ts +7 -3
  143. package/src/util.ts +2 -2
  144. package/src/version.ts +1 -1
  145. package/tree_element.debug.js +3752 -3784
  146. package/tree_element.debug.js.map +1 -1
  147. package/tree_element.js +2 -2
  148. package/tree_element.js.map +1 -1
package/src/node.ts CHANGED
@@ -12,9 +12,9 @@ export type NodeData = NodeRecord | string;
12
12
  export type NodeId = number | string;
13
13
 
14
14
  export interface NodeRecord {
15
- [key: string]: unknown;
16
- children?: NodeData[];
17
- id?: NodeId;
15
+ [key: string]: unknown;
16
+ children?: NodeData[];
17
+ id?: NodeId;
18
18
  }
19
19
 
20
20
  export type Position = "after" | "before" | "inside";
@@ -40,792 +40,787 @@ type IterateCallback = (node: Node, level: number) => boolean;
40
40
  * :::
41
41
  */
42
42
  export class Node {
43
- /** @hidden */
44
- [key: string]: unknown;
45
-
46
- /** The child nodes. */
47
- public children: Node[];
48
- /** The `li` element, once the node is rendered. */
49
- public element?: HTMLElement;
50
- /** The id from the node data. */
51
- public id?: NodeId;
52
- /** @hidden */
53
- public idMapping?: Map<NodeId, Node>;
54
- /** Whether the node's children are being fetched. */
55
- public is_loading?: boolean;
56
- /** Whether the folder is open. */
57
- public is_open?: boolean;
58
- /** Whether the node data had an empty `children` array. */
59
- public isEmptyFolder: boolean;
60
- /** Whether the children still have to be fetched. */
61
- public load_on_demand: boolean;
62
- /** The label. Also settable from the `label` key in node data. */
63
- public name: string;
64
- /** @hidden */
65
- public nodeClass?: typeof Node;
66
- /** The parent; `null` for the root node. */
67
- public parent: Node | null;
68
- /** The root node. */
69
- public tree?: Node;
70
-
71
- /** @hidden */
72
- constructor(
73
- nodeData: NodeData | null = null,
74
- isRoot = false,
75
- nodeClass = Node,
76
- ) {
77
- this.name = "";
78
- this.load_on_demand = false;
79
-
80
- this.isEmptyFolder =
81
- nodeData != null &&
82
- isNodeRecordWithChildren(nodeData) &&
83
- nodeData.children.length === 0;
84
-
85
- this.setData(nodeData);
86
-
87
- this.children = [];
88
- this.parent = null;
89
- this.is_loading = undefined;
90
- this.is_open ??= undefined;
91
-
92
- if (isRoot) {
93
- this.idMapping = new Map<NodeId, Node>();
94
- this.tree = this;
95
- this.nodeClass = nodeClass;
96
- } else {
97
- this.idMapping = undefined;
43
+ /** @hidden */
44
+ [key: string]: unknown;
45
+
46
+ /** The child nodes. */
47
+ public children: Node[];
48
+ /** The `li` element, once the node is rendered. Nodes inside a closed
49
+ * folder are rendered when the folder is opened. */
50
+ public element?: HTMLElement;
51
+ /** The id from the node data. */
52
+ public id?: NodeId;
53
+ /** @hidden */
54
+ public idMapping?: Map<NodeId, Node>;
55
+ /** Whether the node's children are being fetched. */
56
+ public is_loading?: boolean;
57
+ /** Whether the folder is open. */
58
+ public is_open?: boolean;
59
+ /** Whether the node data had an empty `children` array. */
60
+ public isEmptyFolder: boolean;
61
+ /** Whether the children still have to be fetched. */
62
+ public load_on_demand: boolean;
63
+ /** The label. Also settable from the `label` key in node data. */
64
+ public name: string;
65
+ /** @hidden */
66
+ public nodeClass?: typeof Node;
67
+ /** The parent; `null` for the root node. */
68
+ public parent: Node | null;
69
+ /** The root node. */
70
+ public tree?: Node;
71
+
72
+ /** @hidden */
73
+ constructor(
74
+ nodeData: NodeData | null = null,
75
+ isRoot = false,
76
+ nodeClass = Node,
77
+ ) {
78
+ this.name = "";
79
+ this.load_on_demand = false;
80
+
81
+ this.isEmptyFolder =
82
+ nodeData != null &&
83
+ isNodeRecordWithChildren(nodeData) &&
84
+ nodeData.children.length === 0;
85
+
86
+ this.setData(nodeData);
87
+
88
+ this.children = [];
89
+ this.parent = null;
90
+ this.is_loading = undefined;
91
+ this.is_open ??= undefined;
92
+
93
+ if (isRoot) {
94
+ this.idMapping = new Map<NodeId, Node>();
95
+ this.tree = this;
96
+ this.nodeClass = nodeClass;
97
+ } else {
98
+ this.idMapping = undefined;
99
+ }
100
+ }
101
+
102
+ /**
103
+ * Adds a sibling after this node.
104
+ *
105
+ * @returns The new node, or `null` when the node has no parent.
106
+ * @group Changing the tree
107
+ */
108
+ public addAfter(nodeInfo: NodeData): Node | null {
109
+ if (!this.parent) {
110
+ return null;
111
+ } else {
112
+ const node = this.createNode(nodeInfo);
113
+
114
+ const childIndex = this.parent.getChildIndex(this);
115
+ this.parent.addChildAtPosition(node, childIndex + 1);
116
+
117
+ node.loadChildrenFromData(nodeInfo);
118
+ return node;
119
+ }
120
+ }
121
+
122
+ /**
123
+ * Adds a sibling before this node.
124
+ *
125
+ * @returns The new node, or `null` when the node has no parent.
126
+ * @group Changing the tree
127
+ */
128
+ public addBefore(nodeInfo: NodeData): Node | null {
129
+ if (!this.parent) {
130
+ return null;
131
+ } else {
132
+ const node = this.createNode(nodeInfo);
133
+
134
+ const childIndex = this.parent.getChildIndex(this);
135
+ this.parent.addChildAtPosition(node, childIndex);
136
+
137
+ node.loadChildrenFromData(nodeInfo);
138
+ return node;
139
+ }
140
+ }
141
+
142
+ /**
143
+ * Adds an existing `Node` object as the last child.
144
+ *
145
+ * @group Changing the tree
146
+ */
147
+ public addChild(node: Node): void {
148
+ this.children.push(node);
149
+ node.setParent(this);
150
+ }
151
+
152
+ /**
153
+ * Adds an existing `Node` object as a child, at this position in
154
+ * `children`. The index starts at `0`.
155
+ *
156
+ * @group Changing the tree
157
+ */
158
+ public addChildAtPosition(node: Node, index: number): void {
159
+ this.children.splice(index, 0, node);
160
+ node.setParent(this);
161
+ }
162
+
163
+ /** @hidden */
164
+ public addNodeToIndex(node: Node): void {
165
+ if (node.id != null) {
166
+ this.idMapping?.set(node.id, node);
167
+ }
168
+ }
169
+
170
+ /**
171
+ * Inserts a new parent between this node and its current parent.
172
+ *
173
+ * @returns The new node, or `null` when the node has no parent.
174
+ * @group Changing the tree
175
+ */
176
+ public addParent(nodeInfo: NodeData): Node | null {
177
+ if (!this.parent) {
178
+ return null;
179
+ } else {
180
+ const newParent = this.createNode(nodeInfo);
181
+
182
+ if (this.tree) {
183
+ newParent.setParent(this.tree);
184
+ }
185
+ const originalParent = this.parent;
186
+
187
+ for (const child of originalParent.children) {
188
+ newParent.addChild(child);
189
+ }
190
+
191
+ originalParent.children = [];
192
+ originalParent.addChild(newParent);
193
+ return newParent;
194
+ }
195
+ }
196
+
197
+ /**
198
+ * Adds a child at the end.
199
+ *
200
+ * @returns The new node.
201
+ * @group Changing the tree
202
+ */
203
+ public append(nodeInfo: NodeData): Node {
204
+ const node = this.createNode(nodeInfo);
205
+ this.addChild(node);
206
+
207
+ node.loadChildrenFromData(nodeInfo);
208
+ return node;
209
+ }
210
+
211
+ /**
212
+ * Returns all nodes in the subtree for which the callback returns
213
+ * `true`.
214
+ *
215
+ * @example
216
+ * ```js
217
+ * const folders = tree.getTree().filter((node) => node.isFolder());
218
+ * ```
219
+ *
220
+ * @group Searching
221
+ */
222
+ public filter(f: (node: Node) => boolean): Node[] {
223
+ const result: Node[] = [];
224
+
225
+ this.iterate((node: Node) => {
226
+ if (f(node)) {
227
+ result.push(node);
228
+ }
229
+
230
+ return true;
231
+ });
232
+
233
+ return result;
234
+ }
235
+
236
+ /**
237
+ * Returns the position of a child in `children`, or `-1`.
238
+ *
239
+ * @group Inspecting a node
240
+ */
241
+ public getChildIndex(node: Node): number {
242
+ return this.children.indexOf(node);
243
+ }
244
+
245
+ /**
246
+ * Returns the subtree as plain data, ready for `JSON.stringify`.
247
+ * Internal properties (`parent`, `children`, `element`, `tree`,
248
+ * `idMapping`, `nodeClass`, `load_on_demand`, `isEmptyFolder`) are left
249
+ * out; your own properties are kept.
250
+ *
251
+ * ```js
252
+ * tree.getTree().getData();
253
+ * // [{ name: "node1", id: 1, children: [{ name: "child1", id: 2 }] }]
254
+ * ```
255
+ *
256
+ * @param includeParent - Make the result the node itself rather than its
257
+ * children. Default `false`.
258
+ * @group Reading data back
259
+ */
260
+ public getData(includeParent = false): NodeRecord[] {
261
+ const getDataFromNodes = (nodes: Node[]): Record<string, unknown>[] => {
262
+ return nodes.map((node) => {
263
+ const tmpNode: Record<string, unknown> = {};
264
+
265
+ for (const k in node) {
266
+ if (
267
+ [
268
+ "parent",
269
+ "children",
270
+ "element",
271
+ "idMapping",
272
+ "load_on_demand",
273
+ "nodeClass",
274
+ "tree",
275
+ "isEmptyFolder",
276
+ ].indexOf(k) === -1 &&
277
+ Object.prototype.hasOwnProperty.call(node, k)
278
+ ) {
279
+ const v = node[k];
280
+ tmpNode[k] = v;
281
+ }
98
282
  }
99
- }
100
-
101
- /**
102
- * Adds a sibling after this node.
103
- *
104
- * @returns The new node, or `null` when the node has no parent.
105
- * @group Changing the tree
106
- */
107
- public addAfter(nodeInfo: NodeData): Node | null {
108
- if (!this.parent) {
109
- return null;
110
- } else {
111
- const node = this.createNode(nodeInfo);
112
-
113
- const childIndex = this.parent.getChildIndex(this);
114
- this.parent.addChildAtPosition(node, childIndex + 1);
115
283
 
116
- node.loadChildrenFromData(nodeInfo);
117
- return node;
284
+ if (node.hasChildren()) {
285
+ tmpNode.children = getDataFromNodes(node.children);
118
286
  }
119
- }
120
-
121
- /**
122
- * Adds a sibling before this node.
123
- *
124
- * @returns The new node, or `null` when the node has no parent.
125
- * @group Changing the tree
126
- */
127
- public addBefore(nodeInfo: NodeData): Node | null {
128
- if (!this.parent) {
129
- return null;
130
- } else {
131
- const node = this.createNode(nodeInfo);
132
-
133
- const childIndex = this.parent.getChildIndex(this);
134
- this.parent.addChildAtPosition(node, childIndex);
135
-
136
- node.loadChildrenFromData(nodeInfo);
137
- return node;
138
- }
139
- }
140
-
141
- /**
142
- * Adds an existing `Node` object as the last child.
143
- *
144
- * @group Changing the tree
145
- */
146
- public addChild(node: Node): void {
147
- this.children.push(node);
148
- node.setParent(this);
149
- }
150
-
151
- /**
152
- * Adds an existing `Node` object as a child, at this position in
153
- * `children`. The index starts at `0`.
154
- *
155
- * @group Changing the tree
156
- */
157
- public addChildAtPosition(node: Node, index: number): void {
158
- this.children.splice(index, 0, node);
159
- node.setParent(this);
160
- }
161
-
162
- /** @hidden */
163
- public addNodeToIndex(node: Node): void {
164
- if (node.id != null) {
165
- this.idMapping?.set(node.id, node);
166
- }
167
- }
168
287
 
169
- /**
170
- * Inserts a new parent between this node and its current parent.
171
- *
172
- * @returns The new node, or `null` when the node has no parent.
173
- * @group Changing the tree
174
- */
175
- public addParent(nodeInfo: NodeData): Node | null {
176
- if (!this.parent) {
177
- return null;
288
+ return tmpNode;
289
+ });
290
+ };
291
+
292
+ if (includeParent) {
293
+ return getDataFromNodes([this]);
294
+ } else {
295
+ return getDataFromNodes(this.children);
296
+ }
297
+ }
298
+
299
+ /**
300
+ * Returns the last child, or `null`. When that child is an open folder,
301
+ * its own last child, and so on.
302
+ *
303
+ * @group Moving around the tree
304
+ */
305
+ public getLastChild(): Node | null {
306
+ if (!this.hasChildren()) {
307
+ return null;
308
+ } else {
309
+ const lastChild = this.children[this.children.length - 1] as Node;
310
+
311
+ if (!(lastChild.hasChildren() && lastChild.is_open)) {
312
+ return lastChild;
313
+ } else {
314
+ return lastChild.getLastChild();
315
+ }
316
+ }
317
+ }
318
+
319
+ /**
320
+ * Returns the depth of the node, counting the top level as `1`.
321
+ *
322
+ * @group Inspecting a node
323
+ */
324
+ public getLevel(): number {
325
+ let level = 0;
326
+ let node: Node = this; // eslint-disable-line @typescript-eslint/no-this-alias
327
+
328
+ while (node.parent) {
329
+ level += 1;
330
+ node = node.parent;
331
+ }
332
+
333
+ return level;
334
+ }
335
+
336
+ /**
337
+ * Returns the next node in the tree, regardless of whether it is
338
+ * visible. `getNextNode(false)` skips the node's own children.
339
+ *
340
+ * @group Moving around the tree
341
+ */
342
+ public getNextNode(includeChildren = true): Node | null {
343
+ if (includeChildren && this.hasChildren()) {
344
+ return this.children[0] ?? null;
345
+ } else if (!this.parent) {
346
+ return null;
347
+ } else {
348
+ const nextSibling = this.getNextSibling();
349
+
350
+ if (nextSibling) {
351
+ return nextSibling;
352
+ } else {
353
+ return this.parent.getNextNode(false);
354
+ }
355
+ }
356
+ }
357
+
358
+ /**
359
+ * Returns the next sibling, or `null`.
360
+ *
361
+ * @group Moving around the tree
362
+ */
363
+ public getNextSibling(): Node | null {
364
+ if (!this.parent) {
365
+ return null;
366
+ } else {
367
+ const nextIndex = this.parent.getChildIndex(this) + 1;
368
+ if (nextIndex < this.parent.children.length) {
369
+ return this.parent.children[nextIndex] ?? null;
370
+ } else {
371
+ return null;
372
+ }
373
+ }
374
+ }
375
+
376
+ /**
377
+ * Like `getNextNode`, but skipping nodes inside closed folders — this is
378
+ * what the arrow keys use.
379
+ *
380
+ * @group Moving around the tree
381
+ */
382
+ public getNextVisibleNode(): Node | null {
383
+ if (this.hasChildren() && this.is_open) {
384
+ // First child
385
+ return this.children[0] ?? null;
386
+ } else {
387
+ if (!this.parent) {
388
+ return null;
389
+ } else {
390
+ const nextSibling = this.getNextSibling();
391
+ if (nextSibling) {
392
+ // Next sibling
393
+ return nextSibling;
178
394
  } else {
179
- const newParent = this.createNode(nodeInfo);
180
-
181
- if (this.tree) {
182
- newParent.setParent(this.tree);
183
- }
184
- const originalParent = this.parent;
185
-
186
- for (const child of originalParent.children) {
187
- newParent.addChild(child);
188
- }
189
-
190
- originalParent.children = [];
191
- originalParent.addChild(newParent);
192
- return newParent;
395
+ // Next node of parent
396
+ return this.parent.getNextNode(false);
193
397
  }
398
+ }
194
399
  }
400
+ }
195
401
 
196
- /**
197
- * Adds a child at the end.
198
- *
199
- * @returns The new node.
200
- * @group Changing the tree
201
- */
202
- public append(nodeInfo: NodeData): Node {
203
- const node = this.createNode(nodeInfo);
204
- this.addChild(node);
205
-
206
- node.loadChildrenFromData(nodeInfo);
207
- return node;
208
- }
209
-
210
- /**
211
- * Returns all nodes in the subtree for which the callback returns
212
- * `true`.
213
- *
214
- * @example
215
- * ```js
216
- * const folders = tree.getTree().filter((node) => node.isFolder());
217
- * ```
218
- *
219
- * @group Searching
220
- */
221
- public filter(f: (node: Node) => boolean): Node[] {
222
- const result: Node[] = [];
223
-
224
- this.iterate((node: Node) => {
225
- if (f(node)) {
226
- result.push(node);
227
- }
228
-
229
- return true;
230
- });
231
-
232
- return result;
233
- }
234
-
235
- /**
236
- * Returns the position of a child in `children`, or `-1`.
237
- *
238
- * @group Inspecting a node
239
- */
240
- public getChildIndex(node: Node): number {
241
- return this.children.indexOf(node);
242
- }
243
-
244
- /**
245
- * Returns the subtree as plain data, ready for `JSON.stringify`.
246
- * Internal properties (`parent`, `children`, `element`, `tree`,
247
- * `idMapping`, `nodeClass`, `load_on_demand`, `isEmptyFolder`) are left
248
- * out; your own properties are kept.
249
- *
250
- * ```js
251
- * tree.getTree().getData();
252
- * // [{ name: "node1", id: 1, children: [{ name: "child1", id: 2 }] }]
253
- * ```
254
- *
255
- * @param includeParent - Make the result the node itself rather than its
256
- * children. Default `false`.
257
- * @group Reading data back
258
- */
259
- public getData(includeParent = false): NodeRecord[] {
260
- const getDataFromNodes = (nodes: Node[]): Record<string, unknown>[] => {
261
- return nodes.map((node) => {
262
- const tmpNode: Record<string, unknown> = {};
263
-
264
- for (const k in node) {
265
- if (
266
- [
267
- "parent",
268
- "children",
269
- "element",
270
- "idMapping",
271
- "load_on_demand",
272
- "nodeClass",
273
- "tree",
274
- "isEmptyFolder",
275
- ].indexOf(k) === -1 &&
276
- Object.prototype.hasOwnProperty.call(node, k)
277
- ) {
278
- const v = node[k];
279
- tmpNode[k] = v;
280
- }
281
- }
282
-
283
- if (node.hasChildren()) {
284
- tmpNode.children = getDataFromNodes(node.children);
285
- }
286
-
287
- return tmpNode;
288
- });
289
- };
290
-
291
- if (includeParent) {
292
- return getDataFromNodes([this]);
293
- } else {
294
- return getDataFromNodes(this.children);
295
- }
296
- }
297
-
298
- /**
299
- * Returns the last child, or `null`. When that child is an open folder,
300
- * its own last child, and so on.
301
- *
302
- * @group Moving around the tree
303
- */
304
- public getLastChild(): Node | null {
305
- if (!this.hasChildren()) {
306
- return null;
307
- } else {
308
- const lastChild = this.children[this.children.length - 1] as Node;
309
-
310
- if (!(lastChild.hasChildren() && lastChild.is_open)) {
311
- return lastChild;
312
- } else {
313
- return lastChild.getLastChild();
314
- }
315
- }
316
- }
317
-
318
- /**
319
- * Returns the depth of the node, counting the top level as `1`.
320
- *
321
- * @group Inspecting a node
322
- */
323
- public getLevel(): number {
324
- let level = 0;
325
- let node: Node = this; // eslint-disable-line @typescript-eslint/no-this-alias
326
-
327
- while (node.parent) {
328
- level += 1;
329
- node = node.parent;
330
- }
331
-
332
- return level;
333
- }
334
-
335
- /**
336
- * Returns the next node in the tree, regardless of whether it is
337
- * visible. `getNextNode(false)` skips the node's own children.
338
- *
339
- * @group Moving around the tree
340
- */
341
- public getNextNode(includeChildren = true): Node | null {
342
- if (includeChildren && this.hasChildren()) {
343
- return this.children[0] ?? null;
344
- } else if (!this.parent) {
345
- return null;
346
- } else {
347
- const nextSibling = this.getNextSibling();
348
-
349
- if (nextSibling) {
350
- return nextSibling;
351
- } else {
352
- return this.parent.getNextNode(false);
353
- }
354
- }
355
- }
356
-
357
- /**
358
- * Returns the next sibling, or `null`.
359
- *
360
- * @group Moving around the tree
361
- */
362
- public getNextSibling(): Node | null {
363
- if (!this.parent) {
364
- return null;
365
- } else {
366
- const nextIndex = this.parent.getChildIndex(this) + 1;
367
- if (nextIndex < this.parent.children.length) {
368
- return this.parent.children[nextIndex] ?? null;
369
- } else {
370
- return null;
371
- }
372
- }
373
- }
374
-
375
- /**
376
- * Like `getNextNode`, but skipping nodes inside closed folders — this is
377
- * what the arrow keys use.
378
- *
379
- * @group Moving around the tree
380
- */
381
- public getNextVisibleNode(): Node | null {
382
- if (this.hasChildren() && this.is_open) {
383
- // First child
384
- return this.children[0] ?? null;
385
- } else {
386
- if (!this.parent) {
387
- return null;
388
- } else {
389
- const nextSibling = this.getNextSibling();
390
- if (nextSibling) {
391
- // Next sibling
392
- return nextSibling;
393
- } else {
394
- // Next node of parent
395
- return this.parent.getNextNode(false);
396
- }
397
- }
398
- }
399
- }
400
-
401
- /**
402
- * Returns the first node for which the callback returns `true`.
403
- *
404
- * @group Searching
405
- */
406
- public getNodeByCallback(callback: (node: Node) => boolean): Node | null {
407
- let result: Node | null = null;
408
-
409
- this.iterate((node: Node) => {
410
- if (result) {
411
- return false;
412
- } else if (callback(node)) {
413
- result = node;
414
- return false;
415
- } else {
416
- return true;
417
- }
418
- });
419
-
420
- return result;
421
- }
422
-
423
- /**
424
- * Returns the node with this id. Only available on the root node, which
425
- * keeps the id index.
426
- *
427
- * @group Searching
428
- */
429
- public getNodeById(nodeId: NodeId): Node | null {
430
- return this.idMapping?.get(nodeId) ?? null;
431
- }
432
-
433
- /**
434
- * Returns the first node with this name.
435
- *
436
- * @group Searching
437
- */
438
- public getNodeByName(name: string): Node | null {
439
- return this.getNodeByCallback((node: Node) => node.name === name);
440
- }
441
-
442
- /**
443
- * Like `getNodeByName`, but throws when there is no such node.
444
- *
445
- * @group Searching
446
- */
447
- public getNodeByNameMustExist(name: string): Node {
448
- const node = this.getNodeByCallback((n: Node) => n.name === name);
449
-
450
- if (!node) {
451
- throw new Error(`Node with name ${name} not found`);
452
- }
453
-
454
- return node;
455
- }
456
-
457
- /**
458
- * Returns all nodes with this property value.
459
- *
460
- * @group Searching
461
- */
462
- public getNodesByProperty(key: string, value: unknown): Node[] {
463
- return this.filter((node: Node) => node[key] === value);
464
- }
465
-
466
- /**
467
- * Returns the parent: `null` for a top-level node — the root node is not
468
- * returned.
469
- *
470
- * @group Moving around the tree
471
- */
472
- public getParent(): Node | null {
473
- // Return parent except if it is the root node
474
- if (!this.parent) {
475
- return null;
476
- } else if (!this.parent.parent) {
477
- // Root node -> null
478
- return null;
479
- } else {
480
- return this.parent;
481
- }
482
- }
483
-
484
- /**
485
- * Returns the previous node in the tree, regardless of whether it is
486
- * visible.
487
- *
488
- * @group Moving around the tree
489
- */
490
- public getPreviousNode(): Node | null {
491
- if (!this.parent) {
492
- return null;
493
- } else {
494
- const previousSibling = this.getPreviousSibling();
495
-
496
- if (!previousSibling) {
497
- return this.getParent();
498
- } else if (previousSibling.hasChildren()) {
499
- return previousSibling.getLastChild();
500
- } else {
501
- return previousSibling;
502
- }
503
- }
504
- }
505
-
506
- /**
507
- * Returns the previous sibling, or `null`.
508
- *
509
- * @group Moving around the tree
510
- */
511
- public getPreviousSibling(): Node | null {
512
- if (!this.parent) {
513
- return null;
514
- } else {
515
- const previousIndex = this.parent.getChildIndex(this) - 1;
516
- if (previousIndex >= 0) {
517
- return this.parent.children[previousIndex] ?? null;
518
- } else {
519
- return null;
520
- }
521
- }
522
- }
523
-
524
- /**
525
- * Like `getPreviousNode`, but skipping nodes inside closed folders —
526
- * this is what the arrow keys use.
527
- *
528
- * @group Moving around the tree
529
- */
530
- public getPreviousVisibleNode(): Node | null {
531
- if (!this.parent) {
532
- return null;
533
- } else {
534
- const previousSibling = this.getPreviousSibling();
535
-
536
- if (!previousSibling) {
537
- return this.getParent();
538
- } else if (
539
- !previousSibling.hasChildren() ||
540
- !previousSibling.is_open
541
- ) {
542
- // Previous sibling
543
- return previousSibling;
544
- } else {
545
- // Last child of previous sibling
546
- return previousSibling.getLastChild();
547
- }
548
- }
549
- }
550
-
551
- /**
552
- * Whether the node has children.
553
- *
554
- * @group Inspecting a node
555
- */
556
- public hasChildren(): boolean {
557
- return this.children.length !== 0;
558
- }
559
-
560
- /**
561
- * Init Node from data without making it the root of the tree.
562
- *
563
- * @hidden
564
- */
565
- public initFromData(data: NodeData): void {
566
- const addNode = (nodeData: NodeData): void => {
567
- this.setData(nodeData);
568
-
569
- if (
570
- isNodeRecordWithChildren(nodeData) &&
571
- nodeData.children.length
572
- ) {
573
- addChildren(nodeData.children);
574
- }
575
- };
576
-
577
- const addChildren = (childrenData: NodeData[]): void => {
578
- for (const child of childrenData) {
579
- const node = this.createNode();
580
- node.initFromData(child);
581
- this.addChild(node);
582
- }
583
- };
584
-
585
- addNode(data);
586
- }
587
-
588
- /**
589
- * `true` when the node has children, or is marked `load_on_demand`.
590
- *
591
- * @group Inspecting a node
592
- */
593
- public isFolder(): boolean {
594
- return this.hasChildren() || this.load_on_demand;
595
- }
596
-
597
- /**
598
- * Whether this node is an ancestor of the other node.
599
- *
600
- * @group Inspecting a node
601
- */
602
- public isParentOf(node: Node): boolean {
603
- let parent = node.parent;
604
-
605
- while (parent) {
606
- if (parent === this) {
607
- return true;
608
- }
609
-
610
- parent = parent.parent;
611
- }
402
+ /**
403
+ * Returns the first node for which the callback returns `true`.
404
+ *
405
+ * @group Searching
406
+ */
407
+ public getNodeByCallback(callback: (node: Node) => boolean): Node | null {
408
+ let result: Node | null = null;
612
409
 
410
+ this.iterate((node: Node) => {
411
+ if (result) {
613
412
  return false;
614
- }
615
-
616
- /**
617
- * Walks the subtree, calling the callback with `(node, level)`. Return
618
- * `false` from the callback to stop descending into that node:
619
- *
620
- * ```js
621
- * tree.getTree().iterate((node, level) => {
622
- * console.log(" ".repeat(level) + node.name);
623
- * return level <= 2; // don't go deeper than level 2
624
- * });
625
- * ```
626
- *
627
- * @group Searching
628
- */
629
- public iterate(callback: IterateCallback): void {
630
- const _iterate = (node: Node, level: number): void => {
631
- for (const child of node.children) {
632
- const result = callback(child, level);
633
-
634
- if (result && child.hasChildren()) {
635
- _iterate(child, level + 1);
636
- }
637
- }
638
- };
639
-
640
- _iterate(this, 0);
641
- }
642
-
643
- /**
644
- * Replaces the children with new node data.
645
- *
646
- * @group Changing the tree
647
- */
648
- public loadFromData(data: NodeData[]): this {
649
- this.removeChildren();
650
-
651
- for (const childData of data) {
652
- const node = this.createNode(childData);
653
- this.addChild(node);
654
-
655
- if (isNodeRecordWithChildren(childData)) {
656
- node.loadFromData(childData.children);
657
- }
658
- }
659
-
660
- return this;
661
- }
662
-
663
- /**
664
- * Moves a node relative to another node. Called on the root node.
665
- *
666
- * @param position - `"before"`, `"after"` or `"inside"`.
667
- * @returns `false` when the move is impossible, for instance moving a
668
- * node into its own subtree.
669
- * @group Changing the tree
670
- */
671
- public moveNode(
672
- movedNode: Node,
673
- targetNode: Node,
674
- position: Position,
675
- ): boolean {
676
- if (!movedNode.parent || movedNode.isParentOf(targetNode)) {
677
- // - Node is parent of target node
678
- // - Or, parent is empty
679
- return false;
680
- } else {
681
- movedNode.parent.doRemoveChild(movedNode);
682
-
683
- switch (position) {
684
- case "after": {
685
- if (targetNode.parent) {
686
- targetNode.parent.addChildAtPosition(
687
- movedNode,
688
- targetNode.parent.getChildIndex(targetNode) + 1,
689
- );
690
- return true;
691
- }
692
- return false;
693
- }
694
-
695
- case "before": {
696
- if (targetNode.parent) {
697
- targetNode.parent.addChildAtPosition(
698
- movedNode,
699
- targetNode.parent.getChildIndex(targetNode),
700
- );
701
- return true;
702
- }
703
- return false;
704
- }
705
-
706
- case "inside": {
707
- // move inside as first child
708
- targetNode.addChildAtPosition(movedNode, 0);
709
- return true;
710
- }
711
- }
413
+ } else if (callback(node)) {
414
+ result = node;
415
+ return false;
416
+ } else {
417
+ return true;
418
+ }
419
+ });
420
+
421
+ return result;
422
+ }
423
+
424
+ /**
425
+ * Returns the node with this id. Only available on the root node, which
426
+ * keeps the id index.
427
+ *
428
+ * @group Searching
429
+ */
430
+ public getNodeById(nodeId: NodeId): Node | null {
431
+ return this.idMapping?.get(nodeId) ?? null;
432
+ }
433
+
434
+ /**
435
+ * Returns the first node with this name.
436
+ *
437
+ * @group Searching
438
+ */
439
+ public getNodeByName(name: string): Node | null {
440
+ return this.getNodeByCallback((node: Node) => node.name === name);
441
+ }
442
+
443
+ /**
444
+ * Like `getNodeByName`, but throws when there is no such node.
445
+ *
446
+ * @group Searching
447
+ */
448
+ public getNodeByNameMustExist(name: string): Node {
449
+ const node = this.getNodeByCallback((n: Node) => n.name === name);
450
+
451
+ if (!node) {
452
+ throw new Error(`Node with name ${name} not found`);
453
+ }
454
+
455
+ return node;
456
+ }
457
+
458
+ /**
459
+ * Returns all nodes with this property value.
460
+ *
461
+ * @group Searching
462
+ */
463
+ public getNodesByProperty(key: string, value: unknown): Node[] {
464
+ return this.filter((node: Node) => node[key] === value);
465
+ }
466
+
467
+ /**
468
+ * Returns the parent: `null` for a top-level node — the root node is not
469
+ * returned.
470
+ *
471
+ * @group Moving around the tree
472
+ */
473
+ public getParent(): Node | null {
474
+ // Return parent except if it is the root node
475
+ if (!this.parent) {
476
+ return null;
477
+ } else if (!this.parent.parent) {
478
+ // Root node -> null
479
+ return null;
480
+ } else {
481
+ return this.parent;
482
+ }
483
+ }
484
+
485
+ /**
486
+ * Returns the previous node in the tree, regardless of whether it is
487
+ * visible.
488
+ *
489
+ * @group Moving around the tree
490
+ */
491
+ public getPreviousNode(): Node | null {
492
+ if (!this.parent) {
493
+ return null;
494
+ } else {
495
+ const previousSibling = this.getPreviousSibling();
496
+
497
+ if (!previousSibling) {
498
+ return this.getParent();
499
+ } else if (previousSibling.hasChildren()) {
500
+ return previousSibling.getLastChild();
501
+ } else {
502
+ return previousSibling;
503
+ }
504
+ }
505
+ }
506
+
507
+ /**
508
+ * Returns the previous sibling, or `null`.
509
+ *
510
+ * @group Moving around the tree
511
+ */
512
+ public getPreviousSibling(): Node | null {
513
+ if (!this.parent) {
514
+ return null;
515
+ } else {
516
+ const previousIndex = this.parent.getChildIndex(this) - 1;
517
+ if (previousIndex >= 0) {
518
+ return this.parent.children[previousIndex] ?? null;
519
+ } else {
520
+ return null;
521
+ }
522
+ }
523
+ }
524
+
525
+ /**
526
+ * Like `getPreviousNode`, but skipping nodes inside closed folders —
527
+ * this is what the arrow keys use.
528
+ *
529
+ * @group Moving around the tree
530
+ */
531
+ public getPreviousVisibleNode(): Node | null {
532
+ if (!this.parent) {
533
+ return null;
534
+ } else {
535
+ const previousSibling = this.getPreviousSibling();
536
+
537
+ if (!previousSibling) {
538
+ return this.getParent();
539
+ } else if (!previousSibling.hasChildren() || !previousSibling.is_open) {
540
+ // Previous sibling
541
+ return previousSibling;
542
+ } else {
543
+ // Last child of previous sibling
544
+ return previousSibling.getLastChild();
545
+ }
546
+ }
547
+ }
548
+
549
+ /**
550
+ * Whether the node has children.
551
+ *
552
+ * @group Inspecting a node
553
+ */
554
+ public hasChildren(): boolean {
555
+ return this.children.length !== 0;
556
+ }
557
+
558
+ /**
559
+ * Init Node from data without making it the root of the tree.
560
+ *
561
+ * @hidden
562
+ */
563
+ public initFromData(data: NodeData): void {
564
+ const addNode = (nodeData: NodeData): void => {
565
+ this.setData(nodeData);
566
+
567
+ if (isNodeRecordWithChildren(nodeData) && nodeData.children.length) {
568
+ addChildren(nodeData.children);
569
+ }
570
+ };
571
+
572
+ const addChildren = (childrenData: NodeData[]): void => {
573
+ for (const child of childrenData) {
574
+ const node = this.createNode();
575
+ node.initFromData(child);
576
+ this.addChild(node);
577
+ }
578
+ };
579
+
580
+ addNode(data);
581
+ }
582
+
583
+ /**
584
+ * `true` when the node has children, or is marked `load_on_demand`.
585
+ *
586
+ * @group Inspecting a node
587
+ */
588
+ public isFolder(): boolean {
589
+ return this.hasChildren() || this.load_on_demand;
590
+ }
591
+
592
+ /**
593
+ * Whether this node is an ancestor of the other node.
594
+ *
595
+ * @group Inspecting a node
596
+ */
597
+ public isParentOf(node: Node): boolean {
598
+ let parent = node.parent;
599
+
600
+ while (parent) {
601
+ if (parent === this) {
602
+ return true;
603
+ }
604
+
605
+ parent = parent.parent;
606
+ }
607
+
608
+ return false;
609
+ }
610
+
611
+ /**
612
+ * Walks the subtree, calling the callback with `(node, level)`. Return
613
+ * `false` from the callback to stop descending into that node:
614
+ *
615
+ * ```js
616
+ * tree.getTree().iterate((node, level) => {
617
+ * console.log(" ".repeat(level) + node.name);
618
+ * return level <= 2; // don't go deeper than level 2
619
+ * });
620
+ * ```
621
+ *
622
+ * @group Searching
623
+ */
624
+ public iterate(callback: IterateCallback): void {
625
+ const _iterate = (node: Node, level: number): void => {
626
+ for (const child of node.children) {
627
+ const result = callback(child, level);
628
+
629
+ if (result && child.hasChildren()) {
630
+ _iterate(child, level + 1);
712
631
  }
713
- }
714
-
715
- /**
716
- * Adds a child at the start.
717
- *
718
- * @returns The new node.
719
- * @group Changing the tree
720
- */
721
- public prepend(nodeInfo: NodeData): Node {
722
- const node = this.createNode(nodeInfo);
723
- this.addChildAtPosition(node, 0);
724
-
725
- node.loadChildrenFromData(nodeInfo);
726
- return node;
727
- }
728
-
729
- /**
730
- * Removes this node from its parent.
731
- *
732
- * @group Changing the tree
733
- */
734
- public remove(): void {
735
- if (this.parent) {
736
- this.parent.removeChild(this);
737
- this.parent = null;
632
+ }
633
+ };
634
+
635
+ _iterate(this, 0);
636
+ }
637
+
638
+ /**
639
+ * Replaces the children with new node data.
640
+ *
641
+ * @group Changing the tree
642
+ */
643
+ public loadFromData(data: NodeData[]): this {
644
+ this.removeChildren();
645
+
646
+ for (const childData of data) {
647
+ const node = this.createNode(childData);
648
+ this.addChild(node);
649
+
650
+ if (isNodeRecordWithChildren(childData)) {
651
+ node.loadFromData(childData.children);
652
+ }
653
+ }
654
+
655
+ return this;
656
+ }
657
+
658
+ /**
659
+ * Moves a node relative to another node. Called on the root node.
660
+ *
661
+ * @param position - `"before"`, `"after"` or `"inside"`.
662
+ * @returns `false` when the move is impossible, for instance moving a
663
+ * node into its own subtree.
664
+ * @group Changing the tree
665
+ */
666
+ public moveNode(
667
+ movedNode: Node,
668
+ targetNode: Node,
669
+ position: Position,
670
+ ): boolean {
671
+ if (!movedNode.parent || movedNode.isParentOf(targetNode)) {
672
+ // - Node is parent of target node
673
+ // - Or, parent is empty
674
+ return false;
675
+ } else {
676
+ movedNode.parent.doRemoveChild(movedNode);
677
+
678
+ switch (position) {
679
+ case "after": {
680
+ if (targetNode.parent) {
681
+ targetNode.parent.addChildAtPosition(
682
+ movedNode,
683
+ targetNode.parent.getChildIndex(targetNode) + 1,
684
+ );
685
+ return true;
686
+ }
687
+ return false;
738
688
  }
739
- }
740
689
 
741
- /**
742
- * Removes a child and its children.
743
- *
744
- * @group Changing the tree
745
- */
746
- public removeChild(node: Node): void {
747
- // remove children from the index
748
- node.removeChildren();
749
-
750
- this.doRemoveChild(node);
751
- }
752
-
753
- /**
754
- * Removes all children.
755
- *
756
- * @group Changing the tree
757
- */
758
- public removeChildren(): void {
759
- this.iterate((child: Node) => {
760
- this.tree?.removeNodeFromIndex(child);
690
+ case "before": {
691
+ if (targetNode.parent) {
692
+ targetNode.parent.addChildAtPosition(
693
+ movedNode,
694
+ targetNode.parent.getChildIndex(targetNode),
695
+ );
761
696
  return true;
762
- });
763
-
764
- this.children = [];
765
- }
766
-
767
- /** @hidden */
768
- public removeNodeFromIndex(node: Node): void {
769
- if (node.id != null) {
770
- this.idMapping?.delete(node.id);
697
+ }
698
+ return false;
771
699
  }
772
- }
773
700
 
774
- /**
775
- * Updates the node's properties from node data. `children` and `parent`
776
- * are ignored. A string sets the name. Existing node values are not
777
- * removed.
778
- *
779
- * @group Changing the tree
780
- */
781
- public setData(o: NodeData | null): void {
782
- if (!o) {
783
- return;
784
- } else if (typeof o === "string") {
785
- this.name = o;
786
- } else if (typeof o === "object") {
787
- for (const key in o) {
788
- if (Object.prototype.hasOwnProperty.call(o, key)) {
789
- const value = o[key];
790
-
791
- if (key === "label" || key === "name") {
792
- // You can use the 'label' key instead of 'name'; this is a legacy feature
793
- if (typeof value === "string") {
794
- this.name = value;
795
- }
796
- } else if (key !== "children" && key !== "parent") {
797
- // You can't update the children or the parent using this function
798
- this[key] = value;
799
- }
800
- }
701
+ case "inside": {
702
+ // move inside as first child
703
+ targetNode.addChildAtPosition(movedNode, 0);
704
+ return true;
705
+ }
706
+ }
707
+ }
708
+ }
709
+
710
+ /**
711
+ * Adds a child at the start.
712
+ *
713
+ * @returns The new node.
714
+ * @group Changing the tree
715
+ */
716
+ public prepend(nodeInfo: NodeData): Node {
717
+ const node = this.createNode(nodeInfo);
718
+ this.addChildAtPosition(node, 0);
719
+
720
+ node.loadChildrenFromData(nodeInfo);
721
+ return node;
722
+ }
723
+
724
+ /**
725
+ * Removes this node from its parent.
726
+ *
727
+ * @group Changing the tree
728
+ */
729
+ public remove(): void {
730
+ if (this.parent) {
731
+ this.parent.removeChild(this);
732
+ this.parent = null;
733
+ }
734
+ }
735
+
736
+ /**
737
+ * Removes a child and its children.
738
+ *
739
+ * @group Changing the tree
740
+ */
741
+ public removeChild(node: Node): void {
742
+ // remove children from the index
743
+ node.removeChildren();
744
+
745
+ this.doRemoveChild(node);
746
+ }
747
+
748
+ /**
749
+ * Removes all children.
750
+ *
751
+ * @group Changing the tree
752
+ */
753
+ public removeChildren(): void {
754
+ this.iterate((child: Node) => {
755
+ this.tree?.removeNodeFromIndex(child);
756
+ return true;
757
+ });
758
+
759
+ this.children = [];
760
+ }
761
+
762
+ /** @hidden */
763
+ public removeNodeFromIndex(node: Node): void {
764
+ if (node.id != null) {
765
+ this.idMapping?.delete(node.id);
766
+ }
767
+ }
768
+
769
+ /**
770
+ * Updates the node's properties from node data. `children` and `parent`
771
+ * are ignored. A string sets the name. Existing node values are not
772
+ * removed.
773
+ *
774
+ * @group Changing the tree
775
+ */
776
+ public setData(o: NodeData | null): void {
777
+ if (!o) {
778
+ return;
779
+ } else if (typeof o === "string") {
780
+ this.name = o;
781
+ } else if (typeof o === "object") {
782
+ for (const key in o) {
783
+ if (Object.prototype.hasOwnProperty.call(o, key)) {
784
+ const value = o[key];
785
+
786
+ if (key === "label" || key === "name") {
787
+ // You can use the 'label' key instead of 'name'; this is a legacy feature
788
+ if (typeof value === "string") {
789
+ this.name = value;
801
790
  }
791
+ } else if (key !== "children" && key !== "parent") {
792
+ // You can't update the children or the parent using this function
793
+ this[key] = value;
794
+ }
802
795
  }
796
+ }
803
797
  }
798
+ }
804
799
 
805
- private createNode(nodeData?: NodeData): Node {
806
- const nodeClass = this.getNodeClass();
807
- return new nodeClass(nodeData);
808
- }
800
+ private createNode(nodeData?: NodeData): Node {
801
+ const nodeClass = this.getNodeClass();
802
+ return new nodeClass(nodeData);
803
+ }
809
804
 
810
- private doRemoveChild(node: Node): void {
811
- this.children.splice(this.getChildIndex(node), 1);
812
- this.tree?.removeNodeFromIndex(node);
813
- }
805
+ private doRemoveChild(node: Node): void {
806
+ this.children.splice(this.getChildIndex(node), 1);
807
+ this.tree?.removeNodeFromIndex(node);
808
+ }
814
809
 
815
- private getNodeClass(): typeof Node {
816
- return this.nodeClass ?? this.tree?.nodeClass ?? Node;
817
- }
810
+ private getNodeClass(): typeof Node {
811
+ return this.nodeClass ?? this.tree?.nodeClass ?? Node;
812
+ }
818
813
 
819
- // Load children data from nodeInfo if it has children
820
- private loadChildrenFromData(nodeInfo: NodeData) {
821
- if (isNodeRecordWithChildren(nodeInfo) && nodeInfo.children.length) {
822
- this.loadFromData(nodeInfo.children);
823
- }
814
+ // Load children data from nodeInfo if it has children
815
+ private loadChildrenFromData(nodeInfo: NodeData) {
816
+ if (isNodeRecordWithChildren(nodeInfo) && nodeInfo.children.length) {
817
+ this.loadFromData(nodeInfo.children);
824
818
  }
819
+ }
825
820
 
826
- private setParent(parent: Node): void {
827
- this.parent = parent;
828
- this.tree = parent.tree;
829
- this.tree?.addNodeToIndex(this);
830
- }
821
+ private setParent(parent: Node): void {
822
+ this.parent = parent;
823
+ this.tree = parent.tree;
824
+ this.tree?.addNodeToIndex(this);
825
+ }
831
826
  }