@apia/tree 2.0.9 → 2.0.11
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/dist/OOTree/OOTreeChildren.d.ts +14 -0
- package/dist/OOTree/OOTreeChildren.d.ts.map +1 -0
- package/dist/OOTree/OOTreeChildren.js +17 -0
- package/dist/OOTree/OOTreeChildren.js.map +1 -0
- package/dist/OOTree/OOTreeNode.d.ts +30 -0
- package/dist/OOTree/OOTreeNode.d.ts.map +1 -0
- package/dist/OOTree/OOTreeNode.js +133 -0
- package/dist/OOTree/OOTreeNode.js.map +1 -0
- package/dist/OOTree/index.d.ts +36 -0
- package/dist/OOTree/index.d.ts.map +1 -0
- package/dist/OOTree/index.js +126 -0
- package/dist/OOTree/index.js.map +1 -0
- package/dist/OOTree/types.d.ts +11 -0
- package/dist/OOTree/types.d.ts.map +1 -0
- package/dist/SearchLabel.js +31 -0
- package/dist/SearchLabel.js.map +1 -0
- package/dist/Tree.d.ts +7 -0
- package/dist/Tree.d.ts.map +1 -0
- package/dist/Tree.js +131 -0
- package/dist/Tree.js.map +1 -0
- package/dist/TreeContext.d.ts +13 -0
- package/dist/TreeContext.d.ts.map +1 -0
- package/dist/TreeContext.js +22 -0
- package/dist/TreeContext.js.map +1 -0
- package/dist/TreeDataController.d.ts +116 -0
- package/dist/TreeDataController.d.ts.map +1 -0
- package/dist/TreeDataController.js +616 -0
- package/dist/TreeDataController.js.map +1 -0
- package/dist/TreeItem.js +51 -0
- package/dist/TreeItem.js.map +1 -0
- package/dist/TreeItemChildren.js +20 -0
- package/dist/TreeItemChildren.js.map +1 -0
- package/dist/TreeItemLabel.js +72 -0
- package/dist/TreeItemLabel.js.map +1 -0
- package/dist/getDomProps.js +37 -0
- package/dist/getDomProps.js.map +1 -0
- package/dist/index.d.ts +7 -417
- package/dist/index.js +6 -1546
- package/dist/index.js.map +1 -1
- package/dist/renderers/DefaultIconRenderer.js +18 -0
- package/dist/renderers/DefaultIconRenderer.js.map +1 -0
- package/dist/renderers/DefaultLabelRenderer.js +10 -0
- package/dist/renderers/DefaultLabelRenderer.js.map +1 -0
- package/dist/renderers/Spacer.js +10 -0
- package/dist/renderers/Spacer.js.map +1 -0
- package/dist/types.d.ts +211 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/useTreeData.d.ts +25 -0
- package/dist/useTreeData.d.ts.map +1 -0
- package/dist/useTreeData.js +131 -0
- package/dist/useTreeData.js.map +1 -0
- package/dist/util.js +220 -0
- package/dist/util.js.map +1 -0
- package/package.json +6 -6
|
@@ -0,0 +1,616 @@
|
|
|
1
|
+
import { useState, useEffect } from 'react';
|
|
2
|
+
import { getNextNodeWithKey, getNextChild, getPreviousChild, getFirstNonFilteredChild, getLastVisibleChild, selectAllNodesBetweenTwoNodes } from './util.js';
|
|
3
|
+
import { EventEmitter, PropsStore, addBoundary, getSpecificParent, usePropsSelector, useLatest, useMount, shallowEqual } from '@apia/util';
|
|
4
|
+
|
|
5
|
+
var __defProp = Object.defineProperty;
|
|
6
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
7
|
+
var __publicField = (obj, key, value) => {
|
|
8
|
+
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
9
|
+
return value;
|
|
10
|
+
};
|
|
11
|
+
const trees = {};
|
|
12
|
+
const treesRecordEmitter = new class TreesRecordEmitter extends EventEmitter {
|
|
13
|
+
emit(eventName, params) {
|
|
14
|
+
super.emit(eventName, params);
|
|
15
|
+
trees[params.name] = params.controller;
|
|
16
|
+
}
|
|
17
|
+
}();
|
|
18
|
+
function useTreeDataController(name) {
|
|
19
|
+
const [controller, setDataController] = useState(
|
|
20
|
+
trees[name]
|
|
21
|
+
);
|
|
22
|
+
useEffect(() => {
|
|
23
|
+
if (trees[name] !== controller)
|
|
24
|
+
setDataController(trees[name]);
|
|
25
|
+
return treesRecordEmitter.on("addController", (ev) => {
|
|
26
|
+
if (ev.name === name)
|
|
27
|
+
setDataController(ev.controller);
|
|
28
|
+
});
|
|
29
|
+
}, []);
|
|
30
|
+
return controller;
|
|
31
|
+
}
|
|
32
|
+
function getTreeDataController(name) {
|
|
33
|
+
return trees[name];
|
|
34
|
+
}
|
|
35
|
+
class TreeDataController extends EventEmitter {
|
|
36
|
+
constructor(name, configuration, propsStore = new PropsStore({
|
|
37
|
+
logCommands: {
|
|
38
|
+
propsStore: `treeProps_${name}`,
|
|
39
|
+
propsLog: `treeLog_${name}`,
|
|
40
|
+
propsSuscriptors: `propsSuscriptors_${name}`,
|
|
41
|
+
updateProp: `updateProp_${name}`
|
|
42
|
+
}
|
|
43
|
+
})) {
|
|
44
|
+
super();
|
|
45
|
+
this.name = name;
|
|
46
|
+
this.propsStore = propsStore;
|
|
47
|
+
__publicField(this, "_configuration");
|
|
48
|
+
__publicField(this, "hasApendedFirstChild", false);
|
|
49
|
+
/**
|
|
50
|
+
* Este array se usa para conocer los padres faltantes al momento de la
|
|
51
|
+
* construcción del árbol, de forma de optimizar el proceso
|
|
52
|
+
*/
|
|
53
|
+
__publicField(this, "missingParents", []);
|
|
54
|
+
__publicField(this, "nonEmittedUpdates", []);
|
|
55
|
+
__publicField(this, "stateKey", "treeState");
|
|
56
|
+
__publicField(this, "previousNodes", []);
|
|
57
|
+
__publicField(this, "setState", (updateProps, conf) => {
|
|
58
|
+
this.propsStore.updateField(
|
|
59
|
+
this.stateKey,
|
|
60
|
+
{
|
|
61
|
+
...this.state,
|
|
62
|
+
...updateProps
|
|
63
|
+
},
|
|
64
|
+
conf
|
|
65
|
+
);
|
|
66
|
+
this.emit("stateUpdate", null);
|
|
67
|
+
});
|
|
68
|
+
__publicField(this, "useState", (selector) => {
|
|
69
|
+
const [selectedState, setSelectedState] = useState(selector(this.state));
|
|
70
|
+
const latestState = useLatest(selectedState);
|
|
71
|
+
useMount(() => {
|
|
72
|
+
return this.on("stateUpdate", () => {
|
|
73
|
+
const newSelection = selector(this.state);
|
|
74
|
+
if (!shallowEqual(newSelection, latestState.current)) {
|
|
75
|
+
setSelectedState(newSelection);
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
return selectedState;
|
|
80
|
+
});
|
|
81
|
+
this._configuration = configuration ?? {
|
|
82
|
+
current: { emitUpdates: true }
|
|
83
|
+
};
|
|
84
|
+
this._configuration.current.emitUpdates = this._configuration.current.emitUpdates ?? true;
|
|
85
|
+
this.setState(this.getInitialState());
|
|
86
|
+
treesRecordEmitter.emit("addController", {
|
|
87
|
+
name,
|
|
88
|
+
controller: this
|
|
89
|
+
});
|
|
90
|
+
this.initRoot();
|
|
91
|
+
}
|
|
92
|
+
get configuration() {
|
|
93
|
+
return { ...this._configuration };
|
|
94
|
+
}
|
|
95
|
+
get state() {
|
|
96
|
+
return this.propsStore.getFieldProps(
|
|
97
|
+
this.stateKey
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
get stateStore() {
|
|
101
|
+
return this.propsStore;
|
|
102
|
+
}
|
|
103
|
+
destructor() {
|
|
104
|
+
this.propsStore.destructor();
|
|
105
|
+
}
|
|
106
|
+
append(node) {
|
|
107
|
+
const newNode = { ...node, nodeProps: node.nodeProps ?? {} };
|
|
108
|
+
let father;
|
|
109
|
+
if (newNode.parentId !== void 0 && this.propsStore.getFieldProps(newNode.parentId)?.children) {
|
|
110
|
+
father = newNode.parentId;
|
|
111
|
+
} else {
|
|
112
|
+
father = "root";
|
|
113
|
+
if (newNode.parentId)
|
|
114
|
+
this.missingParents.push(newNode.parentId);
|
|
115
|
+
newNode.actualParentId = newNode.parentId;
|
|
116
|
+
newNode.parentId = "root";
|
|
117
|
+
}
|
|
118
|
+
this.propsStore.updateField(
|
|
119
|
+
newNode.id,
|
|
120
|
+
{
|
|
121
|
+
...newNode,
|
|
122
|
+
children: newNode.children ?? []
|
|
123
|
+
},
|
|
124
|
+
{ isUrgent: true }
|
|
125
|
+
);
|
|
126
|
+
this.setState({ length: this.state.length + 1 });
|
|
127
|
+
if (this.missingParents.includes(newNode.id)) {
|
|
128
|
+
this.propsStore.getFieldProps("root").children.forEach((currentChild) => {
|
|
129
|
+
if (this.propsStore.getFieldProps(currentChild).actualParentId === newNode.id) {
|
|
130
|
+
this.move(currentChild, newNode.id);
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
this.missingParents = this.missingParents.filter(
|
|
134
|
+
(current) => current !== newNode.id
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
this.propsStore.updateField(
|
|
138
|
+
father,
|
|
139
|
+
{
|
|
140
|
+
children: [
|
|
141
|
+
...this.propsStore.getFieldProps(father).children,
|
|
142
|
+
newNode.id
|
|
143
|
+
],
|
|
144
|
+
hasLoaded: true
|
|
145
|
+
},
|
|
146
|
+
{ noEmit: this._configuration.current?.emitUpdates === false }
|
|
147
|
+
);
|
|
148
|
+
if (this._configuration.current?.emitUpdates === false)
|
|
149
|
+
this.nonEmittedUpdates.push(father);
|
|
150
|
+
if (!this.hasApendedFirstChild) {
|
|
151
|
+
this.hasApendedFirstChild = true;
|
|
152
|
+
this.setFocusedNode(newNode.id, true);
|
|
153
|
+
}
|
|
154
|
+
if (newNode.isExpanded)
|
|
155
|
+
this.toggleNodeExpandedState(newNode.id, true);
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Cuando se quieren agregar muchos nodos, es conveniente llamar primero al
|
|
159
|
+
* método batchInit y luego de finalizar, al método batchFinish()
|
|
160
|
+
*/
|
|
161
|
+
batchInit() {
|
|
162
|
+
this.config({ emitUpdates: false });
|
|
163
|
+
this.setState({ isLoading: true }, { isUrgent: true });
|
|
164
|
+
}
|
|
165
|
+
getNodesRecursive(nodeId = "root") {
|
|
166
|
+
const node = this.propsStore.getFieldProps(nodeId);
|
|
167
|
+
return node.children.reduce(
|
|
168
|
+
(prev, current) => [...prev, ...this.getNodesRecursive(current)],
|
|
169
|
+
[...node.children]
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Cuando se quieren agregar muchos nodos, es conveniente llamar primero al
|
|
174
|
+
* método batchInit y luego de finalizar, al método batchFinish()
|
|
175
|
+
*/
|
|
176
|
+
batchFinish() {
|
|
177
|
+
this.config({ emitUpdates: true });
|
|
178
|
+
setTimeout(() => this.setState({ isLoading: false }), 0);
|
|
179
|
+
const children = [...this.getNodesRecursive()];
|
|
180
|
+
const deleteNodes = this.previousNodes.filter(
|
|
181
|
+
(current) => !children.find((search) => search === current)
|
|
182
|
+
);
|
|
183
|
+
deleteNodes.forEach((current) => this.remove(current));
|
|
184
|
+
this.config({ emitUpdates: true });
|
|
185
|
+
setTimeout(() => this.setState({ isLoading: false }), 0);
|
|
186
|
+
}
|
|
187
|
+
config(newConf) {
|
|
188
|
+
if (this._configuration.current?.emitUpdates === false && newConf.emitUpdates !== false) {
|
|
189
|
+
this.nonEmittedUpdates.forEach(
|
|
190
|
+
(current) => this.propsStore.updateField(current, {
|
|
191
|
+
children: [...this.propsStore.getFieldProps(current).children]
|
|
192
|
+
})
|
|
193
|
+
);
|
|
194
|
+
this.nonEmittedUpdates = [];
|
|
195
|
+
}
|
|
196
|
+
Object.assign(
|
|
197
|
+
this._configuration.current,
|
|
198
|
+
newConf
|
|
199
|
+
);
|
|
200
|
+
}
|
|
201
|
+
deselectAll() {
|
|
202
|
+
this.state.selectedNodes.forEach(
|
|
203
|
+
(currentId) => this.propsStore.updateField(currentId, { isSelected: false })
|
|
204
|
+
);
|
|
205
|
+
this.setState({ selectedNodes: [] });
|
|
206
|
+
}
|
|
207
|
+
forceEmitUpdate() {
|
|
208
|
+
this.propsStore.updateField("root", {
|
|
209
|
+
forceUpdate: (this.propsStore.getFieldProps("root").forceUpdate ?? 0) + 1
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
getInitialState() {
|
|
213
|
+
return {
|
|
214
|
+
expandedNodes: [],
|
|
215
|
+
filteredNodes: [],
|
|
216
|
+
focusedNode: null,
|
|
217
|
+
isLoading: false,
|
|
218
|
+
length: 0,
|
|
219
|
+
selectedNodes: []
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
getNodesAsArray() {
|
|
223
|
+
const allFields = { ...this.propsStore.fields };
|
|
224
|
+
delete allFields[this.stateKey];
|
|
225
|
+
delete allFields.root;
|
|
226
|
+
return Object.values(allFields);
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* Devuelve un array que contiene el id de todos los nodos pertenecientes al
|
|
230
|
+
* árbol
|
|
231
|
+
*/
|
|
232
|
+
getNodesIds() {
|
|
233
|
+
const allFields = { ...this.propsStore.fields };
|
|
234
|
+
delete allFields[this.stateKey];
|
|
235
|
+
delete allFields.root;
|
|
236
|
+
return Object.keys(allFields);
|
|
237
|
+
}
|
|
238
|
+
getParentId(parentId) {
|
|
239
|
+
return this.propsStore.getFieldProps(parentId).parentId;
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* Este método permite controlar el comportamiento con el teclado desde fuera
|
|
243
|
+
* del componente.
|
|
244
|
+
*/
|
|
245
|
+
handleKey(ev) {
|
|
246
|
+
const focusedId = this.state.focusedNode;
|
|
247
|
+
const nodeProps = this.propsStore.getFieldProps(focusedId);
|
|
248
|
+
if (ev.key === "*") {
|
|
249
|
+
if (this.state.focusedNode === null)
|
|
250
|
+
return;
|
|
251
|
+
const parent = this.propsStore.getFieldProps(
|
|
252
|
+
this.propsStore.getFieldProps(this.state.focusedNode).parentId
|
|
253
|
+
);
|
|
254
|
+
parent.children.forEach(
|
|
255
|
+
(current) => this.toggleNodeExpandedState(current, true)
|
|
256
|
+
);
|
|
257
|
+
} else if (ev.key.match(/^\w$/)) {
|
|
258
|
+
const nextChildWithKey = getNextNodeWithKey(
|
|
259
|
+
this,
|
|
260
|
+
focusedId,
|
|
261
|
+
ev.key,
|
|
262
|
+
true
|
|
263
|
+
);
|
|
264
|
+
if (nextChildWithKey !== null) {
|
|
265
|
+
this.setFocusedNode(nextChildWithKey);
|
|
266
|
+
this.emit("mustFocus", nextChildWithKey);
|
|
267
|
+
}
|
|
268
|
+
} else
|
|
269
|
+
switch (ev.code) {
|
|
270
|
+
case "Home": {
|
|
271
|
+
const firstChild = getFirstNonFilteredChild(this, "root");
|
|
272
|
+
if (firstChild === null)
|
|
273
|
+
return;
|
|
274
|
+
if (ev.shiftKey && ev.ctrlKey && this.state.focusedNode !== null) {
|
|
275
|
+
selectAllNodesBetweenTwoNodes(
|
|
276
|
+
this,
|
|
277
|
+
firstChild,
|
|
278
|
+
this.state.focusedNode,
|
|
279
|
+
true
|
|
280
|
+
);
|
|
281
|
+
}
|
|
282
|
+
this.setFocusedNode(firstChild);
|
|
283
|
+
this.emit("mustFocus", firstChild);
|
|
284
|
+
break;
|
|
285
|
+
}
|
|
286
|
+
case "End": {
|
|
287
|
+
const lastVisibleChild = getLastVisibleChild(this, "root", true);
|
|
288
|
+
if (lastVisibleChild !== null) {
|
|
289
|
+
if (ev.shiftKey && ev.ctrlKey && this.state.focusedNode !== null) {
|
|
290
|
+
selectAllNodesBetweenTwoNodes(
|
|
291
|
+
this,
|
|
292
|
+
lastVisibleChild,
|
|
293
|
+
this.state.focusedNode,
|
|
294
|
+
true
|
|
295
|
+
);
|
|
296
|
+
}
|
|
297
|
+
this.setFocusedNode(lastVisibleChild);
|
|
298
|
+
this.emit("mustFocus", lastVisibleChild);
|
|
299
|
+
}
|
|
300
|
+
break;
|
|
301
|
+
}
|
|
302
|
+
case "ArrowRight": {
|
|
303
|
+
if (nodeProps.isLeaf)
|
|
304
|
+
return;
|
|
305
|
+
ev.preventDefault();
|
|
306
|
+
if (nodeProps.isExpanded) {
|
|
307
|
+
const firstChild = getFirstNonFilteredChild(this, nodeProps.id);
|
|
308
|
+
if (firstChild !== null) {
|
|
309
|
+
this.setFocusedNode(firstChild);
|
|
310
|
+
this.emit("mustFocus", firstChild);
|
|
311
|
+
}
|
|
312
|
+
} else
|
|
313
|
+
this.toggleNodeExpandedState(nodeProps.id, true);
|
|
314
|
+
break;
|
|
315
|
+
}
|
|
316
|
+
case "ArrowLeft": {
|
|
317
|
+
ev.preventDefault();
|
|
318
|
+
if (nodeProps.isLeaf || !nodeProps.isExpanded) {
|
|
319
|
+
if (nodeProps.parentId !== "root" && nodeProps.parentId !== void 0) {
|
|
320
|
+
this.setFocusedNode(nodeProps.parentId);
|
|
321
|
+
this.emit("mustFocus", nodeProps.parentId);
|
|
322
|
+
}
|
|
323
|
+
} else
|
|
324
|
+
this.toggleNodeExpandedState(nodeProps.id, false);
|
|
325
|
+
break;
|
|
326
|
+
}
|
|
327
|
+
case "ArrowUp": {
|
|
328
|
+
ev.preventDefault();
|
|
329
|
+
const prevChild = getPreviousChild(this, nodeProps.id, true);
|
|
330
|
+
if (prevChild !== null) {
|
|
331
|
+
if (ev.shiftKey)
|
|
332
|
+
this.toggleNodeSelectedState(prevChild, true);
|
|
333
|
+
this.setFocusedNode(prevChild);
|
|
334
|
+
this.emit("mustFocus", prevChild);
|
|
335
|
+
} else
|
|
336
|
+
this.emit("onArrowUpOnFirstElement", true);
|
|
337
|
+
break;
|
|
338
|
+
}
|
|
339
|
+
case "ArrowDown": {
|
|
340
|
+
ev.preventDefault();
|
|
341
|
+
const nextChild = getNextChild(this, nodeProps.id, false, true);
|
|
342
|
+
if (nextChild !== null) {
|
|
343
|
+
if (ev.shiftKey)
|
|
344
|
+
this.toggleNodeSelectedState(nextChild, true);
|
|
345
|
+
this.setFocusedNode(nextChild);
|
|
346
|
+
this.emit("mustFocus", nextChild);
|
|
347
|
+
}
|
|
348
|
+
break;
|
|
349
|
+
}
|
|
350
|
+
case "Space": {
|
|
351
|
+
if (this._configuration.current?.disableSelection)
|
|
352
|
+
return;
|
|
353
|
+
ev.preventDefault();
|
|
354
|
+
this.toggleNodeSelectedState(nodeProps.id);
|
|
355
|
+
break;
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
isNode(node) {
|
|
360
|
+
return typeof node === "object" && "parentId" in node;
|
|
361
|
+
}
|
|
362
|
+
isNodeContainer(node) {
|
|
363
|
+
return typeof node === "object" && !this.isNode(node);
|
|
364
|
+
}
|
|
365
|
+
includes(searchNode) {
|
|
366
|
+
return !!this.propsStore.getFieldProps(searchNode.id);
|
|
367
|
+
}
|
|
368
|
+
initRoot() {
|
|
369
|
+
this.propsStore.updateField(
|
|
370
|
+
"root",
|
|
371
|
+
{ children: [], id: "root" },
|
|
372
|
+
{ isUrgent: true }
|
|
373
|
+
);
|
|
374
|
+
}
|
|
375
|
+
move(moveNode, destinationNode, afterOrBefore) {
|
|
376
|
+
const currentFather = this.propsStore.getFieldProps(
|
|
377
|
+
this.propsStore.getFieldProps(moveNode).parentId
|
|
378
|
+
);
|
|
379
|
+
const newFather = this.propsStore.getFieldProps(destinationNode);
|
|
380
|
+
if (!currentFather) {
|
|
381
|
+
console.warn("The current node does not belong to the tree.", moveNode);
|
|
382
|
+
return;
|
|
383
|
+
}
|
|
384
|
+
if (!newFather) {
|
|
385
|
+
console.warn(
|
|
386
|
+
"The destination node does not belong to the tree. No action will be made",
|
|
387
|
+
destinationNode
|
|
388
|
+
);
|
|
389
|
+
return;
|
|
390
|
+
}
|
|
391
|
+
currentFather.children = currentFather.children?.filter(
|
|
392
|
+
(search) => search !== moveNode
|
|
393
|
+
);
|
|
394
|
+
if (!newFather.children)
|
|
395
|
+
newFather.children = [];
|
|
396
|
+
if (!afterOrBefore)
|
|
397
|
+
newFather.children.push(moveNode);
|
|
398
|
+
else if (afterOrBefore.position) {
|
|
399
|
+
newFather.children.splice(afterOrBefore.position - 1, 0, moveNode);
|
|
400
|
+
} else {
|
|
401
|
+
const key = afterOrBefore.after ? afterOrBefore.after : afterOrBefore.before;
|
|
402
|
+
const relatedNodeKey = key && this.isNode(key) ? key.id : key;
|
|
403
|
+
const relatedNodeIndex = newFather.children.findIndex(
|
|
404
|
+
(search) => search === relatedNodeKey
|
|
405
|
+
);
|
|
406
|
+
const actualIndex = addBoundary(
|
|
407
|
+
afterOrBefore.before !== void 0 ? relatedNodeIndex : relatedNodeIndex + 1,
|
|
408
|
+
0
|
|
409
|
+
);
|
|
410
|
+
if (actualIndex === -1 || actualIndex === newFather.children.length)
|
|
411
|
+
newFather.children.push(moveNode);
|
|
412
|
+
else
|
|
413
|
+
newFather.children.splice(actualIndex, 0, moveNode);
|
|
414
|
+
}
|
|
415
|
+
this.propsStore.updateField(currentFather.id, {
|
|
416
|
+
children: [...currentFather.children ?? []]
|
|
417
|
+
});
|
|
418
|
+
this.propsStore.updateField(newFather.id, {
|
|
419
|
+
children: [...newFather.children]
|
|
420
|
+
});
|
|
421
|
+
this.propsStore.updateField(moveNode, {
|
|
422
|
+
parentId: newFather.id
|
|
423
|
+
});
|
|
424
|
+
}
|
|
425
|
+
/**
|
|
426
|
+
* Borra el nodo del árbol, y dependiendo del parámetro removeChildren, borra
|
|
427
|
+
* también sus hijos.
|
|
428
|
+
*
|
|
429
|
+
* @param removeChildren - Si se pasa en false, los nodos hijos son movidos al root
|
|
430
|
+
*/
|
|
431
|
+
remove(removeNode, removeChildren = true) {
|
|
432
|
+
const removeNodeId = this.isNode(removeNode) ? removeNode.id : removeNode;
|
|
433
|
+
const removingNode = this.propsStore.getFieldProps(removeNodeId);
|
|
434
|
+
if (!removingNode)
|
|
435
|
+
return;
|
|
436
|
+
const father = this.propsStore.getFieldProps(removingNode.parentId);
|
|
437
|
+
if (father) {
|
|
438
|
+
this.propsStore.updateField(father.id, {
|
|
439
|
+
children: father.children?.filter((search) => search !== removeNodeId)
|
|
440
|
+
});
|
|
441
|
+
}
|
|
442
|
+
removingNode.children?.forEach((current) => {
|
|
443
|
+
if (removeChildren) {
|
|
444
|
+
this.remove(current);
|
|
445
|
+
} else {
|
|
446
|
+
this.move(current, "root");
|
|
447
|
+
}
|
|
448
|
+
});
|
|
449
|
+
this.setState({
|
|
450
|
+
selectedNodes: this.state.selectedNodes.filter(
|
|
451
|
+
(current) => current !== removeNodeId
|
|
452
|
+
),
|
|
453
|
+
focusedNode: this.state.focusedNode === removeNodeId ? null : this.state.focusedNode,
|
|
454
|
+
length: this.state.length - 1
|
|
455
|
+
});
|
|
456
|
+
this.propsStore.removeField(removeNodeId);
|
|
457
|
+
}
|
|
458
|
+
removeMultiple(nodes, removeChildren = true) {
|
|
459
|
+
nodes.forEach((current) => this.remove(current, removeChildren));
|
|
460
|
+
}
|
|
461
|
+
/**
|
|
462
|
+
* Borra todos los nodos del árbol.
|
|
463
|
+
*/
|
|
464
|
+
removeAll() {
|
|
465
|
+
if (this.configuration.current?.emitUpdates === false) {
|
|
466
|
+
this.previousNodes = this.getNodesIds();
|
|
467
|
+
this.propsStore.updateField(
|
|
468
|
+
"root",
|
|
469
|
+
{ children: [], id: "root" },
|
|
470
|
+
{ isUrgent: true, noEmit: true }
|
|
471
|
+
);
|
|
472
|
+
} else {
|
|
473
|
+
this.hasApendedFirstChild = false;
|
|
474
|
+
this.setState({ focusedNode: null }, { isUrgent: true });
|
|
475
|
+
this.setSelectedNodes([]);
|
|
476
|
+
this.initRoot();
|
|
477
|
+
Object.keys(this.propsStore.fields).forEach((current) => {
|
|
478
|
+
if (!["root", this.stateKey].includes(current))
|
|
479
|
+
this.propsStore.removeField(current);
|
|
480
|
+
});
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
selectAll() {
|
|
484
|
+
this.setState({
|
|
485
|
+
selectedNodes: this.getNodesIds()
|
|
486
|
+
});
|
|
487
|
+
this.state.selectedNodes.forEach(
|
|
488
|
+
(currentId) => this.propsStore.updateField(currentId, { isSelected: true })
|
|
489
|
+
);
|
|
490
|
+
}
|
|
491
|
+
setExpandedNodes(nodes) {
|
|
492
|
+
this.state.expandedNodes.forEach(
|
|
493
|
+
(current) => this.propsStore.updateField(current, { isExpanded: false })
|
|
494
|
+
);
|
|
495
|
+
this.setState({ expandedNodes: nodes });
|
|
496
|
+
nodes.forEach(
|
|
497
|
+
(current) => this.propsStore.updateField(current, { isExpanded: true })
|
|
498
|
+
);
|
|
499
|
+
}
|
|
500
|
+
setFocusedNode(key, avoidSelection) {
|
|
501
|
+
if (this.state.focusedNode !== null)
|
|
502
|
+
this.propsStore.updateField(this.state.focusedNode, { isFocused: false });
|
|
503
|
+
this.setState({
|
|
504
|
+
focusedNode: key
|
|
505
|
+
});
|
|
506
|
+
if (!this._configuration.current?.isMultiple && !avoidSelection && (this._configuration.current?.selectionMode ?? "onFocus") === "onFocus")
|
|
507
|
+
this.setSelectedNodes([key]);
|
|
508
|
+
this.propsStore.updateField(key, { isFocused: true });
|
|
509
|
+
}
|
|
510
|
+
setSelectedNodes(nodes, force = false) {
|
|
511
|
+
if (this._configuration.current?.disableSelection && !force)
|
|
512
|
+
return;
|
|
513
|
+
this.state.selectedNodes.forEach(
|
|
514
|
+
(current) => this.propsStore.updateField(current, { isSelected: false })
|
|
515
|
+
);
|
|
516
|
+
this.setState({
|
|
517
|
+
selectedNodes: nodes.filter((current) => {
|
|
518
|
+
const { isSelectable } = this.propsStore.getFieldProps(current);
|
|
519
|
+
if (isSelectable !== false)
|
|
520
|
+
this.propsStore.updateField(current, { isSelected: true });
|
|
521
|
+
return isSelectable !== false;
|
|
522
|
+
})
|
|
523
|
+
});
|
|
524
|
+
}
|
|
525
|
+
setSelectedNodesByClickEvent(ev) {
|
|
526
|
+
if (this._configuration.current?.disableSelection)
|
|
527
|
+
return;
|
|
528
|
+
const nodeKey = getSpecificParent(
|
|
529
|
+
ev.target,
|
|
530
|
+
(current) => current.getAttribute("role") === "treeitem"
|
|
531
|
+
)?.getAttribute("data-key");
|
|
532
|
+
if (nodeKey) {
|
|
533
|
+
const nodeProps = this.propsStore.getFieldProps(nodeKey);
|
|
534
|
+
if (nodeProps.isDisabled || nodeProps.isSelectable === false)
|
|
535
|
+
return;
|
|
536
|
+
const previousSelectionKeys = [...this.state.selectedNodes];
|
|
537
|
+
const allowMultiple = this._configuration.current?.isMultiple && (this._configuration.current?.selectionMode !== "explicitMultiple" || ev.ctrlKey || ev.shiftKey);
|
|
538
|
+
const newSelection = allowMultiple ? nodeProps.isSelected ? previousSelectionKeys.filter((current) => current !== nodeKey) : [...previousSelectionKeys, nodeKey] : [nodeKey];
|
|
539
|
+
this.setSelectedNodes(newSelection);
|
|
540
|
+
} else
|
|
541
|
+
console.warn("Cannot set selection, no treeitem found", ev);
|
|
542
|
+
}
|
|
543
|
+
toggleNodeExpandedState(key, shouldExpand) {
|
|
544
|
+
const nodeProps = this.propsStore.getFieldProps(key);
|
|
545
|
+
if (nodeProps.isDisabled || nodeProps.isLeaf || nodeProps.isLoading)
|
|
546
|
+
return;
|
|
547
|
+
if (this._configuration.current?.onLoadData && !nodeProps.hasLoaded) {
|
|
548
|
+
this.propsStore.updateField(key, { isLoading: true });
|
|
549
|
+
this._configuration.current?.onLoadData(nodeProps).finally(() => {
|
|
550
|
+
this.propsStore.updateField(key, {
|
|
551
|
+
isLoading: false,
|
|
552
|
+
isExpanded: true,
|
|
553
|
+
hasLoaded: true
|
|
554
|
+
});
|
|
555
|
+
this.setState({
|
|
556
|
+
expandedNodes: [...this.state.expandedNodes, key]
|
|
557
|
+
});
|
|
558
|
+
this._configuration.current?.onExpand?.(
|
|
559
|
+
this.propsStore.getFieldProps(key)
|
|
560
|
+
);
|
|
561
|
+
});
|
|
562
|
+
} else {
|
|
563
|
+
const { expandedNodes } = this.state;
|
|
564
|
+
const shouldExpandInner = shouldExpand !== void 0 ? shouldExpand : !expandedNodes.includes(key);
|
|
565
|
+
if (this.propsStore.getFieldProps(key)?.isDisabled)
|
|
566
|
+
return;
|
|
567
|
+
this.setState({
|
|
568
|
+
expandedNodes: shouldExpandInner ? [...expandedNodes, key] : expandedNodes.filter((current) => current !== key)
|
|
569
|
+
});
|
|
570
|
+
this.propsStore.updateField(key, { isExpanded: shouldExpandInner });
|
|
571
|
+
this._configuration.current?.onExpand?.(
|
|
572
|
+
this.propsStore.getFieldProps(key)
|
|
573
|
+
);
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
toggleNodeSelectedState(key, isSelected) {
|
|
577
|
+
if (this._configuration.current?.disableSelection)
|
|
578
|
+
return;
|
|
579
|
+
const nodeProps = this.propsStore.getFieldProps(key);
|
|
580
|
+
if (nodeProps.isDisabled || nodeProps.isSelectable === false)
|
|
581
|
+
return;
|
|
582
|
+
const shouldSelect = isSelected !== void 0 ? isSelected : !this.state.selectedNodes.includes(key);
|
|
583
|
+
if (shouldSelect && nodeProps.isSelected || isSelected === false && !nodeProps.isSelected)
|
|
584
|
+
return;
|
|
585
|
+
if (shouldSelect && !this._configuration.current?.isMultiple && this.state.selectedNodes[0])
|
|
586
|
+
this.toggleNodeSelectedState(this.state.selectedNodes[0], false);
|
|
587
|
+
this.propsStore.updateField(key, { isSelected: shouldSelect });
|
|
588
|
+
this.setState({
|
|
589
|
+
selectedNodes: shouldSelect ? [...this.state.selectedNodes, key] : this.state.selectedNodes.filter((current) => current !== key)
|
|
590
|
+
});
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
function useTreeSelector(handler, configuration) {
|
|
594
|
+
return usePropsSelector(
|
|
595
|
+
handler?.stateKey ?? "__NO__TREE__KEY__",
|
|
596
|
+
{
|
|
597
|
+
propsStore: handler?.propsStore,
|
|
598
|
+
...configuration
|
|
599
|
+
}
|
|
600
|
+
);
|
|
601
|
+
}
|
|
602
|
+
function useTreeSelectorByName(treeName, configuration) {
|
|
603
|
+
const handler = useTreeDataController(treeName);
|
|
604
|
+
const selection = usePropsSelector(
|
|
605
|
+
handler?.stateKey ?? "__NO__TREE__KEY__",
|
|
606
|
+
{
|
|
607
|
+
propsStore: handler?.propsStore,
|
|
608
|
+
...configuration
|
|
609
|
+
}
|
|
610
|
+
);
|
|
611
|
+
return { selection, handler };
|
|
612
|
+
}
|
|
613
|
+
var TreeDataController$1 = TreeDataController;
|
|
614
|
+
|
|
615
|
+
export { TreeDataController$1 as default, getTreeDataController, useTreeDataController, useTreeSelector, useTreeSelectorByName };
|
|
616
|
+
//# sourceMappingURL=TreeDataController.js.map
|