react-arborist 3.3.0-rc.1 → 3.3.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.
- package/dist/main/components/cursor.d.ts +1 -0
- package/dist/main/components/cursor.js +20 -0
- package/dist/{components → main/components}/default-container.d.ts +1 -2
- package/dist/main/components/default-container.js +238 -0
- package/dist/main/components/default-cursor.js +35 -0
- package/dist/{components → main/components}/default-drag-preview.d.ts +1 -2
- package/dist/main/components/default-drag-preview.js +57 -0
- package/dist/{components → main/components}/default-node.d.ts +1 -2
- package/dist/main/components/default-node.js +32 -0
- package/dist/{components → main/components}/default-row.d.ts +1 -2
- package/dist/main/components/default-row.js +8 -0
- package/dist/main/components/drag-preview-container.d.ts +1 -0
- package/dist/main/components/drag-preview-container.js +21 -0
- package/dist/{components → main/components}/list-inner-element.d.ts +1 -1
- package/dist/main/components/list-inner-element.js +25 -0
- package/dist/main/components/list-outer-element.d.ts +2 -0
- package/dist/main/components/list-outer-element.js +38 -0
- package/dist/main/components/outer-drop.js +9 -0
- package/dist/{components → main/components}/provider.d.ts +2 -2
- package/dist/main/components/provider.js +52 -0
- package/dist/{components → main/components}/row-container.d.ts +2 -2
- package/dist/main/components/row-container.js +84 -0
- package/dist/main/components/tree-container.d.ts +1 -0
- package/dist/main/components/tree-container.js +12 -0
- package/dist/{components → main/components}/tree.d.ts +1 -1
- package/dist/main/components/tree.js +15 -0
- package/dist/{context.d.ts → main/context.d.ts} +4 -4
- package/dist/main/context.js +33 -0
- package/dist/main/data/create-index.js +10 -0
- package/dist/main/data/create-list.js +62 -0
- package/dist/main/data/create-root.js +43 -0
- package/dist/main/data/make-tree.d.ts +5 -0
- package/dist/main/data/make-tree.js +40 -0
- package/dist/{data → main/data}/simple-tree.d.ts +1 -1
- package/dist/main/data/simple-tree.js +100 -0
- package/dist/{dnd → main/dnd}/compute-drop.d.ts +6 -6
- package/dist/main/dnd/compute-drop.js +129 -0
- package/dist/main/dnd/drag-hook.js +46 -0
- package/dist/{dnd → main/dnd}/drop-hook.d.ts +1 -1
- package/dist/main/dnd/drop-hook.js +42 -0
- package/dist/main/dnd/measure-hover.d.ts +8 -0
- package/dist/main/dnd/measure-hover.js +21 -0
- package/dist/main/dnd/outer-drop-hook.js +45 -0
- package/dist/main/hooks/use-fresh-node.js +18 -0
- package/dist/{hooks → main/hooks}/use-simple-tree.d.ts +1 -1
- package/dist/main/hooks/use-simple-tree.js +35 -0
- package/dist/main/hooks/use-validated-props.js +29 -0
- package/dist/main/index.js +27 -0
- package/dist/{interfaces → main/interfaces}/node-api.d.ts +1 -1
- package/dist/main/interfaces/node-api.js +164 -0
- package/dist/{interfaces → main/interfaces}/tree-api.d.ts +87 -7
- package/dist/main/interfaces/tree-api.js +649 -0
- package/dist/main/interfaces/tree-api.test.d.ts +1 -0
- package/dist/main/interfaces/tree-api.test.js +14 -0
- package/dist/{state → main/state}/dnd-slice.d.ts +1 -1
- package/dist/main/state/dnd-slice.js +35 -0
- package/dist/{state → main/state}/drag-slice.d.ts +1 -1
- package/dist/main/state/drag-slice.js +24 -0
- package/dist/{state → main/state}/edit-slice.d.ts +1 -1
- package/dist/main/state/edit-slice.js +18 -0
- package/dist/{state → main/state}/focus-slice.d.ts +1 -1
- package/dist/main/state/focus-slice.js +26 -0
- package/dist/main/state/initial.js +29 -0
- package/dist/{state → main/state}/open-slice.d.ts +2 -2
- package/dist/main/state/open-slice.js +48 -0
- package/dist/main/state/root-reducer.d.ts +96 -0
- package/dist/main/state/root-reducer.js +20 -0
- package/dist/{state → main/state}/selection-slice.d.ts +1 -1
- package/dist/main/state/selection-slice.js +59 -0
- package/dist/{types → main/types}/dnd.d.ts +2 -2
- package/dist/main/types/dnd.js +2 -0
- package/dist/{types → main/types}/handlers.d.ts +5 -5
- package/dist/main/types/handlers.js +2 -0
- package/dist/{types → main/types}/renderers.d.ts +4 -4
- package/dist/main/types/renderers.js +2 -0
- package/dist/main/types/state.d.ts +2 -0
- package/dist/main/types/state.js +2 -0
- package/dist/main/types/tree-props.js +2 -0
- package/dist/{types → main/types}/utils.d.ts +5 -5
- package/dist/main/types/utils.js +2 -0
- package/dist/main/utils.js +183 -0
- package/dist/module/components/cursor.d.ts +1 -0
- package/dist/module/components/cursor.js +16 -0
- package/dist/module/components/default-container.d.ts +6 -0
- package/dist/module/components/default-container.js +234 -0
- package/dist/module/components/default-cursor.d.ts +3 -0
- package/dist/module/components/default-cursor.js +29 -0
- package/dist/module/components/default-drag-preview.d.ts +2 -0
- package/dist/module/components/default-drag-preview.js +53 -0
- package/dist/module/components/default-node.d.ts +2 -0
- package/dist/module/components/default-node.js +28 -0
- package/dist/module/components/default-row.d.ts +2 -0
- package/dist/module/components/default-row.js +4 -0
- package/dist/module/components/drag-preview-container.d.ts +1 -0
- package/dist/module/components/drag-preview-container.js +17 -0
- package/dist/module/components/list-inner-element.d.ts +2 -0
- package/dist/module/components/list-inner-element.js +22 -0
- package/dist/module/components/list-outer-element.d.ts +2 -0
- package/dist/module/components/list-outer-element.js +35 -0
- package/dist/module/components/outer-drop.d.ts +4 -0
- package/dist/module/components/outer-drop.js +5 -0
- package/dist/module/components/provider.d.ts +10 -0
- package/dist/module/components/provider.js +48 -0
- package/dist/module/components/row-container.d.ts +7 -0
- package/dist/module/components/row-container.js +58 -0
- package/dist/module/components/tree-container.d.ts +1 -0
- package/dist/module/components/tree-container.js +8 -0
- package/dist/module/components/tree.d.ts +8 -0
- package/dist/module/components/tree.js +12 -0
- package/dist/module/context.d.ts +22 -0
- package/dist/module/context.js +26 -0
- package/dist/module/data/create-index.d.ts +4 -0
- package/dist/module/data/create-index.js +6 -0
- package/dist/module/data/create-list.d.ts +3 -0
- package/dist/module/data/create-list.js +58 -0
- package/dist/module/data/create-root.d.ts +4 -0
- package/dist/module/data/create-root.js +39 -0
- package/dist/module/data/make-tree.d.ts +5 -0
- package/dist/module/data/make-tree.js +36 -0
- package/dist/module/data/simple-tree.d.ts +44 -0
- package/dist/module/data/simple-tree.js +96 -0
- package/dist/module/dnd/compute-drop.d.ts +37 -0
- package/dist/module/dnd/compute-drop.js +125 -0
- package/dist/module/dnd/drag-hook.d.ts +3 -0
- package/dist/module/dnd/drag-hook.js +42 -0
- package/dist/module/dnd/drop-hook.d.ts +8 -0
- package/dist/module/dnd/drop-hook.js +38 -0
- package/dist/module/dnd/measure-hover.d.ts +8 -0
- package/dist/module/dnd/measure-hover.js +17 -0
- package/dist/module/dnd/outer-drop-hook.d.ts +1 -0
- package/dist/module/dnd/outer-drop-hook.js +41 -0
- package/dist/module/hooks/use-fresh-node.d.ts +1 -0
- package/dist/module/hooks/use-fresh-node.js +14 -0
- package/dist/module/hooks/use-simple-tree.d.ts +12 -0
- package/dist/module/hooks/use-simple-tree.js +31 -0
- package/dist/module/hooks/use-validated-props.d.ts +2 -0
- package/dist/module/hooks/use-validated-props.js +25 -0
- package/dist/module/index.d.ts +8 -0
- package/dist/module/index.js +9 -0
- package/dist/module/interfaces/node-api.d.ts +71 -0
- package/dist/module/interfaces/node-api.js +160 -0
- package/dist/module/interfaces/tree-api.d.ts +214 -0
- package/dist/module/interfaces/tree-api.js +622 -0
- package/dist/module/interfaces/tree-api.test.d.ts +1 -0
- package/dist/module/interfaces/tree-api.test.js +12 -0
- package/dist/module/state/dnd-slice.d.ts +29 -0
- package/dist/module/state/dnd-slice.js +31 -0
- package/dist/module/state/drag-slice.d.ts +9 -0
- package/dist/module/state/drag-slice.js +20 -0
- package/dist/module/state/edit-slice.d.ts +8 -0
- package/dist/module/state/edit-slice.js +13 -0
- package/dist/module/state/focus-slice.d.ts +12 -0
- package/dist/module/state/focus-slice.js +20 -0
- package/dist/module/state/initial.d.ts +3 -0
- package/dist/module/state/initial.js +25 -0
- package/dist/module/state/open-slice.d.ts +30 -0
- package/dist/module/state/open-slice.js +44 -0
- package/dist/module/state/root-reducer.d.ts +96 -0
- package/dist/module/state/root-reducer.js +17 -0
- package/dist/module/state/selection-slice.d.ts +42 -0
- package/dist/module/state/selection-slice.js +55 -0
- package/dist/module/types/dnd.d.ts +8 -0
- package/dist/module/types/dnd.js +1 -0
- package/dist/module/types/handlers.d.ts +30 -0
- package/dist/module/types/handlers.js +1 -0
- package/dist/module/types/renderers.d.ts +29 -0
- package/dist/module/types/renderers.js +1 -0
- package/dist/module/types/state.d.ts +2 -0
- package/dist/module/types/state.js +1 -0
- package/dist/module/types/tree-props.d.ts +56 -0
- package/dist/module/types/tree-props.js +1 -0
- package/dist/module/types/utils.d.ts +17 -0
- package/dist/module/types/utils.js +1 -0
- package/dist/module/utils.d.ts +24 -0
- package/dist/module/utils.js +162 -0
- package/package.json +26 -25
- package/src/components/default-container.tsx +2 -0
- package/src/components/provider.tsx +3 -2
- package/src/components/row-container.tsx +1 -1
- package/dist/components/cursor.d.ts +0 -2
- package/dist/components/drag-preview-container.d.ts +0 -2
- package/dist/components/list-outer-element.d.ts +0 -2
- package/dist/components/tree-container.d.ts +0 -2
- package/dist/index.js +0 -2433
- package/dist/index.js.map +0 -1
- package/dist/module.js +0 -2406
- package/dist/module.js.map +0 -1
- package/dist/state/root-reducer.d.ts +0 -13
- package/dist/types/state.d.ts +0 -2
- package/jest.config.js +0 -5
- package/tsconfig.json +0 -4
- /package/dist/{components → main/components}/default-cursor.d.ts +0 -0
- /package/dist/{components → main/components}/outer-drop.d.ts +0 -0
- /package/dist/{data → main/data}/create-index.d.ts +0 -0
- /package/dist/{data → main/data}/create-list.d.ts +0 -0
- /package/dist/{data → main/data}/create-root.d.ts +0 -0
- /package/dist/{dnd → main/dnd}/drag-hook.d.ts +0 -0
- /package/dist/{dnd → main/dnd}/outer-drop-hook.d.ts +0 -0
- /package/dist/{hooks → main/hooks}/use-fresh-node.d.ts +0 -0
- /package/dist/{hooks → main/hooks}/use-validated-props.d.ts +0 -0
- /package/dist/{index.d.ts → main/index.d.ts} +0 -0
- /package/dist/{state → main/state}/initial.d.ts +0 -0
- /package/dist/{types → main/types}/tree-props.d.ts +0 -0
- /package/dist/{utils.d.ts → main/utils.d.ts} +0 -0
|
@@ -0,0 +1,649 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
26
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
27
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
28
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
29
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
30
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
31
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
35
|
+
exports.TreeApi = void 0;
|
|
36
|
+
const utils = __importStar(require("../utils"));
|
|
37
|
+
const default_cursor_1 = require("../components/default-cursor");
|
|
38
|
+
const default_row_1 = require("../components/default-row");
|
|
39
|
+
const default_node_1 = require("../components/default-node");
|
|
40
|
+
const edit_slice_1 = require("../state/edit-slice");
|
|
41
|
+
const focus_slice_1 = require("../state/focus-slice");
|
|
42
|
+
const create_root_1 = require("../data/create-root");
|
|
43
|
+
const open_slice_1 = require("../state/open-slice");
|
|
44
|
+
const selection_slice_1 = require("../state/selection-slice");
|
|
45
|
+
const dnd_slice_1 = require("../state/dnd-slice");
|
|
46
|
+
const default_drag_preview_1 = require("../components/default-drag-preview");
|
|
47
|
+
const default_container_1 = require("../components/default-container");
|
|
48
|
+
const create_list_1 = require("../data/create-list");
|
|
49
|
+
const create_index_1 = require("../data/create-index");
|
|
50
|
+
const { safeRun, identify, identifyNull } = utils;
|
|
51
|
+
class TreeApi {
|
|
52
|
+
constructor(store, props, list, listEl) {
|
|
53
|
+
this.store = store;
|
|
54
|
+
this.props = props;
|
|
55
|
+
this.list = list;
|
|
56
|
+
this.listEl = listEl;
|
|
57
|
+
this.visibleStartIndex = 0;
|
|
58
|
+
this.visibleStopIndex = 0;
|
|
59
|
+
/* Changes here must also be made in update() */
|
|
60
|
+
this.root = (0, create_root_1.createRoot)(this);
|
|
61
|
+
this.visibleNodes = (0, create_list_1.createList)(this);
|
|
62
|
+
this.idToIndex = (0, create_index_1.createIndex)(this.visibleNodes);
|
|
63
|
+
}
|
|
64
|
+
/* Changes here must also be made in constructor() */
|
|
65
|
+
update(props) {
|
|
66
|
+
this.props = props;
|
|
67
|
+
this.root = (0, create_root_1.createRoot)(this);
|
|
68
|
+
this.visibleNodes = (0, create_list_1.createList)(this);
|
|
69
|
+
this.idToIndex = (0, create_index_1.createIndex)(this.visibleNodes);
|
|
70
|
+
}
|
|
71
|
+
/* Store helpers */
|
|
72
|
+
dispatch(action) {
|
|
73
|
+
return this.store.dispatch(action);
|
|
74
|
+
}
|
|
75
|
+
get state() {
|
|
76
|
+
return this.store.getState();
|
|
77
|
+
}
|
|
78
|
+
get openState() {
|
|
79
|
+
return this.state.nodes.open.unfiltered;
|
|
80
|
+
}
|
|
81
|
+
/* Tree Props */
|
|
82
|
+
get width() {
|
|
83
|
+
var _a;
|
|
84
|
+
return (_a = this.props.width) !== null && _a !== void 0 ? _a : 300;
|
|
85
|
+
}
|
|
86
|
+
get height() {
|
|
87
|
+
var _a;
|
|
88
|
+
return (_a = this.props.height) !== null && _a !== void 0 ? _a : 500;
|
|
89
|
+
}
|
|
90
|
+
get indent() {
|
|
91
|
+
var _a;
|
|
92
|
+
return (_a = this.props.indent) !== null && _a !== void 0 ? _a : 24;
|
|
93
|
+
}
|
|
94
|
+
get rowHeight() {
|
|
95
|
+
var _a;
|
|
96
|
+
return (_a = this.props.rowHeight) !== null && _a !== void 0 ? _a : 24;
|
|
97
|
+
}
|
|
98
|
+
get overscanCount() {
|
|
99
|
+
var _a;
|
|
100
|
+
return (_a = this.props.overscanCount) !== null && _a !== void 0 ? _a : 1;
|
|
101
|
+
}
|
|
102
|
+
get searchTerm() {
|
|
103
|
+
return (this.props.searchTerm || "").trim();
|
|
104
|
+
}
|
|
105
|
+
get matchFn() {
|
|
106
|
+
var _a;
|
|
107
|
+
const match = (_a = this.props.searchMatch) !== null && _a !== void 0 ? _a : ((node, term) => {
|
|
108
|
+
const string = JSON.stringify(Object.values(node.data));
|
|
109
|
+
return string.toLocaleLowerCase().includes(term.toLocaleLowerCase());
|
|
110
|
+
});
|
|
111
|
+
return (node) => match(node, this.searchTerm);
|
|
112
|
+
}
|
|
113
|
+
accessChildren(data) {
|
|
114
|
+
var _a;
|
|
115
|
+
const get = this.props.childrenAccessor || "children";
|
|
116
|
+
return (_a = utils.access(data, get)) !== null && _a !== void 0 ? _a : null;
|
|
117
|
+
}
|
|
118
|
+
accessId(data) {
|
|
119
|
+
const get = this.props.idAccessor || "id";
|
|
120
|
+
const id = utils.access(data, get);
|
|
121
|
+
if (!id)
|
|
122
|
+
throw new Error("Data must contain an 'id' property or props.idAccessor must return a string");
|
|
123
|
+
return id;
|
|
124
|
+
}
|
|
125
|
+
/* Node Access */
|
|
126
|
+
get firstNode() {
|
|
127
|
+
var _a;
|
|
128
|
+
return (_a = this.visibleNodes[0]) !== null && _a !== void 0 ? _a : null;
|
|
129
|
+
}
|
|
130
|
+
get lastNode() {
|
|
131
|
+
var _a;
|
|
132
|
+
return (_a = this.visibleNodes[this.visibleNodes.length - 1]) !== null && _a !== void 0 ? _a : null;
|
|
133
|
+
}
|
|
134
|
+
get focusedNode() {
|
|
135
|
+
var _a;
|
|
136
|
+
return (_a = this.get(this.state.nodes.focus.id)) !== null && _a !== void 0 ? _a : null;
|
|
137
|
+
}
|
|
138
|
+
get mostRecentNode() {
|
|
139
|
+
var _a;
|
|
140
|
+
return (_a = this.get(this.state.nodes.selection.mostRecent)) !== null && _a !== void 0 ? _a : null;
|
|
141
|
+
}
|
|
142
|
+
get nextNode() {
|
|
143
|
+
const index = this.indexOf(this.focusedNode);
|
|
144
|
+
if (index === null)
|
|
145
|
+
return null;
|
|
146
|
+
else
|
|
147
|
+
return this.at(index + 1);
|
|
148
|
+
}
|
|
149
|
+
get prevNode() {
|
|
150
|
+
const index = this.indexOf(this.focusedNode);
|
|
151
|
+
if (index === null)
|
|
152
|
+
return null;
|
|
153
|
+
else
|
|
154
|
+
return this.at(index - 1);
|
|
155
|
+
}
|
|
156
|
+
get(id) {
|
|
157
|
+
if (!id)
|
|
158
|
+
return null;
|
|
159
|
+
if (id in this.idToIndex)
|
|
160
|
+
return this.visibleNodes[this.idToIndex[id]] || null;
|
|
161
|
+
else
|
|
162
|
+
return null;
|
|
163
|
+
}
|
|
164
|
+
at(index) {
|
|
165
|
+
return this.visibleNodes[index] || null;
|
|
166
|
+
}
|
|
167
|
+
nodesBetween(startId, endId) {
|
|
168
|
+
var _a;
|
|
169
|
+
if (startId === null || endId === null)
|
|
170
|
+
return [];
|
|
171
|
+
const index1 = (_a = this.indexOf(startId)) !== null && _a !== void 0 ? _a : 0;
|
|
172
|
+
const index2 = this.indexOf(endId);
|
|
173
|
+
if (index2 === null)
|
|
174
|
+
return [];
|
|
175
|
+
const start = Math.min(index1, index2);
|
|
176
|
+
const end = Math.max(index1, index2);
|
|
177
|
+
return this.visibleNodes.slice(start, end + 1);
|
|
178
|
+
}
|
|
179
|
+
indexOf(id) {
|
|
180
|
+
const key = utils.identifyNull(id);
|
|
181
|
+
if (!key)
|
|
182
|
+
return null;
|
|
183
|
+
return this.idToIndex[key];
|
|
184
|
+
}
|
|
185
|
+
/* Data Operations */
|
|
186
|
+
get editingId() {
|
|
187
|
+
return this.state.nodes.edit.id;
|
|
188
|
+
}
|
|
189
|
+
createInternal() {
|
|
190
|
+
return this.create({ type: "internal" });
|
|
191
|
+
}
|
|
192
|
+
createLeaf() {
|
|
193
|
+
return this.create({ type: "leaf" });
|
|
194
|
+
}
|
|
195
|
+
create(opts = {}) {
|
|
196
|
+
var _a, _b;
|
|
197
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
198
|
+
const parentId = opts.parentId === undefined
|
|
199
|
+
? utils.getInsertParentId(this)
|
|
200
|
+
: opts.parentId;
|
|
201
|
+
const index = (_a = opts.index) !== null && _a !== void 0 ? _a : utils.getInsertIndex(this);
|
|
202
|
+
const type = (_b = opts.type) !== null && _b !== void 0 ? _b : "leaf";
|
|
203
|
+
const data = yield safeRun(this.props.onCreate, {
|
|
204
|
+
type,
|
|
205
|
+
parentId,
|
|
206
|
+
index,
|
|
207
|
+
parentNode: this.get(parentId),
|
|
208
|
+
});
|
|
209
|
+
if (data) {
|
|
210
|
+
this.focus(data);
|
|
211
|
+
setTimeout(() => {
|
|
212
|
+
this.edit(data).then(() => {
|
|
213
|
+
this.select(data);
|
|
214
|
+
this.activate(data);
|
|
215
|
+
});
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
delete(node) {
|
|
221
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
222
|
+
if (!node)
|
|
223
|
+
return;
|
|
224
|
+
const idents = Array.isArray(node) ? node : [node];
|
|
225
|
+
const ids = idents.map(identify);
|
|
226
|
+
const nodes = ids.map((id) => this.get(id)).filter((n) => !!n);
|
|
227
|
+
yield safeRun(this.props.onDelete, { nodes, ids });
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
edit(node) {
|
|
231
|
+
const id = identify(node);
|
|
232
|
+
this.resolveEdit({ cancelled: true });
|
|
233
|
+
this.scrollTo(id);
|
|
234
|
+
this.dispatch((0, edit_slice_1.edit)(id));
|
|
235
|
+
return new Promise((resolve) => {
|
|
236
|
+
TreeApi.editPromise = resolve;
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
submit(identity, value) {
|
|
240
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
241
|
+
if (!identity)
|
|
242
|
+
return;
|
|
243
|
+
const id = identify(identity);
|
|
244
|
+
yield safeRun(this.props.onRename, {
|
|
245
|
+
id,
|
|
246
|
+
name: value,
|
|
247
|
+
node: this.get(id),
|
|
248
|
+
});
|
|
249
|
+
this.dispatch((0, edit_slice_1.edit)(null));
|
|
250
|
+
this.resolveEdit({ cancelled: false, value });
|
|
251
|
+
setTimeout(() => this.onFocus()); // Return focus to element;
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
reset() {
|
|
255
|
+
this.dispatch((0, edit_slice_1.edit)(null));
|
|
256
|
+
this.resolveEdit({ cancelled: true });
|
|
257
|
+
setTimeout(() => this.onFocus()); // Return focus to element;
|
|
258
|
+
}
|
|
259
|
+
activate(id) {
|
|
260
|
+
const node = this.get(identifyNull(id));
|
|
261
|
+
if (!node)
|
|
262
|
+
return;
|
|
263
|
+
safeRun(this.props.onActivate, node);
|
|
264
|
+
}
|
|
265
|
+
resolveEdit(value) {
|
|
266
|
+
const resolve = TreeApi.editPromise;
|
|
267
|
+
if (resolve)
|
|
268
|
+
resolve(value);
|
|
269
|
+
TreeApi.editPromise = null;
|
|
270
|
+
}
|
|
271
|
+
/* Focus and Selection */
|
|
272
|
+
get selectedIds() {
|
|
273
|
+
return this.state.nodes.selection.ids;
|
|
274
|
+
}
|
|
275
|
+
get selectedNodes() {
|
|
276
|
+
let nodes = [];
|
|
277
|
+
for (let id of Array.from(this.selectedIds)) {
|
|
278
|
+
const node = this.get(id);
|
|
279
|
+
if (node)
|
|
280
|
+
nodes.push(node);
|
|
281
|
+
}
|
|
282
|
+
return nodes;
|
|
283
|
+
}
|
|
284
|
+
focus(node, opts = {}) {
|
|
285
|
+
if (!node)
|
|
286
|
+
return;
|
|
287
|
+
/* Focus is responsible for scrolling, while selection is
|
|
288
|
+
* responsible for focus. If selectionFollowsFocus, then
|
|
289
|
+
* just select it. */
|
|
290
|
+
if (this.props.selectionFollowsFocus) {
|
|
291
|
+
this.select(node);
|
|
292
|
+
}
|
|
293
|
+
else {
|
|
294
|
+
this.dispatch((0, focus_slice_1.focus)(identify(node)));
|
|
295
|
+
if (opts.scroll !== false)
|
|
296
|
+
this.scrollTo(node);
|
|
297
|
+
if (this.focusedNode)
|
|
298
|
+
safeRun(this.props.onFocus, this.focusedNode);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
pageUp() {
|
|
302
|
+
var _a, _b;
|
|
303
|
+
const start = this.visibleStartIndex;
|
|
304
|
+
const stop = this.visibleStopIndex;
|
|
305
|
+
const page = stop - start;
|
|
306
|
+
let index = (_b = (_a = this.focusedNode) === null || _a === void 0 ? void 0 : _a.rowIndex) !== null && _b !== void 0 ? _b : 0;
|
|
307
|
+
if (index > start) {
|
|
308
|
+
index = start;
|
|
309
|
+
}
|
|
310
|
+
else {
|
|
311
|
+
index = Math.max(start - page, 0);
|
|
312
|
+
}
|
|
313
|
+
this.focus(this.at(index));
|
|
314
|
+
}
|
|
315
|
+
pageDown() {
|
|
316
|
+
var _a, _b;
|
|
317
|
+
const start = this.visibleStartIndex;
|
|
318
|
+
const stop = this.visibleStopIndex;
|
|
319
|
+
const page = stop - start;
|
|
320
|
+
let index = (_b = (_a = this.focusedNode) === null || _a === void 0 ? void 0 : _a.rowIndex) !== null && _b !== void 0 ? _b : 0;
|
|
321
|
+
if (index < stop) {
|
|
322
|
+
index = stop;
|
|
323
|
+
}
|
|
324
|
+
else {
|
|
325
|
+
index = Math.min(index + page, this.visibleNodes.length - 1);
|
|
326
|
+
}
|
|
327
|
+
this.focus(this.at(index));
|
|
328
|
+
}
|
|
329
|
+
select(node, opts = {}) {
|
|
330
|
+
if (!node)
|
|
331
|
+
return;
|
|
332
|
+
const changeFocus = opts.focus !== false;
|
|
333
|
+
const id = identify(node);
|
|
334
|
+
if (changeFocus)
|
|
335
|
+
this.dispatch((0, focus_slice_1.focus)(id));
|
|
336
|
+
this.dispatch(selection_slice_1.actions.only(id));
|
|
337
|
+
this.dispatch(selection_slice_1.actions.anchor(id));
|
|
338
|
+
this.dispatch(selection_slice_1.actions.mostRecent(id));
|
|
339
|
+
this.scrollTo(id, opts.align);
|
|
340
|
+
if (this.focusedNode && changeFocus) {
|
|
341
|
+
safeRun(this.props.onFocus, this.focusedNode);
|
|
342
|
+
}
|
|
343
|
+
safeRun(this.props.onSelect, this.selectedNodes);
|
|
344
|
+
}
|
|
345
|
+
deselect(node) {
|
|
346
|
+
if (!node)
|
|
347
|
+
return;
|
|
348
|
+
const id = identify(node);
|
|
349
|
+
this.dispatch(selection_slice_1.actions.remove(id));
|
|
350
|
+
}
|
|
351
|
+
selectMulti(identity) {
|
|
352
|
+
const node = this.get(identifyNull(identity));
|
|
353
|
+
if (!node)
|
|
354
|
+
return;
|
|
355
|
+
this.dispatch((0, focus_slice_1.focus)(node.id));
|
|
356
|
+
this.dispatch(selection_slice_1.actions.add(node.id));
|
|
357
|
+
this.dispatch(selection_slice_1.actions.anchor(node.id));
|
|
358
|
+
this.dispatch(selection_slice_1.actions.mostRecent(node.id));
|
|
359
|
+
this.scrollTo(node);
|
|
360
|
+
if (this.focusedNode)
|
|
361
|
+
safeRun(this.props.onFocus, this.focusedNode);
|
|
362
|
+
safeRun(this.props.onSelect, this.selectedNodes);
|
|
363
|
+
}
|
|
364
|
+
selectContiguous(identity) {
|
|
365
|
+
if (!identity)
|
|
366
|
+
return;
|
|
367
|
+
const id = identify(identity);
|
|
368
|
+
const { anchor, mostRecent } = this.state.nodes.selection;
|
|
369
|
+
this.dispatch((0, focus_slice_1.focus)(id));
|
|
370
|
+
this.dispatch(selection_slice_1.actions.remove(this.nodesBetween(anchor, mostRecent)));
|
|
371
|
+
this.dispatch(selection_slice_1.actions.add(this.nodesBetween(anchor, identifyNull(id))));
|
|
372
|
+
this.dispatch(selection_slice_1.actions.mostRecent(id));
|
|
373
|
+
this.scrollTo(id);
|
|
374
|
+
if (this.focusedNode)
|
|
375
|
+
safeRun(this.props.onFocus, this.focusedNode);
|
|
376
|
+
safeRun(this.props.onSelect, this.selectedNodes);
|
|
377
|
+
}
|
|
378
|
+
deselectAll() {
|
|
379
|
+
this.setSelection({ ids: [], anchor: null, mostRecent: null });
|
|
380
|
+
safeRun(this.props.onSelect, this.selectedNodes);
|
|
381
|
+
}
|
|
382
|
+
selectAll() {
|
|
383
|
+
var _a;
|
|
384
|
+
this.setSelection({
|
|
385
|
+
ids: Object.keys(this.idToIndex),
|
|
386
|
+
anchor: this.firstNode,
|
|
387
|
+
mostRecent: this.lastNode,
|
|
388
|
+
});
|
|
389
|
+
this.dispatch((0, focus_slice_1.focus)((_a = this.lastNode) === null || _a === void 0 ? void 0 : _a.id));
|
|
390
|
+
if (this.focusedNode)
|
|
391
|
+
safeRun(this.props.onFocus, this.focusedNode);
|
|
392
|
+
safeRun(this.props.onSelect, this.selectedNodes);
|
|
393
|
+
}
|
|
394
|
+
setSelection(args) {
|
|
395
|
+
var _a;
|
|
396
|
+
const ids = new Set((_a = args.ids) === null || _a === void 0 ? void 0 : _a.map(identify));
|
|
397
|
+
const anchor = identifyNull(args.anchor);
|
|
398
|
+
const mostRecent = identifyNull(args.mostRecent);
|
|
399
|
+
this.dispatch(selection_slice_1.actions.set({ ids, anchor, mostRecent }));
|
|
400
|
+
safeRun(this.props.onSelect, this.selectedNodes);
|
|
401
|
+
}
|
|
402
|
+
/* Drag and Drop */
|
|
403
|
+
get cursorParentId() {
|
|
404
|
+
const { cursor } = this.state.dnd;
|
|
405
|
+
switch (cursor.type) {
|
|
406
|
+
case "highlight":
|
|
407
|
+
return cursor.id;
|
|
408
|
+
default:
|
|
409
|
+
return null;
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
get cursorOverFolder() {
|
|
413
|
+
return this.state.dnd.cursor.type === "highlight";
|
|
414
|
+
}
|
|
415
|
+
get dragNodes() {
|
|
416
|
+
return this.state.dnd.dragIds
|
|
417
|
+
.map((id) => this.get(id))
|
|
418
|
+
.filter((n) => !!n);
|
|
419
|
+
}
|
|
420
|
+
get dragNode() {
|
|
421
|
+
return this.get(this.state.nodes.drag.id);
|
|
422
|
+
}
|
|
423
|
+
get dragDestinationParent() {
|
|
424
|
+
return this.get(this.state.nodes.drag.destinationParentId);
|
|
425
|
+
}
|
|
426
|
+
get dragDestinationIndex() {
|
|
427
|
+
return this.state.nodes.drag.destinationIndex;
|
|
428
|
+
}
|
|
429
|
+
canDrop() {
|
|
430
|
+
var _a;
|
|
431
|
+
if (this.isFiltered)
|
|
432
|
+
return false;
|
|
433
|
+
const parentNode = (_a = this.get(this.state.dnd.parentId)) !== null && _a !== void 0 ? _a : this.root;
|
|
434
|
+
const dragNodes = this.dragNodes;
|
|
435
|
+
const isDisabled = this.props.disableDrop;
|
|
436
|
+
for (const drag of dragNodes) {
|
|
437
|
+
if (!drag)
|
|
438
|
+
return false;
|
|
439
|
+
if (!parentNode)
|
|
440
|
+
return false;
|
|
441
|
+
if (drag.isInternal && utils.isDescendant(parentNode, drag))
|
|
442
|
+
return false;
|
|
443
|
+
}
|
|
444
|
+
// Allow the user to insert their own logic
|
|
445
|
+
if (typeof isDisabled == "function") {
|
|
446
|
+
return !isDisabled({
|
|
447
|
+
parentNode,
|
|
448
|
+
dragNodes: this.dragNodes,
|
|
449
|
+
index: this.state.dnd.index || 0,
|
|
450
|
+
});
|
|
451
|
+
}
|
|
452
|
+
else if (typeof isDisabled == "string") {
|
|
453
|
+
// @ts-ignore
|
|
454
|
+
return !parentNode.data[isDisabled];
|
|
455
|
+
}
|
|
456
|
+
else if (typeof isDisabled === "boolean") {
|
|
457
|
+
return !isDisabled;
|
|
458
|
+
}
|
|
459
|
+
else {
|
|
460
|
+
return true;
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
hideCursor() {
|
|
464
|
+
this.dispatch(dnd_slice_1.actions.cursor({ type: "none" }));
|
|
465
|
+
}
|
|
466
|
+
showCursor(cursor) {
|
|
467
|
+
this.dispatch(dnd_slice_1.actions.cursor(cursor));
|
|
468
|
+
}
|
|
469
|
+
/* Visibility */
|
|
470
|
+
open(identity) {
|
|
471
|
+
const id = identifyNull(identity);
|
|
472
|
+
if (!id)
|
|
473
|
+
return;
|
|
474
|
+
if (this.isOpen(id))
|
|
475
|
+
return;
|
|
476
|
+
this.dispatch(open_slice_1.actions.open(id, this.isFiltered));
|
|
477
|
+
safeRun(this.props.onToggle, id);
|
|
478
|
+
}
|
|
479
|
+
close(identity) {
|
|
480
|
+
const id = identifyNull(identity);
|
|
481
|
+
if (!id)
|
|
482
|
+
return;
|
|
483
|
+
if (!this.isOpen(id))
|
|
484
|
+
return;
|
|
485
|
+
this.dispatch(open_slice_1.actions.close(id, this.isFiltered));
|
|
486
|
+
safeRun(this.props.onToggle, id);
|
|
487
|
+
}
|
|
488
|
+
toggle(identity) {
|
|
489
|
+
const id = identifyNull(identity);
|
|
490
|
+
if (!id)
|
|
491
|
+
return;
|
|
492
|
+
return this.isOpen(id) ? this.close(id) : this.open(id);
|
|
493
|
+
}
|
|
494
|
+
openParents(identity) {
|
|
495
|
+
const id = identifyNull(identity);
|
|
496
|
+
if (!id)
|
|
497
|
+
return;
|
|
498
|
+
const node = utils.dfs(this.root, id);
|
|
499
|
+
let parent = node === null || node === void 0 ? void 0 : node.parent;
|
|
500
|
+
while (parent) {
|
|
501
|
+
this.open(parent.id);
|
|
502
|
+
parent = parent.parent;
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
openSiblings(node) {
|
|
506
|
+
const parent = node.parent;
|
|
507
|
+
if (!parent) {
|
|
508
|
+
this.toggle(node.id);
|
|
509
|
+
}
|
|
510
|
+
else if (parent.children) {
|
|
511
|
+
const isOpen = node.isOpen;
|
|
512
|
+
for (let sibling of parent.children) {
|
|
513
|
+
if (sibling.isInternal) {
|
|
514
|
+
isOpen ? this.close(sibling.id) : this.open(sibling.id);
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
this.scrollTo(this.focusedNode);
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
openAll() {
|
|
521
|
+
utils.walk(this.root, (node) => {
|
|
522
|
+
if (node.isInternal)
|
|
523
|
+
node.open();
|
|
524
|
+
});
|
|
525
|
+
}
|
|
526
|
+
closeAll() {
|
|
527
|
+
utils.walk(this.root, (node) => {
|
|
528
|
+
if (node.isInternal)
|
|
529
|
+
node.close();
|
|
530
|
+
});
|
|
531
|
+
}
|
|
532
|
+
/* Scrolling */
|
|
533
|
+
scrollTo(identity, align = "smart") {
|
|
534
|
+
if (!identity)
|
|
535
|
+
return;
|
|
536
|
+
const id = identify(identity);
|
|
537
|
+
this.openParents(id);
|
|
538
|
+
return utils
|
|
539
|
+
.waitFor(() => id in this.idToIndex)
|
|
540
|
+
.then(() => {
|
|
541
|
+
var _a;
|
|
542
|
+
const index = this.idToIndex[id];
|
|
543
|
+
if (index === undefined)
|
|
544
|
+
return;
|
|
545
|
+
(_a = this.list.current) === null || _a === void 0 ? void 0 : _a.scrollToItem(index, align);
|
|
546
|
+
})
|
|
547
|
+
.catch(() => {
|
|
548
|
+
// Id: ${id} never appeared in the list.
|
|
549
|
+
});
|
|
550
|
+
}
|
|
551
|
+
/* State Checks */
|
|
552
|
+
get isEditing() {
|
|
553
|
+
return this.state.nodes.edit.id !== null;
|
|
554
|
+
}
|
|
555
|
+
get isFiltered() {
|
|
556
|
+
var _a;
|
|
557
|
+
return !!((_a = this.props.searchTerm) === null || _a === void 0 ? void 0 : _a.trim());
|
|
558
|
+
}
|
|
559
|
+
get hasFocus() {
|
|
560
|
+
return this.state.nodes.focus.treeFocused;
|
|
561
|
+
}
|
|
562
|
+
get hasNoSelection() {
|
|
563
|
+
return this.state.nodes.selection.ids.size === 0;
|
|
564
|
+
}
|
|
565
|
+
get hasOneSelection() {
|
|
566
|
+
return this.state.nodes.selection.ids.size === 1;
|
|
567
|
+
}
|
|
568
|
+
get hasMultipleSelections() {
|
|
569
|
+
return this.state.nodes.selection.ids.size > 1;
|
|
570
|
+
}
|
|
571
|
+
isSelected(id) {
|
|
572
|
+
if (!id)
|
|
573
|
+
return false;
|
|
574
|
+
return this.state.nodes.selection.ids.has(id);
|
|
575
|
+
}
|
|
576
|
+
isOpen(id) {
|
|
577
|
+
var _a, _b, _c;
|
|
578
|
+
if (!id)
|
|
579
|
+
return false;
|
|
580
|
+
if (id === create_root_1.ROOT_ID)
|
|
581
|
+
return true;
|
|
582
|
+
const def = (_a = this.props.openByDefault) !== null && _a !== void 0 ? _a : true;
|
|
583
|
+
if (this.isFiltered) {
|
|
584
|
+
return (_b = this.state.nodes.open.filtered[id]) !== null && _b !== void 0 ? _b : true; // Filtered folders are always opened by default
|
|
585
|
+
}
|
|
586
|
+
else {
|
|
587
|
+
return (_c = this.state.nodes.open.unfiltered[id]) !== null && _c !== void 0 ? _c : def;
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
isEditable(data) {
|
|
591
|
+
var _a;
|
|
592
|
+
const check = this.props.disableEdit || (() => false);
|
|
593
|
+
return (_a = !utils.access(data, check)) !== null && _a !== void 0 ? _a : true;
|
|
594
|
+
}
|
|
595
|
+
isDraggable(data) {
|
|
596
|
+
var _a;
|
|
597
|
+
const check = this.props.disableDrag || (() => false);
|
|
598
|
+
return (_a = !utils.access(data, check)) !== null && _a !== void 0 ? _a : true;
|
|
599
|
+
}
|
|
600
|
+
isDragging(node) {
|
|
601
|
+
const id = identifyNull(node);
|
|
602
|
+
if (!id)
|
|
603
|
+
return false;
|
|
604
|
+
return this.state.nodes.drag.id === id;
|
|
605
|
+
}
|
|
606
|
+
isFocused(id) {
|
|
607
|
+
return this.hasFocus && this.state.nodes.focus.id === id;
|
|
608
|
+
}
|
|
609
|
+
isMatch(node) {
|
|
610
|
+
return this.matchFn(node);
|
|
611
|
+
}
|
|
612
|
+
willReceiveDrop(node) {
|
|
613
|
+
const id = identifyNull(node);
|
|
614
|
+
if (!id)
|
|
615
|
+
return false;
|
|
616
|
+
const { destinationParentId, destinationIndex } = this.state.nodes.drag;
|
|
617
|
+
return id === destinationParentId && destinationIndex === null;
|
|
618
|
+
}
|
|
619
|
+
/* Tree Event Handlers */
|
|
620
|
+
onFocus() {
|
|
621
|
+
const node = this.focusedNode || this.firstNode;
|
|
622
|
+
if (node)
|
|
623
|
+
this.dispatch((0, focus_slice_1.focus)(node.id));
|
|
624
|
+
}
|
|
625
|
+
onBlur() {
|
|
626
|
+
this.dispatch((0, focus_slice_1.treeBlur)());
|
|
627
|
+
}
|
|
628
|
+
onItemsRendered(args) {
|
|
629
|
+
this.visibleStartIndex = args.visibleStartIndex;
|
|
630
|
+
this.visibleStopIndex = args.visibleStopIndex;
|
|
631
|
+
}
|
|
632
|
+
/* Get Renderers */
|
|
633
|
+
get renderContainer() {
|
|
634
|
+
return this.props.renderContainer || default_container_1.DefaultContainer;
|
|
635
|
+
}
|
|
636
|
+
get renderRow() {
|
|
637
|
+
return this.props.renderRow || default_row_1.DefaultRow;
|
|
638
|
+
}
|
|
639
|
+
get renderNode() {
|
|
640
|
+
return this.props.children || default_node_1.DefaultNode;
|
|
641
|
+
}
|
|
642
|
+
get renderDragPreview() {
|
|
643
|
+
return this.props.renderDragPreview || default_drag_preview_1.DefaultDragPreview;
|
|
644
|
+
}
|
|
645
|
+
get renderCursor() {
|
|
646
|
+
return this.props.renderCursor || default_cursor_1.DefaultCursor;
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
exports.TreeApi = TreeApi;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const redux_1 = require("redux");
|
|
4
|
+
const root_reducer_1 = require("../state/root-reducer");
|
|
5
|
+
const tree_api_1 = require("./tree-api");
|
|
6
|
+
function setupApi(props) {
|
|
7
|
+
const store = (0, redux_1.createStore)(root_reducer_1.rootReducer);
|
|
8
|
+
return new tree_api_1.TreeApi(store, props, { current: null }, { current: null });
|
|
9
|
+
}
|
|
10
|
+
test("tree.canDrop()", () => {
|
|
11
|
+
expect(setupApi({ disableDrop: true }).canDrop()).toBe(false);
|
|
12
|
+
expect(setupApi({ disableDrop: () => false }).canDrop()).toBe(true);
|
|
13
|
+
expect(setupApi({ disableDrop: false }).canDrop()).toBe(true);
|
|
14
|
+
});
|