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